|
| 1 | +import { expect, test } from "bun:test"; |
| 2 | +import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { |
| 6 | + localCliEntrypoint, |
| 7 | + runLocalIntelligenceCli, |
| 8 | +} from "./local-intelligence-cli"; |
| 9 | + |
| 10 | +async function fixture( |
| 11 | + run: ( |
| 12 | + root: string, |
| 13 | + install: (path: string, version?: string, code?: string) => Promise<string>, |
| 14 | + ) => Promise<void>, |
| 15 | +) { |
| 16 | + const root = await mkdtemp(join(tmpdir(), "openbot-cli-discovery-")); |
| 17 | + try { |
| 18 | + await run( |
| 19 | + root, |
| 20 | + async ( |
| 21 | + path, |
| 22 | + version = "4.99.0", |
| 23 | + code = 'console.log(JSON.stringify({status:"completed",stacks:[],args:process.argv.slice(2),secret:process.env.INTELLIGENCE_API_KEY ?? null}));', |
| 24 | + ) => { |
| 25 | + await mkdir(path, { recursive: true }); |
| 26 | + await writeFile( |
| 27 | + join(path, "package.json"), |
| 28 | + JSON.stringify({ |
| 29 | + name: "copilotkit", |
| 30 | + version, |
| 31 | + bin: { copilotkit: "index.js" }, |
| 32 | + }), |
| 33 | + ); |
| 34 | + const entry = join(path, "index.js"); |
| 35 | + await writeFile(entry, code); |
| 36 | + return entry; |
| 37 | + }, |
| 38 | + ); |
| 39 | + } finally { |
| 40 | + await rm(root, { recursive: true, force: true }); |
| 41 | + } |
| 42 | +} |
| 43 | +const minimalPath = "/usr/bin:/bin:/usr/sbin:/sbin"; |
| 44 | + |
| 45 | +test("minimal-PATH child uses an existing npm cache offline without exposing credentials", async () => { |
| 46 | + await fixture(async (root, install) => { |
| 47 | + await install(join(root, ".npm/_npx/cache/node_modules/copilotkit")); |
| 48 | + const environment = { |
| 49 | + PATH: minimalPath, |
| 50 | + HOME: root, |
| 51 | + npm_config_registry: "http://127.0.0.1:1", |
| 52 | + INTELLIGENCE_API_KEY: "private-value", |
| 53 | + }; |
| 54 | + const result = await runLocalIntelligenceCli( |
| 55 | + ["local", "list", "--json"], |
| 56 | + root, |
| 57 | + { home: root, globalRoots: [], environment }, |
| 58 | + ); |
| 59 | + expect(result.exitCode).toBe(0); |
| 60 | + expect(JSON.parse(result.stdout)).toEqual({ |
| 61 | + status: "completed", |
| 62 | + stacks: [], |
| 63 | + args: ["local", "list", "--json"], |
| 64 | + secret: null, |
| 65 | + }); |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +test("installed CLI wins over cache; otherwise newest cached version wins deterministically", async () => { |
| 70 | + await fixture(async (root, install) => { |
| 71 | + await install(join(root, ".npm/_npx/z/node_modules/copilotkit"), "4.9.0"); |
| 72 | + await install( |
| 73 | + join(root, ".npm/_npx/pre/node_modules/copilotkit"), |
| 74 | + "4.10.0-beta.1", |
| 75 | + ); |
| 76 | + await install( |
| 77 | + join(root, ".npm/_npx/broken/node_modules/copilotkit"), |
| 78 | + "invalid-version", |
| 79 | + ); |
| 80 | + const latest = await install( |
| 81 | + join(root, ".npm/_npx/a/node_modules/copilotkit"), |
| 82 | + "4.10.0", |
| 83 | + ); |
| 84 | + expect( |
| 85 | + await localCliEntrypoint(root, { PATH: minimalPath }, root, []), |
| 86 | + ).toBe(latest); |
| 87 | + const installed = await install( |
| 88 | + join(root, "node_modules/copilotkit"), |
| 89 | + "4.8.0", |
| 90 | + ); |
| 91 | + expect( |
| 92 | + await localCliEntrypoint(root, { PATH: minimalPath }, root, []), |
| 93 | + ).toBe(installed); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +test("missing and malformed cache entries are unavailable without a registry invocation", async () => { |
| 98 | + await fixture(async (root) => { |
| 99 | + const cache = join(root, ".npm/_npx/partial/node_modules/copilotkit"); |
| 100 | + await mkdir(cache, { recursive: true }); |
| 101 | + await writeFile(join(cache, "package.json"), "not json"); |
| 102 | + expect( |
| 103 | + await runLocalIntelligenceCli([], root, { |
| 104 | + home: root, |
| 105 | + globalRoots: [], |
| 106 | + environment: { PATH: minimalPath }, |
| 107 | + }), |
| 108 | + ).toEqual({ exitCode: 127, stdout: "" }); |
| 109 | + }); |
| 110 | +}); |
| 111 | + |
| 112 | +test("runtime failure is retained and stderr is not returned", async () => { |
| 113 | + await fixture(async (root, install) => { |
| 114 | + await install( |
| 115 | + join(root, "node_modules/copilotkit"), |
| 116 | + "4.0.0", |
| 117 | + 'console.error("private diagnostic"); process.exit(2);', |
| 118 | + ); |
| 119 | + expect( |
| 120 | + await runLocalIntelligenceCli([], root, { |
| 121 | + home: root, |
| 122 | + globalRoots: [], |
| 123 | + environment: { PATH: minimalPath }, |
| 124 | + }), |
| 125 | + ).toEqual({ exitCode: 2, stdout: "" }); |
| 126 | + }); |
| 127 | +}); |
| 128 | + |
| 129 | +test("cancellation terminates discovery and timeout is a runtime failure", async () => { |
| 130 | + await fixture(async (root, install) => { |
| 131 | + await install( |
| 132 | + join(root, "node_modules/copilotkit"), |
| 133 | + "4.0.0", |
| 134 | + "setInterval(() => {}, 1000);", |
| 135 | + ); |
| 136 | + const controller = new AbortController(); |
| 137 | + const pending = runLocalIntelligenceCli([], root, { |
| 138 | + home: root, |
| 139 | + globalRoots: [], |
| 140 | + environment: { PATH: minimalPath }, |
| 141 | + signal: controller.signal, |
| 142 | + }); |
| 143 | + setTimeout(() => controller.abort(new Error("cancelled")), 50); |
| 144 | + await expect(pending).rejects.toThrow("cancelled"); |
| 145 | + expect( |
| 146 | + ( |
| 147 | + await runLocalIntelligenceCli([], root, { |
| 148 | + home: root, |
| 149 | + globalRoots: [], |
| 150 | + environment: { PATH: minimalPath }, |
| 151 | + timeoutMs: 50, |
| 152 | + }) |
| 153 | + ).exitCode, |
| 154 | + ).toBe(1); |
| 155 | + }); |
| 156 | +}); |
0 commit comments