5433-extension-oauth-prompt-input.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { setKeybindings, type TUI } from "@earendil-works/pi-tui";
  2. import { beforeAll, beforeEach, describe, expect, test, vi } from "vitest";
  3. import { KeybindingsManager } from "../../../src/core/keybindings.ts";
  4. import { LoginDialogComponent } from "../../../src/modes/interactive/components/login-dialog.ts";
  5. import { initTheme } from "../../../src/modes/interactive/theme/theme.ts";
  6. import { stripAnsi } from "../../../src/utils/ansi.ts";
  7. vi.mock("../../../src/utils/open-browser.ts", () => ({
  8. openBrowser: vi.fn(),
  9. }));
  10. function createDialog(): LoginDialogComponent {
  11. return new LoginDialogComponent(
  12. { requestRender: vi.fn() } as unknown as TUI,
  13. "prompt-repro",
  14. () => {},
  15. "Prompt Repro",
  16. );
  17. }
  18. function renderDialog(dialog: LoginDialogComponent): string[] {
  19. return stripAnsi(dialog.render(120).join("\n"))
  20. .split("\n")
  21. .map((line) => line.trimEnd());
  22. }
  23. function countRenderedValue(lines: string[], value: string): number {
  24. return lines.filter((line) => line.trim() === `> ${value}`).length;
  25. }
  26. describe("LoginDialogComponent OAuth prompts", () => {
  27. beforeAll(() => {
  28. initTheme("dark");
  29. });
  30. beforeEach(() => {
  31. setKeybindings(new KeybindingsManager());
  32. });
  33. test("keeps previous prompt input stable when a later prompt is active", async () => {
  34. const dialog = createDialog();
  35. const firstPrompt = dialog.showPrompt("First prompt:", "first-value");
  36. dialog.handleInput("first-value");
  37. dialog.handleInput("\n");
  38. await expect(firstPrompt).resolves.toBe("first-value");
  39. const secondPrompt = dialog.showPrompt("Second prompt:");
  40. dialog.handleInput("second-secret-demo");
  41. const lines = renderDialog(dialog);
  42. expect(lines.join("\n")).toContain("First prompt:");
  43. expect(lines.join("\n")).toContain("Second prompt:");
  44. expect(countRenderedValue(lines, "first-value")).toBe(1);
  45. expect(countRenderedValue(lines, "second-secret-demo")).toBe(1);
  46. dialog.handleInput("\n");
  47. await expect(secondPrompt).resolves.toBe("second-secret-demo");
  48. });
  49. test("preserves auth instructions when showing a prompt", () => {
  50. const dialog = createDialog();
  51. dialog.showAuth("https://example.invalid/login", "Authorize the extension");
  52. dialog.showPrompt("First prompt:");
  53. const output = renderDialog(dialog).join("\n");
  54. expect(output).toContain("https://example.invalid/login");
  55. expect(output).toContain("Authorize the extension");
  56. expect(output).toContain("First prompt:");
  57. });
  58. test("keeps previous manual input stable when a later prompt is active", async () => {
  59. const dialog = createDialog();
  60. const manualInput = dialog.showManualInput("Paste callback URL:");
  61. dialog.handleInput("callback-value");
  62. dialog.handleInput("\n");
  63. await expect(manualInput).resolves.toBe("callback-value");
  64. const prompt = dialog.showPrompt("Second prompt:");
  65. dialog.handleInput("second-secret-demo");
  66. const lines = renderDialog(dialog);
  67. expect(lines.join("\n")).toContain("Paste callback URL:");
  68. expect(lines.join("\n")).toContain("Second prompt:");
  69. expect(countRenderedValue(lines, "callback-value")).toBe(1);
  70. expect(countRenderedValue(lines, "second-secret-demo")).toBe(1);
  71. dialog.handleInput("\n");
  72. await expect(prompt).resolves.toBe("second-secret-demo");
  73. });
  74. });