|
1 | | -import { describe, expect, test } from "bun:test"; |
| 1 | +import { describe, expect, spyOn, test } from "bun:test"; |
2 | 2 | import { |
3 | 3 | composeCursorClaudeWireId, |
4 | 4 | normalizeCursorClaudeId, |
5 | 5 | } from "../../../src/adapters/cursor/claude-id"; |
6 | 6 |
|
7 | 7 | describe("Cursor Claude id normalization", () => { |
| 8 | + test("matches the legacy regex table for both base spellings and suffix edge cases", () => { |
| 9 | + const patterns = [ |
| 10 | + [/^(.*)-thinking-([a-z-]+)-fast$/, true, true], |
| 11 | + [/^(.*)-([a-z-]+)-thinking-fast$/, true, true], |
| 12 | + [/^(.*)-thinking-([a-z-]+)$/, true, false], |
| 13 | + [/^(.*)-([a-z-]+)-thinking$/, true, false], |
| 14 | + [/^(.*)-([a-z-]+)-fast$/, false, true], |
| 15 | + [/^(.*)-thinking-fast$/, true, true], |
| 16 | + [/^(.*)-thinking$/, true, false], |
| 17 | + [/^(.*)-fast$/, false, true], |
| 18 | + ] as const; |
| 19 | + const levels = ["none", "minimal", "low", "medium", "high", "xhigh", "max", "extra-high"]; |
| 20 | + for (const [base, canonicalBaseId, spelling] of [ |
| 21 | + ["claude-opus-5", "claude-opus-5", "anthropic"], |
| 22 | + ["claude-fable-5.1", "claude-fable-5-1", "anthropic"], |
| 23 | + ["claude-4.6-opus", "claude-4.6-opus", "version-first"], |
| 24 | + ] as const) { |
| 25 | + for (const suffix of [ |
| 26 | + "", "-thinking", "-fast", "-thinking-fast", "-high", "-thinking-", "-thinking--", |
| 27 | + "-thinking-thinking-high", "-high-thinking-low", "\n-thinking-high", "-thinking-high\nX", |
| 28 | + ...levels.flatMap(level => [ |
| 29 | + `-thinking-${level}`, `-${level}-thinking`, `-thinking-${level}-fast`, |
| 30 | + `-${level}-thinking-fast`, `-${level}-fast`, |
| 31 | + ]), |
| 32 | + ]) { |
| 33 | + const id = base + suffix; |
| 34 | + let expected; |
| 35 | + for (const [pattern, thinking, fast] of patterns) { |
| 36 | + const match = pattern.exec(id); |
| 37 | + if (!match || match[1] !== base || (match[2] && !levels.includes(match[2]))) continue; |
| 38 | + expected = { base: match[1], canonicalBaseId, sourceBaseId: base, spelling, thinking, fast, |
| 39 | + ...(match[2] ? { level: match[2] } : {}) }; |
| 40 | + break; |
| 41 | + } |
| 42 | + if (suffix === "") expected = { canonicalBaseId, sourceBaseId: base, spelling, thinking: false, fast: false }; |
| 43 | + expect(normalizeCursorClaudeId(` ${id.toUpperCase()} `), id).toEqual(expected); |
| 44 | + } |
| 45 | + } |
| 46 | + for (const id of ["", "-thinking-high", "-thinking-", "claude--thinking-high", "claude-composer-5-high-fast"]) { |
| 47 | + expect(normalizeCursorClaudeId(id), id).toBeUndefined(); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + test("adversarial Claude ids bypass unbounded wildcard regexes", () => { |
| 52 | + const exec = RegExp.prototype.exec; |
| 53 | + const guard = spyOn(RegExp.prototype, "exec").mockImplementation(function (this: RegExp, value: string) { |
| 54 | + if (value.length > 1_000 && this.source.includes("(.*)")) { |
| 55 | + throw new Error("unbounded wildcard regex on adversarial Claude id"); |
| 56 | + } |
| 57 | + return exec.call(this, value); |
| 58 | + }); |
| 59 | + try { |
| 60 | + for (const ending of ["X", "high", "high-fast", "high-thinking", "high-thinking-fast"]) { |
| 61 | + const id = "claude-opus-5" + "-thinking-".repeat(20_000) + ending; |
| 62 | + expect(normalizeCursorClaudeId(id)).toBeUndefined(); |
| 63 | + } |
| 64 | + } finally { |
| 65 | + guard.mockRestore(); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
8 | 69 | test("normalizes every observed Fable 5.1 spelling to one capability base", () => { |
9 | 70 | for (const id of ["claude-fable-5-1", "claude-fable-5.1", "claude-5.1-fable"]) { |
10 | 71 | expect(normalizeCursorClaudeId(id), id).toMatchObject({ |
|
0 commit comments