|
| 1 | +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterEach, beforeEach, expect, test, vi } from "vite-plus/test"; |
| 5 | + |
| 6 | +const registry = vi.hoisted(() => ({ |
| 7 | + version: '"0.7.0"', |
| 8 | + error: null as Error | null, |
| 9 | + manifest: "", |
| 10 | + query: vi.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock("node:child_process", async (importOriginal) => ({ |
| 14 | + ...(await importOriginal<typeof import("node:child_process")>()), |
| 15 | + execFile: ( |
| 16 | + command: string, |
| 17 | + args: string[], |
| 18 | + options: unknown, |
| 19 | + callback: (error: Error | null, output: { stdout: string }) => void, |
| 20 | + ) => { |
| 21 | + registry.query(command, args, options); |
| 22 | + callback(registry.error, { stdout: registry.version }); |
| 23 | + }, |
| 24 | +})); |
| 25 | + |
| 26 | +vi.mock("node:module", () => ({ |
| 27 | + createRequire: () => ({ resolve: () => registry.manifest }), |
| 28 | +})); |
| 29 | + |
| 30 | +import { resolveLauncher } from "../src/commands/managed-agent/_engine/playground-launcher.ts"; |
| 31 | + |
| 32 | +let directory: string; |
| 33 | +let binary: string; |
| 34 | + |
| 35 | +beforeEach(async () => { |
| 36 | + directory = await mkdtemp(join(tmpdir(), "playground-launcher-")); |
| 37 | + binary = join(directory, "playground.js"); |
| 38 | + registry.manifest = join(directory, "package.json"); |
| 39 | + registry.version = '"0.7.0"'; |
| 40 | + registry.error = null; |
| 41 | + registry.query.mockClear(); |
| 42 | + await writeFile(binary, ""); |
| 43 | + vi.stubEnv("BAILIAN_MANAGED_AGENT_PLAYGROUND_BIN", ""); |
| 44 | + vi.stubEnv("AGENTS_PLAYGROUND_BIN", ""); |
| 45 | + vi.stubEnv("BAILIAN_MANAGED_AGENT_PLAYGROUND_VERSION", ""); |
| 46 | + vi.spyOn(process, "cwd").mockReturnValue(directory); |
| 47 | +}); |
| 48 | + |
| 49 | +afterEach(async () => { |
| 50 | + vi.unstubAllEnvs(); |
| 51 | + vi.restoreAllMocks(); |
| 52 | + await rm(directory, { recursive: true, force: true }); |
| 53 | +}); |
| 54 | + |
| 55 | +async function install(version: string) { |
| 56 | + await writeFile(registry.manifest, JSON.stringify({ version, bin: "playground.js" })); |
| 57 | +} |
| 58 | + |
| 59 | +test("checks latest on every resolution and reuses only the matching installed version", async () => { |
| 60 | + await install("0.7.0"); |
| 61 | + expect(await resolveLauncher()).toMatchObject({ |
| 62 | + args: [binary], |
| 63 | + version: "0.7.0", |
| 64 | + fetched: false, |
| 65 | + }); |
| 66 | + registry.version = '"0.8.0"'; |
| 67 | + expect(await resolveLauncher()).toMatchObject({ |
| 68 | + command: "npx", |
| 69 | + args: ["-y", "@openagentpack/playground@0.8.0"], |
| 70 | + version: "0.8.0", |
| 71 | + fetched: true, |
| 72 | + }); |
| 73 | + expect(registry.query).toHaveBeenCalledTimes(2); |
| 74 | + expect(registry.query).toHaveBeenCalledWith( |
| 75 | + "npm", |
| 76 | + expect.arrayContaining([ |
| 77 | + "view", |
| 78 | + "@openagentpack/playground@latest", |
| 79 | + "--prefer-online", |
| 80 | + "--fetch-timeout=10000", |
| 81 | + ]), |
| 82 | + expect.objectContaining({ timeout: 15_000 }), |
| 83 | + ); |
| 84 | +}); |
| 85 | + |
| 86 | +test("downloads the resolved exact version when local installation is old or missing", async () => { |
| 87 | + for (const version of ["0.6.0", undefined]) { |
| 88 | + if (version) await install(version); |
| 89 | + else await rm(registry.manifest); |
| 90 | + expect(await resolveLauncher()).toMatchObject({ |
| 91 | + command: "npx", |
| 92 | + args: ["-y", "@openagentpack/playground@0.7.0"], |
| 93 | + }); |
| 94 | + } |
| 95 | +}); |
| 96 | + |
| 97 | +test("explicit version overrides an incompatible installed version", async () => { |
| 98 | + await install("0.6.0"); |
| 99 | + vi.stubEnv("BAILIAN_MANAGED_AGENT_PLAYGROUND_VERSION", "0.7.0"); |
| 100 | + expect(await resolveLauncher()).toMatchObject({ fetched: true, version: "0.7.0" }); |
| 101 | + expect(registry.query.mock.calls[0]?.[1]).toContain("@openagentpack/playground@0.7.0"); |
| 102 | +}); |
| 103 | + |
| 104 | +test("explicit binary remains an offline development override", async () => { |
| 105 | + vi.stubEnv("BAILIAN_MANAGED_AGENT_PLAYGROUND_BIN", binary); |
| 106 | + expect(await resolveLauncher()).toMatchObject({ args: [binary], fetched: false }); |
| 107 | + expect(registry.query).not.toHaveBeenCalled(); |
| 108 | +}); |
| 109 | + |
| 110 | +test("registry failure does not silently reuse an old installation", async () => { |
| 111 | + await install("0.6.0"); |
| 112 | + registry.error = new Error("registry unavailable"); |
| 113 | + await expect(resolveLauncher()).rejects.toThrow("registry unavailable"); |
| 114 | +}); |
| 115 | + |
| 116 | +test("rejects malformed or ambiguous registry metadata with localized diagnostics", async () => { |
| 117 | + for (const output of ["invalid", '["0.6.0","0.7.0"]', '"--unsafe"']) { |
| 118 | + registry.version = output; |
| 119 | + await expect(resolveLauncher()).rejects.toThrow("single valid Playground version"); |
| 120 | + await expect(resolveLauncher()).rejects.toThrow("唯一有效的 Playground 版本号"); |
| 121 | + } |
| 122 | +}); |
| 123 | + |
| 124 | +test("local source builds must also match the resolved version", async () => { |
| 125 | + const packageRoot = join(directory, "packages/playground"); |
| 126 | + const sourceBinary = join(packageRoot, "dist/bin/playground.js"); |
| 127 | + await mkdir(join(packageRoot, "dist/bin"), { recursive: true }); |
| 128 | + await writeFile(sourceBinary, ""); |
| 129 | + await writeFile(join(packageRoot, "package.json"), JSON.stringify({ version: "0.6.0" })); |
| 130 | + expect(await resolveLauncher()).toMatchObject({ fetched: true }); |
| 131 | + await writeFile(join(packageRoot, "package.json"), JSON.stringify({ version: "0.7.0" })); |
| 132 | + expect(await resolveLauncher()).toMatchObject({ |
| 133 | + args: [sourceBinary], |
| 134 | + version: "0.7.0", |
| 135 | + fetched: false, |
| 136 | + }); |
| 137 | +}); |
0 commit comments