wrap.test.ts (2256B)
1 import { describe, it, expect, beforeAll } from "vitest"; 2 import * as path from "node:path"; 3 4 import type { OpenpodContext } from "../context"; 5 import { init } from "../context"; 6 import { wrap } from "./wrap"; 7 8 describe("wrap", () => { 9 let ctx: OpenpodContext; 10 11 beforeAll(async () => { 12 ctx = await init(); 13 }); 14 15 it("returns original command when cwd is outside openpodRoot", () => { 16 const command = "echo hello"; 17 const result = wrap(ctx, "/tmp/some/random/dir", command); 18 expect(result).toBe(command); 19 }); 20 21 it("returns original command when cwd is exactly openpodRoot", () => { 22 const command = "ls"; 23 const result = wrap(ctx, ctx.openpodRoot, command); 24 expect(result).toBe(command); 25 }); 26 27 it("wraps command using first directory segment as lima name", () => { 28 const session = "my-session-abc"; 29 const cwd = path.join(ctx.openpodRoot, session); 30 const command = "npm run dev"; 31 const result = wrap(ctx, cwd, command); 32 33 expect(result).toBe( 34 `limactl shell --preserve-env --workdir='${cwd}' ${session} bash -c '${command}'`, 35 ); 36 }); 37 38 it("uses only the first segment even for nested directories", () => { 39 const session = "sess-xyz"; 40 const cwd = path.join(ctx.openpodRoot, session, "src", "app"); 41 const command = "cat package.json"; 42 const result = wrap(ctx, cwd, command); 43 44 expect(result).toBe( 45 `limactl shell --preserve-env --workdir='${cwd}' ${session} bash -c '${command}'`, 46 ); 47 }); 48 49 it("escapes single quotes in cwd and command", () => { 50 const session = "sess'with'quotes"; 51 const cwd = path.join(ctx.openpodRoot, session, "sub'dir"); 52 const command = "echo 'hello world'"; 53 const result = wrap(ctx, cwd, command); 54 55 const escapedCwd = cwd.replace(/'/g, "'\\''"); 56 const escapedCmd = command.replace(/'/g, "'\\''"); 57 const expected = `limactl shell --preserve-env --workdir='${escapedCwd}' ${session} bash -c '${escapedCmd}'`; 58 59 expect(result).toBe(expected); 60 }); 61 62 it("returns original command for paths that would escape the tree", () => { 63 const command = "pwd"; 64 const outside = path.join(ctx.openpodRoot, "..", "escape"); 65 const result = wrap(ctx, outside, command); 66 expect(result).toBe(command); 67 }); 68 });