|
| 1 | +import { afterEach, describe, expect, test } from "bun:test"; |
| 2 | +import { act, createElement } from "react"; |
| 3 | +import { createRoot } from "react-dom/client"; |
| 4 | +import { renderToStaticMarkup } from "react-dom/server"; |
| 5 | + |
| 6 | +import { ThinkingLabel } from "./thinking-label.js"; |
| 7 | + |
| 8 | +type Restore = () => void; |
| 9 | +type Listener = (event: { matches: boolean }) => void; |
| 10 | + |
| 11 | +function stubMatchMedia(initialReducedMotion: boolean): { restore: Restore; set: (value: boolean) => void } { |
| 12 | + let matches = initialReducedMotion; |
| 13 | + const listeners = new Set<Listener>(); |
| 14 | + const original = window.matchMedia; |
| 15 | + window.matchMedia = ((query: string) => |
| 16 | + ({ |
| 17 | + get matches() { |
| 18 | + return matches && query.includes("prefers-reduced-motion"); |
| 19 | + }, |
| 20 | + media: query, |
| 21 | + addEventListener: (_type: string, listener: Listener) => listeners.add(listener), |
| 22 | + removeEventListener: (_type: string, listener: Listener) => listeners.delete(listener), |
| 23 | + }) as unknown as MediaQueryList) as typeof window.matchMedia; |
| 24 | + return { |
| 25 | + set: (value: boolean) => { |
| 26 | + matches = value; |
| 27 | + for (const listener of listeners) listener({ matches: value }); |
| 28 | + }, |
| 29 | + restore: () => { |
| 30 | + window.matchMedia = original; |
| 31 | + }, |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +function stubInterval(): { |
| 36 | + readonly calls: Array<{ fn: () => void; ms: number | undefined }>; |
| 37 | + readonly clears: number; |
| 38 | + readonly restore: Restore; |
| 39 | +} { |
| 40 | + const originalSet = globalThis.setInterval; |
| 41 | + const originalClear = globalThis.clearInterval; |
| 42 | + const calls: Array<{ fn: () => void; ms: number | undefined }> = []; |
| 43 | + const state = { clears: 0 }; |
| 44 | + globalThis.setInterval = ((fn: TimerHandler, ms?: number) => { |
| 45 | + calls.push({ fn: fn as () => void, ms }); |
| 46 | + return 1 as unknown as ReturnType<typeof setInterval>; |
| 47 | + }) as unknown as typeof setInterval; |
| 48 | + globalThis.clearInterval = (() => { |
| 49 | + state.clears += 1; |
| 50 | + }) as unknown as typeof clearInterval; |
| 51 | + return { |
| 52 | + calls, |
| 53 | + get clears() { |
| 54 | + return state.clears; |
| 55 | + }, |
| 56 | + restore: () => { |
| 57 | + globalThis.setInterval = originalSet; |
| 58 | + globalThis.clearInterval = originalClear; |
| 59 | + }, |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +function mount(props: { verbs: readonly string[]; intervalMs?: number }) { |
| 64 | + const container = document.createElement("div"); |
| 65 | + document.body.appendChild(container); |
| 66 | + const root = createRoot(container); |
| 67 | + act(() => { |
| 68 | + root.render(createElement(ThinkingLabel, props)); |
| 69 | + }); |
| 70 | + return { |
| 71 | + container, |
| 72 | + // Every verb stays mounted in the stacked grid — the visible one is the |
| 73 | + // cell carrying opacity-100. |
| 74 | + visible: () => container.querySelector('[aria-hidden] .opacity-100')?.textContent ?? "", |
| 75 | + text: () => container.textContent ?? "", |
| 76 | + status: () => container.querySelector('[role="status"]'), |
| 77 | + unmount: () => { |
| 78 | + act(() => root.unmount()); |
| 79 | + container.remove(); |
| 80 | + }, |
| 81 | + }; |
| 82 | +} |
| 83 | + |
| 84 | +describe("ThinkingLabel", () => { |
| 85 | + const restores: Restore[] = []; |
| 86 | + afterEach(() => { |
| 87 | + while (restores.length > 0) restores.pop()?.(); |
| 88 | + }); |
| 89 | + |
| 90 | + test("empty verbs render nothing", () => { |
| 91 | + const html = renderToStaticMarkup(createElement(ThinkingLabel, { verbs: [] })); |
| 92 | + expect(html).toBe(""); |
| 93 | + }); |
| 94 | + |
| 95 | + test("reduced motion does not setInterval", () => { |
| 96 | + restores.push(stubMatchMedia(true).restore); |
| 97 | + const interval = stubInterval(); |
| 98 | + restores.push(interval.restore); |
| 99 | + |
| 100 | + const handle = mount({ verbs: ["Reading", "Thinking"] }); |
| 101 | + expect(interval.calls).toHaveLength(0); |
| 102 | + expect(handle.visible()).toBe("Reading"); |
| 103 | + handle.unmount(); |
| 104 | + }); |
| 105 | + |
| 106 | + test("turning reduced motion on live stops the interval", () => { |
| 107 | + const media = stubMatchMedia(false); |
| 108 | + restores.push(media.restore); |
| 109 | + const interval = stubInterval(); |
| 110 | + restores.push(interval.restore); |
| 111 | + |
| 112 | + const handle = mount({ verbs: ["Reading", "Thinking"] }); |
| 113 | + expect(interval.calls).toHaveLength(1); |
| 114 | + |
| 115 | + act(() => { |
| 116 | + interval.calls[0]?.fn(); |
| 117 | + }); |
| 118 | + expect(handle.visible()).toBe("Thinking"); |
| 119 | + |
| 120 | + act(() => media.set(true)); |
| 121 | + expect(interval.clears).toBeGreaterThan(0); |
| 122 | + expect(handle.visible()).toBe("Reading"); |
| 123 | + |
| 124 | + handle.unmount(); |
| 125 | + }); |
| 126 | + |
| 127 | + test("rotation advances after intervalMs", () => { |
| 128 | + restores.push(stubMatchMedia(false).restore); |
| 129 | + const interval = stubInterval(); |
| 130 | + restores.push(interval.restore); |
| 131 | + |
| 132 | + const handle = mount({ verbs: ["Reading", "Thinking", "Writing"], intervalMs: 50 }); |
| 133 | + expect(interval.calls).toHaveLength(1); |
| 134 | + expect(interval.calls[0]?.ms).toBe(50); |
| 135 | + expect(handle.visible()).toBe("Reading"); |
| 136 | + |
| 137 | + act(() => { |
| 138 | + interval.calls[0]?.fn(); |
| 139 | + }); |
| 140 | + expect(handle.visible()).toBe("Thinking"); |
| 141 | + |
| 142 | + act(() => { |
| 143 | + interval.calls[0]?.fn(); |
| 144 | + }); |
| 145 | + expect(handle.visible()).toBe("Writing"); |
| 146 | + |
| 147 | + handle.unmount(); |
| 148 | + }); |
| 149 | + |
| 150 | + test("status announces Working once and does not live-announce verb ticks", () => { |
| 151 | + restores.push(stubMatchMedia(false).restore); |
| 152 | + const interval = stubInterval(); |
| 153 | + restores.push(interval.restore); |
| 154 | + |
| 155 | + const handle = mount({ verbs: ["Reading", "Thinking"], intervalMs: 50 }); |
| 156 | + expect(handle.status()?.textContent).toBe("Working"); |
| 157 | + expect(handle.status()?.getAttribute("aria-live")).toBeNull(); |
| 158 | + expect(handle.container.querySelector("[aria-live]")).toBeNull(); |
| 159 | + |
| 160 | + act(() => { |
| 161 | + interval.calls[0]?.fn(); |
| 162 | + }); |
| 163 | + expect(handle.status()?.textContent).toBe("Working"); |
| 164 | + expect(handle.visible()).toBe("Thinking"); |
| 165 | + |
| 166 | + handle.unmount(); |
| 167 | + }); |
| 168 | +}); |
0 commit comments