application-login-gate.test.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { describe, expect, it, vi } from 'vitest';
  2. import {
  3. MINIMAX_CODE_HEADLESS_LOGIN_REQUIRED_MESSAGE,
  4. TuiLoginRequiredError,
  5. requireTuiAccountLogin,
  6. requireTuiAgentAccess,
  7. tuiAccountNeedsLoginPrompt,
  8. } from '../../src/application/login-gate.js';
  9. describe('MiniMax login gate', () => {
  10. it.each([
  11. { authMode: 'byok', modelSource: 'byok' as const, managedTokenPresent: false },
  12. { authMode: 'byok', modelSource: 'byok' as const, managedTokenPresent: undefined },
  13. {
  14. authMode: 'managed-login',
  15. modelSource: 'byok' as const,
  16. managedTokenPresent: false,
  17. },
  18. ])('allows an unauthenticated BYOK Agent Run: %s', async (selection) => {
  19. const account = { status: 'ready' as const, ...selection, warnings: [] };
  20. const getAccountStatus = vi.fn(async () => account);
  21. await expect(requireTuiAgentAccess({ getAccountStatus }, 'session-1')).resolves.toBe(
  22. account,
  23. );
  24. expect(getAccountStatus).toHaveBeenCalledWith('session-1', undefined);
  25. expect(tuiAccountNeedsLoginPrompt(account)).toBe(false);
  26. });
  27. it('accepts a shared managed token for a managed model', async () => {
  28. const account = {
  29. status: 'ready' as const,
  30. authMode: 'managed-login',
  31. modelSource: 'token-plan' as const,
  32. managedTokenPresent: true,
  33. warnings: [],
  34. };
  35. await expect(
  36. requireTuiAgentAccess({ getAccountStatus: async () => account }),
  37. ).resolves.toBe(account);
  38. });
  39. it.each([
  40. { authMode: 'managed-login', modelSource: 'token-plan' as const },
  41. { authMode: 'managed-login', modelSource: undefined },
  42. { authMode: undefined, modelSource: 'token-plan' as const },
  43. ])('rejects an unauthenticated managed Agent Run: %s', async (selection) => {
  44. const account = {
  45. status: 'needs-login' as const,
  46. ...selection,
  47. managedTokenPresent: false,
  48. warnings: [],
  49. };
  50. expect(tuiAccountNeedsLoginPrompt(account)).toBe(true);
  51. await expect(
  52. requireTuiAgentAccess({ getAccountStatus: async () => account }),
  53. ).rejects.toMatchObject({
  54. name: 'TuiLoginRequiredError',
  55. category: 'config',
  56. code: 'auth.login_required',
  57. retryable: false,
  58. message: MINIMAX_CODE_HEADLESS_LOGIN_REQUIRED_MESSAGE,
  59. account,
  60. } satisfies Partial<TuiLoginRequiredError>);
  61. });
  62. it('still requires MiniMax login for account-backed services while BYOK is selected', async () => {
  63. const account = {
  64. status: 'ready' as const,
  65. authMode: 'byok',
  66. modelSource: 'byok' as const,
  67. managedTokenPresent: false,
  68. warnings: [],
  69. };
  70. await expect(
  71. requireTuiAccountLogin({ getAccountStatus: async () => account }),
  72. ).rejects.toMatchObject({
  73. code: 'auth.login_required',
  74. account,
  75. });
  76. });
  77. });