oauth-selector.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { setKeybindings } from "@earendil-works/pi-tui";
  2. import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
  3. import { AuthStorage } from "../src/core/auth-storage.ts";
  4. import { KeybindingsManager } from "../src/core/keybindings.ts";
  5. import { BUILT_IN_PROVIDER_DISPLAY_NAMES } from "../src/core/provider-display-names.ts";
  6. import { OAuthSelectorComponent } from "../src/modes/interactive/components/oauth-selector.ts";
  7. import { isApiKeyLoginProvider } from "../src/modes/interactive/interactive-mode.ts";
  8. import { initTheme } from "../src/modes/interactive/theme/theme.ts";
  9. import { stripAnsi } from "../src/utils/ansi.ts";
  10. const originalOpenAiApiKey = process.env.OPENAI_API_KEY;
  11. describe("OAuthSelectorComponent", () => {
  12. beforeAll(() => {
  13. initTheme("dark");
  14. });
  15. beforeEach(() => {
  16. setKeybindings(new KeybindingsManager());
  17. });
  18. afterEach(() => {
  19. if (originalOpenAiApiKey === undefined) {
  20. delete process.env.OPENAI_API_KEY;
  21. } else {
  22. process.env.OPENAI_API_KEY = originalOpenAiApiKey;
  23. }
  24. });
  25. it("keeps built-in API key providers separate from OAuth-only providers", () => {
  26. const oauthProviderIds = new Set(["anthropic", "github-copilot", "custom-oauth"]);
  27. const builtInProviderIds = new Set(["anthropic", "github-copilot", "amazon-bedrock", "openai"]);
  28. expect(isApiKeyLoginProvider("anthropic", oauthProviderIds, builtInProviderIds)).toBe(true);
  29. expect(BUILT_IN_PROVIDER_DISPLAY_NAMES.anthropic).toBe("Anthropic");
  30. expect(isApiKeyLoginProvider("openai", oauthProviderIds, builtInProviderIds)).toBe(true);
  31. expect(isApiKeyLoginProvider("github-copilot", oauthProviderIds, builtInProviderIds)).toBe(false);
  32. expect(isApiKeyLoginProvider("amazon-bedrock", oauthProviderIds, builtInProviderIds)).toBe(true);
  33. expect(isApiKeyLoginProvider("custom-oauth", oauthProviderIds, builtInProviderIds)).toBe(false);
  34. expect(isApiKeyLoginProvider("custom-api", oauthProviderIds, builtInProviderIds)).toBe(true);
  35. });
  36. it("shows stored OAuth auth distinctly in the API key selector", () => {
  37. const authStorage = AuthStorage.inMemory({
  38. anthropic: {
  39. type: "oauth",
  40. access: "access-token",
  41. refresh: "refresh-token",
  42. expires: Date.now() + 60_000,
  43. },
  44. });
  45. const selector = new OAuthSelectorComponent(
  46. "login",
  47. authStorage,
  48. [{ id: "anthropic", name: "Anthropic", authType: "api_key" }],
  49. () => {},
  50. () => {},
  51. );
  52. const output = stripAnsi(selector.render(120).join("\n"));
  53. expect(output).toContain("Anthropic");
  54. expect(output).toContain("subscription configured");
  55. });
  56. it("shows environment API key auth as configured", () => {
  57. process.env.OPENAI_API_KEY = "test-openai-key";
  58. const authStorage = AuthStorage.inMemory();
  59. const selector = new OAuthSelectorComponent(
  60. "login",
  61. authStorage,
  62. [{ id: "openai", name: "OpenAI", authType: "api_key" }],
  63. () => {},
  64. () => {},
  65. );
  66. const output = stripAnsi(selector.render(120).join("\n"));
  67. expect(output).toContain("OpenAI");
  68. expect(output).toContain("✓ env: OPENAI_API_KEY");
  69. expect(output).not.toContain("unconfigured");
  70. });
  71. it("shows custom provider environment API key auth from status resolver", () => {
  72. const authStorage = AuthStorage.inMemory();
  73. const selector = new OAuthSelectorComponent(
  74. "login",
  75. authStorage,
  76. [{ id: "ollama", name: "ollama", authType: "api_key" }],
  77. () => {},
  78. () => {},
  79. () => ({ configured: true, source: "environment", label: "OLLAMA_API_KEY" }),
  80. );
  81. const output = stripAnsi(selector.render(120).join("\n"));
  82. expect(output).toContain("ollama");
  83. expect(output).toContain("✓ env: OLLAMA_API_KEY");
  84. expect(output).not.toContain("unconfigured");
  85. });
  86. it("shows models.json API key auth as configured", () => {
  87. const authStorage = AuthStorage.inMemory();
  88. const selector = new OAuthSelectorComponent(
  89. "login",
  90. authStorage,
  91. [{ id: "local-proxy", name: "local-proxy", authType: "api_key" }],
  92. () => {},
  93. () => {},
  94. () => ({ configured: true, source: "models_json_key" }),
  95. );
  96. const output = stripAnsi(selector.render(120).join("\n"));
  97. expect(output).toContain("local-proxy");
  98. expect(output).toContain("✓ key in models.json");
  99. expect(output).not.toContain("unconfigured");
  100. });
  101. it("shows models.json command auth as configured", () => {
  102. const authStorage = AuthStorage.inMemory();
  103. const selector = new OAuthSelectorComponent(
  104. "login",
  105. authStorage,
  106. [{ id: "op-proxy", name: "op-proxy", authType: "api_key" }],
  107. () => {},
  108. () => {},
  109. () => ({ configured: true, source: "models_json_command" }),
  110. );
  111. const output = stripAnsi(selector.render(120).join("\n"));
  112. expect(output).toContain("op-proxy");
  113. expect(output).toContain("✓ command in models.json");
  114. expect(output).not.toContain("unconfigured");
  115. });
  116. });