Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

announce.browser.test.ts

85 lines2.8 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.advanceTimersByTime(250);
37 const first = regionTexts();
38 announce("Claude");
39 vi.advanceTimersByTime(250);
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.advanceTimersByTime(250);
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.advanceTimersByTime(250);
59​
60 expect(spokenText()).toEqual(["Opening in Zed"]);
61 });
62​
63 it("removes a spoken message from the page once it has had time to be read", () => {
64 using _timers = fakeTimers();
65​
66 announce("Model: Claude Sonnet 5.5");
67 vi.advanceTimersByTime(250);
68 expect(spokenText()).toEqual(["Model: Claude Sonnet 5.5"]);
69​
70 vi.advanceTimersByTime(60_000);
71 expect(spokenText()).toEqual([]);
72 });
73​
74 it("removes a spoken message from the page when its source cancels it", () => {
75 using _timers = fakeTimers();
76​
77 announce("Settled 1 thread, Ctrl+Z to undo", "thread-undo-notice");
78 vi.advanceTimersByTime(250);
79 expect(spokenText()).toEqual(["Settled 1 thread, Ctrl+Z to undo"]);
80​
81 vi.advanceTimersByTime(4_750);
82 cancelAnnouncement("thread-undo-notice");
83 expect(spokenText()).toEqual([]);
84 });
85});