|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { ZodTypeAny } from "zod"; |
| 3 | +import { buildApiTools } from "./tool-api"; |
| 4 | +import { createApiClient } from "./tool-api-client"; |
| 5 | +import { |
| 6 | + getErrorSchema, |
| 7 | + getQueueSchema, |
| 8 | + getRunSchema, |
| 9 | + getRunTraceSchema, |
| 10 | + listRunsSchema, |
| 11 | +} from "./tool-schemas"; |
| 12 | + |
| 13 | +/** |
| 14 | + * The `project`/`environment` override on data lookups: the JWT exchange has to target |
| 15 | + * the override, not ctx, and cache per target the same way the default path does. The |
| 16 | + * default path (no override) must be byte-for-byte unchanged. |
| 17 | + */ |
| 18 | + |
| 19 | +const ORIGIN = "https://api.example.com"; |
| 20 | + |
| 21 | +type Call = { url: string; body?: unknown }; |
| 22 | +let calls: Call[] = []; |
| 23 | + |
| 24 | +function stubFetch() { |
| 25 | + return vi.fn(async (input: any, init: any = {}) => { |
| 26 | + const url = typeof input === "string" ? input : input.url; |
| 27 | + calls.push({ url, body: init.body ? JSON.parse(init.body) : undefined }); |
| 28 | + if (url.endsWith("/jwt")) { |
| 29 | + // The env JWT is minted for whichever project/environment segment the exchange |
| 30 | + // addressed, so the token echoes it back for the assertions below. |
| 31 | + const match = url.match(/\/api\/v1\/projects\/([^/]+)\/([^/]+)\/jwt$/); |
| 32 | + return Response.json({ token: `jwt:${match![1]}/${match![2]}` }); |
| 33 | + } |
| 34 | + return Response.json({ data: [] }); |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +function tools() { |
| 39 | + const ctx = { |
| 40 | + userActorToken: "uat", |
| 41 | + apiOrigin: ORIGIN, |
| 42 | + projectRef: "proj_current", |
| 43 | + environmentName: "prod", |
| 44 | + }; |
| 45 | + return buildApiTools({ |
| 46 | + ctx, |
| 47 | + client: createApiClient(ctx), |
| 48 | + renderInvestigations: (() => []) as any, |
| 49 | + spanLedger: { recordTraceSpans: () => {} }, |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +const jwtCalls = () => calls.filter((c) => c.url.endsWith("/jwt")); |
| 54 | + |
| 55 | +beforeEach(() => { |
| 56 | + calls = []; |
| 57 | + vi.stubGlobal("fetch", stubFetch()); |
| 58 | +}); |
| 59 | +afterEach(() => vi.unstubAllGlobals()); |
| 60 | + |
| 61 | +describe("the project/environment override", () => { |
| 62 | + it("exchanges the JWT for the overridden project and environment, not ctx's", async () => { |
| 63 | + const t = tools(); |
| 64 | + |
| 65 | + await (t.list_runs as any).execute( |
| 66 | + { project: "proj_other", environment: "staging" }, |
| 67 | + {} as any |
| 68 | + ); |
| 69 | + |
| 70 | + expect(jwtCalls()).toHaveLength(1); |
| 71 | + expect(jwtCalls()[0].url).toBe(`${ORIGIN}/api/v1/projects/proj_other/staging/jwt`); |
| 72 | + }); |
| 73 | + |
| 74 | + it("leaves the default path (no override) unchanged", async () => { |
| 75 | + const t = tools(); |
| 76 | + |
| 77 | + await (t.list_runs as any).execute({}, {} as any); |
| 78 | + |
| 79 | + expect(jwtCalls()).toHaveLength(1); |
| 80 | + expect(jwtCalls()[0].url).toBe(`${ORIGIN}/api/v1/projects/proj_current/prod/jwt`); |
| 81 | + }); |
| 82 | + |
| 83 | + it("caches the exchanged JWT per target within the turn", async () => { |
| 84 | + const t = tools(); |
| 85 | + |
| 86 | + await (t.list_runs as any).execute( |
| 87 | + { project: "proj_other", environment: "staging" }, |
| 88 | + {} as any |
| 89 | + ); |
| 90 | + await (t.get_error as any).execute( |
| 91 | + { errorId: "error_1", project: "proj_other", environment: "staging" }, |
| 92 | + {} as any |
| 93 | + ); |
| 94 | + await (t.list_runs as any).execute({}, {} as any); |
| 95 | + |
| 96 | + // One exchange for the override target, one for the default target — never re-exchanged. |
| 97 | + expect(jwtCalls()).toHaveLength(2); |
| 98 | + }); |
| 99 | + |
| 100 | + it("defaults environment to the current environment's name when only project is given", async () => { |
| 101 | + const t = tools(); |
| 102 | + |
| 103 | + await (t.get_run as any).execute({ runId: "run_1", project: "proj_other" }, {} as any); |
| 104 | + |
| 105 | + expect(jwtCalls()[0].url).toBe(`${ORIGIN}/api/v1/projects/proj_other/prod/jwt`); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +describe("project/environment schema round-trip", () => { |
| 110 | + it.each([ |
| 111 | + ["list_runs", listRunsSchema, {}], |
| 112 | + ["get_run", getRunSchema, { runId: "run_1" }], |
| 113 | + ["get_run_trace", getRunTraceSchema, { runId: "run_1" }], |
| 114 | + ["get_error", getErrorSchema, { errorId: "error_1" }], |
| 115 | + ["get_queue", getQueueSchema, { queue: "my-queue" }], |
| 116 | + ])("%s accepts project/environment and stays valid without them", (_name, schema, base) => { |
| 117 | + const inputSchema = schema.inputSchema as ZodTypeAny; |
| 118 | + const withOverride = inputSchema.safeParse({ |
| 119 | + ...base, |
| 120 | + project: "proj_other", |
| 121 | + environment: "staging", |
| 122 | + }); |
| 123 | + expect(withOverride.success).toBe(true); |
| 124 | + |
| 125 | + const withoutOverride = inputSchema.safeParse(base); |
| 126 | + expect(withoutOverride.success).toBe(true); |
| 127 | + }); |
| 128 | +}); |
0 commit comments