import { describe, expect, it, vi } from "vitest"; import { createContractHttpExecutor } from "../../../src/adapters/http/index.ts"; import { createHttpObservationProjector } from "../../../src/bootstrap/runtime-adapters.ts"; import { createReferenceFeatureInstalledInput } from "../../../src/features/reference-feature/adapters/create-reference-feature-input.ts"; import { REFERENCE_FEATURE_TEMPLATE_CONTRIBUTION } from "../../../src/features/reference-feature/contracts/reference-feature-contract-contribution.ts"; function scopeSnapshot() { return Object.freeze({ generation: 1, fingerprint: "reference-scope", identities: Object.freeze({}) as never, signal: new AbortController().signal, isCurrent: () => true, }); } describe("reference feature HTTP diagnostics", () => { it("preserves route and operation identity through the installed V3 path", async () => { const record = vi.fn(); const contractHttp = createContractHttpExecutor({ baseUrl: "https://api.test", maxRetryAttempts: 0, attachCredentials: () => ({ kind: "READY" as const, headers: { authorization: "Bearer diagnostics-test-token" }, }), fetcher: (async () => Response.json([ { id: "reference-1", name: "Reference" }, ])) as unknown as typeof fetch, observe: createHttpObservationProjector({ diagnostics: { record }, telemetry: { emit: vi.fn() }, }), }); const operations = new Map( REFERENCE_FEATURE_TEMPLATE_CONTRIBUTION.http.map((operation) => [ operation.contract.operationId, operation, ]), ); const installed = createReferenceFeatureInstalledInput({ contractOperations: Object.freeze({ async execute(operationId, input, context) { const operation = operations.get(operationId); if (!operation) throw new Error("Unregistered reference operation"); return contractHttp.execute(operation, input, { routeId: context.routeId, scope: scopeSnapshot(), ...(context.signal === undefined ? {} : { signal: context.signal }), ...(context.intent === undefined ? {} : { intent: context.intent }), }); }, }), }); await expect( installed.input.listResources({ limit: 20 }), ).resolves.toMatchObject({ ok: true }); expect(record).toHaveBeenCalledOnce(); expect(record).toHaveBeenCalledWith({ level: "info", eventId: "http.request.completed", context: expect.objectContaining({ route_id: "REFERENCE_RESOURCE_LIST", operation_id: "LIST_REFERENCE_RESOURCES", outcome: "SUCCESS", }), }); }); });