@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 100 lines 3.79 KB
Raw
1 import { expect, it } from "vite-plus/test";
2 import { userEvent } from "vite-plus/test/browser";
3 import { createWorkspaceFocusController } from "../lib/workspaceFocus";
4 import { shouldRefocusComposerOnWindowFocus } from "./ChatView.logic";
5
6 it("keeps keyboard focus on a sidebar row on window return, but not a clicked row", async () => {
7 const list = document.createElement("ul");
8 const row = (name: string) => {
9 const element = document.createElement("div");
10 element.setAttribute("role", "button");
11 element.tabIndex = 0;
12 element.dataset.sidebarRow = name;
13 element.textContent = name;
14 list.append(element);
15 return element;
16 };
17 const clicked = row("Clicked thread");
18 const tabbed = row("Tabbed thread");
19 document.body.append(list);
20 using _list = { [Symbol.dispose]: () => list.remove() };
21
22 await userEvent.click(clicked);
23 expect(document.activeElement).toBe(clicked);
24 expect(shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection())).toBe(
25 true,
26 );
27
28 await userEvent.tab();
29 expect(document.activeElement).toBe(tabbed);
30 expect(shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection())).toBe(
31 false,
32 );
33 });
34
35 it("leaves a text selection in the timeline alone on window return", async () => {
36 const timeline = document.createElement("div");
37 timeline.tabIndex = -1;
38 const reply = document.createElement("p");
39 reply.textContent = "Steel bridges are also easy to recycle at the end of their life.";
40 timeline.append(reply);
41 document.body.append(timeline);
42 using _timeline = { [Symbol.dispose]: () => timeline.remove() };
43
44 await userEvent.tripleClick(reply);
45 expect(document.activeElement).toBe(timeline);
46 expect(document.getSelection()?.toString()).toContain("Steel bridges");
47 expect(
48 shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection()),
49 "refocusing the composer would clear the selected reply text",
50 ).toBe(false);
51 });
52
53 it("keeps focus in a region with its own keys on window return", async () => {
54 const region = document.createElement("div");
55 region.tabIndex = -1;
56 region.setAttribute("aria-keyshortcuts", "J K");
57 region.textContent = "Guide prose";
58 document.body.append(region);
59 using _region = { [Symbol.dispose]: () => region.remove() };
60
61 await userEvent.click(region);
62 expect(document.activeElement).toBe(region);
63 expect(shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection())).toBe(
64 false,
65 );
66 });
67
68 it("refocuses the composer on window return from a clicked area after a bare key press", async () => {
69 const controller = createWorkspaceFocusController(document);
70 const root = document.createElement("div");
71 root.innerHTML =
72 '<nav data-workspace-area="sidebar" data-workspace-order="0" tabindex="-1"><button>Thread</button></nav>' +
73 '<section data-workspace-area="conversation" data-workspace-order="1" tabindex="-1" style="height: 200px"></section>';
74 document.body.append(root);
75 using _root = {
76 [Symbol.dispose]: () => {
77 controller.dispose();
78 root.remove();
79 },
80 };
81 const conversation = root.querySelector("section")!;
82
83 await userEvent.click(conversation);
84 expect(document.activeElement).toBe(conversation);
85 // A window switch can deliver a lone modifier key without moving focus.
86 await userEvent.keyboard("{Shift}");
87 expect(
88 shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection()),
89 "a clicked area counts as reached from the keyboard after a bare key press",
90 ).toBe(true);
91
92 await userEvent.keyboard("{F6}");
93 controller.move(-1);
94 await userEvent.keyboard("{F6}");
95 controller.move(1);
96 expect(document.activeElement).toBe(conversation);
97 expect(shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection())).toBe(
98 false,
99 );
100 });