mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-08-19 21:53:48 +00:00
47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import type * as ChildProcess from "node:child_process";
|
|
import type * as Fs from "node:fs";
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { ensureTool, type ToolStatus } from "../src/utils/tools-manager.ts";
|
|
|
|
const originalOffline = process.env.PI_OFFLINE;
|
|
|
|
vi.mock("fs", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof Fs>();
|
|
return {
|
|
...actual,
|
|
existsSync: vi.fn(() => false),
|
|
};
|
|
});
|
|
|
|
vi.mock("child_process", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof ChildProcess>();
|
|
return {
|
|
...actual,
|
|
spawnSync: vi.fn(() => ({ error: new Error("not found") })),
|
|
};
|
|
});
|
|
|
|
afterEach(() => {
|
|
if (originalOffline === undefined) delete process.env.PI_OFFLINE;
|
|
else process.env.PI_OFFLINE = originalOffline;
|
|
});
|
|
|
|
describe("ensureTool", () => {
|
|
it("reports status through a callback without writing to the console", async () => {
|
|
process.env.PI_OFFLINE = "1";
|
|
const statuses: ToolStatus[] = [];
|
|
const consoleLog = vi.spyOn(console, "log").mockImplementation(() => {});
|
|
|
|
const result = await ensureTool("fd", (status) => statuses.push(status));
|
|
|
|
expect(result).toBeUndefined();
|
|
expect(statuses).toEqual([
|
|
{
|
|
type: "warning",
|
|
message: "fd not found. Offline mode enabled, skipping download.",
|
|
},
|
|
]);
|
|
expect(consoleLog).not.toHaveBeenCalled();
|
|
consoleLog.mockRestore();
|
|
});
|
|
});
|