(opts: {
installer: {
kind: "npm" | "bun" | "pnpm" | "brew" | "skip";
command: string | null;
};
devMode?: boolean;
config?: ConfigShape;
env?: Record<string, string | undefined>;
})
| 13 | }; |
| 14 | |
| 15 | function setupMocks(opts: { |
| 16 | installer: { |
| 17 | kind: "npm" | "bun" | "pnpm" | "brew" | "skip"; |
| 18 | command: string | null; |
| 19 | }; |
| 20 | devMode?: boolean; |
| 21 | config?: ConfigShape; |
| 22 | env?: Record<string, string | undefined>; |
| 23 | }): { |
| 24 | writeSpy: ReturnType<typeof vi.fn>; |
| 25 | spawnSpy: ReturnType<typeof vi.fn>; |
| 26 | config: ConfigShape; |
| 27 | } { |
| 28 | vi.resetModules(); |
| 29 | |
| 30 | const config = { ...(opts.config ?? {}) }; |
| 31 | const writeSpy = vi.fn((next: ConfigShape) => { |
| 32 | Object.assign(config, next); |
| 33 | // writeConfig is given a full replacement — mirror that by pruning keys |
| 34 | // that disappeared. |
| 35 | for (const k of Object.keys(config)) { |
| 36 | if (!(k in next)) delete (config as Record<string, unknown>)[k]; |
| 37 | } |
| 38 | }); |
| 39 | |
| 40 | vi.doMock("../telemetry/config.js", () => ({ |
| 41 | readConfig: () => ({ ...config }), |
| 42 | writeConfig: writeSpy, |
| 43 | })); |
| 44 | vi.doMock("./env.js", () => ({ isDevMode: () => !!opts.devMode })); |
| 45 | vi.doMock("./installerDetection.js", () => ({ |
| 46 | detectInstaller: () => ({ |
| 47 | kind: opts.installer.kind, |
| 48 | installCommand: () => opts.installer.command, |
| 49 | reason: "test", |
| 50 | }), |
| 51 | })); |
| 52 | |
| 53 | const spawnSpy = vi.fn(() => ({ |
| 54 | pid: 42, |
| 55 | unref: () => {}, |
| 56 | })); |
| 57 | vi.doMock("node:child_process", () => ({ spawn: spawnSpy })); |
| 58 | vi.doMock("node:fs", async () => { |
| 59 | const actual = await vi.importActual<typeof import("node:fs")>("node:fs"); |
| 60 | return { |
| 61 | ...actual, |
| 62 | mkdirSync: () => {}, |
| 63 | openSync: () => 99, |
| 64 | appendFileSync: () => {}, |
| 65 | }; |
| 66 | }); |
| 67 | |
| 68 | // Clear any env knobs that would otherwise bypass the scheduling policy |
| 69 | // before the test runs. Critical for CI, where GitHub Actions always sets |
| 70 | // CI=true and would cause every scheduling assertion to fail false-negative. |
| 71 | // Tests that specifically want one of these set pass it via opts.env. |
| 72 | delete process.env["CI"]; |
no test coverage detected