interactive-mode-anthropic-warning.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { describe, expect, test, vi } from "vitest";
  2. import { InteractiveMode } from "../src/modes/interactive/interactive-mode.ts";
  3. function createSettingsManager(warnings: { anthropicExtraUsage?: boolean } = {}) {
  4. return {
  5. getWarnings: vi.fn().mockReturnValue(warnings),
  6. };
  7. }
  8. describe("InteractiveMode.maybeWarnAboutAnthropicSubscriptionAuth", () => {
  9. test("warns once when Anthropic subscription auth is detected", async () => {
  10. const fakeThis: any = {
  11. anthropicSubscriptionWarningShown: false,
  12. settingsManager: createSettingsManager(),
  13. session: {
  14. modelRegistry: {
  15. authStorage: {
  16. get: vi.fn().mockReturnValue(undefined),
  17. },
  18. getApiKeyForProvider: vi.fn().mockResolvedValue("sk-ant-oat01-test"),
  19. },
  20. },
  21. showWarning: vi.fn(),
  22. };
  23. await (InteractiveMode as any).prototype.maybeWarnAboutAnthropicSubscriptionAuth.call(fakeThis, {
  24. provider: "anthropic",
  25. });
  26. await (InteractiveMode as any).prototype.maybeWarnAboutAnthropicSubscriptionAuth.call(fakeThis, {
  27. provider: "anthropic",
  28. });
  29. expect(fakeThis.showWarning).toHaveBeenCalledTimes(1);
  30. expect(fakeThis.session.modelRegistry.getApiKeyForProvider).toHaveBeenCalledTimes(1);
  31. });
  32. test("warns when Anthropic OAuth is stored even if token refresh lookup would fail", async () => {
  33. const fakeThis: any = {
  34. anthropicSubscriptionWarningShown: false,
  35. settingsManager: createSettingsManager(),
  36. session: {
  37. modelRegistry: {
  38. authStorage: {
  39. get: vi.fn().mockReturnValue({ type: "oauth" }),
  40. },
  41. getApiKeyForProvider: vi.fn().mockResolvedValue(undefined),
  42. },
  43. },
  44. showWarning: vi.fn(),
  45. };
  46. await (InteractiveMode as any).prototype.maybeWarnAboutAnthropicSubscriptionAuth.call(fakeThis, {
  47. provider: "anthropic",
  48. });
  49. expect(fakeThis.showWarning).toHaveBeenCalledTimes(1);
  50. expect(fakeThis.session.modelRegistry.getApiKeyForProvider).not.toHaveBeenCalled();
  51. });
  52. test("does not warn for non-Anthropic models", async () => {
  53. const fakeThis: any = {
  54. anthropicSubscriptionWarningShown: false,
  55. settingsManager: createSettingsManager(),
  56. session: {
  57. modelRegistry: {
  58. authStorage: {
  59. get: vi.fn(),
  60. },
  61. getApiKeyForProvider: vi.fn(),
  62. },
  63. },
  64. showWarning: vi.fn(),
  65. };
  66. await (InteractiveMode as any).prototype.maybeWarnAboutAnthropicSubscriptionAuth.call(fakeThis, {
  67. provider: "openai",
  68. });
  69. expect(fakeThis.showWarning).not.toHaveBeenCalled();
  70. expect(fakeThis.session.modelRegistry.getApiKeyForProvider).not.toHaveBeenCalled();
  71. });
  72. test("does not warn when Anthropic extra usage warning is disabled", async () => {
  73. const fakeThis: any = {
  74. anthropicSubscriptionWarningShown: false,
  75. settingsManager: createSettingsManager({ anthropicExtraUsage: false }),
  76. session: {
  77. modelRegistry: {
  78. authStorage: {
  79. get: vi.fn(),
  80. },
  81. getApiKeyForProvider: vi.fn(),
  82. },
  83. },
  84. showWarning: vi.fn(),
  85. };
  86. await (InteractiveMode as any).prototype.maybeWarnAboutAnthropicSubscriptionAuth.call(fakeThis, {
  87. provider: "anthropic",
  88. });
  89. expect(fakeThis.showWarning).not.toHaveBeenCalled();
  90. expect(fakeThis.session.modelRegistry.authStorage.get).not.toHaveBeenCalled();
  91. expect(fakeThis.session.modelRegistry.getApiKeyForProvider).not.toHaveBeenCalled();
  92. });
  93. });