handle.test.ts (8289B)
1 import { 2 describe, 3 it, 4 expect, 5 beforeAll, 6 beforeEach, 7 afterEach, 8 vi, 9 } from "vitest"; 10 import * as fs from "node:fs"; 11 import * as path from "node:path"; 12 import * as os from "node:os"; 13 14 import type { OpenpodContext } from "../context"; 15 import { init } from "../context"; 16 import { exec } from "../context/exec"; 17 import { handle } from "./handle"; 18 19 vi.mock("../context/exec", () => ({ 20 exec: vi.fn(), 21 })); 22 23 describe("handle", () => { 24 let mockMove: ReturnType< 25 typeof vi.fn<(sessionId: string, directory: string) => void> 26 >; 27 let ctx: OpenpodContext; 28 let baseCtx: OpenpodContext; 29 const execMock = vi.mocked(exec); 30 31 beforeAll(async () => { 32 execMock.mockResolvedValue({ 33 status: 0, 34 stdout: "", 35 stderr: "", 36 }); 37 baseCtx = await init(); 38 }); 39 40 beforeEach(() => { 41 vi.clearAllMocks(); 42 execMock.mockReset(); 43 execMock.mockResolvedValue({ 44 status: 0, 45 stdout: "", 46 stderr: "", 47 }); 48 49 const tempRoot = path.join( 50 os.tmpdir(), 51 `openpod-test-${Date.now()}-${Math.random()}`, 52 ); 53 if (fs.existsSync(tempRoot)) { 54 fs.rmSync(tempRoot, { recursive: true, force: true }); 55 } 56 fs.mkdirSync(tempRoot, { recursive: true }); 57 58 ctx = { 59 ...baseCtx, 60 openpodRoot: tempRoot, 61 }; 62 ctx.cleanup.execute = vi.fn(); 63 mockMove = vi.fn(); 64 }); 65 66 afterEach(() => { 67 if (fs.existsSync(ctx.openpodRoot)) { 68 fs.rmSync(ctx.openpodRoot, { recursive: true, force: true }); 69 } 70 }); 71 72 it("returns a handle function that can be called", async () => { 73 // just ensure no throw on basic call 74 await handle({ 75 ctx, 76 sessionId: "test", 77 dir: "/tmp/some", 78 move: mockMove, 79 }); 80 }); 81 82 it("returns early if not git enabled", async () => { 83 execMock.mockResolvedValue({ status: 1, stdout: "", stderr: "" }); 84 85 await handle({ 86 ctx, 87 sessionId: "sess1", 88 dir: "/project/dir", 89 move: mockMove, 90 }); 91 92 expect(mockMove).not.toHaveBeenCalled(); 93 expect(execMock).toHaveBeenCalledWith("git", [ 94 "-C", 95 "/project/dir", 96 "rev-parse", 97 "--is-inside-work-tree", 98 ]); 99 }); 100 101 it("returns early for invalid git (no origin)", async () => { 102 execMock 103 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }) // isGitEnabled 104 .mockResolvedValueOnce({ status: 1, stdout: "", stderr: "" }); // getOriginUrl 105 106 const dir = "/project/dir"; 107 await handle({ ctx, sessionId: "sess1", dir, move: mockMove }); 108 109 expect(mockMove).not.toHaveBeenCalled(); 110 expect(execMock).toHaveBeenCalledWith( 111 "git", 112 ["-C", dir, "remote", "get-url", "origin"], 113 { encoding: "utf8" }, 114 ); 115 }); 116 117 it("clones project and moves when needs move and target does not exist", async () => { 118 const sessionId = "clone-sess"; 119 const dir = "/real/project"; 120 const target = path.join(ctx.openpodRoot, sessionId); 121 const origin = "https://example.com/repo.git"; 122 123 execMock 124 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }) // isGit 125 .mockResolvedValueOnce({ 126 status: 0, 127 stdout: origin + "\n", 128 stderr: "", 129 }) // getOrigin 130 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }) // clone git 131 .mockResolvedValueOnce({ status: 1, stdout: "", stderr: "" }) // lima list (not exists) 132 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }) // create lima 133 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }); // start lima 134 135 await handle({ ctx, sessionId, dir, move: mockMove }); 136 137 expect(execMock).toHaveBeenCalledWith("git", ["clone", origin, target]); 138 expect(mockMove).toHaveBeenCalledWith(sessionId, target); 139 140 // lima create called 141 expect(execMock).toHaveBeenCalledWith("limactl", [ 142 "create", 143 `--name=${sessionId}`, 144 `--mount-only=${target}:w`, 145 "template:default", 146 "--yes", 147 ]); 148 // start 149 expect(execMock).toHaveBeenCalledWith("limactl", [ 150 "start", 151 sessionId, 152 "--yes", 153 ]); 154 }); 155 156 it("moves session when target already exists (no clone)", async () => { 157 const sessionId = "existing"; 158 const dir = "/other/location"; 159 const target = path.join(ctx.openpodRoot, sessionId); 160 fs.mkdirSync(target, { recursive: true }); 161 162 execMock 163 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }) // is git 164 .mockResolvedValueOnce({ status: 1, stdout: "", stderr: "" }) // lima not 165 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }) // create 166 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }); // start 167 168 await handle({ ctx, sessionId, dir, move: mockMove }); 169 170 expect(mockMove).toHaveBeenCalledWith(sessionId, target); 171 expect(execMock).not.toHaveBeenCalledWith("git", [ 172 "clone", 173 expect.anything(), 174 expect.anything(), 175 ]); 176 }); 177 178 it("does not move when already at target dir", async () => { 179 const sessionId = "same"; 180 const target = path.join(ctx.openpodRoot, sessionId); 181 fs.mkdirSync(target, { recursive: true }); 182 183 execMock 184 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }) // git 185 .mockResolvedValueOnce({ status: 1, stdout: "", stderr: "" }) // lima not exist 186 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }) 187 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }); 188 189 await handle({ ctx, sessionId, dir: target, move: mockMove }); 190 191 expect(mockMove).not.toHaveBeenCalled(); 192 }); 193 194 it("skips create if lima instance already exists", async () => { 195 const sessionId = "has-lima"; 196 const dir = path.join(ctx.openpodRoot, sessionId); 197 fs.mkdirSync(dir, { recursive: true }); 198 199 execMock 200 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }) // git 201 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }) // lima exists 202 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }); // start only 203 204 await handle({ ctx, sessionId, dir, move: mockMove }); 205 206 expect(execMock).not.toHaveBeenCalledWith( 207 "limactl", 208 expect.arrayContaining(["create"]), 209 ); 210 expect(execMock).toHaveBeenCalledWith("limactl", [ 211 "start", 212 sessionId, 213 "--yes", 214 ]); 215 }); 216 217 it("handles lima create failure but continues to start", async () => { 218 const sessionId = "fail-create"; 219 const dir = "/proj"; 220 221 execMock 222 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }) // git 223 .mockResolvedValueOnce({ status: 0, stdout: "url\n", stderr: "" }) // origin 224 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }) // clone 225 .mockResolvedValueOnce({ status: 1, stdout: "", stderr: "" }) // lima not 226 .mockResolvedValueOnce({ status: 42, stdout: "", stderr: "" }) // create fails 227 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }); // start succeeds 228 229 await handle({ ctx, sessionId, dir, move: mockMove }); 230 231 expect(execMock).toHaveBeenCalledWith("limactl", [ 232 "start", 233 sessionId, 234 "--yes", 235 ]); 236 }); 237 238 it("handles lima start failure (does not throw)", async () => { 239 const sessionId = "fail-start"; 240 const dir = path.join(ctx.openpodRoot, sessionId); 241 fs.mkdirSync(dir, { recursive: true }); 242 243 execMock 244 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }) // git 245 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }) // lima exists 246 .mockResolvedValueOnce({ status: 1, stdout: "", stderr: "" }); // start fails 247 248 await handle({ ctx, sessionId, dir, move: mockMove }); 249 }); 250 251 it("handles git clone failure and does not move", async () => { 252 const sessionId = "clone-fail"; 253 const dir = "/proj"; 254 const origin = "https://bad"; 255 256 execMock 257 .mockResolvedValueOnce({ status: 0, stdout: "", stderr: "" }) // git 258 .mockResolvedValueOnce({ 259 status: 0, 260 stdout: origin + "\n", 261 stderr: "", 262 }) 263 .mockResolvedValueOnce({ status: 1, stdout: "", stderr: "" }); // clone fails 264 265 await handle({ ctx, sessionId, dir, move: mockMove }); 266 267 expect(mockMove).not.toHaveBeenCalled(); 268 // no lima/start since returned early 269 expect(execMock).not.toHaveBeenCalledWith("limactl", expect.any(Array)); 270 }); 271 });