Open History to see the last commit
1import { EnvironmentId, ProjectId, type PullRequestReviewThread } from "@t3tools/contracts";
2import { getTranslationsSnapshot, GTProvider } from "gt-react";
3import { expect, it, vi } from "vite-plus/test";
4import { page, userEvent } from "vite-plus/test/browser";
5
6import { initializeI18n } from "~/i18n/initializeI18n";
7import { registerCommands } from "~/shortcuts/dispatcher";
8import { act, render } from "../../../test/render";
9import { RightPanelSheet } from "../RightPanelSheet";
10import { ReviewThreadCard } from "./PullRequestReviewAnnotation";
11
12const thread: PullRequestReviewThread = {
13 id: "thread",
14 path: "a.ts",
15 line: 3,
16 side: "right",
17 isResolved: false,
18 isOutdated: false,
19 comments: [
20 {
21 id: "comment",
22 author: { login: "reviewer", name: null, avatarUrl: null },
23 body: "Rename this.",
24 createdAt: "2026-09-01T00:00:00Z",
25 url: null,
26 },
27 ],
28};
29
30it("leaves IME keys in a review reply to the composition", async () => {
31 const onReply = vi.fn(async () => true);
32 await using screen = await render(
33 <ReviewThreadCard
34 thread={thread}
35 workspaceRoot="/workspace"
36 canReply
37 canResolve={false}
38 canReact={false}
39 environmentId={EnvironmentId.make("environment")}
40 reference={{ projectId: ProjectId.make("project"), repository: "owner/repo", number: 1 }}
41 completionScope={null}
42 pending={false}
43 onReply={onReply}
44 onLoadMore={async () => null}
45 canEditComment={() => false}
46 onEditComment={async () => false}
47 onToggleResolved={() => {}}
48 onReacted={() => {}}
49 />,
50 );
51 const heard: Array<{ key: string; consumed: boolean }> = [];
52 using _observer = {
53 [Symbol.dispose]: registerCommands(
54 {},
55 { observe: (event, consumed) => heard.push({ key: event.key, consumed }) },
56 ),
57 };
58 const reply = () => screen.getByRole("textbox", { name: "Reply to this conversation" });
59 await screen.getByRole("button", { name: "Reply", exact: true }).click();
60 await reply().fill("Draft");
61
62 for (const composing of [{ isComposing: true }, { keyCode: 229 }]) {
63 for (const keys of [
64 { key: "Escape" },
65 { key: "Enter", ctrlKey: true },
66 { key: "Enter", metaKey: true },
67 ]) {
68 const label = JSON.stringify({ ...keys, ...composing });
69 const event = new KeyboardEvent("keydown", {
70 ...keys,
71 ...composing,
72 bubbles: true,
73 cancelable: true,
74 });
75 await act(() => reply().element().dispatchEvent(event));
76 expect(event.defaultPrevented, label).toBe(false);
77 // The pull requests page leaves on an Escape nothing consumed.
78 expect(heard, label).toEqual([]);
79 await expect.element(reply()).toHaveValue("Draft");
80 }
81 }
82 expect(onReply).not.toHaveBeenCalled();
83
84 await userEvent.keyboard("{Escape}");
85 await expect.element(reply()).not.toBeInTheDocument();
86 expect(heard).toEqual([]);
87
88 await screen.getByRole("button", { name: "Reply", exact: true }).click();
89 await reply().fill("Draft");
90 await userEvent.keyboard("{Control>}{Enter}{/Control}");
91 expect(onReply).toHaveBeenCalledExactlyOnceWith("Draft");
92});
93
94it("Escape cancels a review reply without closing the right panel sheet", async () => {
95 const onCloseSheet = vi.fn();
96 await using _screen = await render(
97 <RightPanelSheet animationDurationMs={0} open onClose={onCloseSheet}>
98 <ReviewThreadCard
99 thread={thread}
100 workspaceRoot="/workspace"
101 canReply
102 canResolve={false}
103 canReact={false}
104 environmentId={EnvironmentId.make("environment")}
105 reference={{ projectId: ProjectId.make("project"), repository: "owner/repo", number: 1 }}
106 completionScope={null}
107 pending={false}
108 onReply={async () => true}
109 onLoadMore={async () => null}
110 canEditComment={() => false}
111 onEditComment={async () => false}
112 onToggleResolved={() => {}}
113 onReacted={() => {}}
114 />
115 </RightPanelSheet>,
116 );
117 await act(() =>
118 (page.getByRole("button", { name: "Reply", exact: true }).element() as HTMLElement).focus(),
119 );
120 await userEvent.keyboard("{Enter}");
121 const reply = page.getByRole("textbox", { name: "Reply to this conversation" });
122 await reply.fill("Draft");
123
124 await userEvent.keyboard("{Escape}");
125 await expect.element(reply).not.toBeInTheDocument();
126 expect(onCloseSheet).not.toHaveBeenCalled();
127
128 await userEvent.keyboard("{Escape}");
129 expect(onCloseSheet).toHaveBeenCalledOnce();
130});
131
132it("renders the outdated label in the UI language", async () => {
133 const translations = await getTranslationsSnapshot("de");
134 using _locale = {
135 [Symbol.dispose]() {
136 // GTProvider leaves its locale in a cookie; this sets it back to English.
137 initializeI18n(() => {});
138 },
139 };
140 await using screen = await render(
141 <GTProvider locale="de" translations={translations}>
142 <ReviewThreadCard
143 thread={{ ...thread, isOutdated: true }}
144 workspaceRoot="/workspace"
145 canReply
146 canResolve={false}
147 canReact={false}
148 environmentId={EnvironmentId.make("environment")}
149 reference={{ projectId: ProjectId.make("project"), repository: "owner/repo", number: 1 }}
150 completionScope={null}
151 pending={false}
152 onReply={async () => true}
153 onLoadMore={async () => null}
154 canEditComment={() => false}
155 onEditComment={async () => false}
156 onToggleResolved={() => {}}
157 onReacted={() => {}}
158 />
159 </GTProvider>,
160 );
161
162 await expect.element(screen.getByText("Veraltet", { exact: true })).toBeVisible();
163});