import { mkdirSync, mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; import type { PrismaClient } from "./client.js"; import { assertProjectWorkspaceDirectory, InvalidProjectWorkspacePathError, normalizeProjectWorkspacePath, wireProjectWorkspace, } from "./project-workspace.js"; vi.mock("@rakazo/core/git-checkout", () => ({ readGitCheckout: vi.fn(async () => ({ present: true, remoteUrl: "https://github.com/example/demo.git", defaultBranch: "main", headCommit: "abc123", dirty: false, dirtyPaths: [], })), })); describe("project workspace wiring", () => { const roots: string[] = []; afterEach(() => { for (const root of roots.splice(0)) { rmSync(root, { recursive: true, force: true }); } }); function tempDir(prefix: string) { const root = mkdtempSync(path.join(tmpdir(), prefix)); roots.push(root); return root; } it("normalizes absolute workspace paths", () => { const root = tempDir("project-workspace-normalize-"); expect(normalizeProjectWorkspacePath(root)).toBe(path.resolve(root)); }); it("rejects missing folders", async () => { const missing = path.join(tmpdir(), `missing-${Date.now()}`); await expect(assertProjectWorkspaceDirectory(missing)).rejects.toBeInstanceOf( InvalidProjectWorkspacePathError, ); }); it("persists workspace and git metadata on the space", async () => { const root = tempDir("project-workspace-wire-"); mkdirSync(path.join(root, ".git")); const updates: Array> = []; const prisma = { space: { update: vi.fn(async ({ data }: { data: Record }) => { updates.push(data); }), }, }; const wired = await wireProjectWorkspace(prisma as unknown as Pick, { spaceId: "space-1", workspacePath: root, }); expect(wired.path).toBe(path.resolve(root)); expect(wired.git).toEqual({ remoteUrl: "https://github.com/example/demo.git", defaultBranch: "main", headCommit: "abc123", dirty: false, dirtyPaths: [], }); expect(updates[0]).toMatchObject({ workspacePath: path.resolve(root), gitRemoteUrl: "https://github.com/example/demo.git", gitDefaultBranch: "main", gitHeadCommit: "abc123", }); }); });