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