73 lines
2.6 KiB
TypeScript
73 lines
2.6 KiB
TypeScript
import { describe, expect, test, mock, beforeEach, afterAll } from "bun:test";
|
|
|
|
// Mock child_process BEFORE importing the service
|
|
const mockExec = mock((cmd: string, callback: any) => {
|
|
// console.log("Mock Exec Called with:", cmd);
|
|
if (cmd.includes("git rev-parse")) {
|
|
callback(null, { stdout: "main\n" });
|
|
} else if (cmd.includes("git fetch")) {
|
|
callback(null, { stdout: "" });
|
|
} else if (cmd.includes("git log")) {
|
|
callback(null, { stdout: "abcdef Update 1\n123456 Update 2" });
|
|
} else if (cmd.includes("git diff")) {
|
|
callback(null, { stdout: "package.json\nsrc/index.ts" });
|
|
} else if (cmd.includes("bun install")) {
|
|
callback(null, { stdout: "Installed dependencies" });
|
|
} else {
|
|
callback(null, { stdout: "" });
|
|
}
|
|
});
|
|
|
|
mock.module("child_process", () => ({
|
|
exec: mockExec
|
|
}));
|
|
|
|
describe("UpdateService", () => {
|
|
let UpdateService: any;
|
|
|
|
beforeEach(async () => {
|
|
mockExec.mockClear();
|
|
// Dynamically import to ensure mock is used
|
|
const module = await import("./update.service");
|
|
UpdateService = module.UpdateService;
|
|
});
|
|
|
|
afterAll(() => {
|
|
mock.restore();
|
|
});
|
|
|
|
test("checkForUpdates should return updates if log is not empty", async () => {
|
|
const result = await UpdateService.checkForUpdates();
|
|
expect(result.hasUpdates).toBe(true);
|
|
expect(result.branch).toBe("main");
|
|
// Check calls. Note: promisify wraps exec, so expecting specific arguments might be tricky if promisify adds options.
|
|
// But the command string should be there.
|
|
// calls[0] -> rev-parse
|
|
// calls[1] -> fetch
|
|
// calls[2] -> log
|
|
expect(mockExec).toHaveBeenCalledTimes(3);
|
|
});
|
|
|
|
test("checkDependencies should detect package.json change", async () => {
|
|
const changed = await UpdateService.checkDependencies("main");
|
|
expect(changed).toBe(true);
|
|
// Note: checking args on mockExec when called via promisify:
|
|
// promisify passes (command, callback) or (command, options, callback).
|
|
// call arguments: [cmd, callback]
|
|
const lastCall = mockExec.mock.lastCall;
|
|
expect(lastCall).toBeDefined();
|
|
if (lastCall) {
|
|
expect(lastCall[0]).toContain("git diff");
|
|
}
|
|
});
|
|
|
|
test("installDependencies should run bun install", async () => {
|
|
await UpdateService.installDependencies();
|
|
const lastCall = mockExec.mock.lastCall;
|
|
expect(lastCall).toBeDefined();
|
|
if (lastCall) {
|
|
expect(lastCall[0]).toContain("bun install");
|
|
}
|
|
});
|
|
});
|