102 lines
4.2 KiB
TypeScript
102 lines
4.2 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import type { InstalledHttpContract } from "../../../src/contracts/external-contract-runtime.ts";
|
|
import { safeTraceparent } from "../../../src/adapters/telemetry/best-effort-telemetry.ts";
|
|
import {
|
|
buildRequestHeaders,
|
|
createTraceContext,
|
|
} from "../../../src/platform/telemetry/trace-context.ts";
|
|
import { createExecutionContext } from "../../../src/platform/execution/execution-context.ts";
|
|
import { TEST_LIST_HTTP_CONTRACT } from "../../helpers/external-contract-fixture.ts";
|
|
import {
|
|
jsonRows,
|
|
scopeSnapshot,
|
|
testExecutor,
|
|
type Transport,
|
|
} from "../../helpers/traffic-test-kit.ts";
|
|
|
|
const VALID_TRACE_ID = "4bf92f3577b34da6a3ce929d0e0e4736";
|
|
|
|
function testExecutionContext(
|
|
options: Readonly<{ interactionId: string; traceId: string }>,
|
|
) {
|
|
return createExecutionContext({
|
|
interactionId: options.interactionId,
|
|
deadlineMs: 5_000,
|
|
traffic: { maxPhysicalRequests: 4, maxRetries: 1, maxConcurrentRequests: 2 },
|
|
trace: createTraceContext({ traceId: options.traceId }),
|
|
});
|
|
}
|
|
|
|
describe("trace and correlation propagation", () => {
|
|
it("propagates one interaction trace identity to child requests", () => {
|
|
const context = testExecutionContext({ interactionId: "i-1", traceId: VALID_TRACE_ID });
|
|
|
|
const a = buildRequestHeaders(context);
|
|
const b = buildRequestHeaders(context);
|
|
|
|
expect(a.get("traceparent")).toBe(b.get("traceparent"));
|
|
expect(a.get("x-correlation-id")).toBe("i-1");
|
|
});
|
|
|
|
it("emits a W3C traceparent the existing validator accepts", () => {
|
|
const trace = createTraceContext({ traceId: VALID_TRACE_ID });
|
|
expect(safeTraceparent(trace.traceparent)).toBe(trace.traceparent);
|
|
expect(trace.traceparent.startsWith(`00-${VALID_TRACE_ID}-`)).toBe(true);
|
|
expect(safeTraceparent(createTraceContext().traceparent)).not.toBeNull();
|
|
});
|
|
|
|
it("rejects malformed or all-zero trace ids", () => {
|
|
expect(() => createTraceContext({ traceId: "0".repeat(32) })).toThrow(TypeError);
|
|
expect(() => createTraceContext({ traceId: "XYZ" })).toThrow(TypeError);
|
|
});
|
|
|
|
it("sends the same trace headers on every physical request of the interaction", async () => {
|
|
const seen: Array<Headers> = [];
|
|
const transport = vi.fn<Transport>(async (_input, init) => {
|
|
seen.push(new Headers(init?.headers));
|
|
return jsonRows(1);
|
|
});
|
|
const execution = testExecutionContext({ interactionId: "i-9", traceId: VALID_TRACE_ID });
|
|
const executor = testExecutor(transport);
|
|
const operation = TEST_LIST_HTTP_CONTRACT as InstalledHttpContract<unknown, unknown, unknown>;
|
|
const context = { routeId: "TEST_ROUTE", scope: scopeSnapshot(), execution };
|
|
|
|
await executor.execute(operation, { limit: 1 }, context);
|
|
await executor.execute(operation, { limit: 1 }, context);
|
|
|
|
expect(seen).toHaveLength(2);
|
|
expect(seen[0]?.get("traceparent")).toBe(execution.trace?.traceparent);
|
|
expect(seen[1]?.get("traceparent")).toBe(execution.trace?.traceparent);
|
|
expect(seen.every((headers) => headers.get("x-correlation-id") === "i-9")).toBe(true);
|
|
});
|
|
|
|
it("adds no trace headers to an interaction that did not opt in", async () => {
|
|
const seen: Array<Headers> = [];
|
|
const transport = vi.fn<Transport>(async (_input, init) => {
|
|
seen.push(new Headers(init?.headers));
|
|
return jsonRows(1);
|
|
});
|
|
await testExecutor(transport).execute(
|
|
TEST_LIST_HTTP_CONTRACT as InstalledHttpContract<unknown, unknown, unknown>,
|
|
{ limit: 1 },
|
|
{ routeId: "TEST_ROUTE", scope: scopeSnapshot() },
|
|
);
|
|
expect(seen[0]?.get("traceparent")).toBeNull();
|
|
expect(seen[0]?.get("x-correlation-id")).toBeNull();
|
|
});
|
|
|
|
it("does not let a credential collaborator forge the trace headers", async () => {
|
|
const transport = vi.fn<Transport>(async () => jsonRows(1));
|
|
const outcome = await testExecutor(transport, {
|
|
credentialHeaders: { traceparent: `00-${VALID_TRACE_ID}-00f067aa0ba902b7-01` },
|
|
}).execute(
|
|
TEST_LIST_HTTP_CONTRACT as InstalledHttpContract<unknown, unknown, unknown>,
|
|
{ limit: 1 },
|
|
{ routeId: "TEST_ROUTE", scope: scopeSnapshot() },
|
|
);
|
|
expect(outcome.kind).toBe("AUTH_INTEGRATION_FAILURE");
|
|
expect(transport).not.toHaveBeenCalled();
|
|
});
|
|
});
|