|
| 1 | +import { afterEach, beforeEach, expect, test } from "bun:test"; |
| 2 | +import { Window } from "happy-dom"; |
| 3 | +import { act } from "react"; |
| 4 | +import type { Root } from "react-dom/client"; |
| 5 | +import ProviderSettings from "../src/components/provider-workspace/ProviderSettings"; |
| 6 | +import type { ProviderUpdatePatch } from "../src/components/provider-workspace/types"; |
| 7 | +import { LanguageProvider } from "../src/i18n/provider"; |
| 8 | +import type { WorkspaceItem } from "../src/provider-workspace/catalog"; |
| 9 | + |
| 10 | +const globals = ["document", "window", "navigator", "localStorage", "IS_REACT_ACT_ENVIRONMENT"] as const; |
| 11 | +let previousGlobals: Record<(typeof globals)[number], unknown>; |
| 12 | +let testWindow: Window; |
| 13 | + |
| 14 | +beforeEach(() => { |
| 15 | + previousGlobals = Object.fromEntries(globals.map(key => [key, Reflect.get(globalThis, key)])) as typeof previousGlobals; |
| 16 | + testWindow = new Window({ url: "http://localhost/#providers/workspace" }); |
| 17 | + Object.defineProperty(testWindow.navigator, "language", { configurable: true, value: "en-US" }); |
| 18 | + Object.defineProperties(globalThis, { |
| 19 | + document: { configurable: true, value: testWindow.document }, |
| 20 | + window: { configurable: true, value: testWindow }, |
| 21 | + navigator: { configurable: true, value: testWindow.navigator }, |
| 22 | + localStorage: { configurable: true, value: testWindow.localStorage }, |
| 23 | + }); |
| 24 | + setConfirm(true); |
| 25 | + (globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 26 | +}); |
| 27 | + |
| 28 | +afterEach(() => { |
| 29 | + testWindow.close(); |
| 30 | + for (const key of globals) { |
| 31 | + Object.defineProperty(globalThis, key, { configurable: true, value: previousGlobals[key] }); |
| 32 | + } |
| 33 | +}); |
| 34 | + |
| 35 | +function setConfirm(result: boolean): void { |
| 36 | + Object.defineProperty(testWindow, "confirm", { configurable: true, value: () => result }); |
| 37 | +} |
| 38 | + |
| 39 | +function openAiItem(mode: "pool" | "direct" = "pool"): WorkspaceItem { |
| 40 | + return { |
| 41 | + name: "openai", |
| 42 | + adapter: "openai-responses", |
| 43 | + baseUrl: "https://chatgpt.com/backend-api/codex", |
| 44 | + authMode: "forward", |
| 45 | + codexAccountMode: mode, |
| 46 | + } as WorkspaceItem; |
| 47 | +} |
| 48 | + |
| 49 | +async function mountSettings( |
| 50 | + item: WorkspaceItem, |
| 51 | + onUpdateProvider?: (name: string, patch: ProviderUpdatePatch) => Promise<{ ok: boolean; error?: string }>, |
| 52 | +): Promise<{ |
| 53 | + root: Root; |
| 54 | + container: HTMLElement; |
| 55 | + patches: ProviderUpdatePatch[]; |
| 56 | +}> { |
| 57 | + const patches: ProviderUpdatePatch[] = []; |
| 58 | + const container = document.createElement("div"); |
| 59 | + document.body.append(container); |
| 60 | + const { createRoot } = await import("react-dom/client"); |
| 61 | + let root!: Root; |
| 62 | + await act(async () => { |
| 63 | + root = createRoot(container); |
| 64 | + root.render( |
| 65 | + <LanguageProvider> |
| 66 | + <ProviderSettings |
| 67 | + item={item} |
| 68 | + onUpdateProvider={async (name, patch) => { |
| 69 | + patches.push(patch); |
| 70 | + return onUpdateProvider ? onUpdateProvider(name, patch) : { ok: true }; |
| 71 | + }} |
| 72 | + /> |
| 73 | + </LanguageProvider>, |
| 74 | + ); |
| 75 | + }); |
| 76 | + return { root, container, patches }; |
| 77 | +} |
| 78 | + |
| 79 | +function modeSelect(container: HTMLElement): HTMLSelectElement { |
| 80 | + const select = [...container.querySelectorAll<HTMLSelectElement>("select")] |
| 81 | + .find(s => s.value === "pool" || s.value === "direct"); |
| 82 | + expect(select).toBeTruthy(); |
| 83 | + return select!; |
| 84 | +} |
| 85 | + |
| 86 | +async function chooseMode(select: HTMLSelectElement, value: string): Promise<void> { |
| 87 | + await act(async () => { |
| 88 | + Object.getOwnPropertyDescriptor(testWindow.HTMLSelectElement.prototype, "value")! |
| 89 | + .set!.call(select, value); |
| 90 | + select.dispatchEvent(new testWindow.Event("change", { bubbles: true })); |
| 91 | + }); |
| 92 | +} |
| 93 | + |
| 94 | +test("a confirmed mode change sends the exact standalone codexAccountMode patch", async () => { |
| 95 | + const { root, container, patches } = await mountSettings(openAiItem("pool")); |
| 96 | + const select = modeSelect(container); |
| 97 | + |
| 98 | + await chooseMode(select, "direct"); |
| 99 | + |
| 100 | + expect(patches).toEqual([{ codexAccountMode: "direct" }]); |
| 101 | + expect(select.value).toBe("direct"); |
| 102 | + expect(container.querySelector('[role="status"]')?.textContent).toContain("Account mode saved."); |
| 103 | + await act(async () => { root.unmount(); }); |
| 104 | +}); |
| 105 | + |
| 106 | +test("a cancelled confirmation sends no patch and snaps the select back", async () => { |
| 107 | + const { root, container, patches } = await mountSettings(openAiItem("pool")); |
| 108 | + const select = modeSelect(container); |
| 109 | + setConfirm(false); |
| 110 | + |
| 111 | + await chooseMode(select, "direct"); |
| 112 | + |
| 113 | + expect(patches).toHaveLength(0); |
| 114 | + expect(select.value).toBe("pool"); |
| 115 | + await act(async () => { root.unmount(); }); |
| 116 | +}); |
| 117 | + |
| 118 | +test("a failed mode patch keeps the applied mode and announces the error", async () => { |
| 119 | + const { root, container } = await mountSettings(openAiItem("pool"), async () => ({ ok: false, error: "mode rejected" })); |
| 120 | + const select = modeSelect(container); |
| 121 | + |
| 122 | + await chooseMode(select, "direct"); |
| 123 | + |
| 124 | + expect(select.value).toBe("pool"); |
| 125 | + const alert = container.querySelector('[role="alert"]'); |
| 126 | + expect(alert?.textContent).toContain("mode rejected"); |
| 127 | + await act(async () => { root.unmount(); }); |
| 128 | +}); |
| 129 | + |
| 130 | +test("the mode select is disabled while its own PATCH is in flight", async () => { |
| 131 | + let resolvePatch!: (result: { ok: boolean }) => void; |
| 132 | + const pending = new Promise<{ ok: boolean }>(resolve => { |
| 133 | + resolvePatch = resolve; |
| 134 | + }); |
| 135 | + const { root, container } = await mountSettings(openAiItem(), async () => pending); |
| 136 | + const select = modeSelect(container); |
| 137 | + |
| 138 | + await chooseMode(select, "direct"); |
| 139 | + expect(select.disabled).toBe(true); |
| 140 | + |
| 141 | + await act(async () => { |
| 142 | + resolvePatch({ ok: true }); |
| 143 | + await pending; |
| 144 | + }); |
| 145 | + expect(select.disabled).toBe(false); |
| 146 | + await act(async () => { root.unmount(); }); |
| 147 | +}); |
| 148 | + |
| 149 | +test("the mode select is disabled while an ordinary settings save is in flight", async () => { |
| 150 | + let resolveSave!: (result: { ok: boolean; error?: string }) => void; |
| 151 | + const pending = new Promise<{ ok: boolean; error?: string }>(resolve => { |
| 152 | + resolveSave = resolve; |
| 153 | + }); |
| 154 | + const { root, container } = await mountSettings(openAiItem(), async () => pending); |
| 155 | + const note = container.querySelector<HTMLTextAreaElement>(".pwi-settings-textarea")!; |
| 156 | + |
| 157 | + await act(async () => { |
| 158 | + Object.getOwnPropertyDescriptor(testWindow.HTMLTextAreaElement.prototype, "value")! |
| 159 | + .set!.call(note, "changed"); |
| 160 | + note.dispatchEvent(new testWindow.Event("input", { bubbles: true })); |
| 161 | + }); |
| 162 | + const saveButton = container.querySelector<HTMLButtonElement>(".pwi-settings-sticky-bar .btn-primary")!; |
| 163 | + expect(saveButton).toBeTruthy(); |
| 164 | + await act(async () => { |
| 165 | + saveButton!.click(); |
| 166 | + await Promise.resolve(); |
| 167 | + }); |
| 168 | + |
| 169 | + expect(modeSelect(container).disabled).toBe(true); |
| 170 | + await act(async () => { |
| 171 | + resolveSave({ ok: true }); |
| 172 | + await pending; |
| 173 | + }); |
| 174 | + expect(modeSelect(container).disabled).toBe(false); |
| 175 | + await act(async () => { root.unmount(); }); |
| 176 | +}); |
0 commit comments