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

PullRequestDetailPanel.permissions.browser.test.tsx

198 lines6.2 KB

1import {
2 AuthOrchestrationOperateScope,
3 AuthSourceControlWriteScope,
4 EnvironmentId,
5 ProjectId,
6 SourceControlProviderKind,
7 type PullRequestDetailView,
8} from "@t3tools/contracts";
9import { expect, it, vi } from "vite-plus/test";
10import { userEvent } from "vite-plus/test/browser";
11import { DraftId, useComposerDraftStore } from "~/composerDraftStore";
12import { act, fakeTimers, render } from "../../../test/render";
13​
14const { permissions, reads } = vi.hoisted(() => ({
15 permissions: { checkout: true, operate: true },
16 reads: { diff: [] as unknown[] },
17}));
18​
19vi.mock(import("~/state/session"), async (original) => ({
20 ...(await original()),
21 useEnvironmentScope: (_environmentId: unknown, scope: unknown) =>
22 scope === AuthOrchestrationOperateScope ? permissions.operate : permissions.checkout,
23 readEnvironmentScope: (_environmentId: unknown, scope: unknown) =>
24 scope === AuthOrchestrationOperateScope ? permissions.operate : permissions.checkout,
25}));
26vi.mock(import("~/state/sourceControlActions"), async (original) => ({
27 ...(await original()),
28 usePreparePullRequestThreadAction: (() => ({
29 isAllowed: permissions.checkout,
30 run: vi.fn(),
31 error: null,
32 })) as never,
33}));
34vi.mock(import("./usePullRequestDetail"), async (original) => ({
35 ...(await original()),
36 usePullRequestDetail: (() => ({
37 canWriteSourceControl: permissions.checkout,
38 detailQuery: { isPending: false, refresh: vi.fn() },
39 activityQuery: { isPending: false, refresh: vi.fn(), error: null },
40 activity: null,
41 sharedSummary: detail,
42 coreDetail: detail,
43 detail,
44 })) as never,
45}));
46vi.mock(import("~/state/usePullRequestStack"), async (original) => ({
47 ...(await original()),
48 usePullRequestStack: (() => ({
49 data: null,
50 isFresh: true,
51 isPending: false,
52 error: null,
53 refresh: vi.fn(),
54 })) as never,
55}));
56vi.mock(import("~/state/pullRequests"), async (original) => {
57 const actual = await original();
58 return {
59 ...actual,
60 pullRequestEnvironment: {
61 ...actual.pullRequestEnvironment,
62 diff: ((target: unknown) => {
63 reads.diff.push(target);
64 return actual.pullRequestEnvironment.diff(target as never);
65 }) as never,
66 },
67 };
68});
69vi.mock(import("~/state/use-atom-command"), async (original) => ({
70 ...(await original()),
71 useAtomCommand: (() => vi.fn()) as never,
72}));
73​
74import { PullRequestDetailPanel } from "./PullRequestDetailPanel";
75​
76const detail: PullRequestDetailView = {
77 provider: SourceControlProviderKind.make("github"),
78 projectId: ProjectId.make("project"),
79 projectTitle: "Project",
80 workspaceRoot: "/workspace",
81 repository: "owner/repo",
82 number: 1,
83 title: "Test pull request",
84 body: "Original description",
85 url: "https://github.com/owner/repo/pull/1",
86 author: { login: "author", name: null, avatarUrl: null },
87 viewer: "author",
88 state: "open",
89 isDraft: false,
90 mergeability: "conflicting",
91 additions: 1,
92 deletions: 0,
93 changedFiles: 1,
94 headBranch: "feature",
95 baseBranch: "main",
96 createdAt: "2026-09-01T00:00:00Z",
97 updatedAt: "2026-09-01T00:00:00Z",
98 mergedAt: null,
99 closedAt: null,
100 reviewers: [],
101 labels: [],
102 checks: [{ name: "Unit tests", status: "success", description: null, url: null }],
103 comments: [],
104 commentCount: 0,
105 commentsTruncated: false,
106 reviewThreads: [],
107 commits: [],
108 mergeCapabilities: { merge: false, squash: false, rebase: false },
109 capabilities: {
110 diff: true,
111 comment: false,
112 search: true,
113 actions: [],
114 mergeMethods: [],
115 review: { inlineComment: false, reply: false, resolve: false, verdicts: [] },
116 reviewers: { request: false, listCandidates: false },
117 edit: { changeRequest: true, comment: false },
118 },
119 viewerPermissions: {
120 actions: [],
121 comment: false,
122 resolve: false,
123 verdicts: [],
124 requestReviewers: false,
125 },
126};
127​
128const environmentId = EnvironmentId.make("environment");
129const draftId = DraftId.make("conflicts-draft");
130const panel = (attach = false) => (
131 <PullRequestDetailPanel
132 environmentId={environmentId}
133 reference={detail}
134 context="page"
135 shortcutsEnabled={false}
136 getShortcutContext={() => ({
137 terminalFocus: false,
138 terminalOpen: false,
139 previewFocus: false,
140 previewOpen: false,
141 isWeb: true,
142 isDesktop: false,
143 })}
144 {...(attach ? { composerDraftTarget: draftId } : {})}
145 />
146);
147​
148function setup() {
149 permissions.checkout = true;
150 permissions.operate = true;
151 reads.diff = [];
152 useComposerDraftStore.setState({ draftsByThreadKey: {} });
154​
155it.each([AuthSourceControlWriteScope, AuthOrchestrationOperateScope])(
156 "disables conflict checkout when the connection loses %s",
157 async (scope) => {
158 setup();
159 await using screen = await render(panel());
160 const resolve = screen.getByRole("button", { name: "Resolve conflicts" });
161 await expect.element(resolve).toBeEnabled();
162 if (scope === AuthSourceControlWriteScope) permissions.checkout = false;
163 else permissions.operate = false;
164 await screen.rerender(panel());
165 await expect.element(resolve).toBeDisabled();
166 },
167);
168​
169it("can still add conflict resolution to an existing composer without checkout scopes", async () => {
170 setup();
171 permissions.checkout = false;
172 permissions.operate = false;
173 await using screen = await render(panel(true));
174 const resolve = screen.getByRole("button", { name: "Resolve conflicts" });
175 await expect.element(resolve).toBeEnabled();
176 await act(() => resolve.click());
177 const draft = useComposerDraftStore.getState().getComposerDraft(draftId);
178 expect(draft?.prompt).toContain("conflicts");
179 expect(draft?.prompt).toContain(detail.url);
180});
181​
182it("reads the first diff page once the pointer rests on the Code tab", async () => {
183 setup();
184 await using screen = await render(panel());
185 using _timers = fakeTimers({ now: 0 });
186 const code = screen.getByRole("button", { name: "Code", exact: true });
187 await expect.element(code).toBeVisible();
188​
189 await userEvent.hover(code);
190 await act(() => vi.advanceTimersByTime(149));
191 expect(reads.diff).toEqual([]);
192 await act(() => vi.advanceTimersByTime(1));
193 // The page the Code tab reads first, keyed as the tab keys it, so a click paints from it.
194 expect(reads.diff).toContainEqual({
195 environmentId,
196 input: { projectId: detail.projectId, repository: detail.repository, number: detail.number },
197 });
198});