| 1 | import { parse } from "yaml"; |
| 2 | |
| 3 | import { updateAnthropicModelInYaml } from "./yamlConfigUpdater.js"; |
| 4 | |
| 5 | describe("updateAnthropicModelInYaml", () => { |
| 6 | const testApiKey = "sk-ant-test123456789"; |
| 7 | |
| 8 | describe("empty or invalid input", () => { |
| 9 | it("should create new config from empty string", () => { |
| 10 | const result = updateAnthropicModelInYaml("", testApiKey); |
| 11 | |
| 12 | expect(result).toContain("name: Local Config"); |
| 13 | expect(result).toContain("version: 1.0.0"); |
| 14 | expect(result).toContain("schema: v1"); |
| 15 | expect(result).toContain("uses: anthropic/claude-4-sonnet"); |
| 16 | expect(result).toContain("ANTHROPIC_API_KEY: sk-ant-test123456789"); |
| 17 | }); |
| 18 | |
| 19 | it("should create new config from invalid YAML", () => { |
| 20 | const invalidYaml = "invalid: [yaml content"; |
| 21 | const result = updateAnthropicModelInYaml(invalidYaml, testApiKey); |
| 22 | |
| 23 | expect(result).toContain("name: Local Config"); |
| 24 | expect(result).toContain("uses: anthropic/claude-4-sonnet"); |
| 25 | expect(result).toContain("ANTHROPIC_API_KEY: sk-ant-test123456789"); |
| 26 | }); |
| 27 | }); |
| 28 | |
| 29 | describe("comment preservation", () => { |
| 30 | it("should preserve comments when adding new model", () => { |
| 31 | const yamlWithComments = `# My Continue config |
| 32 | name: Local Config |
| 33 | version: 1.0.0 |
| 34 | schema: v1 |
| 35 | # List of available models |
| 36 | models: |
| 37 | - uses: openai/gpt-4 |
| 38 | with: |
| 39 | OPENAI_API_KEY: TEST-openai-test |
| 40 | `; |
| 41 | |
| 42 | const result = updateAnthropicModelInYaml(yamlWithComments, testApiKey); |
| 43 | |
| 44 | expect(result).toContain("# My Continue config"); |
| 45 | expect(result).toContain("# List of available models"); |
| 46 | expect(result).toContain("uses: openai/gpt-4"); |
| 47 | expect(result).toContain("uses: anthropic/claude-4-sonnet"); |
| 48 | expect(result).toContain("ANTHROPIC_API_KEY: sk-ant-test123456789"); |
| 49 | }); |
| 50 | |
| 51 | it("should preserve comments when updating existing model", () => { |
| 52 | const yamlWithComments = `# My Continue config |
| 53 | name: Local Config |
| 54 | version: 1.0.0 |
| 55 | schema: v1 |
| 56 | # List of available models |
| 57 | models: |
| 58 | - uses: anthropic/claude-4-sonnet |
| 59 | with: |
| 60 | ANTHROPIC_API_KEY: old-key |
| 61 | `; |
| 62 | |
| 63 | const result = updateAnthropicModelInYaml(yamlWithComments, testApiKey); |
| 64 | |
| 65 | expect(result).toContain("# My Continue config"); |
| 66 | expect(result).toContain("# List of available models"); |
| 67 | expect(result).toContain("uses: anthropic/claude-4-sonnet"); |
| 68 | expect(result).toContain("ANTHROPIC_API_KEY: sk-ant-test123456789"); |
| 69 | expect(result).not.toContain("old-key"); |
| 70 | }); |
| 71 | }); |
| 72 | |
| 73 | describe("model management", () => { |
| 74 | it("should add new anthropic model when none exists", () => { |
| 75 | const existingConfig = `name: Local Config |
| 76 | version: 1.0.0 |
| 77 | schema: v1 |
| 78 | models: |
| 79 | - uses: openai/gpt-4 |
| 80 | with: |
| 81 | OPENAI_API_KEY: TEST-openai-test |
| 82 | `; |
| 83 | |
| 84 | const result = updateAnthropicModelInYaml(existingConfig, testApiKey); |
| 85 | |
| 86 | expect(result).toContain("uses: openai/gpt-4"); |
| 87 | expect(result).toContain("uses: anthropic/claude-4-sonnet"); |
| 88 | expect(result).toContain("ANTHROPIC_API_KEY: sk-ant-test123456789"); |
| 89 | expect(result).toContain("OPENAI_API_KEY: TEST-openai-test"); |
| 90 | }); |
| 91 | |
| 92 | it("should update existing anthropic model", () => { |
| 93 | const existingConfig = `name: Local Config |
| 94 | version: 1.0.0 |
| 95 | schema: v1 |
| 96 | models: |
| 97 | - uses: anthropic/claude-4-sonnet |
| 98 | with: |
| 99 | ANTHROPIC_API_KEY: old-anthropic-key |
| 100 | - uses: openai/gpt-4 |
| 101 | with: |
| 102 | OPENAI_API_KEY: TEST-openai-test |
| 103 | `; |
| 104 | |
| 105 | const result = updateAnthropicModelInYaml(existingConfig, testApiKey); |
| 106 | |
| 107 | expect(result).toContain("uses: anthropic/claude-4-sonnet"); |
| 108 | expect(result).toContain("uses: openai/gpt-4"); |
| 109 | expect(result).toContain("ANTHROPIC_API_KEY: sk-ant-test123456789"); |
| 110 | expect(result).toContain("OPENAI_API_KEY: TEST-openai-test"); |
| 111 | expect(result).not.toContain("old-anthropic-key"); |
| 112 | |
| 113 | // Should only have one anthropic model |
| 114 | const anthropicMatches = result.match( |
| 115 | /uses: anthropic\/claude-4-sonnet/g, |
| 116 | ); |
| 117 | expect(anthropicMatches).toHaveLength(1); |
| 118 | }); |
| 119 | |
| 120 | it("should handle config with no models array", () => { |
| 121 | const configWithoutModels = `name: Local Config |
| 122 | version: 1.0.0 |
| 123 | schema: v1 |
| 124 | `; |
| 125 | |
| 126 | const result = updateAnthropicModelInYaml( |
| 127 | configWithoutModels, |
| 128 | testApiKey, |
| 129 | ); |
| 130 | |
| 131 | expect(result).toContain("name: Local Config"); |
| 132 | expect(result).toContain("models:"); |
| 133 | expect(result).toContain("uses: anthropic/claude-4-sonnet"); |
| 134 | expect(result).toContain("ANTHROPIC_API_KEY: sk-ant-test123456789"); |
| 135 | }); |
| 136 | |
| 137 | it("should handle config with empty models array", () => { |
| 138 | const configWithEmptyModels = `name: Local Config |
| 139 | version: 1.0.0 |
| 140 | schema: v1 |
| 141 | models: [] |
| 142 | `; |
| 143 | |
| 144 | const result = updateAnthropicModelInYaml( |
| 145 | configWithEmptyModels, |
| 146 | testApiKey, |
| 147 | ); |
| 148 | |
| 149 | expect(result).toContain("name: Local Config"); |
| 150 | expect(result).toContain("uses: anthropic/claude-4-sonnet"); |
| 151 | expect(result).toContain("ANTHROPIC_API_KEY: sk-ant-test123456789"); |
| 152 | }); |
| 153 | }); |
| 154 | |
| 155 | describe("structure validation", () => { |
| 156 | it("should produce valid YAML that can be parsed", () => { |
| 157 | const input = `# Test config |
| 158 | name: Test |
| 159 | models: |
| 160 | - uses: existing/model |
| 161 | `; |
| 162 | |
| 163 | const result = updateAnthropicModelInYaml(input, testApiKey); |
| 164 | |
| 165 | // Should not throw when parsing |
| 166 | expect(() => { |
| 167 | parse(result); |
| 168 | }).not.toThrow(); |
| 169 | }); |
| 170 | |
| 171 | it("should maintain proper YAML structure", () => { |
| 172 | const result = updateAnthropicModelInYaml("", testApiKey); |
| 173 | |
| 174 | expect(result).toMatch(/^name: /m); |
| 175 | expect(result).toMatch(/^version: /m); |
| 176 | expect(result).toMatch(/^schema: /m); |
| 177 | expect(result).toMatch(/^models:/m); |
| 178 | expect(result).toMatch(/^\s+- uses: /m); |
| 179 | expect(result).toMatch(/^\s+with:/m); |
| 180 | expect(result).toMatch(/^\s+ANTHROPIC_API_KEY: /m); |
| 181 | }); |
| 182 | }); |
| 183 | |
| 184 | describe("edge cases", () => { |
| 185 | it("should handle malformed models array gracefully", () => { |
| 186 | const malformedConfig = `name: Local Config |
| 187 | models: "not an array" |
| 188 | `; |
| 189 | |
| 190 | const result = updateAnthropicModelInYaml(malformedConfig, testApiKey); |
| 191 | |
| 192 | expect(result).toContain("uses: anthropic/claude-4-sonnet"); |
| 193 | expect(result).toContain("ANTHROPIC_API_KEY: sk-ant-test123456789"); |
| 194 | }); |
| 195 | |
| 196 | it("should handle different API key formats", () => { |
| 197 | const differentKeys = [ |
| 198 | "sk-ant-1234567890", |
| 199 | "sk-ant-abcdefghijklmnop", |
| 200 | "sk-ant-test-key-with-dashes", |
| 201 | ]; |
| 202 | |
| 203 | differentKeys.forEach((key) => { |
| 204 | const result = updateAnthropicModelInYaml("", key); |
| 205 | expect(result).toContain(`ANTHROPIC_API_KEY: ${key}`); |
| 206 | }); |
| 207 | }); |
| 208 | }); |
| 209 | }); |