|
| 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 { createRulesGenerationTaskMessage } from "../rulesGenerator" |
| 5 | + |
| 6 | +// Mock fs module |
| 7 | +vi.mock("fs/promises", () => ({ |
| 8 | + mkdir: vi.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +describe("rulesGenerator", () => { |
| 12 | + const mockWorkspacePath = "/test/workspace" |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + vi.clearAllMocks() |
| 16 | + }) |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + vi.restoreAllMocks() |
| 20 | + }) |
| 21 | + |
| 22 | + describe("createRulesGenerationTaskMessage", () => { |
| 23 | + it("should create directories when alwaysAllowWriteProtected is true", async () => { |
| 24 | + await createRulesGenerationTaskMessage(mockWorkspacePath, ["general"], false, true) |
| 25 | + |
| 26 | + // Verify mkdir was called for each directory |
| 27 | + expect(fs.mkdir).toHaveBeenCalledWith(path.join(mockWorkspacePath, ".roo", "rules"), { recursive: true }) |
| 28 | + expect(fs.mkdir).toHaveBeenCalledWith(path.join(mockWorkspacePath, ".roo", "rules-code"), { |
| 29 | + recursive: true, |
| 30 | + }) |
| 31 | + expect(fs.mkdir).toHaveBeenCalledWith(path.join(mockWorkspacePath, ".roo", "rules-architect"), { |
| 32 | + recursive: true, |
| 33 | + }) |
| 34 | + expect(fs.mkdir).toHaveBeenCalledWith(path.join(mockWorkspacePath, ".roo", "rules-debug"), { |
| 35 | + recursive: true, |
| 36 | + }) |
| 37 | + expect(fs.mkdir).toHaveBeenCalledWith(path.join(mockWorkspacePath, ".roo", "rules-docs-extractor"), { |
| 38 | + recursive: true, |
| 39 | + }) |
| 40 | + }) |
| 41 | + |
| 42 | + it("should NOT create directories when alwaysAllowWriteProtected is false", async () => { |
| 43 | + await createRulesGenerationTaskMessage(mockWorkspacePath, ["general"], false, false) |
| 44 | + |
| 45 | + // Verify mkdir was NOT called |
| 46 | + expect(fs.mkdir).not.toHaveBeenCalled() |
| 47 | + }) |
| 48 | + |
| 49 | + it("should include auto-approval note in message when alwaysAllowWriteProtected is true", async () => { |
| 50 | + const message = await createRulesGenerationTaskMessage(mockWorkspacePath, ["general"], false, true) |
| 51 | + |
| 52 | + expect(message).toContain("The directory has already been created for you") |
| 53 | + expect(message).toContain("Auto-approval for protected file writes is enabled") |
| 54 | + }) |
| 55 | + |
| 56 | + it("should include manual approval note in message when alwaysAllowWriteProtected is false", async () => { |
| 57 | + const message = await createRulesGenerationTaskMessage(mockWorkspacePath, ["general"], false, false) |
| 58 | + |
| 59 | + expect(message).toContain("Create the necessary directories if they don't exist") |
| 60 | + expect(message).toContain("You will need to approve the creation of protected directories and files") |
| 61 | + }) |
| 62 | + |
| 63 | + it("should handle multiple rule types", async () => { |
| 64 | + const message = await createRulesGenerationTaskMessage( |
| 65 | + mockWorkspacePath, |
| 66 | + ["general", "code", "architect"], |
| 67 | + false, |
| 68 | + true, |
| 69 | + ) |
| 70 | + |
| 71 | + expect(message).toContain(".roo/rules/coding-standards.md") |
| 72 | + expect(message).toContain(".roo/rules-code/implementation-rules.md") |
| 73 | + expect(message).toContain(".roo/rules-architect/architecture-rules.md") |
| 74 | + }) |
| 75 | + |
| 76 | + it("should include gitignore instructions when addToGitignore is true", async () => { |
| 77 | + const message = await createRulesGenerationTaskMessage(mockWorkspacePath, ["general"], true, false) |
| 78 | + |
| 79 | + expect(message).toContain("Add the generated files to .gitignore") |
| 80 | + }) |
| 81 | + |
| 82 | + it("should include analysis steps for each rule type", async () => { |
| 83 | + const message = await createRulesGenerationTaskMessage(mockWorkspacePath, ["code"], false, false) |
| 84 | + |
| 85 | + // Check that code-specific analysis steps are included |
| 86 | + expect(message).toContain("Analyze package.json or equivalent files") |
| 87 | + expect(message).toContain("Check for linting and formatting tools") |
| 88 | + expect(message).toContain("Examine test files to understand testing patterns") |
| 89 | + }) |
| 90 | + |
| 91 | + it("should include different analysis steps for different rule types", async () => { |
| 92 | + const message = await createRulesGenerationTaskMessage(mockWorkspacePath, ["architect"], false, false) |
| 93 | + |
| 94 | + // Check that architect-specific analysis steps are included |
| 95 | + expect(message).toContain("Analyze the overall directory structure") |
| 96 | + expect(message).toContain("Identify architectural patterns") |
| 97 | + expect(message).toContain("separation of concerns") |
| 98 | + }) |
| 99 | + }) |
| 100 | +}) |
0 commit comments