| 1 | import { BlockType, RULE_FILE_EXTENSION } from "@continuedev/config-yaml"; |
| 2 | import { describe, expect, test } from "vitest"; |
| 3 | import { findAvailableFilename, getFileContent } from "./workspaceBlocks"; |
| 4 | |
| 5 | describe("getFileContent", () => { |
| 6 | test("returns markdown content for rules block type", () => { |
| 7 | const result = getFileContent("rules"); |
| 8 | expect(result).toContain("Your rule content"); |
| 9 | expect(result).toContain("A description of your rule"); |
| 10 | }); |
| 11 | |
| 12 | test("returns YAML content for non-rules block types", () => { |
| 13 | const result = getFileContent("models"); |
| 14 | expect(result).toContain("name: New model"); |
| 15 | expect(result).toContain("version: 0.0.1"); |
| 16 | expect(result).toContain("schema: v1"); |
| 17 | expect(result).toContain("models:"); |
| 18 | expect(result).toContain("provider: anthropic"); |
| 19 | }); |
| 20 | |
| 21 | test("generates correct content for different block types", () => { |
| 22 | const contextResult = getFileContent("context"); |
| 23 | expect(contextResult).toContain("name: New context"); |
| 24 | expect(contextResult).toContain("context:"); |
| 25 | expect(contextResult).toContain("provider: file"); |
| 26 | |
| 27 | const docsResult = getFileContent("docs"); |
| 28 | expect(docsResult).toContain("name: New doc"); |
| 29 | expect(docsResult).toContain("docs:"); |
| 30 | expect(docsResult).toContain("startUrl: https://docs.continue.dev"); |
| 31 | |
| 32 | const promptsResult = getFileContent("prompts"); |
| 33 | expect(promptsResult).toContain("name: New prompt"); |
| 34 | expect(promptsResult).toContain("description: New prompt"); |
| 35 | expect(promptsResult).toContain("invokable: true"); |
| 36 | expect(promptsResult).toContain("thorough suite of unit tests"); |
| 37 | expect(promptsResult).toContain("---"); // Should be markdown with frontmatter |
| 38 | |
| 39 | const mcpResult = getFileContent("mcpServers"); |
| 40 | expect(mcpResult).toContain("name: New MCP server"); |
| 41 | expect(mcpResult).toContain("mcpServers:"); |
| 42 | expect(mcpResult).toContain("command: npx"); |
| 43 | }); |
| 44 | }); |
| 45 | |
| 46 | describe("findAvailableFilename", () => { |
| 47 | test("returns base filename when it doesn't exist", async () => { |
| 48 | const mockFileExists = async (uri: string) => false; |
| 49 | |
| 50 | const result = await findAvailableFilename( |
| 51 | "/workspace/.continue/models", |
| 52 | "models", |
| 53 | mockFileExists, |
| 54 | ); |
| 55 | |
| 56 | expect(result).toBe("/workspace/.continue/models/new-model.yaml"); |
| 57 | }); |
| 58 | |
| 59 | test("returns filename with counter when base exists", async () => { |
| 60 | const mockFileExists = async (uri: string) => { |
| 61 | return uri === "/workspace/.continue/models/new-model.yaml"; |
| 62 | }; |
| 63 | |
| 64 | const result = await findAvailableFilename( |
| 65 | "/workspace/.continue/models", |
| 66 | "models", |
| 67 | mockFileExists, |
| 68 | ); |
| 69 | |
| 70 | expect(result).toBe("/workspace/.continue/models/new-model-1.yaml"); |
| 71 | }); |
| 72 | |
| 73 | test("increments counter until available filename is found", async () => { |
| 74 | const existingFiles = new Set([ |
| 75 | "/workspace/.continue/context/new-context.yaml", |
| 76 | "/workspace/.continue/context/new-context-1.yaml", |
| 77 | "/workspace/.continue/context/new-context-2.yaml", |
| 78 | ]); |
| 79 | |
| 80 | const mockFileExists = async (uri: string) => { |
| 81 | return existingFiles.has(uri); |
| 82 | }; |
| 83 | |
| 84 | const result = await findAvailableFilename( |
| 85 | "/workspace/.continue/context", |
| 86 | "context", |
| 87 | mockFileExists, |
| 88 | ); |
| 89 | |
| 90 | expect(result).toBe("/workspace/.continue/context/new-context-3.yaml"); |
| 91 | }); |
| 92 | |
| 93 | test("handles different block types correctly with proper extensions", async () => { |
| 94 | const mockFileExists = async (uri: string) => false; |
| 95 | |
| 96 | const testCases: Array<{ blockType: BlockType; expected: string }> = [ |
| 97 | { blockType: "models", expected: "/test/new-model.yaml" }, |
| 98 | { blockType: "context", expected: "/test/new-context.yaml" }, |
| 99 | { blockType: "rules", expected: `/test/new-rule.${RULE_FILE_EXTENSION}` }, |
| 100 | { blockType: "docs", expected: "/test/new-doc.yaml" }, |
| 101 | { blockType: "prompts", expected: "/test/new-prompt.md" }, |
| 102 | { blockType: "mcpServers", expected: "/test/new-mcp-server.yaml" }, |
| 103 | ]; |
| 104 | |
| 105 | for (const { blockType, expected } of testCases) { |
| 106 | const result = await findAvailableFilename( |
| 107 | "/test", |
| 108 | blockType, |
| 109 | mockFileExists, |
| 110 | ); |
| 111 | expect(result).toBe(expected); |
| 112 | } |
| 113 | }); |
| 114 | |
| 115 | test("respects custom extension parameter", async () => { |
| 116 | const mockFileExists = async (uri: string) => false; |
| 117 | |
| 118 | const result = await findAvailableFilename( |
| 119 | "/test", |
| 120 | "models", |
| 121 | mockFileExists, |
| 122 | "json", |
| 123 | ); |
| 124 | |
| 125 | expect(result).toBe("/test/new-model.json"); |
| 126 | }); |
| 127 | |
| 128 | test("handles rules markdown files with counter", async () => { |
| 129 | const existingFiles = new Set([ |
| 130 | `/workspace/.continue/rules/new-rule.${RULE_FILE_EXTENSION}`, |
| 131 | `/workspace/.continue/rules/new-rule-1.${RULE_FILE_EXTENSION}`, |
| 132 | ]); |
| 133 | |
| 134 | const mockFileExists = async (uri: string) => { |
| 135 | return existingFiles.has(uri); |
| 136 | }; |
| 137 | |
| 138 | const result = await findAvailableFilename( |
| 139 | "/workspace/.continue/rules", |
| 140 | "rules", |
| 141 | mockFileExists, |
| 142 | ); |
| 143 | |
| 144 | expect(result).toBe( |
| 145 | `/workspace/.continue/rules/new-rule-2.${RULE_FILE_EXTENSION}`, |
| 146 | ); |
| 147 | }); |
| 148 | |
| 149 | test("handles large counter values", async () => { |
| 150 | const existingFiles = new Set( |
| 151 | Array.from({ length: 100 }, (_, i) => |
| 152 | i === 0 |
| 153 | ? "/workspace/.continue/prompts/new-prompt.md" |
| 154 | : `/workspace/.continue/prompts/new-prompt-${i}.md`, |
| 155 | ), |
| 156 | ); |
| 157 | |
| 158 | const mockFileExists = async (uri: string) => { |
| 159 | return existingFiles.has(uri); |
| 160 | }; |
| 161 | |
| 162 | const result = await findAvailableFilename( |
| 163 | "/workspace/.continue/prompts", |
| 164 | "prompts", |
| 165 | mockFileExists, |
| 166 | ); |
| 167 | |
| 168 | expect(result).toBe("/workspace/.continue/prompts/new-prompt-100.md"); |
| 169 | }); |
| 170 | }); |