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