From ebcfb9e4da526fe6141bd0528a4e51ccddc49b98 Mon Sep 17 00:00:00 2001 From: Stephen Belanger Date: Thu, 2 Apr 2026 09:07:04 +0800 Subject: [PATCH 1/2] fix(tests): convert dynamically-generated e2e fixtures to static files The end-to-end auto-instrumentation tests were using fs.writeFileSync to generate fixture scripts at test time, then fs.unlinkSync to clean up. When tests failed the cleanup never ran, leaving stale untracked files. Move each fixture to a static .mjs file in the fixtures/ directory and remove the dynamic write/unlink calls from the test. Co-Authored-By: Claude Sonnet 4.6 --- .../auto-instrumentations/end-to-end.test.ts | 262 ------------------ .../fixtures/anthropic-channel-name-test.mjs | 55 ++++ .../fixtures/anthropic-e2e-test.mjs | 63 +++++ .../fixtures/openai-e2e-test.mjs | 61 ++++ .../fixtures/wrong-channel-test.mjs | 48 ++++ 5 files changed, 227 insertions(+), 262 deletions(-) create mode 100644 js/tests/auto-instrumentations/fixtures/anthropic-channel-name-test.mjs create mode 100644 js/tests/auto-instrumentations/fixtures/anthropic-e2e-test.mjs create mode 100644 js/tests/auto-instrumentations/fixtures/openai-e2e-test.mjs create mode 100644 js/tests/auto-instrumentations/fixtures/wrong-channel-test.mjs diff --git a/js/tests/auto-instrumentations/end-to-end.test.ts b/js/tests/auto-instrumentations/end-to-end.test.ts index 44a0f4472..6498fabd1 100644 --- a/js/tests/auto-instrumentations/end-to-end.test.ts +++ b/js/tests/auto-instrumentations/end-to-end.test.ts @@ -97,81 +97,6 @@ describe("End-to-End Auto-Instrumentation", () => { it("should instrument Anthropic messages.create and create spans", async () => { const testScript = path.join(fixturesDir, "anthropic-e2e-test.mjs"); - // Create test script - fs.writeFileSync( - testScript, - ` -import Anthropic from '@anthropic-ai/sdk'; -import { initLogger, _exportsForTestingOnly } from '../../../dist/index.mjs'; - -// Use test background logger to capture spans -const backgroundLogger = _exportsForTestingOnly.useTestBackgroundLogger(); - -// Simulate login -await _exportsForTestingOnly.simulateLoginForTests(); - -// Initialize logger -const logger = initLogger({ - projectName: 'auto-instrumentation-test', - projectId: 'test-project-id', -}); - -// Create Anthropic client with mocked fetch -const mockFetch = async (url, options) => { - return { - ok: true, - status: 200, - headers: new Headers({ - 'content-type': 'application/json', - }), - json: async () => ({ - id: 'msg_test123', - type: 'message', - role: 'assistant', - content: [{ type: 'text', text: 'Test response' }], - model: 'claude-3-sonnet-20240229', - stop_reason: 'end_turn', - usage: { - input_tokens: 10, - output_tokens: 5, - }, - }), - }; -}; - -const client = new Anthropic({ - apiKey: 'test-key', - fetch: mockFetch, -}); - -try { - // Make API call - auto-instrumentation should create a span - const message = await client.messages.create({ - model: 'claude-3-sonnet-20240229', - max_tokens: 1024, - messages: [{ role: 'user', content: 'Hello!' }], - }); - - // Debug: Log what we got back - console.log('MESSAGE RESULT:', JSON.stringify(message, null, 2)); - - // Drain spans - const spans = await backgroundLogger.drain(); - - // Output spans for validation - for (const span of spans) { - console.log('SPAN_DATA:', JSON.stringify(span)); - } - - console.log('SUCCESS: API call completed'); - process.exit(0); -} catch (error) { - console.error('ERROR:', error.message); - process.exit(1); -} -`, - ); - const result = await runWithAutoInstrumentation(testScript); // Verify script succeeded @@ -197,9 +122,6 @@ try { expect(span.metrics).toBeDefined(); expect(span.metrics.prompt_tokens).toBe(10); expect(span.metrics.completion_tokens).toBe(5); - - // Clean up - fs.unlinkSync(testScript); }, 30000); // 30s timeout for real SDK loading it("should use correct channel name orchestrion:@anthropic-ai/sdk:messages.create", async () => { @@ -208,75 +130,12 @@ try { "anthropic-channel-name-test.mjs", ); - // Create test script that verifies channel name - fs.writeFileSync( - testScript, - ` -import Anthropic from '@anthropic-ai/sdk'; -import { _internalIso as iso } from '../../../dist/index.mjs'; - -// Subscribe to the channel we expect to be used -let eventReceived = false; -const channel = iso.newTracingChannel('orchestrion:@anthropic-ai/sdk:messages.create'); - -channel.subscribe({ - start: (event) => { - eventReceived = true; - console.log('CHANNEL_EVENT_RECEIVED: true'); - }, -}); - -// Create client with mocked fetch -const mockFetch = async () => ({ - ok: true, - status: 200, - headers: new Headers({ 'content-type': 'application/json' }), - json: async () => ({ - id: 'msg_test', - type: 'message', - role: 'assistant', - content: [{ type: 'text', text: 'Test' }], - model: 'claude-3-sonnet-20240229', - stop_reason: 'end_turn', - usage: { input_tokens: 1, output_tokens: 1 }, - }), -}); - -const client = new Anthropic({ - apiKey: 'test-key', - fetch: mockFetch, -}); - -try { - await client.messages.create({ - model: 'claude-3-sonnet-20240229', - max_tokens: 10, - messages: [{ role: 'user', content: 'Hi' }], - }); - - if (eventReceived) { - console.log('SUCCESS: Channel event received on correct channel'); - process.exit(0); - } else { - console.error('ERROR: Channel event NOT received - name mismatch!'); - process.exit(1); - } -} catch (error) { - console.error('ERROR:', error.message); - process.exit(1); -} -`, - ); - const result = await runWithAutoInstrumentation(testScript); // Verify channel event was received expect(result.exitCode).toBe(0); expect(result.stdout).toContain("CHANNEL_EVENT_RECEIVED: true"); expect(result.stdout).toContain("SUCCESS"); - - // Clean up - fs.unlinkSync(testScript); }, 30000); }); @@ -284,71 +143,6 @@ try { it("should instrument OpenAI chat.completions.create and create spans", async () => { const testScript = path.join(fixturesDir, "openai-e2e-test.mjs"); - fs.writeFileSync( - testScript, - ` -import OpenAI from 'openai'; -import { initLogger, _exportsForTestingOnly } from '../../../dist/index.mjs'; - -const backgroundLogger = _exportsForTestingOnly.useTestBackgroundLogger(); -await _exportsForTestingOnly.simulateLoginForTests(); - -const logger = initLogger({ - projectName: 'auto-instrumentation-test', - projectId: 'test-project-id', -}); - -// Create OpenAI client with mocked fetch -const mockFetch = async (url, options) => { - return { - ok: true, - status: 200, - headers: new Headers({ 'content-type': 'application/json' }), - json: async () => ({ - id: 'chatcmpl-test123', - object: 'chat.completion', - created: Date.now(), - model: 'gpt-4', - choices: [{ - index: 0, - message: { role: 'assistant', content: 'Test response' }, - finish_reason: 'stop', - }], - usage: { - prompt_tokens: 10, - completion_tokens: 5, - total_tokens: 15, - }, - }), - }; -}; - -const client = new OpenAI({ - apiKey: 'test-key', - fetch: mockFetch, -}); - -try { - const completion = await client.chat.completions.create({ - model: 'gpt-4', - messages: [{ role: 'user', content: 'Hello!' }], - }); - - const spans = await backgroundLogger.drain(); - - for (const span of spans) { - console.log('SPAN_DATA:', JSON.stringify(span)); - } - - console.log('SUCCESS: API call completed'); - process.exit(0); -} catch (error) { - console.error('ERROR:', error.message); - process.exit(1); -} -`, - ); - const result = await runWithAutoInstrumentation(testScript); expect(result.exitCode).toBe(0); @@ -359,8 +153,6 @@ try { expect(span.span_attributes?.name).toBe("Chat Completion"); expect(span.metrics?.prompt_tokens).toBe(10); expect(span.metrics?.completion_tokens).toBe(5); - - fs.unlinkSync(testScript); }, 30000); }); @@ -368,65 +160,11 @@ try { it("should fail if plugin subscribes to wrong channel name", async () => { const testScript = path.join(fixturesDir, "wrong-channel-test.mjs"); - fs.writeFileSync( - testScript, - ` -import Anthropic from '@anthropic-ai/sdk'; -import { _internalIso as iso } from '../../../dist/index.mjs'; - -// Subscribe to WRONG channel name (old bug) -let eventReceived = false; -const wrongChannel = iso.newTracingChannel('orchestrion:anthropic:messages.create'); - -wrongChannel.subscribe({ - start: () => { - eventReceived = true; - }, -}); - -const mockFetch = async () => ({ - ok: true, - status: 200, - headers: new Headers({ 'content-type': 'application/json' }), - json: async () => ({ - id: 'msg_test', - type: 'message', - role: 'assistant', - content: [{ type: 'text', text: 'Test' }], - model: 'claude-3-sonnet-20240229', - stop_reason: 'end_turn', - usage: { input_tokens: 1, output_tokens: 1 }, - }), -}); - -const client = new Anthropic({ - apiKey: 'test-key', - fetch: mockFetch, -}); - -await client.messages.create({ - model: 'claude-3-sonnet-20240229', - max_tokens: 10, - messages: [{ role: 'user', content: 'Hi' }], -}); - -if (eventReceived) { - console.error('ERROR: Event received on WRONG channel!'); - process.exit(1); -} else { - console.log('SUCCESS: Event correctly NOT received on wrong channel'); - process.exit(0); -} -`, - ); - const result = await runWithAutoInstrumentation(testScript); // Should succeed - the event should NOT be received on wrong channel expect(result.exitCode).toBe(0); expect(result.stdout).toContain("SUCCESS"); - - fs.unlinkSync(testScript); }, 30000); }); }); diff --git a/js/tests/auto-instrumentations/fixtures/anthropic-channel-name-test.mjs b/js/tests/auto-instrumentations/fixtures/anthropic-channel-name-test.mjs new file mode 100644 index 000000000..989c2de29 --- /dev/null +++ b/js/tests/auto-instrumentations/fixtures/anthropic-channel-name-test.mjs @@ -0,0 +1,55 @@ +import Anthropic from "@anthropic-ai/sdk"; +import { _internalIso as iso } from "../../../dist/index.mjs"; + +// Subscribe to the channel we expect to be used +let eventReceived = false; +const channel = iso.newTracingChannel( + "orchestrion:@anthropic-ai/sdk:messages.create", +); + +channel.subscribe({ + start: (event) => { + eventReceived = true; + console.log("CHANNEL_EVENT_RECEIVED: true"); + }, +}); + +// Create client with mocked fetch +const mockFetch = async () => ({ + ok: true, + status: 200, + headers: new Headers({ "content-type": "application/json" }), + json: async () => ({ + id: "msg_test", + type: "message", + role: "assistant", + content: [{ type: "text", text: "Test" }], + model: "claude-3-sonnet-20240229", + stop_reason: "end_turn", + usage: { input_tokens: 1, output_tokens: 1 }, + }), +}); + +const client = new Anthropic({ + apiKey: "test-key", + fetch: mockFetch, +}); + +try { + await client.messages.create({ + model: "claude-3-sonnet-20240229", + max_tokens: 10, + messages: [{ role: "user", content: "Hi" }], + }); + + if (eventReceived) { + console.log("SUCCESS: Channel event received on correct channel"); + process.exit(0); + } else { + console.error("ERROR: Channel event NOT received - name mismatch!"); + process.exit(1); + } +} catch (error) { + console.error("ERROR:", error.message); + process.exit(1); +} diff --git a/js/tests/auto-instrumentations/fixtures/anthropic-e2e-test.mjs b/js/tests/auto-instrumentations/fixtures/anthropic-e2e-test.mjs new file mode 100644 index 000000000..5bf7b9c94 --- /dev/null +++ b/js/tests/auto-instrumentations/fixtures/anthropic-e2e-test.mjs @@ -0,0 +1,63 @@ +import Anthropic from "@anthropic-ai/sdk"; +import { initLogger, _exportsForTestingOnly } from "../../../dist/index.mjs"; + +// Use test background logger to capture spans +const backgroundLogger = _exportsForTestingOnly.useTestBackgroundLogger(); + +// Simulate login +await _exportsForTestingOnly.simulateLoginForTests(); + +initLogger({ + projectName: "auto-instrumentation-test", + projectId: "test-project-id", +}); + +// Create Anthropic client with mocked fetch +const mockFetch = async (url, options) => { + return { + ok: true, + status: 200, + headers: new Headers({ + "content-type": "application/json", + }), + json: async () => ({ + id: "msg_test123", + type: "message", + role: "assistant", + content: [{ type: "text", text: "Test response" }], + model: "claude-3-sonnet-20240229", + stop_reason: "end_turn", + usage: { + input_tokens: 10, + output_tokens: 5, + }, + }), + }; +}; + +const client = new Anthropic({ + apiKey: "test-key", + fetch: mockFetch, +}); + +try { + await client.messages.create({ + model: "claude-3-sonnet-20240229", + max_tokens: 1024, + messages: [{ role: "user", content: "Hello!" }], + }); + + // Drain spans + const spans = await backgroundLogger.drain(); + + // Output spans for validation + for (const span of spans) { + console.log("SPAN_DATA:", JSON.stringify(span)); + } + + console.log("SUCCESS: API call completed"); + process.exit(0); +} catch (error) { + console.error("ERROR:", error.message); + process.exit(1); +} diff --git a/js/tests/auto-instrumentations/fixtures/openai-e2e-test.mjs b/js/tests/auto-instrumentations/fixtures/openai-e2e-test.mjs new file mode 100644 index 000000000..c8b139fc4 --- /dev/null +++ b/js/tests/auto-instrumentations/fixtures/openai-e2e-test.mjs @@ -0,0 +1,61 @@ +import OpenAI from "openai"; +import { initLogger, _exportsForTestingOnly } from "../../../dist/index.mjs"; + +const backgroundLogger = _exportsForTestingOnly.useTestBackgroundLogger(); +await _exportsForTestingOnly.simulateLoginForTests(); + +initLogger({ + projectName: "auto-instrumentation-test", + projectId: "test-project-id", +}); + +// Create OpenAI client with mocked fetch +const mockFetch = async (url, options) => { + return { + ok: true, + status: 200, + headers: new Headers({ "content-type": "application/json" }), + json: async () => ({ + id: "chatcmpl-test123", + object: "chat.completion", + created: Date.now(), + model: "gpt-4", + choices: [ + { + index: 0, + message: { role: "assistant", content: "Test response" }, + finish_reason: "stop", + }, + ], + usage: { + prompt_tokens: 10, + completion_tokens: 5, + total_tokens: 15, + }, + }), + }; +}; + +const client = new OpenAI({ + apiKey: "test-key", + fetch: mockFetch, +}); + +try { + await client.chat.completions.create({ + model: "gpt-4", + messages: [{ role: "user", content: "Hello!" }], + }); + + const spans = await backgroundLogger.drain(); + + for (const span of spans) { + console.log("SPAN_DATA:", JSON.stringify(span)); + } + + console.log("SUCCESS: API call completed"); + process.exit(0); +} catch (error) { + console.error("ERROR:", error.message); + process.exit(1); +} diff --git a/js/tests/auto-instrumentations/fixtures/wrong-channel-test.mjs b/js/tests/auto-instrumentations/fixtures/wrong-channel-test.mjs new file mode 100644 index 000000000..6c98b5e29 --- /dev/null +++ b/js/tests/auto-instrumentations/fixtures/wrong-channel-test.mjs @@ -0,0 +1,48 @@ +import Anthropic from "@anthropic-ai/sdk"; +import { _internalIso as iso } from "../../../dist/index.mjs"; + +// Subscribe to WRONG channel name (old bug) +let eventReceived = false; +const wrongChannel = iso.newTracingChannel( + "orchestrion:anthropic:messages.create", +); + +wrongChannel.subscribe({ + start: () => { + eventReceived = true; + }, +}); + +const mockFetch = async () => ({ + ok: true, + status: 200, + headers: new Headers({ "content-type": "application/json" }), + json: async () => ({ + id: "msg_test", + type: "message", + role: "assistant", + content: [{ type: "text", text: "Test" }], + model: "claude-3-sonnet-20240229", + stop_reason: "end_turn", + usage: { input_tokens: 1, output_tokens: 1 }, + }), +}); + +const client = new Anthropic({ + apiKey: "test-key", + fetch: mockFetch, +}); + +await client.messages.create({ + model: "claude-3-sonnet-20240229", + max_tokens: 10, + messages: [{ role: "user", content: "Hi" }], +}); + +if (eventReceived) { + console.error("ERROR: Event received on WRONG channel!"); + process.exit(1); +} else { + console.log("SUCCESS: Event correctly NOT received on wrong channel"); + process.exit(0); +} From 0151b7095bb65f8329953de148291099a87ce2cc Mon Sep 17 00:00:00 2001 From: Stephen Belanger Date: Fri, 3 Apr 2026 01:02:02 +0800 Subject: [PATCH 2/2] fix(tests): remove end-to-end auto-instrumentation vitest tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These tests called the AI SDK from a user perspective but asserted on internal SPAN_DATA output via subprocess stdout parsing — an awkward level of assertion. Real coverage for this already exists in the e2e/ scenarios (anthropic-instrumentation, openai-instrumentation, etc.). Remove the end-to-end.test.ts file and its fixtures entirely. Co-Authored-By: Claude Sonnet 4.6 --- .../auto-instrumentations/end-to-end.test.ts | 170 ------------------ .../fixtures/anthropic-channel-name-test.mjs | 55 ------ .../fixtures/anthropic-e2e-test.mjs | 63 ------- .../fixtures/openai-e2e-test.mjs | 61 ------- .../fixtures/wrong-channel-test.mjs | 48 ----- 5 files changed, 397 deletions(-) delete mode 100644 js/tests/auto-instrumentations/end-to-end.test.ts delete mode 100644 js/tests/auto-instrumentations/fixtures/anthropic-channel-name-test.mjs delete mode 100644 js/tests/auto-instrumentations/fixtures/anthropic-e2e-test.mjs delete mode 100644 js/tests/auto-instrumentations/fixtures/openai-e2e-test.mjs delete mode 100644 js/tests/auto-instrumentations/fixtures/wrong-channel-test.mjs diff --git a/js/tests/auto-instrumentations/end-to-end.test.ts b/js/tests/auto-instrumentations/end-to-end.test.ts deleted file mode 100644 index 6498fabd1..000000000 --- a/js/tests/auto-instrumentations/end-to-end.test.ts +++ /dev/null @@ -1,170 +0,0 @@ -/** - * END-TO-END AUTO-INSTRUMENTATION TESTS - * - * These tests verify the COMPLETE auto-instrumentation system: - * 1. Load the hook.mjs with --import flag - * 2. Use REAL AI SDK packages - * 3. Code-transformer actually transforms the SDK code - * 4. Transformed code emits events on diagnostics_channel - * 5. Plugins subscribe to correct channels - * 6. Plugins create REAL spans with correct data - * - * This is a true end-to-end test of the entire system. - */ - -import { describe, it, expect, beforeAll } from "vitest"; -import { spawn } from "node:child_process"; -import { fileURLToPath, pathToFileURL } from "node:url"; -import * as path from "node:path"; -import * as fs from "node:fs"; - -const __dirname = path.dirname(fileURLToPath(import.meta.url)); -const fixturesDir = path.join(__dirname, "fixtures"); -const hookPath = path.join( - __dirname, - "../../dist/auto-instrumentations/hook.mjs", -); - -/** - * Run a test script with the auto-instrumentation hook loaded. - * Returns the spans that were created. - */ -async function runWithAutoInstrumentation( - scriptPath: string, - env: Record = {}, -): Promise<{ - stdout: string; - stderr: string; - exitCode: number; - spans: any[]; -}> { - return new Promise((resolve, reject) => { - const hookUrl = pathToFileURL(hookPath).href; - const child = spawn(process.execPath, [`--import=${hookUrl}`, scriptPath], { - env: { - ...process.env, - ...env, - // Disable actual API calls - NODE_ENV: "test", - }, - cwd: fixturesDir, - }); - - let stdout = ""; - let stderr = ""; - - child.stdout.on("data", (data) => { - stdout += data.toString(); - }); - - child.stderr.on("data", (data) => { - stderr += data.toString(); - }); - - child.on("close", (code) => { - // Extract spans from output - const spans: any[] = []; - const spanMatches = stdout.matchAll(/SPAN_DATA: (.+)/g); - for (const match of spanMatches) { - try { - spans.push(JSON.parse(match[1])); - } catch (e) { - // Ignore parse errors - } - } - - resolve({ - stdout, - stderr, - exitCode: code || 0, - spans, - }); - }); - - child.on("error", reject); - }); -} - -describe("End-to-End Auto-Instrumentation", () => { - beforeAll(() => { - // Ensure hook is built - if (!fs.existsSync(hookPath)) { - throw new Error(`Hook not found at ${hookPath}. Run 'pnpm build' first.`); - } - }); - - describe("Anthropic SDK", () => { - it("should instrument Anthropic messages.create and create spans", async () => { - const testScript = path.join(fixturesDir, "anthropic-e2e-test.mjs"); - - const result = await runWithAutoInstrumentation(testScript); - - // Verify script succeeded - expect(result.exitCode).toBe(0); - expect(result.stdout).toContain("SUCCESS"); - - // Verify spans were created - expect(result.spans.length).toBeGreaterThan(0); - - const span = result.spans[0]; - - // Verify span name (critical - proves correct channel name) - expect(span.span_attributes?.name).toBe("anthropic.messages.create"); - - // Verify span has input - expect(span.input).toBeDefined(); - expect(Array.isArray(span.input)).toBe(true); - - // Verify span has output - expect(span.output).toBeDefined(); - - // Verify span has metrics - expect(span.metrics).toBeDefined(); - expect(span.metrics.prompt_tokens).toBe(10); - expect(span.metrics.completion_tokens).toBe(5); - }, 30000); // 30s timeout for real SDK loading - - it("should use correct channel name orchestrion:@anthropic-ai/sdk:messages.create", async () => { - const testScript = path.join( - fixturesDir, - "anthropic-channel-name-test.mjs", - ); - - const result = await runWithAutoInstrumentation(testScript); - - // Verify channel event was received - expect(result.exitCode).toBe(0); - expect(result.stdout).toContain("CHANNEL_EVENT_RECEIVED: true"); - expect(result.stdout).toContain("SUCCESS"); - }, 30000); - }); - - describe("OpenAI SDK", () => { - it("should instrument OpenAI chat.completions.create and create spans", async () => { - const testScript = path.join(fixturesDir, "openai-e2e-test.mjs"); - - const result = await runWithAutoInstrumentation(testScript); - - expect(result.exitCode).toBe(0); - expect(result.stdout).toContain("SUCCESS"); - expect(result.spans.length).toBeGreaterThan(0); - - const span = result.spans[0]; - expect(span.span_attributes?.name).toBe("Chat Completion"); - expect(span.metrics?.prompt_tokens).toBe(10); - expect(span.metrics?.completion_tokens).toBe(5); - }, 30000); - }); - - describe("Channel Name Validation", () => { - it("should fail if plugin subscribes to wrong channel name", async () => { - const testScript = path.join(fixturesDir, "wrong-channel-test.mjs"); - - const result = await runWithAutoInstrumentation(testScript); - - // Should succeed - the event should NOT be received on wrong channel - expect(result.exitCode).toBe(0); - expect(result.stdout).toContain("SUCCESS"); - }, 30000); - }); -}); diff --git a/js/tests/auto-instrumentations/fixtures/anthropic-channel-name-test.mjs b/js/tests/auto-instrumentations/fixtures/anthropic-channel-name-test.mjs deleted file mode 100644 index 989c2de29..000000000 --- a/js/tests/auto-instrumentations/fixtures/anthropic-channel-name-test.mjs +++ /dev/null @@ -1,55 +0,0 @@ -import Anthropic from "@anthropic-ai/sdk"; -import { _internalIso as iso } from "../../../dist/index.mjs"; - -// Subscribe to the channel we expect to be used -let eventReceived = false; -const channel = iso.newTracingChannel( - "orchestrion:@anthropic-ai/sdk:messages.create", -); - -channel.subscribe({ - start: (event) => { - eventReceived = true; - console.log("CHANNEL_EVENT_RECEIVED: true"); - }, -}); - -// Create client with mocked fetch -const mockFetch = async () => ({ - ok: true, - status: 200, - headers: new Headers({ "content-type": "application/json" }), - json: async () => ({ - id: "msg_test", - type: "message", - role: "assistant", - content: [{ type: "text", text: "Test" }], - model: "claude-3-sonnet-20240229", - stop_reason: "end_turn", - usage: { input_tokens: 1, output_tokens: 1 }, - }), -}); - -const client = new Anthropic({ - apiKey: "test-key", - fetch: mockFetch, -}); - -try { - await client.messages.create({ - model: "claude-3-sonnet-20240229", - max_tokens: 10, - messages: [{ role: "user", content: "Hi" }], - }); - - if (eventReceived) { - console.log("SUCCESS: Channel event received on correct channel"); - process.exit(0); - } else { - console.error("ERROR: Channel event NOT received - name mismatch!"); - process.exit(1); - } -} catch (error) { - console.error("ERROR:", error.message); - process.exit(1); -} diff --git a/js/tests/auto-instrumentations/fixtures/anthropic-e2e-test.mjs b/js/tests/auto-instrumentations/fixtures/anthropic-e2e-test.mjs deleted file mode 100644 index 5bf7b9c94..000000000 --- a/js/tests/auto-instrumentations/fixtures/anthropic-e2e-test.mjs +++ /dev/null @@ -1,63 +0,0 @@ -import Anthropic from "@anthropic-ai/sdk"; -import { initLogger, _exportsForTestingOnly } from "../../../dist/index.mjs"; - -// Use test background logger to capture spans -const backgroundLogger = _exportsForTestingOnly.useTestBackgroundLogger(); - -// Simulate login -await _exportsForTestingOnly.simulateLoginForTests(); - -initLogger({ - projectName: "auto-instrumentation-test", - projectId: "test-project-id", -}); - -// Create Anthropic client with mocked fetch -const mockFetch = async (url, options) => { - return { - ok: true, - status: 200, - headers: new Headers({ - "content-type": "application/json", - }), - json: async () => ({ - id: "msg_test123", - type: "message", - role: "assistant", - content: [{ type: "text", text: "Test response" }], - model: "claude-3-sonnet-20240229", - stop_reason: "end_turn", - usage: { - input_tokens: 10, - output_tokens: 5, - }, - }), - }; -}; - -const client = new Anthropic({ - apiKey: "test-key", - fetch: mockFetch, -}); - -try { - await client.messages.create({ - model: "claude-3-sonnet-20240229", - max_tokens: 1024, - messages: [{ role: "user", content: "Hello!" }], - }); - - // Drain spans - const spans = await backgroundLogger.drain(); - - // Output spans for validation - for (const span of spans) { - console.log("SPAN_DATA:", JSON.stringify(span)); - } - - console.log("SUCCESS: API call completed"); - process.exit(0); -} catch (error) { - console.error("ERROR:", error.message); - process.exit(1); -} diff --git a/js/tests/auto-instrumentations/fixtures/openai-e2e-test.mjs b/js/tests/auto-instrumentations/fixtures/openai-e2e-test.mjs deleted file mode 100644 index c8b139fc4..000000000 --- a/js/tests/auto-instrumentations/fixtures/openai-e2e-test.mjs +++ /dev/null @@ -1,61 +0,0 @@ -import OpenAI from "openai"; -import { initLogger, _exportsForTestingOnly } from "../../../dist/index.mjs"; - -const backgroundLogger = _exportsForTestingOnly.useTestBackgroundLogger(); -await _exportsForTestingOnly.simulateLoginForTests(); - -initLogger({ - projectName: "auto-instrumentation-test", - projectId: "test-project-id", -}); - -// Create OpenAI client with mocked fetch -const mockFetch = async (url, options) => { - return { - ok: true, - status: 200, - headers: new Headers({ "content-type": "application/json" }), - json: async () => ({ - id: "chatcmpl-test123", - object: "chat.completion", - created: Date.now(), - model: "gpt-4", - choices: [ - { - index: 0, - message: { role: "assistant", content: "Test response" }, - finish_reason: "stop", - }, - ], - usage: { - prompt_tokens: 10, - completion_tokens: 5, - total_tokens: 15, - }, - }), - }; -}; - -const client = new OpenAI({ - apiKey: "test-key", - fetch: mockFetch, -}); - -try { - await client.chat.completions.create({ - model: "gpt-4", - messages: [{ role: "user", content: "Hello!" }], - }); - - const spans = await backgroundLogger.drain(); - - for (const span of spans) { - console.log("SPAN_DATA:", JSON.stringify(span)); - } - - console.log("SUCCESS: API call completed"); - process.exit(0); -} catch (error) { - console.error("ERROR:", error.message); - process.exit(1); -} diff --git a/js/tests/auto-instrumentations/fixtures/wrong-channel-test.mjs b/js/tests/auto-instrumentations/fixtures/wrong-channel-test.mjs deleted file mode 100644 index 6c98b5e29..000000000 --- a/js/tests/auto-instrumentations/fixtures/wrong-channel-test.mjs +++ /dev/null @@ -1,48 +0,0 @@ -import Anthropic from "@anthropic-ai/sdk"; -import { _internalIso as iso } from "../../../dist/index.mjs"; - -// Subscribe to WRONG channel name (old bug) -let eventReceived = false; -const wrongChannel = iso.newTracingChannel( - "orchestrion:anthropic:messages.create", -); - -wrongChannel.subscribe({ - start: () => { - eventReceived = true; - }, -}); - -const mockFetch = async () => ({ - ok: true, - status: 200, - headers: new Headers({ "content-type": "application/json" }), - json: async () => ({ - id: "msg_test", - type: "message", - role: "assistant", - content: [{ type: "text", text: "Test" }], - model: "claude-3-sonnet-20240229", - stop_reason: "end_turn", - usage: { input_tokens: 1, output_tokens: 1 }, - }), -}); - -const client = new Anthropic({ - apiKey: "test-key", - fetch: mockFetch, -}); - -await client.messages.create({ - model: "claude-3-sonnet-20240229", - max_tokens: 10, - messages: [{ role: "user", content: "Hi" }], -}); - -if (eventReceived) { - console.error("ERROR: Event received on WRONG channel!"); - process.exit(1); -} else { - console.log("SUCCESS: Event correctly NOT received on wrong channel"); - process.exit(0); -}