tui-codex-login.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import type { McodeCodexOAuthStatus } from "../../src/provider/contract.js";
  3. import { TuiCodexLogin } from "../../src/tui/features/auth/codex-login.js";
  4. import { stripAnsi, visibleWidth } from "../../src/tui/rendering/text.js";
  5. const disconnected: McodeCodexOAuthStatus = {
  6. state: "disconnected",
  7. providerId: "openai-codex",
  8. };
  9. const device: McodeCodexOAuthStatus = {
  10. state: "pending",
  11. providerId: "openai-codex",
  12. method: "device_code",
  13. loginId: "attempt-1",
  14. deviceCode: {
  15. verificationUri: "https://auth.openai.com/codex/device",
  16. userCode: "ABCD-EFGH",
  17. expiresAt: Date.now() + 900_000,
  18. },
  19. };
  20. function harness(initial = disconnected) {
  21. const application = {
  22. getCodexOAuthStatus: vi.fn(
  23. async (): Promise<McodeCodexOAuthStatus> => initial,
  24. ),
  25. connectCodexOAuth: vi.fn(
  26. async (): Promise<McodeCodexOAuthStatus> => device,
  27. ),
  28. cancelCodexOAuthLogin: vi.fn(
  29. async (): Promise<McodeCodexOAuthStatus> => disconnected,
  30. ),
  31. };
  32. const openExternalTarget = vi.fn(async () => undefined);
  33. const onConnected = vi.fn();
  34. const onClose = vi.fn();
  35. const panel = new TuiCodexLogin({
  36. application,
  37. openExternalTarget,
  38. onConnected,
  39. onClose,
  40. requestRender: vi.fn(),
  41. });
  42. return {
  43. panel,
  44. application,
  45. openExternalTarget,
  46. onConnected,
  47. onClose,
  48. text: () => stripAnsi(panel.render(90).join("\n")),
  49. };
  50. }
  51. afterEach(() => vi.useRealTimers());
  52. describe("Codex sign-in panel", () => {
  53. it("defaults to browser login and displays its URL when browser launch fails", async () => {
  54. const h = harness();
  55. h.application.connectCodexOAuth.mockResolvedValue({
  56. ...device,
  57. method: "browser",
  58. deviceCode: undefined,
  59. authUrl: "https://auth.openai.com/oauth/authorize?state=test",
  60. });
  61. h.openExternalTarget.mockRejectedValue(new Error("No browser"));
  62. await h.panel.resume();
  63. h.panel.handleInput("\r");
  64. await vi.waitFor(() =>
  65. expect(h.text()).toContain("Open the link above manually"),
  66. );
  67. expect(h.application.connectCodexOAuth).toHaveBeenCalledWith({
  68. method: "browser",
  69. });
  70. expect(h.text()).toContain(
  71. "https://auth.openai.com/oauth/authorize?state=test",
  72. );
  73. h.panel.dispose();
  74. });
  75. it("selects device code, displays the code and polls until connected even without a browser", async () => {
  76. vi.useFakeTimers();
  77. const h = harness();
  78. h.openExternalTarget.mockRejectedValue(new Error("xdg-open unavailable"));
  79. await h.panel.resume();
  80. h.panel.handleInput("\u001b[B");
  81. h.panel.handleInput("\r");
  82. await vi.advanceTimersByTimeAsync(0);
  83. expect(h.application.connectCodexOAuth).toHaveBeenCalledWith({
  84. method: "device_code",
  85. });
  86. expect(h.text()).toContain("ABCD-EFGH");
  87. expect(h.text()).toContain("https://auth.openai.com/codex/device");
  88. expect(h.text()).toContain("Waiting for authorization");
  89. h.application.getCodexOAuthStatus.mockResolvedValue({
  90. ...disconnected,
  91. state: "connected",
  92. });
  93. await vi.advanceTimersByTimeAsync(1000);
  94. expect(h.onConnected).toHaveBeenCalledOnce();
  95. h.panel.dispose();
  96. });
  97. it("resumes a pending device login without generating a second code", async () => {
  98. const h = harness(device);
  99. await h.panel.resume();
  100. expect(h.application.connectCodexOAuth).not.toHaveBeenCalled();
  101. expect(h.text()).toContain("ABCD-EFGH");
  102. h.panel.handleInput("\u001b");
  103. await vi.waitFor(() => expect(h.onClose).toHaveBeenCalledOnce());
  104. expect(h.application.cancelCodexOAuthLogin).toHaveBeenCalledWith(
  105. "attempt-1",
  106. );
  107. h.panel.dispose();
  108. });
  109. it("cancels an authorization that arrives after the user closes the starting panel", async () => {
  110. const h = harness();
  111. let release!: (status: McodeCodexOAuthStatus) => void;
  112. h.application.connectCodexOAuth.mockImplementation(
  113. () =>
  114. new Promise((resolve) => {
  115. release = resolve;
  116. }),
  117. );
  118. await h.panel.resume();
  119. h.panel.handleInput("\u001b[B");
  120. h.panel.handleInput("\r");
  121. h.panel.handleInput("\u001b");
  122. h.panel.dispose();
  123. release(device);
  124. await vi.waitFor(() =>
  125. expect(h.application.cancelCodexOAuthLogin).toHaveBeenCalledWith(
  126. "attempt-1",
  127. ),
  128. );
  129. expect(h.openExternalTarget).not.toHaveBeenCalled();
  130. });
  131. it("shows expiry and retries the selected method, and stops polling after disposal", async () => {
  132. vi.useFakeTimers();
  133. const h = harness(device);
  134. await h.panel.resume();
  135. h.application.getCodexOAuthStatus.mockResolvedValue({
  136. ...disconnected,
  137. state: "failed",
  138. error: "Device code expired. Start login again.",
  139. });
  140. await vi.advanceTimersByTimeAsync(1000);
  141. expect(h.text()).toContain("Device code expired");
  142. h.panel.handleInput("\r");
  143. await vi.advanceTimersByTimeAsync(0);
  144. expect(h.application.connectCodexOAuth).toHaveBeenCalledWith({
  145. method: "device_code",
  146. });
  147. h.panel.dispose();
  148. const calls = h.application.getCodexOAuthStatus.mock.calls.length;
  149. await vi.advanceTimersByTimeAsync(10_000);
  150. expect(h.application.getCodexOAuthStatus).toHaveBeenCalledTimes(calls);
  151. });
  152. it("keeps the user code and cancel action visible in a narrow terminal", async () => {
  153. const h = harness(device);
  154. await h.panel.resume();
  155. const rows = h.panel.renderViewport(40, 12);
  156. const text = stripAnsi(rows.join("\n"));
  157. expect(text).toContain("ABCD-EFGH");
  158. expect(text).toContain("Esc cancel");
  159. expect(rows.every((row) => visibleWidth(row) <= 40)).toBe(true);
  160. expect(rows.length).toBeLessThanOrEqual(12);
  161. h.panel.dispose();
  162. });
  163. });