|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | +import type { AccessToken } from "../src/api/client.js"; |
| 4 | +import { AuthenticationError } from "../src/auth/controller.js"; |
| 5 | +import type { Authentication } from "../src/auth/controller.js"; |
| 6 | +import { createAuthenticationPrompt } from "../src/auth/prompt.js"; |
| 7 | + |
| 8 | +class Dialog extends EventTarget { |
| 9 | + open = false; |
| 10 | + showModal(): void { this.open = true; } |
| 11 | + close(): void { |
| 12 | + this.open = false; |
| 13 | + this.dispatchEvent(new Event("close")); |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +const token = { value: "token", expiresAt: 2_000_000_000, creationGrant: "grant" } as AccessToken; |
| 18 | + |
| 19 | +function fixture(existing: AccessToken | null = null) { |
| 20 | + const dialog = new Dialog(); |
| 21 | + const continueButton = new EventTarget(); |
| 22 | + const closeButton = new EventTarget(); |
| 23 | + const message = { textContent: "" }; |
| 24 | + let calls = 0; |
| 25 | + let resolveOperation!: (value: AccessToken) => void; |
| 26 | + let rejectOperation!: (reason: Error) => void; |
| 27 | + const authentication = { |
| 28 | + token: () => existing, |
| 29 | + authenticate: () => { |
| 30 | + calls++; |
| 31 | + return new Promise<AccessToken>((resolve, reject) => { |
| 32 | + resolveOperation = resolve; |
| 33 | + rejectOperation = reject; |
| 34 | + }); |
| 35 | + }, |
| 36 | + } as unknown as Authentication; |
| 37 | + const prompt = createAuthenticationPrompt({ |
| 38 | + authentication, |
| 39 | + dialog: dialog as unknown as HTMLDialogElement, |
| 40 | + continueButton: continueButton as unknown as HTMLButtonElement, |
| 41 | + closeButtons: [closeButton as unknown as HTMLElement], |
| 42 | + message: message as unknown as HTMLElement, |
| 43 | + }); |
| 44 | + return { dialog, continueButton, closeButton, message, prompt, |
| 45 | + calls: () => calls, resolve: (value = token) => { resolveOperation(value); }, |
| 46 | + reject: (error: Error) => { rejectOperation(error); } }; |
| 47 | +} |
| 48 | + |
| 49 | +void test("existing session bypasses the prompt", async () => { |
| 50 | + const f = fixture(token); |
| 51 | + assert.equal(await f.prompt.authenticate("Hello"), token); |
| 52 | + assert.equal(f.dialog.open, false); |
| 53 | + assert.equal(f.calls(), 0); |
| 54 | + f.prompt.destroy(); |
| 55 | +}); |
| 56 | + |
| 57 | +void test("Continue starts one OAuth operation for simultaneous callers", async () => { |
| 58 | + const f = fixture(); |
| 59 | + const first = f.prompt.authenticate("Sign in to vote"); |
| 60 | + const second = f.prompt.authenticate("Other message"); |
| 61 | + assert.equal(first, second); |
| 62 | + assert.equal(f.message.textContent, "Sign in to vote"); |
| 63 | + f.continueButton.dispatchEvent(new Event("click")); |
| 64 | + assert.equal(f.calls(), 1); |
| 65 | + assert.equal(f.dialog.open, false); |
| 66 | + f.resolve(); |
| 67 | + assert.equal(await first, token); |
| 68 | + f.prompt.destroy(); |
| 69 | +}); |
| 70 | + |
| 71 | +void test("close button, native cancel, and external close cancel the prompt", async () => { |
| 72 | + for (const method of ["button", "cancel", "close"] as const) { |
| 73 | + const f = fixture(); |
| 74 | + const pending = f.prompt.authenticate(); |
| 75 | + if (method === "button") f.closeButton.dispatchEvent(new Event("click")); |
| 76 | + if (method === "cancel") f.dialog.dispatchEvent(new Event("cancel")); |
| 77 | + if (method === "close") f.dialog.close(); |
| 78 | + await assert.rejects(pending, (error: unknown) => |
| 79 | + error instanceof AuthenticationError && error.code === "cancelled"); |
| 80 | + f.prompt.destroy(); |
| 81 | + } |
| 82 | +}); |
| 83 | + |
| 84 | +void test("destroy settles pending work and removes listeners", async () => { |
| 85 | + const f = fixture(); |
| 86 | + const pending = f.prompt.authenticate(); |
| 87 | + f.prompt.destroy(); |
| 88 | + await assert.rejects(pending, (error: unknown) => |
| 89 | + error instanceof AuthenticationError && error.code === "cancelled"); |
| 90 | + f.continueButton.dispatchEvent(new Event("click")); |
| 91 | + assert.equal(f.calls(), 0); |
| 92 | +}); |
| 93 | + |
| 94 | +void test("failed OAuth closes the prompt and permits retry", async () => { |
| 95 | + const f = fixture(); |
| 96 | + const first = f.prompt.authenticate(); |
| 97 | + f.continueButton.dispatchEvent(new Event("click")); |
| 98 | + f.reject(new Error("OAuth failed")); |
| 99 | + await assert.rejects(first, /OAuth failed/); |
| 100 | + const second = f.prompt.authenticate(); |
| 101 | + assert.equal(f.dialog.open, true); |
| 102 | + f.continueButton.dispatchEvent(new Event("click")); |
| 103 | + f.resolve(); |
| 104 | + assert.equal(await second, token); |
| 105 | + assert.equal(f.calls(), 2); |
| 106 | + f.prompt.destroy(); |
| 107 | +}); |
0 commit comments