|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest" |
| 2 | +import fs from "fs/promises" |
| 3 | +import path from "path" |
| 4 | +import { v4 as uuidv4 } from "uuid" |
| 5 | +import { getProjectId, generateProjectId, hasProjectId } from "../projectId" |
| 6 | +import { fileExistsAtPath } from "../fs" |
| 7 | + |
| 8 | +// Mock dependencies |
| 9 | +vi.mock("fs/promises") |
| 10 | +vi.mock("../fs") |
| 11 | +vi.mock("uuid") |
| 12 | + |
| 13 | +describe("projectId", () => { |
| 14 | + const mockWorkspaceRoot = "/test/workspace" |
| 15 | + const mockProjectId = "12345678-1234-1234-1234-123456789012" |
| 16 | + const projectIdPath = path.join(mockWorkspaceRoot, ".rooprojectid") |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + vi.clearAllMocks() |
| 20 | + }) |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + vi.restoreAllMocks() |
| 24 | + }) |
| 25 | + |
| 26 | + describe("getProjectId", () => { |
| 27 | + it("should return project ID when file exists and contains valid ID", async () => { |
| 28 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 29 | + vi.mocked(fs.readFile).mockResolvedValue(mockProjectId) |
| 30 | + |
| 31 | + const result = await getProjectId(mockWorkspaceRoot) |
| 32 | + |
| 33 | + expect(result).toBe(mockProjectId) |
| 34 | + expect(fileExistsAtPath).toHaveBeenCalledWith(projectIdPath) |
| 35 | + expect(fs.readFile).toHaveBeenCalledWith(projectIdPath, "utf8") |
| 36 | + }) |
| 37 | + |
| 38 | + it("should return null when file does not exist", async () => { |
| 39 | + vi.mocked(fileExistsAtPath).mockResolvedValue(false) |
| 40 | + |
| 41 | + const result = await getProjectId(mockWorkspaceRoot) |
| 42 | + |
| 43 | + expect(result).toBeNull() |
| 44 | + expect(fileExistsAtPath).toHaveBeenCalledWith(projectIdPath) |
| 45 | + expect(fs.readFile).not.toHaveBeenCalled() |
| 46 | + }) |
| 47 | + |
| 48 | + it("should return null when file is empty", async () => { |
| 49 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 50 | + vi.mocked(fs.readFile).mockResolvedValue("") |
| 51 | + |
| 52 | + const result = await getProjectId(mockWorkspaceRoot) |
| 53 | + |
| 54 | + expect(result).toBeNull() |
| 55 | + }) |
| 56 | + |
| 57 | + it("should trim whitespace from project ID", async () => { |
| 58 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 59 | + vi.mocked(fs.readFile).mockResolvedValue(` ${mockProjectId} \n`) |
| 60 | + |
| 61 | + const result = await getProjectId(mockWorkspaceRoot) |
| 62 | + |
| 63 | + expect(result).toBe(mockProjectId) |
| 64 | + }) |
| 65 | + |
| 66 | + it("should handle read errors gracefully", async () => { |
| 67 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 68 | + vi.mocked(fs.readFile).mockRejectedValue(new Error("Read error")) |
| 69 | + |
| 70 | + const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) |
| 71 | + |
| 72 | + const result = await getProjectId(mockWorkspaceRoot) |
| 73 | + |
| 74 | + expect(result).toBeNull() |
| 75 | + expect(consoleErrorSpy).toHaveBeenCalledWith("Failed to read project ID: Error: Read error") |
| 76 | + }) |
| 77 | + }) |
| 78 | + |
| 79 | + describe("generateProjectId", () => { |
| 80 | + it("should generate and write a new project ID", async () => { |
| 81 | + vi.mocked(uuidv4).mockReturnValue(mockProjectId as any) |
| 82 | + vi.mocked(fs.writeFile).mockResolvedValue() |
| 83 | + |
| 84 | + const result = await generateProjectId(mockWorkspaceRoot) |
| 85 | + |
| 86 | + expect(result).toBe(mockProjectId) |
| 87 | + expect(uuidv4).toHaveBeenCalled() |
| 88 | + expect(fs.writeFile).toHaveBeenCalledWith(projectIdPath, mockProjectId, "utf8") |
| 89 | + }) |
| 90 | + |
| 91 | + it("should handle write errors", async () => { |
| 92 | + vi.mocked(uuidv4).mockReturnValue(mockProjectId as any) |
| 93 | + vi.mocked(fs.writeFile).mockRejectedValue(new Error("Write error")) |
| 94 | + |
| 95 | + await expect(generateProjectId(mockWorkspaceRoot)).rejects.toThrow("Write error") |
| 96 | + }) |
| 97 | + }) |
| 98 | + |
| 99 | + describe("hasProjectId", () => { |
| 100 | + it("should return true when project ID file exists", async () => { |
| 101 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 102 | + |
| 103 | + const result = await hasProjectId(mockWorkspaceRoot) |
| 104 | + |
| 105 | + expect(result).toBe(true) |
| 106 | + expect(fileExistsAtPath).toHaveBeenCalledWith(projectIdPath) |
| 107 | + }) |
| 108 | + |
| 109 | + it("should return false when project ID file does not exist", async () => { |
| 110 | + vi.mocked(fileExistsAtPath).mockResolvedValue(false) |
| 111 | + |
| 112 | + const result = await hasProjectId(mockWorkspaceRoot) |
| 113 | + |
| 114 | + expect(result).toBe(false) |
| 115 | + expect(fileExistsAtPath).toHaveBeenCalledWith(projectIdPath) |
| 116 | + }) |
| 117 | + }) |
| 118 | +}) |
0 commit comments