| 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 YAML 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("prompts:"); |
| 35 | expect(promptsResult).toContain("thorough suite of unit tests"); |
| 36 | |
| 37 | const mcpResult = getFileContent("mcpServers"); |
| 38 | expect(mcpResult).toContain("name: New MCP server"); |
| 39 | expect(mcpResult).toContain("mcpServers:"); |
| 40 | expect(mcpResult).toContain("command: npx"); |
| 41 | }); |
| 42 | }); |
| 43 | |
| 44 | describe("findAvailableFilename", () => { |
| 45 | test("returns base filename when it doesn't exist", async () => { |
| 46 | const mockFileExists = async (uri: string) => false; |
| 47 | |
| 48 | const result = await findAvailableFilename( |
| 49 | "/workspace/.continue/models", |
| 50 | "models", |
| 51 | mockFileExists, |
| 52 | ); |
| 53 | |
| 54 | expect(result).toBe("/workspace/.continue/models/new-model.yaml"); |
| 55 | }); |
| 56 | |
| 57 | test("returns filename with counter when base exists", async () => { |
| 58 | const mockFileExists = async (uri: string) => { |
| 59 | return uri === "/workspace/.continue/models/new-model.yaml"; |
| 60 | }; |
| 61 | |
| 62 | const result = await findAvailableFilename( |
| 63 | "/workspace/.continue/models", |
| 64 | "models", |
| 65 | mockFileExists, |
| 66 | ); |
| 67 | |
| 68 | expect(result).toBe("/workspace/.continue/models/new-model-1.yaml"); |
| 69 | }); |
| 70 | |
| 71 | test("increments counter until available filename is found", async () => { |
| 72 | const existingFiles = new Set([ |
| 73 | "/workspace/.continue/context/new-context.yaml", |
| 74 | "/workspace/.continue/context/new-context-1.yaml", |
| 75 | "/workspace/.continue/context/new-context-2.yaml", |
| 76 | ]); |
| 77 | |
| 78 | const mockFileExists = async (uri: string) => { |
| 79 | return existingFiles.has(uri); |
| 80 | }; |
| 81 | |
| 82 | const result = await findAvailableFilename( |
| 83 | "/workspace/.continue/context", |
| 84 | "context", |
| 85 | mockFileExists, |
| 86 | ); |
| 87 | |
| 88 | expect(result).toBe("/workspace/.continue/context/new-context-3.yaml"); |
| 89 | }); |
| 90 | |
| 91 | test("handles different block types correctly with proper extensions", async () => { |
| 92 | const mockFileExists = async (uri: string) => false; |
| 93 | |
| 94 | const testCases: Array<{ blockType: BlockType; expected: string }> = [ |
| 95 | { blockType: "models", expected: "/test/new-model.yaml" }, |
| 96 | { blockType: "context", expected: "/test/new-context.yaml" }, |
| 97 | { blockType: "rules", expected: `/test/new-rule.${RULE_FILE_EXTENSION}` }, |
| 98 | { blockType: "docs", expected: "/test/new-doc.yaml" }, |
| 99 | { blockType: "prompts", expected: "/test/new-prompt.yaml" }, |
| 100 | { blockType: "mcpServers", expected: "/test/new-mcp-server.yaml" }, |
| 101 | ]; |
| 102 | |
| 103 | for (const { blockType, expected } of testCases) { |
| 104 | const result = await findAvailableFilename( |
| 105 | "/test", |
| 106 | blockType, |
| 107 | mockFileExists, |
| 108 | ); |
| 109 | expect(result).toBe(expected); |
| 110 | } |
| 111 | }); |
| 112 | |
| 113 | test("respects custom extension parameter", async () => { |
| 114 | const mockFileExists = async (uri: string) => false; |
| 115 | |
| 116 | const result = await findAvailableFilename( |
| 117 | "/test", |
| 118 | "models", |
| 119 | mockFileExists, |
| 120 | "json", |
| 121 | ); |
| 122 | |
| 123 | expect(result).toBe("/test/new-model.json"); |
| 124 | }); |
| 125 | |
| 126 | test("handles rules markdown files with counter", async () => { |
| 127 | const existingFiles = new Set([ |
| 128 | `/workspace/.continue/rules/new-rule.${RULE_FILE_EXTENSION}`, |
| 129 | `/workspace/.continue/rules/new-rule-1.${RULE_FILE_EXTENSION}`, |
| 130 | ]); |
| 131 | |
| 132 | const mockFileExists = async (uri: string) => { |
| 133 | return existingFiles.has(uri); |
| 134 | }; |
| 135 | |
| 136 | const result = await findAvailableFilename( |
| 137 | "/workspace/.continue/rules", |
| 138 | "rules", |
| 139 | mockFileExists, |
| 140 | ); |
| 141 | |
| 142 | expect(result).toBe( |
| 143 | `/workspace/.continue/rules/new-rule-2.${RULE_FILE_EXTENSION}`, |
| 144 | ); |
| 145 | }); |
| 146 | |
| 147 | test("handles large counter values", async () => { |
| 148 | const existingFiles = new Set( |
| 149 | Array.from({ length: 100 }, (_, i) => |
| 150 | i === 0 |
| 151 | ? "/workspace/.continue/prompts/new-prompt.yaml" |
| 152 | : `/workspace/.continue/prompts/new-prompt-${i}.yaml`, |
| 153 | ), |
| 154 | ); |
| 155 | |
| 156 | const mockFileExists = async (uri: string) => { |
| 157 | return existingFiles.has(uri); |
| 158 | }; |
| 159 | |
| 160 | const result = await findAvailableFilename( |
| 161 | "/workspace/.continue/prompts", |
| 162 | "prompts", |
| 163 | mockFileExists, |
| 164 | ); |
| 165 | |
| 166 | expect(result).toBe("/workspace/.continue/prompts/new-prompt-100.yaml"); |
| 167 | }); |
| 168 | }); |