cleanup.test.ts (2255B)
1 import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; 2 import * as fs from "node:fs"; 3 import * as path from "node:path"; 4 import * as os from "node:os"; 5 6 import { initCleanup } from "./cleanup"; 7 import { exec } from "./exec"; 8 9 vi.mock("./exec", () => ({ 10 exec: vi.fn(), 11 })); 12 13 describe("cleanup", () => { 14 let tempRoot: string; 15 const execMock = vi.mocked(exec); 16 17 beforeEach(() => { 18 vi.clearAllMocks(); 19 execMock.mockResolvedValue({ 20 status: 0, 21 stdout: "", 22 stderr: "", 23 }); 24 tempRoot = path.join( 25 os.tmpdir(), 26 `openpod-test-${Date.now()}-${Math.random()}`, 27 ); 28 if (fs.existsSync(tempRoot)) { 29 fs.rmSync(tempRoot, { recursive: true, force: true }); 30 } 31 fs.mkdirSync(tempRoot, { recursive: true }); 32 }); 33 34 afterEach(() => { 35 if (fs.existsSync(tempRoot)) { 36 fs.rmSync(tempRoot, { recursive: true, force: true }); 37 } 38 }); 39 40 it("returns a cleanup with execute function", () => { 41 const cleanup = initCleanup(tempRoot); 42 expect(cleanup).toHaveProperty("execute"); 43 expect(typeof cleanup.execute).toBe("function"); 44 }); 45 46 it("returns early for invalid sessionID", async () => { 47 const cleanup = initCleanup(tempRoot); 48 await cleanup.execute(""); 49 // @ts-expect-error testing runtime guard 50 await cleanup.execute(null); 51 // @ts-expect-error testing runtime guard 52 await cleanup.execute(undefined); 53 54 expect(execMock).not.toHaveBeenCalled(); 55 }); 56 57 it("attempts lima stop/delete", async () => { 58 const cleanup = initCleanup(tempRoot); 59 const sessionID = "test-session"; 60 await cleanup.execute(sessionID); 61 62 expect(execMock).toHaveBeenCalledWith("limactl", [ 63 "stop", 64 sessionID, 65 "--force", 66 ]); 67 expect(execMock).toHaveBeenCalledWith("limactl", [ 68 "delete", 69 sessionID, 70 "--force", 71 ]); 72 }); 73 74 it("removes existing pod directory", async () => { 75 const sessionID = "to-remove"; 76 const podDir = path.join(tempRoot, sessionID); 77 fs.mkdirSync(podDir, { recursive: true }); 78 fs.writeFileSync(path.join(podDir, "file.txt"), "data"); 79 80 const cleanup = initCleanup(tempRoot); 81 await cleanup.execute(sessionID); 82 83 expect(fs.existsSync(podDir)).toBe(false); 84 }); 85 });