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

ComposerPendingUserInputPanel.browser.test.tsx

178 lines5.1 KB

1import { EnvironmentId, RuntimeRequestId, ThreadId } from "@t3tools/contracts";
2import type { ReactNode } from "react";
3import { expect, it, vi } from "vite-plus/test";
4import { userEvent } from "vite-plus/test/browser";
5​
6import { render } from "../../../test/render";
7import type { PendingUserInput } from "../../session-logic";
8import { Select, SelectTrigger } from "../ui/select";
9import { ComposerPendingUserInputPanel } from "./ComposerPendingUserInputPanel";
10​
11const threadRef = { environmentId: EnvironmentId.make("env"), threadId: ThreadId.make("thread") };
12​
13const prompt: PendingUserInput = {
14 requestId: RuntimeRequestId.make("question-shortcuts"),
15 createdAt: "2026-10-31T00:00:00.000Z",
16 dismissible: false,
17 responseCapability: "live",
18 questions: [
19 {
20 id: "question",
21 header: "Rollout",
22 question: "How should this ship?",
23 options: [{ label: "Incremental", description: "One module at a time" }],
24 multiSelect: true,
25 },
26 ],
27};
28​
29function panel(onToggleOption: (questionId: string, optionValue: string) => void) {
30 return (
31 <ComposerPendingUserInputPanel
32 pendingUserInputs={[prompt]}
33 cwd={undefined}
34 threadRef={threadRef}
35 respondingRequestIds={[]}
36 answers={{}}
37 questionIndex={0}
38 onToggleOption={onToggleOption}
39 onAdvance={vi.fn()}
40 onDismiss={vi.fn()}
41 />
42 );
44​
45it.each<{ focus: string; control: ReactNode; toggles: boolean }>([
46 { focus: "a page button", control: <button type="button" aria-label="Focused" />, toggles: true },
47 {
48 focus: "a dialog",
49 control: (
50 <div role="dialog">
51 <button type="button" aria-label="Focused" />
52 </div>
53 ),
54 toggles: false,
55 },
56 {
57 focus: "a popup",
58 control: (
59 <div data-slot="menu-popup">
60 <button type="button" aria-label="Focused" />
61 </div>
62 ),
63 toggles: false,
64 },
65 {
66 focus: "a menu",
67 control: (
68 <div role="menu">
69 <button type="button" role="menuitem" aria-label="Focused" />
70 </div>
71 ),
72 toggles: false,
73 },
74 {
75 focus: "a select",
76 control: (
77 <select aria-label="Focused">
78 <option>One</option>
79 </select>
80 ),
81 toggles: false,
82 },
83 {
84 focus: "a closed select menu",
85 control: (
86 <Select>
87 <SelectTrigger aria-label="Focused" />
88 </Select>
89 ),
90 toggles: false,
91 },
92 {
93 focus: "a device screen that takes the key",
94 control: (
95 <div
96 role="application"
97 aria-label="Focused"
98 tabIndex={0}
99 onKeyDown={(event) => event.preventDefault()}
100 />
101 ),
102 toggles: false,
103 },
104])(
105 "digit shortcut with focus in $focus toggles an option: $toggles",
106 async ({ control, toggles }) => {
107 const onToggleOption = vi.fn();
108 await using screen = await render(
109 <>
110 {control}
111 {panel(onToggleOption)}
112 </>,
113 );
114 await expect.element(screen.getByText("Incremental")).toBeVisible();
115 screen.getByLabelText("Focused").element().focus();
116​
117 await userEvent.keyboard("1");
118​
119 expect(onToggleOption.mock.calls).toEqual(toggles ? [["question", "Incremental"]] : []);
120 },
121);
122​
123it.each<{ focus: string; html: string; nested?: boolean; typed: string; toggles: boolean }>([
124 {
125 focus: "a file editor",
126 html: '<div data-focus contenteditable="true"></div>',
127 typed: "1",
128 toggles: false,
129 },
130 { focus: "a find bar", html: "<input data-focus />", nested: true, typed: "1", toggles: false },
131 { focus: "a button", html: "<button data-focus></button>", typed: "", toggles: true },
132])(
133 "digit shortcut with focus in $focus inside a shadow root toggles an option: $toggles",
134 async ({ html, nested, typed, toggles }) => {
135 const onToggleOption = vi.fn();
136 await using screen = await render(
137 <>
138 <div data-testid="shadow-host" />
139 {panel(onToggleOption)}
140 </>,
141 );
142 await expect.element(screen.getByText("Incremental")).toBeVisible();
143 let root = screen.getByTestId("shadow-host").element().attachShadow({ mode: "open" });
144 if (nested)
145 root = root.appendChild(document.createElement("div")).attachShadow({ mode: "open" });
146 root.innerHTML = html;
147 const focused = root.querySelector<HTMLElement>("[data-focus]")!;
148 focused.focus();
149​
150 await userEvent.keyboard("1");
151​
152 expect(onToggleOption.mock.calls).toEqual(toggles ? [["question", "Incremental"]] : []);
153 expect("value" in focused ? focused.value : focused.textContent).toBe(typed);
154 },
155);
156​
157it("holding a digit key toggles its option once", async () => {
158 const onToggleOption = vi.fn();
159 await using screen = await render(
160 <>
161 <button type="button" aria-label="Focused" />
162 {panel(onToggleOption)}
163 </>,
164 );
165 await expect.element(screen.getByText("Incremental")).toBeVisible();
166 const focused = screen.getByLabelText("Focused").element();
167 focused.focus();
168​
169 const consumed = [false, true, true].map(
170 (repeat) =>
171 !focused.dispatchEvent(
172 new KeyboardEvent("keydown", { key: "1", repeat, bubbles: true, cancelable: true }),
173 ),
174 );
175​
176 expect(onToggleOption.mock.calls).toEqual([["question", "Incremental"]]);
177 expect(consumed).toEqual([true, true, true]);
178});