Skip to content
Source
Sign inSign up

announce.browser.test.ts

62 lines2.0 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2​
3import { fakeTimers } from "../../test/render";
4import { announce, cancelAnnouncement } from "./announce";
5​
6function spokenText() {
7 return Array.from(document.querySelectorAll('[aria-live="polite"]'), (region) =>
8 region.textContent?.trim(),
9 ).filter(Boolean);
11​
12describe("announce", () => {
13 it("waits for focus to settle, then speaks waiting messages together", () => {
14 using _timers = fakeTimers();
15 const composer = document.createElement("textarea");
16 document.body.append(composer);
17 using _composer = { [Symbol.dispose]: () => composer.remove() };
18​
19 announce("Settled 1 thread, Ctrl+Z to undo");
20 announce("Model: Claude Sonnet 5.5");
21 vi.advanceTimersByTime(200);
22 composer.focus();
23 vi.advanceTimersByTime(200);
24 expect(spokenText()).toEqual([]);
25​
26 vi.advanceTimersByTime(50);
27 expect(spokenText()).toEqual(["Settled 1 thread, Ctrl+Z to undo. Model: Claude Sonnet 5.5"]);
28 });
29​
30 it("speaks the same message twice in a row through the other region", () => {
31 using _timers = fakeTimers();
32 const regionTexts = () =>
33 Array.from(document.querySelectorAll('[aria-live="polite"]'), (region) => region.textContent);
34​
35 announce("Claude");
36 vi.runAllTimers();
37 const first = regionTexts();
38 announce("Claude");
39 vi.runAllTimers();
40​
41 expect(regionTexts()).toEqual([...first].reverse());
42 expect(regionTexts()).toContain("Claude");
43 });
44​
45 it("replaces a waiting message from the same source", () => {
46 using _timers = fakeTimers();
47​
48 announce("Settled 1 thread, Ctrl+Z to undo", "undo");
49 announce("Opening in VS Code");
50 announce("Settled 2 threads, Ctrl+Z to undo", "undo");
51 vi.runAllTimers();
52​
53 expect(spokenText()).toEqual(["Opening in VS Code. Settled 2 threads, Ctrl+Z to undo"]);
54​
55 announce("Settled 1 thread, Ctrl+Z to undo", "undo");
56 announce("Opening in Zed");
57 cancelAnnouncement("undo");
58 vi.runAllTimers();
59​
60 expect(spokenText()).toEqual(["Opening in Zed"]);
61 });
62});