application-login-gate.test.ts 2.6 KB

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