|
| 1 | +import { Linter } from "eslint" |
| 2 | +import { describe, expect, it } from "vitest" |
| 3 | + |
| 4 | +import { noRawProviderIdentifiers } from "../eslint-rules/no-raw-provider-identifiers.mjs" |
| 5 | + |
| 6 | +const linter = new Linter({ configType: "eslintrc" }) |
| 7 | + |
| 8 | +linter.defineRule("zoo/no-raw-provider-identifiers", noRawProviderIdentifiers) |
| 9 | + |
| 10 | +function lint(code) { |
| 11 | + return linter.verify(code, { |
| 12 | + parserOptions: { ecmaVersion: 2022, sourceType: "module" }, |
| 13 | + rules: { "zoo/no-raw-provider-identifiers": "error" }, |
| 14 | + }) |
| 15 | +} |
| 16 | + |
| 17 | +describe("no-raw-provider-identifiers", () => { |
| 18 | + it("rejects a canonical provider literal in an apiProvider property", () => { |
| 19 | + const messages = lint('const config = { apiProvider: "poe" }') |
| 20 | + |
| 21 | + expect(messages).toHaveLength(1) |
| 22 | + expect(messages[0]).toMatchObject({ |
| 23 | + ruleId: "zoo/no-raw-provider-identifiers", |
| 24 | + message: 'Use providerIdentifiers.poe instead of the raw provider identifier "poe".', |
| 25 | + }) |
| 26 | + }) |
| 27 | + |
| 28 | + it("allows a non-canonical literal and a canonical registry member", () => { |
| 29 | + expect(lint('const config = { apiProvider: "external-provider" }')).toHaveLength(0) |
| 30 | + expect(lint("const config = { apiProvider: providerIdentifiers.poe }")).toHaveLength(0) |
| 31 | + }) |
| 32 | + |
| 33 | + it("matches provider-like property names and static template literals", () => { |
| 34 | + const messages = lint('const config = { provider: "poe", imageProvider: `openrouter` }') |
| 35 | + |
| 36 | + expect(messages).toHaveLength(2) |
| 37 | + }) |
| 38 | + |
| 39 | + it("rejects canonical literals in provider-like assignments and comparisons", () => { |
| 40 | + const messages = lint(` |
| 41 | + config["apiProvider"] = "poe" |
| 42 | + if (imageProvider === "openrouter") {} |
| 43 | + if ("openai-native" !== config.fallbackProvider) {} |
| 44 | + `) |
| 45 | + |
| 46 | + expect(messages.map(({ message }) => message)).toEqual([ |
| 47 | + 'Use providerIdentifiers.poe instead of the raw provider identifier "poe".', |
| 48 | + 'Use providerIdentifiers.openrouter instead of the raw provider identifier "openrouter".', |
| 49 | + 'Use providerIdentifiers.openaiNative instead of the raw provider identifier "openai-native".', |
| 50 | + ]) |
| 51 | + }) |
| 52 | + |
| 53 | + it("rejects canonical literals in provider-like switch cases", () => { |
| 54 | + const messages = lint(` |
| 55 | + switch (config.apiProvider) { |
| 56 | + case "poe": break |
| 57 | + case providerIdentifiers.openrouter: break |
| 58 | + } |
| 59 | + `) |
| 60 | + |
| 61 | + expect(messages).toHaveLength(1) |
| 62 | + expect(messages[0].message).toContain("providerIdentifiers.poe") |
| 63 | + }) |
| 64 | + |
| 65 | + it("does not report canonical values outside provider-like contexts", () => { |
| 66 | + const messages = lint(` |
| 67 | + const label = "poe" |
| 68 | + const config = { protocol: "anthropic", format: "openai" } |
| 69 | + config[dynamicKey] = "poe" |
| 70 | + if (apiProtocol === "anthropic") {} |
| 71 | + if (provider > "poe") {} |
| 72 | + switch (format) { case "openai": break } |
| 73 | + `) |
| 74 | + |
| 75 | + expect(messages).toHaveLength(0) |
| 76 | + }) |
| 77 | +}) |
0 commit comments