|
| 1 | +import { test, expect, describe } from "bun:test" |
| 2 | +import { resolvePluginProviders } from "../../src/cli/cmd/auth" |
| 3 | +import type { Hooks } from "@opencode-ai/plugin" |
| 4 | + |
| 5 | +function hookWithAuth(provider: string): Hooks { |
| 6 | + return { |
| 7 | + auth: { |
| 8 | + provider, |
| 9 | + methods: [], |
| 10 | + }, |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +function hookWithoutAuth(): Hooks { |
| 15 | + return {} |
| 16 | +} |
| 17 | + |
| 18 | +describe("resolvePluginProviders", () => { |
| 19 | + test("returns plugin providers not in models.dev", () => { |
| 20 | + const result = resolvePluginProviders({ |
| 21 | + hooks: [hookWithAuth("portkey")], |
| 22 | + existingProviders: {}, |
| 23 | + disabled: new Set(), |
| 24 | + providerNames: {}, |
| 25 | + }) |
| 26 | + expect(result).toEqual([{ id: "portkey", name: "portkey" }]) |
| 27 | + }) |
| 28 | + |
| 29 | + test("skips providers already in models.dev", () => { |
| 30 | + const result = resolvePluginProviders({ |
| 31 | + hooks: [hookWithAuth("anthropic")], |
| 32 | + existingProviders: { anthropic: {} }, |
| 33 | + disabled: new Set(), |
| 34 | + providerNames: {}, |
| 35 | + }) |
| 36 | + expect(result).toEqual([]) |
| 37 | + }) |
| 38 | + |
| 39 | + test("deduplicates across plugins", () => { |
| 40 | + const result = resolvePluginProviders({ |
| 41 | + hooks: [hookWithAuth("portkey"), hookWithAuth("portkey")], |
| 42 | + existingProviders: {}, |
| 43 | + disabled: new Set(), |
| 44 | + providerNames: {}, |
| 45 | + }) |
| 46 | + expect(result).toEqual([{ id: "portkey", name: "portkey" }]) |
| 47 | + }) |
| 48 | + |
| 49 | + test("respects disabled_providers", () => { |
| 50 | + const result = resolvePluginProviders({ |
| 51 | + hooks: [hookWithAuth("portkey")], |
| 52 | + existingProviders: {}, |
| 53 | + disabled: new Set(["portkey"]), |
| 54 | + providerNames: {}, |
| 55 | + }) |
| 56 | + expect(result).toEqual([]) |
| 57 | + }) |
| 58 | + |
| 59 | + test("respects enabled_providers when provider is absent", () => { |
| 60 | + const result = resolvePluginProviders({ |
| 61 | + hooks: [hookWithAuth("portkey")], |
| 62 | + existingProviders: {}, |
| 63 | + disabled: new Set(), |
| 64 | + enabled: new Set(["anthropic"]), |
| 65 | + providerNames: {}, |
| 66 | + }) |
| 67 | + expect(result).toEqual([]) |
| 68 | + }) |
| 69 | + |
| 70 | + test("includes provider when in enabled set", () => { |
| 71 | + const result = resolvePluginProviders({ |
| 72 | + hooks: [hookWithAuth("portkey")], |
| 73 | + existingProviders: {}, |
| 74 | + disabled: new Set(), |
| 75 | + enabled: new Set(["portkey"]), |
| 76 | + providerNames: {}, |
| 77 | + }) |
| 78 | + expect(result).toEqual([{ id: "portkey", name: "portkey" }]) |
| 79 | + }) |
| 80 | + |
| 81 | + test("resolves name from providerNames", () => { |
| 82 | + const result = resolvePluginProviders({ |
| 83 | + hooks: [hookWithAuth("portkey")], |
| 84 | + existingProviders: {}, |
| 85 | + disabled: new Set(), |
| 86 | + providerNames: { portkey: "Portkey AI" }, |
| 87 | + }) |
| 88 | + expect(result).toEqual([{ id: "portkey", name: "Portkey AI" }]) |
| 89 | + }) |
| 90 | + |
| 91 | + test("falls back to id when no name configured", () => { |
| 92 | + const result = resolvePluginProviders({ |
| 93 | + hooks: [hookWithAuth("portkey")], |
| 94 | + existingProviders: {}, |
| 95 | + disabled: new Set(), |
| 96 | + providerNames: {}, |
| 97 | + }) |
| 98 | + expect(result).toEqual([{ id: "portkey", name: "portkey" }]) |
| 99 | + }) |
| 100 | + |
| 101 | + test("skips hooks without auth", () => { |
| 102 | + const result = resolvePluginProviders({ |
| 103 | + hooks: [hookWithoutAuth(), hookWithAuth("portkey"), hookWithoutAuth()], |
| 104 | + existingProviders: {}, |
| 105 | + disabled: new Set(), |
| 106 | + providerNames: {}, |
| 107 | + }) |
| 108 | + expect(result).toEqual([{ id: "portkey", name: "portkey" }]) |
| 109 | + }) |
| 110 | + |
| 111 | + test("returns empty for no hooks", () => { |
| 112 | + const result = resolvePluginProviders({ |
| 113 | + hooks: [], |
| 114 | + existingProviders: {}, |
| 115 | + disabled: new Set(), |
| 116 | + providerNames: {}, |
| 117 | + }) |
| 118 | + expect(result).toEqual([]) |
| 119 | + }) |
| 120 | +}) |
0 commit comments