|
| 1 | +import { GlobalWindow } from "happy-dom"; |
| 2 | + |
| 3 | +// Setup basic DOM environment for testing-library |
| 4 | +const dom = new GlobalWindow(); |
| 5 | +/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access */ |
| 6 | +(global as any).window = dom.window; |
| 7 | +(global as any).document = dom.window.document; |
| 8 | + |
| 9 | +// Ensure globals exist for instanceof checks inside usePersistedState |
| 10 | +(globalThis as any).StorageEvent = dom.window.StorageEvent; |
| 11 | +(globalThis as any).CustomEvent = dom.window.CustomEvent; |
| 12 | + |
| 13 | +// happy-dom's requestAnimationFrame behavior can vary; ensure it's present so |
| 14 | +// usePersistedState listener updates (which batch via RAF) are flushed. |
| 15 | +if (!globalThis.requestAnimationFrame) { |
| 16 | + globalThis.requestAnimationFrame = (cb: FrameRequestCallback) => |
| 17 | + setTimeout(() => cb(Date.now()), 0) as unknown as number; |
| 18 | +} |
| 19 | +if (!globalThis.cancelAnimationFrame) { |
| 20 | + globalThis.cancelAnimationFrame = (id: number) => { |
| 21 | + clearTimeout(id as unknown as NodeJS.Timeout); |
| 22 | + }; |
| 23 | +} |
| 24 | +(global as any).console = console; |
| 25 | +/* eslint-enable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access */ |
| 26 | + |
| 27 | +import { afterEach, beforeEach, describe, expect, test } from "bun:test"; |
| 28 | +import { act, cleanup, render, waitFor } from "@testing-library/react"; |
| 29 | +import React from "react"; |
| 30 | +import { ThinkingProvider } from "./ThinkingContext"; |
| 31 | +import { useThinkingLevel } from "@/browser/hooks/useThinkingLevel"; |
| 32 | +import { getModelKey, getThinkingLevelByModelKey } from "@/common/constants/storage"; |
| 33 | +import { updatePersistedState } from "@/browser/hooks/usePersistedState"; |
| 34 | + |
| 35 | +interface TestProps { |
| 36 | + workspaceId: string; |
| 37 | +} |
| 38 | + |
| 39 | +const TestComponent: React.FC<TestProps> = (props) => { |
| 40 | + const [thinkingLevel] = useThinkingLevel(); |
| 41 | + return ( |
| 42 | + <div data-testid="thinking"> |
| 43 | + {thinkingLevel}:{props.workspaceId} |
| 44 | + </div> |
| 45 | + ); |
| 46 | +}; |
| 47 | + |
| 48 | +describe("ThinkingContext", () => { |
| 49 | + // Make getDefaultModel deterministic. |
| 50 | + // (getDefaultModel reads from the global "model-default" localStorage key.) |
| 51 | + beforeEach(() => { |
| 52 | + window.localStorage.setItem("model-default", JSON.stringify("openai:default")); |
| 53 | + }); |
| 54 | + beforeEach(() => { |
| 55 | + window.localStorage.clear(); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + cleanup(); |
| 60 | + }); |
| 61 | + |
| 62 | + test("switching models restores the per-model thinking level", async () => { |
| 63 | + const workspaceId = "ws-1"; |
| 64 | + |
| 65 | + // Model A |
| 66 | + updatePersistedState(getModelKey(workspaceId), "openai:gpt-5.2"); |
| 67 | + updatePersistedState(getThinkingLevelByModelKey("openai:gpt-5.2"), "high"); |
| 68 | + |
| 69 | + // Model B |
| 70 | + updatePersistedState(getThinkingLevelByModelKey("anthropic:claude-3.5"), "low"); |
| 71 | + |
| 72 | + const view = render( |
| 73 | + <ThinkingProvider workspaceId={workspaceId}> |
| 74 | + <TestComponent workspaceId={workspaceId} /> |
| 75 | + </ThinkingProvider> |
| 76 | + ); |
| 77 | + |
| 78 | + await waitFor(() => { |
| 79 | + expect(view.getByTestId("thinking").textContent).toBe("high:ws-1"); |
| 80 | + }); |
| 81 | + |
| 82 | + // Change model -> should restore that model's stored thinking level |
| 83 | + act(() => { |
| 84 | + updatePersistedState(getModelKey(workspaceId), "anthropic:claude-3.5"); |
| 85 | + }); |
| 86 | + |
| 87 | + await waitFor(() => { |
| 88 | + expect(view.getByTestId("thinking").textContent).toBe("low:ws-1"); |
| 89 | + }); |
| 90 | + }); |
| 91 | +}); |
0 commit comments