cli-auth-command.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { describe, expect, it, vi } from 'vitest';
  2. import { runTuiLogin, runTuiLogout } from '../../src/cli/auth-command.js';
  3. describe('Minimax Code auth commands', () => {
  4. it.each([true, false])('returns after dispatching browser logout (%s)', async (openBrowser) => {
  5. const writeError = vi.fn();
  6. const logoutUrl =
  7. 'https://agent.minimax.cn/auth/logout?logout_redirect_uri=https%3A%2F%2Fagent.minimax.cn';
  8. const logout = vi.fn(async () => ({
  9. state: 'signed-out' as const,
  10. message: 'Signed out of MiniMax.',
  11. logoutUrl,
  12. }));
  13. const openExternalTarget = vi.fn(() => {
  14. expect(logout).toHaveBeenCalledOnce();
  15. expect(writeError).toHaveBeenCalledWith(expect.stringContaining(logoutUrl));
  16. return new Promise<void>(() => {});
  17. });
  18. await expect(
  19. runTuiLogout({
  20. createApplication: () => ({ login: vi.fn(), logout }),
  21. writeError,
  22. openBrowser,
  23. openExternalTarget,
  24. }),
  25. ).resolves.toBe('Signed out of MiniMax.');
  26. expect(logout).toHaveBeenCalledOnce();
  27. expect(writeError).toHaveBeenCalledWith(`Finish signing out in your browser:\n${logoutUrl}\n`);
  28. if (openBrowser) expect(openExternalTarget).toHaveBeenCalledWith(logoutUrl);
  29. else expect(openExternalTarget).not.toHaveBeenCalled();
  30. });
  31. it.each(['reject', 'throw'] as const)(
  32. 'keeps logout successful when browser startup fails by %s',
  33. async (failure) => {
  34. const writeError = vi.fn();
  35. const logoutUrl =
  36. 'https://agent.minimax.cn/auth/logout?logout_redirect_uri=https%3A%2F%2Fagent.minimax.cn';
  37. const logout = vi.fn(async () => ({
  38. state: 'signed-out' as const,
  39. message: 'Signed out of MiniMax.',
  40. logoutUrl,
  41. }));
  42. await expect(
  43. runTuiLogout({
  44. createApplication: () => ({ login: vi.fn(), logout }),
  45. writeError,
  46. openExternalTarget: () => {
  47. const error = new Error('no desktop session');
  48. if (failure === 'throw') throw error;
  49. return Promise.reject(error);
  50. },
  51. }),
  52. ).resolves.toBe('Signed out of MiniMax.');
  53. expect(writeError).toHaveBeenCalledWith(expect.stringContaining(logoutUrl));
  54. expect(writeError).toHaveBeenLastCalledWith(
  55. "Couldn't open the default browser. Open the sign-out URL above manually.\n",
  56. );
  57. },
  58. );
  59. it('creates the logout application for the explicitly selected region', async () => {
  60. const logout = vi.fn(async () => ({
  61. state: 'signed-out' as const,
  62. message: 'Signed out of MiniMax Global.',
  63. }));
  64. const createApplication = vi.fn(() => ({ login: vi.fn(), logout }));
  65. await runTuiLogout({ region: 'en', createApplication });
  66. expect(createApplication).toHaveBeenCalledWith('en');
  67. expect(logout).toHaveBeenCalledOnce();
  68. });
  69. it('prints Device Flow instructions without exposing the internal device_code', async () => {
  70. const stderr: string[] = [];
  71. const openedTargets: string[] = [];
  72. const login = vi.fn(async (onProgress) => {
  73. onProgress?.({
  74. state: 'device-authorization',
  75. userCode: 'ABCD-EFGH',
  76. verificationUri: 'https://account.example.test/device',
  77. verificationUriComplete:
  78. 'https://account.example.test/device?user_code=ABCD-EFGH',
  79. expiresInSec: 300,
  80. });
  81. return { state: 'authenticated' as const, message: 'Signed in with MiniMax.' };
  82. });
  83. await runTuiLogin({
  84. region: 'en',
  85. createApplication: () => ({ login, logout: vi.fn() }),
  86. writeError: (value) => stderr.push(value),
  87. openExternalTarget: async (target) => {
  88. openedTargets.push(target);
  89. },
  90. });
  91. expect(stderr.join('')).toBe(
  92. 'Open: https://account.example.test/device?user_code=ABCD-EFGH&client_surface=tui&download_source=mcode-internal\nCode: ABCD-EFGH\nWaiting for authorization…\n',
  93. );
  94. expect(openedTargets).toEqual([
  95. 'https://account.example.test/device?user_code=ABCD-EFGH&client_surface=tui&download_source=mcode-internal',
  96. ]);
  97. expect(stderr.join('')).not.toContain('device-secret');
  98. });
  99. it('keeps mcode login usable without a desktop browser when explicitly disabled', async () => {
  100. const stderr: string[] = [];
  101. const openedTargets: string[] = [];
  102. const login = vi.fn(async (onProgress) => {
  103. onProgress?.({
  104. state: 'device-authorization',
  105. userCode: 'ABCD-EFGH',
  106. verificationUri: 'https://account.example.test/device',
  107. expiresInSec: 300,
  108. });
  109. return { state: 'authenticated' as const, message: 'Signed in with MiniMax.' };
  110. });
  111. await runTuiLogin({
  112. openBrowser: false,
  113. createApplication: () => ({ login, logout: vi.fn() }),
  114. writeError: (value) => stderr.push(value),
  115. openExternalTarget: async (target) => {
  116. openedTargets.push(target);
  117. },
  118. });
  119. expect(openedTargets).toEqual([]);
  120. expect(stderr.join('')).toContain(
  121. 'Open: https://account.example.test/device?client_surface=tui&download_source=mcode-internal',
  122. );
  123. });
  124. it('continues Device Flow when opening the default browser fails', async () => {
  125. const stderr: string[] = [];
  126. const login = vi.fn(async (onProgress) => {
  127. onProgress?.({
  128. state: 'device-authorization',
  129. userCode: 'ABCD-EFGH',
  130. verificationUri: 'https://account.example.test/device',
  131. expiresInSec: 300,
  132. });
  133. return { state: 'authenticated' as const, message: 'Signed in with MiniMax.' };
  134. });
  135. await expect(
  136. runTuiLogin({
  137. createApplication: () => ({ login, logout: vi.fn() }),
  138. writeError: (value) => stderr.push(value),
  139. openExternalTarget: async () => {
  140. throw new Error('no desktop session');
  141. },
  142. }),
  143. ).resolves.toBe('Signed in with MiniMax.');
  144. await vi.waitFor(() =>
  145. expect(stderr.join('')).toContain(
  146. "Couldn't open the default browser. Open the authorization URL above manually.",
  147. ),
  148. );
  149. });
  150. });