|
| 1 | +import assert from "node:assert" |
| 2 | +import { describe, it } from "node:test" |
| 3 | +import { getInvalidConfigKeys, validateConfigTypes } from "../lib/config" |
| 4 | + |
| 5 | +function createConfig(modelLimits?: Record<string, number | string>) { |
| 6 | + return { |
| 7 | + enabled: true, |
| 8 | + debug: false, |
| 9 | + pruneNotification: "minimal", |
| 10 | + pruneNotificationType: "chat", |
| 11 | + commands: { |
| 12 | + enabled: true, |
| 13 | + protectedTools: [], |
| 14 | + }, |
| 15 | + turnProtection: { |
| 16 | + enabled: false, |
| 17 | + turns: 0, |
| 18 | + }, |
| 19 | + protectedFilePatterns: [], |
| 20 | + tools: { |
| 21 | + settings: { |
| 22 | + nudgeEnabled: true, |
| 23 | + nudgeFrequency: 5, |
| 24 | + protectedTools: [], |
| 25 | + contextLimit: "60%", |
| 26 | + ...(modelLimits !== undefined ? { modelLimits } : {}), |
| 27 | + }, |
| 28 | + distill: { |
| 29 | + permission: "allow", |
| 30 | + showDistillation: false, |
| 31 | + }, |
| 32 | + compress: { |
| 33 | + permission: "deny", |
| 34 | + showCompression: false, |
| 35 | + }, |
| 36 | + prune: { |
| 37 | + permission: "allow", |
| 38 | + }, |
| 39 | + }, |
| 40 | + strategies: { |
| 41 | + deduplication: { |
| 42 | + enabled: true, |
| 43 | + protectedTools: [], |
| 44 | + }, |
| 45 | + supersedeWrites: { |
| 46 | + enabled: true, |
| 47 | + }, |
| 48 | + purgeErrors: { |
| 49 | + enabled: true, |
| 50 | + turns: 4, |
| 51 | + protectedTools: [], |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +describe("Config Validation - modelLimits", () => { |
| 58 | + it("accepts valid modelLimits configuration", () => { |
| 59 | + const config = createConfig({ |
| 60 | + "anthropic/claude-3.5-sonnet": "70%", |
| 61 | + "anthropic/claude-3-opus": 150000, |
| 62 | + "gpt-4": "80%", |
| 63 | + }) |
| 64 | + |
| 65 | + const errors = validateConfigTypes(config) |
| 66 | + assert.strictEqual(errors.length, 0) |
| 67 | + }) |
| 68 | + |
| 69 | + it("rejects invalid modelLimits string value", () => { |
| 70 | + const config = createConfig({ |
| 71 | + "anthropic/claude-3.5-sonnet": "invalid", |
| 72 | + }) |
| 73 | + |
| 74 | + const errors = validateConfigTypes(config) |
| 75 | + assert.ok( |
| 76 | + errors.some( |
| 77 | + (error) => error.key === "tools.settings.modelLimits.anthropic/claude-3.5-sonnet", |
| 78 | + ), |
| 79 | + ) |
| 80 | + }) |
| 81 | + |
| 82 | + it("rejects modelLimits when not an object", () => { |
| 83 | + const config = createConfig() |
| 84 | + ;(config.tools.settings as any).modelLimits = "not-an-object" |
| 85 | + |
| 86 | + const errors = validateConfigTypes(config) |
| 87 | + assert.ok(errors.some((error) => error.key === "tools.settings.modelLimits")) |
| 88 | + }) |
| 89 | + |
| 90 | + it("works without modelLimits", () => { |
| 91 | + const config = createConfig() |
| 92 | + |
| 93 | + const errors = validateConfigTypes(config) |
| 94 | + assert.strictEqual(errors.length, 0) |
| 95 | + }) |
| 96 | + |
| 97 | + it("rejects malformed percentage strings", () => { |
| 98 | + const config = createConfig({ |
| 99 | + model1: "abc%", |
| 100 | + model2: "50 %", |
| 101 | + model3: "%50", |
| 102 | + model4: "50.5.5%", |
| 103 | + }) |
| 104 | + |
| 105 | + const errors = validateConfigTypes(config) |
| 106 | + assert.ok(errors.some((error) => error.key === "tools.settings.modelLimits.model1")) |
| 107 | + assert.ok(errors.some((error) => error.key === "tools.settings.modelLimits.model2")) |
| 108 | + assert.ok(errors.some((error) => error.key === "tools.settings.modelLimits.model3")) |
| 109 | + assert.ok(errors.some((error) => error.key === "tools.settings.modelLimits.model4")) |
| 110 | + }) |
| 111 | + |
| 112 | + it("rejects strings without percent suffix", () => { |
| 113 | + const config = createConfig({ model: "50" }) |
| 114 | + |
| 115 | + const errors = validateConfigTypes(config) |
| 116 | + assert.ok(errors.some((error) => error.key === "tools.settings.modelLimits.model")) |
| 117 | + }) |
| 118 | + |
| 119 | + it("rejects empty strings", () => { |
| 120 | + const config = createConfig({ model: "" }) |
| 121 | + |
| 122 | + const errors = validateConfigTypes(config) |
| 123 | + assert.ok(errors.some((error) => error.key === "tools.settings.modelLimits.model")) |
| 124 | + }) |
| 125 | + |
| 126 | + it("accepts boundary percentages and numbers", () => { |
| 127 | + const config = createConfig({ |
| 128 | + p0: "0%", |
| 129 | + p100: "100%", |
| 130 | + n0: 0, |
| 131 | + negative: -50000, |
| 132 | + above100: "150%", |
| 133 | + decimal: "50.5%", |
| 134 | + huge: 1000000000000, |
| 135 | + }) |
| 136 | + |
| 137 | + const errors = validateConfigTypes(config) |
| 138 | + assert.strictEqual(errors.length, 0) |
| 139 | + }) |
| 140 | + |
| 141 | + it("rejects modelLimits arrays", () => { |
| 142 | + const config = createConfig() |
| 143 | + ;(config.tools.settings as any).modelLimits = ["not-an-object"] |
| 144 | + |
| 145 | + const errors = validateConfigTypes(config) |
| 146 | + assert.ok(errors.some((error) => error.key === "tools.settings.modelLimits")) |
| 147 | + }) |
| 148 | + |
| 149 | + it("does not flag model-specific keys as unknown config keys", () => { |
| 150 | + const config = createConfig({ |
| 151 | + "anthropic/claude-3.5-sonnet": "70%", |
| 152 | + "openai/gpt-4o": 120000, |
| 153 | + }) |
| 154 | + |
| 155 | + const invalidKeys = getInvalidConfigKeys(config) |
| 156 | + assert.strictEqual(invalidKeys.length, 0) |
| 157 | + }) |
| 158 | +}) |
0 commit comments