|
| 1 | +import { join } from "node:path"; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { RULESYNC_COMMANDS_RELATIVE_DIR_PATH } from "../../constants/rulesync-paths.js"; |
| 4 | +import { setupTestDirectory } from "../../test-utils/test-directories.js"; |
| 5 | +import { writeFileContent } from "../../utils/file.js"; |
| 6 | +import { KiloCommand } from "./kilo-command.js"; |
| 7 | +import { RulesyncCommand } from "./rulesync-command.js"; |
| 8 | + |
| 9 | +describe("KiloCommand", () => { |
| 10 | + let testDir: string; |
| 11 | + let cleanup: () => Promise<void>; |
| 12 | + |
| 13 | + const validContent = "# Sample workflow\n\nFollow these steps."; |
| 14 | + |
| 15 | + const markdownWithFrontmatter = `--- |
| 16 | +title: Example |
| 17 | +--- |
| 18 | +
|
| 19 | +# Workflow |
| 20 | +Step 1`; |
| 21 | + |
| 22 | + beforeEach(async () => { |
| 23 | + ({ testDir, cleanup } = await setupTestDirectory()); |
| 24 | + vi.spyOn(process, "cwd").mockReturnValue(testDir); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(async () => { |
| 28 | + await cleanup(); |
| 29 | + vi.restoreAllMocks(); |
| 30 | + }); |
| 31 | + |
| 32 | + describe("getSettablePaths", () => { |
| 33 | + it("should return workflow path for project mode", () => { |
| 34 | + const paths = KiloCommand.getSettablePaths(); |
| 35 | + |
| 36 | + expect(paths).toEqual({ relativeDirPath: join(".kilocode", "workflows") }); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should use the same path in global mode", () => { |
| 40 | + const paths = KiloCommand.getSettablePaths({ global: true }); |
| 41 | + |
| 42 | + expect(paths).toEqual({ relativeDirPath: join(".kilocode", "workflows") }); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe("toRulesyncCommand", () => { |
| 47 | + it("should convert to RulesyncCommand with default frontmatter", () => { |
| 48 | + const kiloCommand = new KiloCommand({ |
| 49 | + baseDir: testDir, |
| 50 | + relativeDirPath: ".kilocode/workflows", |
| 51 | + relativeFilePath: "test.md", |
| 52 | + fileContent: validContent, |
| 53 | + validate: true, |
| 54 | + }); |
| 55 | + |
| 56 | + const rulesyncCommand = kiloCommand.toRulesyncCommand(); |
| 57 | + |
| 58 | + expect(rulesyncCommand).toBeInstanceOf(RulesyncCommand); |
| 59 | + expect(rulesyncCommand.getFrontmatter()).toEqual({ targets: ["*"], description: "" }); |
| 60 | + expect(rulesyncCommand.getBody()).toBe(validContent); |
| 61 | + expect(rulesyncCommand.getRelativeDirPath()).toBe(RULESYNC_COMMANDS_RELATIVE_DIR_PATH); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe("fromRulesyncCommand", () => { |
| 66 | + it("should create KiloCommand from RulesyncCommand", () => { |
| 67 | + const rulesyncCommand = new RulesyncCommand({ |
| 68 | + baseDir: testDir, |
| 69 | + relativeDirPath: RULESYNC_COMMANDS_RELATIVE_DIR_PATH, |
| 70 | + relativeFilePath: "workflow.md", |
| 71 | + frontmatter: { targets: ["kilo"], description: "" }, |
| 72 | + body: validContent, |
| 73 | + fileContent: validContent, |
| 74 | + validate: true, |
| 75 | + }); |
| 76 | + |
| 77 | + const kiloCommand = KiloCommand.fromRulesyncCommand({ |
| 78 | + baseDir: testDir, |
| 79 | + rulesyncCommand, |
| 80 | + }); |
| 81 | + |
| 82 | + expect(kiloCommand).toBeInstanceOf(KiloCommand); |
| 83 | + expect(kiloCommand.getRelativeDirPath()).toBe(join(".kilocode", "workflows")); |
| 84 | + expect(kiloCommand.getFileContent()).toBe(validContent); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe("validate", () => { |
| 89 | + it("should always succeed", () => { |
| 90 | + const command = new KiloCommand({ |
| 91 | + baseDir: testDir, |
| 92 | + relativeDirPath: ".kilocode/workflows", |
| 93 | + relativeFilePath: "test.md", |
| 94 | + fileContent: validContent, |
| 95 | + validate: true, |
| 96 | + }); |
| 97 | + |
| 98 | + expect(command.validate()).toEqual({ success: true, error: null }); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe("fromFile", () => { |
| 103 | + it("should load and strip frontmatter", async () => { |
| 104 | + const workflowsDir = join(testDir, ".kilocode", "workflows"); |
| 105 | + const filePath = join(workflowsDir, "workflow.md"); |
| 106 | + await writeFileContent(filePath, markdownWithFrontmatter); |
| 107 | + |
| 108 | + const command = await KiloCommand.fromFile({ |
| 109 | + baseDir: testDir, |
| 110 | + relativeFilePath: "workflow.md", |
| 111 | + }); |
| 112 | + |
| 113 | + expect(command).toBeInstanceOf(KiloCommand); |
| 114 | + expect(command.getRelativeDirPath()).toBe(join(".kilocode", "workflows")); |
| 115 | + expect(command.getFileContent()).toBe("# Workflow\nStep 1"); |
| 116 | + }); |
| 117 | + |
| 118 | + it("should support global workflows", async () => { |
| 119 | + const workflowsDir = join(testDir, ".kilocode", "workflows"); |
| 120 | + const filePath = join(workflowsDir, "global.md"); |
| 121 | + await writeFileContent(filePath, validContent); |
| 122 | + |
| 123 | + const command = await KiloCommand.fromFile({ |
| 124 | + baseDir: testDir, |
| 125 | + relativeFilePath: "global.md", |
| 126 | + global: true, |
| 127 | + }); |
| 128 | + |
| 129 | + expect(command.getRelativeDirPath()).toBe(join(".kilocode", "workflows")); |
| 130 | + expect(command.getFileContent()).toBe(validContent); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe("isTargetedByRulesyncCommand", () => { |
| 135 | + it("should return true when rulesync targets include kilo", () => { |
| 136 | + const rulesyncCommand = new RulesyncCommand({ |
| 137 | + baseDir: testDir, |
| 138 | + relativeDirPath: RULESYNC_COMMANDS_RELATIVE_DIR_PATH, |
| 139 | + relativeFilePath: "workflow.md", |
| 140 | + frontmatter: { targets: ["kilo"], description: "" }, |
| 141 | + body: validContent, |
| 142 | + fileContent: validContent, |
| 143 | + validate: true, |
| 144 | + }); |
| 145 | + |
| 146 | + expect(KiloCommand.isTargetedByRulesyncCommand(rulesyncCommand)).toBe(true); |
| 147 | + }); |
| 148 | + |
| 149 | + it("should return false when kilo is not targeted", () => { |
| 150 | + const rulesyncCommand = new RulesyncCommand({ |
| 151 | + baseDir: testDir, |
| 152 | + relativeDirPath: RULESYNC_COMMANDS_RELATIVE_DIR_PATH, |
| 153 | + relativeFilePath: "workflow.md", |
| 154 | + frontmatter: { targets: ["cursor"], description: "" }, |
| 155 | + body: validContent, |
| 156 | + fileContent: validContent, |
| 157 | + validate: true, |
| 158 | + }); |
| 159 | + |
| 160 | + expect(KiloCommand.isTargetedByRulesyncCommand(rulesyncCommand)).toBe(false); |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + describe("forDeletion", () => { |
| 165 | + it("should create deletable command placeholder", () => { |
| 166 | + const command = KiloCommand.forDeletion({ |
| 167 | + baseDir: testDir, |
| 168 | + relativeDirPath: ".kilocode/workflows", |
| 169 | + relativeFilePath: "obsolete.md", |
| 170 | + }); |
| 171 | + |
| 172 | + expect(command.isDeletable()).toBe(true); |
| 173 | + expect(command.getFileContent()).toBe(""); |
| 174 | + }); |
| 175 | + }); |
| 176 | +}); |
0 commit comments