Open History to see the last commit
1import { EnvironmentId, type IssueDetail } from "@t3tools/contracts";
2import * as Cause from "effect/Cause";
3import * as AsyncResult from "effect/reactivity/AsyncResult";
4import { describe, expect, it, vi } from "vite-plus/test";
5import { page, userEvent } from "vite-plus/test/browser";
6
7import { act, render } from "../../../test/render";
8
9const url = "https://github.com/owner/repo/issues/7";
10const detail = vi.hoisted(() => ({
11 tracker: "github",
12 host: "github.com",
13 scope: "owner/repo",
14 key: "7",
15 displayKey: "#7",
16 title: "Something is broken",
17 url: "https://github.com/owner/repo/issues/7",
18 author: null,
19 state: { category: "open", name: "Open" },
20 createdAt: "2026-01-01T00:00:00.000Z",
21 updatedAt: "2026-01-01T00:00:00.000Z",
22 closedAt: null,
23 assignees: [],
24 labels: [],
25 commentCount: 0,
26 projectId: "project-1",
27 projectTitle: "Project",
28 writable: true,
29 capabilities: {
30 create: false,
31 edit: true,
32 comment: false,
33 labels: true,
34 assignees: true,
35 reactions: false,
36 templates: false,
37 linkedPullRequests: false,
38 events: false,
39 search: false,
40 },
41 viewerPermissions: {
42 setState: false,
43 comment: false,
44 edit: true,
45 labels: true,
46 assignees: true,
47 },
48 states: [],
49 viewer: null,
50 body: "",
51 comments: [],
52 commentsCursor: null,
53 events: [],
54 linkedPullRequests: [],
55})) as unknown as IssueDetail;
56
57const state = vi.hoisted(() => ({
58 openLink: vi.fn(async () => {}),
59 comment: vi.fn(async (_value: unknown): Promise<AsyncResult.AsyncResult<void, string>> =>
60 AsyncResult.success(undefined),
61 ),
62}));
63
64vi.mock(import("~/browser/useOpenLink"), () => ({ useOpenLink: () => state.openLink }));
65vi.mock(import("~/state/use-atom-command"), async (original) => {
66 const actual = await original();
67 const { issueEnvironment } = await import("~/state/issues");
68 return {
69 useAtomCommand: ((command, options) =>
70 command.label === issueEnvironment.comment.label
71 ? state.comment
72 : actual.useAtomCommand(command, options)) as typeof actual.useAtomCommand,
73 };
74});
75vi.mock(import("~/state/session"), async (original) => ({
76 ...(await original()),
77 useEnvironmentScope: () => true,
78}));
79vi.mock(import("~/state/issues"), async (original) => {
80 const { Atom } = await import("effect/reactivity");
81 const actual = await original();
82 const detailAtom = Atom.make(AsyncResult.success(detail));
83 const serviceDeskDetail = {
84 ...detail,
85 tracker: "jira",
86 host: "example.atlassian.net",
87 scope: "SUP",
88 key: "SUP-12",
89 displayKey: "SUP-12",
90 url: "https://example.atlassian.net/browse/SUP-12",
91 capabilities: { ...detail.capabilities, comment: true },
92 viewerPermissions: { ...detail.viewerPermissions, comment: true },
93 commentAudiences: true,
94 };
95 const serviceDeskAtom = Atom.make(AsyncResult.success(serviceDeskDetail));
96 const otherIssueAtom = Atom.make(
97 AsyncResult.success({
98 ...serviceDeskDetail,
99 key: "SUP-13",
100 displayKey: "SUP-13",
101 url: "https://example.atlassian.net/browse/SUP-13",
102 }),
103 );
104 const candidatesAtom = Atom.make(
105 AsyncResult.success({
106 labels: [
107 { name: "bug", color: "d73a4a", description: null },
108 { name: "docs", color: "0075ca", description: null },
109 ],
110 assignees: [
111 { id: "1", login: "alice", name: null, avatarUrl: null },
112 { id: "2", login: "bob", name: null, avatarUrl: null },
113 ],
114 truncated: false,
115 }),
116 );
117 const allowed = Atom.make(true);
118 return {
119 ...actual,
120 issueEnvironment: {
121 ...actual.issueEnvironment,
122 detail: (({ input }: { input: { key: string } }) =>
123 input.key === "SUP-12"
124 ? serviceDeskAtom
125 : input.key === "SUP-13"
126 ? otherIssueAtom
127 : detailAtom) as never,
128 candidates: (() => candidatesAtom) as never,
129 comment: { ...actual.issueEnvironment.comment, permissionAtom: () => allowed },
130 },
131 };
132});
133
134import { RightPanelSheet } from "../RightPanelSheet";
135import { IssueDetailPanel } from "./IssueDetailPanel";
136
137const reference = {
138 projectId: "project-1" as never,
139 tracker: "github" as never,
140 scope: "owner/repo",
141 key: "7",
142};
143
144function setComments(comments: IssueDetail["comments"]) {
145 state.openLink.mockClear();
146 (detail as { comments: unknown }).comments = comments;
147}
148
149describe("IssueDetailPanel", () => {
150 it("opens the issue number through the link-target setting, not a popup", async () => {
151 setComments([]);
152 const threadRef = { environmentId: "env-1", threadId: "thread-1" } as never;
153 await using screen = await render(
154 <IssueDetailPanel
155 environmentId={"env-1" as never}
156 reference={reference}
157 threadRef={threadRef}
158 />,
159 );
160 const number = screen.getByRole("link", { name: "Open #7 on GitHub" });
161 const click = new MouseEvent("click", { bubbles: true, cancelable: true });
162 (await number.element()).dispatchEvent(click);
163
164 expect(click.defaultPrevented).toBe(true);
165 expect(state.openLink).toHaveBeenCalledOnce();
166 expect(state.openLink).toHaveBeenCalledWith(url, { event: expect.anything() });
167 });
168
169 it("opens a comment's timestamp through the link-target setting, not a popup", async () => {
170 const commentUrl = `${url}#issuecomment-1`;
171 setComments([
172 {
173 id: "1",
174 author: null,
175 body: "Same here",
176 createdAt: "2026-01-02T00:00:00.000Z",
177 url: commentUrl,
178 },
179 ]);
180 await using screen = await render(
181 <IssueDetailPanel
182 environmentId={"env-1" as never}
183 reference={reference}
184 threadRef={{ environmentId: "env-1", threadId: "thread-1" } as never}
185 />,
186 );
187 const link = await vi.waitFor(() => {
188 const found = screen.container.querySelector(`a[href="${commentUrl}"]`);
189 expect(found).not.toBeNull();
190 return found!;
191 });
192 const click = new MouseEvent("click", { bubbles: true, cancelable: true });
193 link.dispatchEvent(click);
194
195 expect(click.defaultPrevented).toBe(true);
196 expect(state.openLink).toHaveBeenCalledOnce();
197 expect(state.openLink).toHaveBeenCalledWith(commentUrl, { event: expect.anything() });
198 });
199
200 it("Escape cancels the title edit without closing the right panel sheet", async () => {
201 setComments([]);
202 const onCloseSheet = vi.fn();
203 await using _screen = await render(
204 <RightPanelSheet animationDurationMs={0} open onClose={onCloseSheet}>
205 <IssueDetailPanel environmentId={"env-1" as never} reference={reference} />
206 </RightPanelSheet>,
207 );
208 const editTitleButton = page.getByRole("button", { name: "Edit title" });
209 await act(() => (editTitleButton.element() as HTMLElement).click());
210 const input = page.getByRole("textbox", { name: "Issue title" });
211 await expect.element(input).toHaveFocus();
212
213 await userEvent.keyboard("{Escape}");
214 await expect.element(input).not.toBeInTheDocument();
215 await expect.element(editTitleButton).toHaveFocus();
216 expect(onCloseSheet).not.toHaveBeenCalled();
217
218 await userEvent.keyboard("{Escape}");
219 await vi.waitFor(() => expect(onCloseSheet).toHaveBeenCalledTimes(1));
220 });
221
222 it("returns focus to Edit description when cancelling the description edit", async () => {
223 setComments([]);
224 await using screen = await render(
225 <IssueDetailPanel environmentId={"env-1" as never} reference={reference} />,
226 );
227 const editDescriptionButton = screen.getByRole("button", { name: "Edit description" });
228 await editDescriptionButton.click();
229 const editor = screen.getByRole("textbox", { name: "Description" });
230 await expect.element(editor).toHaveFocus();
231
232 await userEvent.keyboard("{Tab}");
233 await expect.element(screen.getByRole("button", { name: "Cancel" })).toHaveFocus();
234 await userEvent.keyboard("{Enter}");
235 await expect.element(editor).not.toBeInTheDocument();
236 await expect.element(editDescriptionButton).toHaveFocus();
237 });
238
239 it("finds a label when the search has surrounding spaces", async () => {
240 setComments([]);
241 await using screen = await render(
242 <IssueDetailPanel environmentId={"env-1" as never} reference={reference} />,
243 );
244 await screen.getByRole("combobox", { name: "Change labels" }).click();
245 await userEvent.fill(page.getByRole("combobox", { name: "Search labels" }), " bug ");
246
247 await expect.element(page.getByRole("option", { name: "bug" })).toBeInTheDocument();
248 await expect.element(page.getByRole("option", { name: "docs" })).not.toBeInTheDocument();
249 });
250
251 it("finds an assignee when the search has surrounding spaces", async () => {
252 setComments([]);
253 await using screen = await render(
254 <IssueDetailPanel environmentId={"env-1" as never} reference={reference} />,
255 );
256 await screen.getByRole("combobox", { name: "Change assignees" }).click();
257 await userEvent.fill(page.getByRole("combobox", { name: "Search people" }), " alice ");
258
259 await expect.element(page.getByRole("option", { name: "alice" })).toBeInTheDocument();
260 await expect.element(page.getByRole("option", { name: "bob" })).not.toBeInTheDocument();
261 });
262
263 it("goes back to an internal note after a reply to the customer is posted", async () => {
264 state.comment.mockClear();
265 await using _screen = await render(
266 <IssueDetailPanel
267 environmentId={"env-1" as never}
268 reference={{ ...reference, tracker: "jira" as never, scope: "SUP", key: "SUP-12" }}
269 />,
270 );
271 const field = page.getByRole("textbox", { name: "Add a comment" });
272 const send = page.getByRole("button", { name: "Comment", exact: true });
273
274 await page.getByRole("button", { name: "Internal note" }).click();
275 await page.getByRole("menuitemradio", { name: "Reply to customer" }).click();
276 await userEvent.fill(field, "We have shipped a fix.");
277 await send.click();
278 await expect.element(page.getByRole("button", { name: "Internal note" })).toBeInTheDocument();
279 await userEvent.fill(field, "Customer confirmed on a call.");
280 await send.click();
281 await vi.waitFor(() =>
282 expect(
283 state.comment.mock.calls.map(
284 ([value]) => (value as { input: { audience?: string } }).input.audience,
285 ),
286 ).toEqual(["public", "internal"]),
287 );
288 });
289
290 it("keeps an expanded comment expanded after switching to another tab and back", async () => {
291 setComments([
292 {
293 id: "long",
294 author: null,
295 body: Array.from({ length: 40 }, (_, index) => `Log line ${index + 1}`).join("\n\n"),
296 createdAt: "2026-01-02T00:00:00.000Z",
297 url: `${url}#issuecomment-long`,
298 },
299 ]);
300 const threadRef = { environmentId: "env-1", threadId: "thread-1" } as never;
301 const issueTab = (
302 <IssueDetailPanel
303 key="issue-tab"
304 environmentId={"env-1" as never}
305 reference={reference}
306 threadRef={threadRef}
307 />
308 );
309 await using screen = await render(issueTab);
310 await screen.getByRole("button", { name: "Show full comment" }).click();
311 await expect.element(screen.getByRole("button", { name: "Show less" })).toBeInTheDocument();
312
313 await screen.rerender(<div>Another tab</div>);
314 await screen.rerender(issueTab);
315
316 await expect
317 .element(screen.getByRole("button", { name: "Show less" }))
318 .toHaveAttribute("aria-expanded", "true");
319 });
320
321 it("keeps an unsent draft and its audience across issues until it posts", async () => {
322 state.comment.mockClear();
323 state.comment.mockResolvedValueOnce(AsyncResult.failure(Cause.fail("Try again")));
324 const panel = (key: string) => (
325 <IssueDetailPanel
326 key={key}
327 environmentId={EnvironmentId.make("draft-env")}
328 reference={{ ...reference, tracker: "jira" as never, scope: "SUP", key }}
329 />
330 );
331 const body = "The billing portal is available again.";
332 await using screen = await render(panel("SUP-12"));
333 const field = screen.getByRole("textbox", { name: "Add a comment" });
334 const send = screen.getByRole("button", { name: "Comment", exact: true });
335 const internal = screen.getByRole("button", { name: "Internal note" });
336 const reply = screen.getByRole("button", { name: "Reply to customer" });
337
338 await userEvent.fill(field, body);
339 await internal.click();
340 await page.getByRole("menuitemradio", { name: "Reply to customer" }).click();
341
342 await screen.rerender(panel("SUP-13"));
343 await expect.element(field).toHaveValue("");
344 await expect.element(internal).toBeInTheDocument();
345
346 await screen.rerender(panel("SUP-12"));
347 await expect.element(field).toHaveValue(body);
348 await expect.element(reply).toBeInTheDocument();
349 await send.click();
350 await expect.element(send).toBeEnabled();
351 await expect.element(field).toHaveValue(body);
352
353 await send.click();
354 await expect.element(field).toHaveValue("");
355 await expect.element(field).toHaveFocus();
356 await expect.element(internal).toBeInTheDocument();
357 expect(state.comment).toHaveBeenLastCalledWith(
358 expect.objectContaining({ input: expect.objectContaining({ body, audience: "public" }) }),
359 );
360 });
361
362 it("returns focus to Link to thread when Escape closes the thread picker", async () => {
363 setComments([]);
364 await using screen = await render(
365 <IssueDetailPanel environmentId={"env-1" as never} reference={reference} />,
366 );
367 const linkButton = screen.getByRole("button", { name: "Link to thread" });
368 await act(() => (linkButton.element() as HTMLElement).focus());
369
370 await userEvent.keyboard("{Enter}");
371 const search = page.getByPlaceholder("Search threads or projects...");
372 await expect.element(search).toHaveFocus();
373
374 await userEvent.keyboard("{Escape}");
375 await expect.element(search).not.toBeInTheDocument();
376 await expect.element(linkButton).toHaveFocus();
377 });
378});