|
| 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 | + (globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 25 | +}); |
| 26 | + |
| 27 | +afterEach(() => { |
| 28 | + testWindow.close(); |
| 29 | + for (const key of globals) { |
| 30 | + Object.defineProperty(globalThis, key, { configurable: true, value: previousGlobals[key] }); |
| 31 | + } |
| 32 | +}); |
| 33 | + |
| 34 | +function provider(liveModels?: boolean): WorkspaceItem { |
| 35 | + return { |
| 36 | + name: "custom-provider", |
| 37 | + adapter: "openai-chat", |
| 38 | + baseUrl: "https://example.test/v1", |
| 39 | + authMode: "key", |
| 40 | + note: "before", |
| 41 | + ...(liveModels === undefined ? {} : { liveModels }), |
| 42 | + } as WorkspaceItem; |
| 43 | +} |
| 44 | + |
| 45 | +async function mountSettings(item: WorkspaceItem): Promise<{ |
| 46 | + root: Root; |
| 47 | + container: HTMLElement; |
| 48 | + patches: ProviderUpdatePatch[]; |
| 49 | +}> { |
| 50 | + const patches: ProviderUpdatePatch[] = []; |
| 51 | + const container = document.createElement("div"); |
| 52 | + document.body.append(container); |
| 53 | + const { createRoot } = await import("react-dom/client"); |
| 54 | + let root!: Root; |
| 55 | + await act(async () => { |
| 56 | + root = createRoot(container); |
| 57 | + root.render( |
| 58 | + <LanguageProvider> |
| 59 | + <ProviderSettings |
| 60 | + item={item} |
| 61 | + onUpdateProvider={async (_name, patch) => { |
| 62 | + patches.push(patch); |
| 63 | + return { ok: true }; |
| 64 | + }} |
| 65 | + /> |
| 66 | + </LanguageProvider>, |
| 67 | + ); |
| 68 | + }); |
| 69 | + return { root, container, patches }; |
| 70 | +} |
| 71 | + |
| 72 | +async function save(container: HTMLElement): Promise<void> { |
| 73 | + const button = container.querySelector<HTMLButtonElement>(".pwi-settings-sticky-bar .btn-primary"); |
| 74 | + expect(button).toBeTruthy(); |
| 75 | + await act(async () => { |
| 76 | + button!.click(); |
| 77 | + await Promise.resolve(); |
| 78 | + }); |
| 79 | +} |
| 80 | + |
| 81 | +test("an unrelated settings save does not materialize an omitted liveModels value", async () => { |
| 82 | + const { root, container, patches } = await mountSettings(provider()); |
| 83 | + const note = container.querySelector<HTMLTextAreaElement>(".pwi-settings-textarea")!; |
| 84 | + |
| 85 | + await act(async () => { |
| 86 | + Object.getOwnPropertyDescriptor(testWindow.HTMLTextAreaElement.prototype, "value")! |
| 87 | + .set!.call(note, "after"); |
| 88 | + note.dispatchEvent(new testWindow.Event("input", { bubbles: true })); |
| 89 | + }); |
| 90 | + await save(container); |
| 91 | + |
| 92 | + expect(patches).toHaveLength(1); |
| 93 | + expect(Object.hasOwn(patches[0]!, "liveModels")).toBe(false); |
| 94 | + await act(async () => { root.unmount(); }); |
| 95 | +}); |
| 96 | + |
| 97 | +test("changing an omitted effective true to false sends an explicit liveModels choice", async () => { |
| 98 | + const { root, container, patches } = await mountSettings(provider()); |
| 99 | + const toggles = container.querySelectorAll<HTMLInputElement>('input[type="checkbox"]'); |
| 100 | + |
| 101 | + await act(async () => { toggles[1]!.click(); }); |
| 102 | + await save(container); |
| 103 | + |
| 104 | + expect(patches[0]?.liveModels).toBe(false); |
| 105 | + await act(async () => { root.unmount(); }); |
| 106 | +}); |
| 107 | + |
| 108 | +test("changing an explicit false to true sends an explicit liveModels choice", async () => { |
| 109 | + const { root, container, patches } = await mountSettings(provider(false)); |
| 110 | + const toggles = container.querySelectorAll<HTMLInputElement>('input[type="checkbox"]'); |
| 111 | + |
| 112 | + await act(async () => { toggles[1]!.click(); }); |
| 113 | + await save(container); |
| 114 | + |
| 115 | + expect(patches[0]?.liveModels).toBe(true); |
| 116 | + await act(async () => { root.unmount(); }); |
| 117 | +}); |
0 commit comments