|
| 1 | +import { Linter } from "eslint" |
| 2 | +import typescriptParser from "@typescript-eslint/parser" |
| 3 | +import { describe, expect, it } from "vitest" |
| 4 | + |
| 5 | +import { noRawProviderIdentifiers } from "../eslint-rules/no-raw-provider-identifiers.mjs" |
| 6 | + |
| 7 | +const linter = new Linter({ configType: "eslintrc" }) |
| 8 | + |
| 9 | +linter.defineRule("zoo/no-raw-provider-identifiers", noRawProviderIdentifiers) |
| 10 | +linter.defineParser("@typescript-eslint/parser", typescriptParser) |
| 11 | + |
| 12 | +function lint(code) { |
| 13 | + return linter.verify(code, { |
| 14 | + parserOptions: { ecmaVersion: 2022, sourceType: "module" }, |
| 15 | + rules: { "zoo/no-raw-provider-identifiers": "error" }, |
| 16 | + }) |
| 17 | +} |
| 18 | + |
| 19 | +function lintTypeScript(code) { |
| 20 | + return linter.verify(code, { |
| 21 | + parser: "@typescript-eslint/parser", |
| 22 | + parserOptions: { |
| 23 | + ecmaVersion: 2022, |
| 24 | + sourceType: "module", |
| 25 | + warnOnUnsupportedTypeScriptVersion: false, |
| 26 | + }, |
| 27 | + rules: { "zoo/no-raw-provider-identifiers": "error" }, |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +describe("no-raw-provider-identifiers", () => { |
| 32 | + it("rejects a canonical provider literal in an apiProvider property", () => { |
| 33 | + const messages = lint('const config = { apiProvider: "poe" }') |
| 34 | + |
| 35 | + expect(messages).toHaveLength(1) |
| 36 | + expect(messages[0]).toMatchObject({ |
| 37 | + ruleId: "zoo/no-raw-provider-identifiers", |
| 38 | + message: 'Use providerIdentifiers.poe instead of the raw provider identifier "poe".', |
| 39 | + }) |
| 40 | + }) |
| 41 | + |
| 42 | + it("allows a non-canonical literal and a canonical registry member", () => { |
| 43 | + expect(lint('const config = { apiProvider: "external-provider" }')).toHaveLength(0) |
| 44 | + expect(lint("const config = { apiProvider: providerIdentifiers.poe }")).toHaveLength(0) |
| 45 | + }) |
| 46 | + |
| 47 | + it("matches provider-like property names and static template literals", () => { |
| 48 | + const messages = lint('const config = { provider: "poe", imageProvider: `openrouter` }') |
| 49 | + |
| 50 | + expect(messages).toHaveLength(2) |
| 51 | + }) |
| 52 | + |
| 53 | + it("allows an empty static template in a provider-like context", () => { |
| 54 | + expect(lint("const config = { apiProvider: `` }")).toHaveLength(0) |
| 55 | + }) |
| 56 | + |
| 57 | + it("rejects canonical literals in provider-like variable declarations", () => { |
| 58 | + const messages = lint(` |
| 59 | + const apiProvider = "poe" |
| 60 | + let fallbackProvider = \`openrouter\` |
| 61 | + const label = "poe" |
| 62 | + `) |
| 63 | + |
| 64 | + expect(messages.map(({ message }) => message)).toEqual([ |
| 65 | + 'Use providerIdentifiers.poe instead of the raw provider identifier "poe".', |
| 66 | + 'Use providerIdentifiers.openrouter instead of the raw provider identifier "openrouter".', |
| 67 | + ]) |
| 68 | + }) |
| 69 | + |
| 70 | + it("rejects canonical provider literals wrapped in TypeScript expressions", () => { |
| 71 | + const messages = lintTypeScript(` |
| 72 | + const apiProvider = "poe" as ApiProvider |
| 73 | + const fallbackProvider = "openrouter" satisfies ApiProvider |
| 74 | + const imageProvider = <ApiProvider>"openai-native" |
| 75 | + const nestedProvider = ("anthropic" as ApiProvider)! |
| 76 | + `) |
| 77 | + |
| 78 | + expect(messages.map(({ message }) => message)).toEqual([ |
| 79 | + 'Use providerIdentifiers.poe instead of the raw provider identifier "poe".', |
| 80 | + 'Use providerIdentifiers.openrouter instead of the raw provider identifier "openrouter".', |
| 81 | + 'Use providerIdentifiers.openaiNative instead of the raw provider identifier "openai-native".', |
| 82 | + 'Use providerIdentifiers.anthropic instead of the raw provider identifier "anthropic".', |
| 83 | + ]) |
| 84 | + }) |
| 85 | + |
| 86 | + it("rejects canonical literals in provider-like class fields", () => { |
| 87 | + const messages = lintTypeScript(` |
| 88 | + class Settings { |
| 89 | + apiProvider = "poe" |
| 90 | + label = "openrouter" |
| 91 | + } |
| 92 | + `) |
| 93 | + |
| 94 | + expect(messages.map(({ message }) => message)).toEqual([ |
| 95 | + 'Use providerIdentifiers.poe instead of the raw provider identifier "poe".', |
| 96 | + ]) |
| 97 | + }) |
| 98 | + |
| 99 | + it("rejects canonical literals in provider-like assignments and comparisons", () => { |
| 100 | + const messages = lint(` |
| 101 | + config["apiProvider"] = "poe" |
| 102 | + if (imageProvider === "openrouter") {} |
| 103 | + if ("openai-native" !== config.fallbackProvider) {} |
| 104 | + `) |
| 105 | + |
| 106 | + expect(messages.map(({ message }) => message)).toEqual([ |
| 107 | + 'Use providerIdentifiers.poe instead of the raw provider identifier "poe".', |
| 108 | + 'Use providerIdentifiers.openrouter instead of the raw provider identifier "openrouter".', |
| 109 | + 'Use providerIdentifiers.openaiNative instead of the raw provider identifier "openai-native".', |
| 110 | + ]) |
| 111 | + }) |
| 112 | + |
| 113 | + it("rejects canonical literals in provider-like switch cases", () => { |
| 114 | + const messages = lint(` |
| 115 | + switch (config.apiProvider) { |
| 116 | + case "poe": break |
| 117 | + case providerIdentifiers.openrouter: break |
| 118 | + } |
| 119 | + `) |
| 120 | + |
| 121 | + expect(messages).toHaveLength(1) |
| 122 | + expect(messages[0].message).toContain("providerIdentifiers.poe") |
| 123 | + }) |
| 124 | + |
| 125 | + it("does not report canonical values outside provider-like contexts", () => { |
| 126 | + const messages = lint(` |
| 127 | + const label = "poe" |
| 128 | + const config = { protocol: "anthropic", format: "openai" } |
| 129 | + config[dynamicKey] = "poe" |
| 130 | + if (apiProtocol === "anthropic") {} |
| 131 | + if (provider > "poe") {} |
| 132 | + switch (format) { case "openai": break } |
| 133 | + `) |
| 134 | + |
| 135 | + expect(messages).toHaveLength(0) |
| 136 | + }) |
| 137 | +}) |
0 commit comments