|
| 1 | +import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test"; |
| 2 | +import { mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { setDefaultFetch } from "@openagentpack/sdk"; |
| 6 | +import { initializeDirectoryProject } from "@openagentpack/sdk/project-workspace"; |
| 7 | +import { app } from "@/app"; |
| 8 | +import { ProjectRuntimeManager, projectRuntimeManager } from "@/services/project-manager"; |
| 9 | + |
| 10 | +const environmentKeys = [ |
| 11 | + "DASHSCOPE_API_KEY", |
| 12 | + "BAILIAN_API_KEY", |
| 13 | + "BAILIAN_BASE_URL", |
| 14 | + "BAILIAN_WORKSPACE_ID", |
| 15 | + "AGENTS_PROVIDER", |
| 16 | + "AGENTS_PLAYGROUND_TOKEN", |
| 17 | +] as const; |
| 18 | +let originalEnvironment: Map<string, string | undefined>; |
| 19 | +let directory: string; |
| 20 | +let manager: ProjectRuntimeManager; |
| 21 | +const spies: Array<{ mockRestore(): void }> = []; |
| 22 | +const page = { items: [], total: 0, total_call_count: 0, page_no: 1, page_size: 20, data_updated_at: 0 }; |
| 23 | + |
| 24 | +beforeEach(async () => { |
| 25 | + originalEnvironment = new Map(environmentKeys.map((key) => [key, process.env[key]])); |
| 26 | + for (const key of environmentKeys) delete process.env[key]; |
| 27 | + directory = await mkdtemp(join(tmpdir(), "openagentpack-usage-route-")); |
| 28 | + await initializeDirectoryProject({ projectRoot: directory }); |
| 29 | + await writeFile( |
| 30 | + join(directory, ".env"), |
| 31 | + "DASHSCOPE_API_KEY=project-test-key\nBAILIAN_BASE_URL=https://project-provider.test/api/v1/agentstudio\n", |
| 32 | + ); |
| 33 | + manager = new ProjectRuntimeManager(directory); |
| 34 | + await manager.ensureStarted(); |
| 35 | + expect(manager.getSnapshot().status).toBe("valid"); |
| 36 | + spies.push( |
| 37 | + spyOn(projectRuntimeManager, "ensureStarted").mockImplementation(() => manager.ensureStarted()), |
| 38 | + spyOn(projectRuntimeManager, "requireRuntimeInput").mockImplementation(() => manager.requireRuntimeInput()), |
| 39 | + ); |
| 40 | +}); |
| 41 | + |
| 42 | +afterEach(async () => { |
| 43 | + for (const spy of spies.splice(0)) spy.mockRestore(); |
| 44 | + manager?.close(); |
| 45 | + setDefaultFetch(undefined); |
| 46 | + for (const [key, value] of originalEnvironment) { |
| 47 | + if (value === undefined) delete process.env[key]; |
| 48 | + else process.env[key] = value; |
| 49 | + } |
| 50 | + if (directory) await rm(directory, { recursive: true, force: true }); |
| 51 | +}); |
| 52 | + |
| 53 | +function usageRequest(path: string) { |
| 54 | + return app.request(path, { |
| 55 | + method: "POST", |
| 56 | + headers: { "Content-Type": "application/json" }, |
| 57 | + body: JSON.stringify({ scope: { type: "session", session_id: "test-session" } }), |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +describe("Workbench usage Provider selection", () => { |
| 62 | + for (const [resource, processCredentials] of [ |
| 63 | + ["models", false], |
| 64 | + ["tools", true], |
| 65 | + ] as const) { |
| 66 | + test(`${resource} uses project-local credentials when process credentials are ${processCredentials ? "different" : "absent"}`, async () => { |
| 67 | + if (processCredentials) { |
| 68 | + process.env.DASHSCOPE_API_KEY = "environment-test-key"; |
| 69 | + process.env.BAILIAN_BASE_URL = "https://environment-provider.test/api/v1/agentstudio"; |
| 70 | + process.env.AGENTS_PROVIDER = "claude"; |
| 71 | + } |
| 72 | + const calls: Array<{ url: string; authorization: string | null }> = []; |
| 73 | + setDefaultFetch(async (input, init) => { |
| 74 | + calls.push({ |
| 75 | + url: input instanceof Request ? input.url : input.toString(), |
| 76 | + authorization: new Headers(init?.headers).get("Authorization"), |
| 77 | + }); |
| 78 | + return Response.json({ data: page }); |
| 79 | + }); |
| 80 | + |
| 81 | + const response = await usageRequest(`/api/usage/${resource}`); |
| 82 | + |
| 83 | + expect(response.status).toBe(200); |
| 84 | + expect(await response.json()).toEqual(page); |
| 85 | + expect(calls).toEqual([ |
| 86 | + { |
| 87 | + url: `https://project-provider.test/api/v1/agentstudio/usage/${resource}`, |
| 88 | + authorization: "Bearer project-test-key", |
| 89 | + }, |
| 90 | + ]); |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + test("rejects a missing project instead of querying with process credentials", async () => { |
| 95 | + await rm(join(directory, "project.json")); |
| 96 | + await manager.refreshAfterSourceMutation(); |
| 97 | + expect(manager.getSnapshot().status).toBe("missing"); |
| 98 | + process.env.DASHSCOPE_API_KEY = "environment-test-key"; |
| 99 | + process.env.BAILIAN_BASE_URL = "https://environment-provider.test/api/v1/agentstudio"; |
| 100 | + let requests = 0; |
| 101 | + setDefaultFetch(async () => { |
| 102 | + requests += 1; |
| 103 | + return Response.json({ data: page }); |
| 104 | + }); |
| 105 | + |
| 106 | + const response = await usageRequest("/api/usage/models"); |
| 107 | + |
| 108 | + expect(response.status).toBe(422); |
| 109 | + expect(await response.json()).toMatchObject({ error: { message: expect.stringContaining("missing") } }); |
| 110 | + expect(requests).toBe(0); |
| 111 | + }); |
| 112 | +}); |
0 commit comments