|
| 1 | +import { afterEach, beforeEach, expect, test } from "bun:test"; |
| 2 | +import { Window } from "happy-dom"; |
| 3 | +import { act, useState } from "react"; |
| 4 | +import type { Root } from "react-dom/client"; |
| 5 | +import { LanguageProvider } from "../src/i18n/provider"; |
| 6 | +import { ClaudeCodeSettingsCard } from "../src/pages/claude-code-sections"; |
| 7 | +import type { ClaudeCodeState } from "../src/pages/claude-code-types"; |
| 8 | + |
| 9 | +const globals = ["document", "window", "navigator", "localStorage", "IS_REACT_ACT_ENVIRONMENT"] as const; |
| 10 | +let previousGlobals: Record<(typeof globals)[number], unknown>; |
| 11 | +let testWindow: Window; |
| 12 | + |
| 13 | +beforeEach(() => { |
| 14 | + previousGlobals = Object.fromEntries(globals.map(key => [key, Reflect.get(globalThis, key)])) as typeof previousGlobals; |
| 15 | + testWindow = new Window({ url: "http://localhost/" }); |
| 16 | + Object.defineProperty(testWindow.navigator, "language", { configurable: true, value: "en-US" }); |
| 17 | + Object.defineProperties(globalThis, { |
| 18 | + document: { configurable: true, value: testWindow.document }, |
| 19 | + window: { configurable: true, value: testWindow }, |
| 20 | + navigator: { configurable: true, value: testWindow.navigator }, |
| 21 | + localStorage: { configurable: true, value: testWindow.localStorage }, |
| 22 | + }); |
| 23 | + (globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 24 | +}); |
| 25 | + |
| 26 | +afterEach(() => { |
| 27 | + testWindow.close(); |
| 28 | + for (const key of globals) { |
| 29 | + Object.defineProperty(globalThis, key, { configurable: true, value: previousGlobals[key] }); |
| 30 | + } |
| 31 | +}); |
| 32 | + |
| 33 | +const BASE_STATE: ClaudeCodeState = { |
| 34 | + enabled: true, |
| 35 | + authMode: "proxy", |
| 36 | + autoConnectSupported: false, |
| 37 | + systemEnv: false, |
| 38 | + fastMode: null, |
| 39 | + maxContextTokens: null, |
| 40 | + autoContext: true, |
| 41 | + autoCompactWindow: null, |
| 42 | + injectAgents: true, |
| 43 | + smallFastModel: "", |
| 44 | + effectiveModelEnv: {}, |
| 45 | + available: [], |
| 46 | + aliases: [], |
| 47 | + port: 10100, |
| 48 | +}; |
| 49 | + |
| 50 | +async function mountSettings( |
| 51 | + initial: ClaudeCodeState = BASE_STATE, |
| 52 | +): Promise<{ root: Root; host: HTMLDivElement; getState: () => ClaudeCodeState }> { |
| 53 | + const host = document.createElement("div"); |
| 54 | + document.body.append(host); |
| 55 | + let latest = initial; |
| 56 | + function Harness() { |
| 57 | + const [state, setState] = useState(initial); |
| 58 | + latest = state; |
| 59 | + return ( |
| 60 | + <LanguageProvider> |
| 61 | + <ClaudeCodeSettingsCard |
| 62 | + state={state} |
| 63 | + autoCompactOptions={[{ value: "", label: "Default" }]} |
| 64 | + onStateChange={setState} |
| 65 | + /> |
| 66 | + </LanguageProvider> |
| 67 | + ); |
| 68 | + } |
| 69 | + const { createRoot } = await import("react-dom/client"); |
| 70 | + let root!: Root; |
| 71 | + await act(async () => { |
| 72 | + root = createRoot(host); |
| 73 | + root.render(<Harness />); |
| 74 | + }); |
| 75 | + return { root, host, getState: () => latest }; |
| 76 | +} |
| 77 | + |
| 78 | +function sidecarModelInputs(host: HTMLElement): HTMLInputElement[] { |
| 79 | + return [...host.querySelectorAll<HTMLInputElement>('input[aria-label="Model"]')]; |
| 80 | +} |
| 81 | + |
| 82 | +function sidecarBackendTriggers(host: HTMLElement): HTMLButtonElement[] { |
| 83 | + return [...host.querySelectorAll<HTMLButtonElement>('button.select-trigger[aria-label="Backend"]')]; |
| 84 | +} |
| 85 | + |
| 86 | +async function pickOption(trigger: HTMLButtonElement, label: string): Promise<void> { |
| 87 | + await act(async () => { |
| 88 | + trigger.click(); |
| 89 | + }); |
| 90 | + const option = [...document.body.querySelectorAll<HTMLElement>('[role="option"]')] |
| 91 | + .find(el => el.textContent?.includes(label)); |
| 92 | + expect(option).toBeTruthy(); |
| 93 | + await act(async () => { |
| 94 | + option!.click(); |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +test("Inherit → Auto stays Auto and enables the sidecar model input", async () => { |
| 99 | + const { root, host, getState } = await mountSettings(); |
| 100 | + const [webSearchTrigger] = sidecarBackendTriggers(host); |
| 101 | + const [webSearchModel] = sidecarModelInputs(host); |
| 102 | + expect(webSearchTrigger).toBeTruthy(); |
| 103 | + expect(webSearchModel.disabled).toBe(true); |
| 104 | + expect(getState().webSearchSidecar).toBeUndefined(); |
| 105 | + |
| 106 | + await pickOption(webSearchTrigger!, "Auto"); |
| 107 | + |
| 108 | + expect(getState().webSearchSidecar).toEqual({ backend: undefined }); |
| 109 | + expect(sidecarSelectLabel(host, 0)).toContain("Auto"); |
| 110 | + expect(sidecarModelInputs(host)[0]!.disabled).toBe(false); |
| 111 | + |
| 112 | + await act(async () => { root.unmount(); }); |
| 113 | +}); |
| 114 | + |
| 115 | +test("Empty Auto draft keeps model input enabled until Inherit is chosen", async () => { |
| 116 | + const { root, host, getState } = await mountSettings(); |
| 117 | + const [webSearchTrigger] = sidecarBackendTriggers(host); |
| 118 | + await pickOption(webSearchTrigger!, "Auto"); |
| 119 | + |
| 120 | + const model = sidecarModelInputs(host)[0]!; |
| 121 | + await act(async () => { |
| 122 | + Object.getOwnPropertyDescriptor(testWindow.HTMLInputElement.prototype, "value")! |
| 123 | + .set!.call(model, "gpt-4o"); |
| 124 | + model.dispatchEvent(new testWindow.Event("input", { bubbles: true })); |
| 125 | + }); |
| 126 | + expect(getState().webSearchSidecar).toEqual({ backend: undefined, model: "gpt-4o" }); |
| 127 | + |
| 128 | + await act(async () => { |
| 129 | + Object.getOwnPropertyDescriptor(testWindow.HTMLInputElement.prototype, "value")! |
| 130 | + .set!.call(model, ""); |
| 131 | + model.dispatchEvent(new testWindow.Event("input", { bubbles: true })); |
| 132 | + }); |
| 133 | + |
| 134 | + // Clearing the model must not collapse Auto back to Inherit. |
| 135 | + expect(getState().webSearchSidecar).toEqual({ backend: undefined, model: "" }); |
| 136 | + expect(model.disabled).toBe(false); |
| 137 | + expect(sidecarSelectLabel(host, 0)).toContain("Auto"); |
| 138 | + |
| 139 | + await pickOption(sidecarBackendTriggers(host)[0]!, "Use main setting"); |
| 140 | + expect(getState().webSearchSidecar).toBeUndefined(); |
| 141 | + expect(sidecarModelInputs(host)[0]!.disabled).toBe(true); |
| 142 | + |
| 143 | + await act(async () => { root.unmount(); }); |
| 144 | +}); |
| 145 | + |
| 146 | +function sidecarSelectLabel(host: HTMLElement, index: number): string { |
| 147 | + return sidecarBackendTriggers(host)[index]?.textContent ?? ""; |
| 148 | +} |
0 commit comments