Open History to see the last commit
1import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
2import { EnvironmentId, ProjectId } from "@t3tools/contracts";
3import { expect, it, vi } from "vite-plus/test";
4import { page, userEvent } from "vite-plus/test/browser";
5
6import { DraftId } from "../composerDraftStore";
7import { render } from "../../test/render";
8import { readWorktreeBaseBranch, setWorktreeBaseBranch } from "../worktreeRepositoryChoiceStore";
9import { WorktreeRepositoriesControl } from "./WorktreeRepositoriesControl";
10
11const refresh = vi.fn();
12const nineChoices = Array.from({ length: 9 }, (_, index) => ({
13 repositoryPath: `/work/repo-${index + 1}`,
14 name: `repo-${index + 1}`,
15}));
16let choices: ReadonlyArray<{ repositoryPath: string; name: string }> | null = nineChoices;
17
18vi.mock("../hooks/useWorktreeRepositoryChoices", () => ({
19 useWorktreeRepositoryChoices: () => ({ choices, tracksGitRepositories: true, refresh }),
20}));
21
22function withChoices(next: typeof choices) {
23 choices = next;
24 refresh.mockClear();
25 return { [Symbol.dispose]: () => (choices = nineChoices) };
26}
27
28const project = {
29 id: ProjectId.make("project"),
30 environmentId: EnvironmentId.make("environment"),
31 title: "work",
32 workspaceRoot: "/work",
33 defaultModelSelection: null,
34 createdAt: "2026-12-01T00:00:00.000Z",
35 updatedAt: "2026-12-01T00:00:00.000Z",
36 scripts: [],
37} satisfies EnvironmentProject;
38
39it("focuses the repository filter when the menu opens from the keyboard", async () => {
40 await using screen = await render(
41 <WorktreeRepositoriesControl
42 project={project}
43 draftTarget={DraftId.make("draft")}
44 offersBaseBranch
45 />,
46 );
47
48 screen
49 .getByRole("button", { name: /^Repositories in the new worktree/ })
50 .element()
51 .focus();
52 expect(refresh).not.toHaveBeenCalled();
53 await userEvent.keyboard("{Enter}");
54 const filter = page.getByPlaceholder("Filter repositories");
55 await expect.element(filter).toHaveFocus();
56 // A repository may have had its first commit since the folders were read.
57 expect(refresh).toHaveBeenCalledOnce();
58 await userEvent.keyboard("repo-9");
59 await expect.element(filter).toHaveValue("repo-9");
60 await expect.element(page.getByRole("menuitemcheckbox")).toHaveTextContent("repo-9");
61});
62
63it("keeps Unicode whitespace in the base branch while trimming ASCII padding", async () => {
64 setWorktreeBaseBranch(project, "");
65 await using screen = await render(
66 <WorktreeRepositoriesControl
67 project={project}
68 draftTarget={DraftId.make("draft")}
69 offersBaseBranch
70 />,
71 );
72
73 await userEvent.click(screen.getByRole("button", { name: /^Repositories in the new worktree/ }));
74 const baseBranch = page.getByRole("textbox", { name: "Base branch" });
75
76 await userEvent.fill(baseBranch, " release\u00a0 ");
77 await expect.element(baseBranch).toHaveValue("release\u00a0");
78 expect(readWorktreeBaseBranch(project)).toBe("release\u00a0");
79});
80
81it("re-reads the folders while no repository has a commit", async () => {
82 using _choices = withChoices(null);
83 await using screen = await render(
84 <WorktreeRepositoriesControl
85 project={project}
86 draftTarget={DraftId.make("draft")}
87 offersBaseBranch
88 />,
89 );
90 expect(refresh).not.toHaveBeenCalled();
91
92 // A read that started before the first commit can answer after the picker was offered.
93 choices = [];
94 await screen.rerender(
95 <WorktreeRepositoriesControl
96 project={{ ...project }}
97 draftTarget={DraftId.make("draft")}
98 offersBaseBranch
99 />,
100 );
101 expect(refresh).toHaveBeenCalledOnce();
102 expect(screen.container.textContent).toBe("");
103
104 window.dispatchEvent(new FocusEvent("focus"));
105 expect(refresh).toHaveBeenCalledTimes(2);
106});