|
| 1 | +import { RegistryContext } from "@effect/atom-react"; |
| 2 | +import { canEditPullRequestChangeRequest } from "@t3tools/client-runtime/pull-request-editing"; |
| 3 | +import { |
| 4 | + EnvironmentId, |
| 5 | + ProjectId, |
| 6 | + SourceControlProviderKind, |
| 7 | + type PullRequestDetail, |
| 8 | +} from "@t3tools/contracts"; |
| 9 | +import { Atom, AtomRegistry } from "effect/reactivity"; |
| 10 | +import { act, useLayoutEffect } from "react"; |
| 11 | +import { createRoot } from "react-dom/client"; |
| 12 | +import { expect, it, vi } from "vite-plus/test"; |
| 13 | + |
| 14 | +const mocks = vi.hoisted(() => ({ |
| 15 | + connected: true, |
| 16 | + update: vi.fn(), |
| 17 | + permission: undefined as unknown as Atom.Writable<boolean>, |
| 18 | + detail: undefined as unknown as Atom.Writable<null>, |
| 19 | +})); |
| 20 | +vi.mock("../../state/pull-requests", () => { |
| 21 | + const command = { permissionAtom: () => mocks.permission }; |
| 22 | + return { |
| 23 | + composerPullRequests: { |
| 24 | + runAction: command, |
| 25 | + comment: command, |
| 26 | + submitReview: command, |
| 27 | + replyToThread: command, |
| 28 | + setThreadResolution: command, |
| 29 | + update: command, |
| 30 | + updateComment: command, |
| 31 | + setReaction: command, |
| 32 | + detail: () => mocks.detail, |
| 33 | + }, |
| 34 | + }; |
| 35 | +}); |
| 36 | +vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => mocks.update })); |
| 37 | +vi.mock("../../components/ui/Dialog", () => ({ alert: vi.fn() })); |
| 38 | +vi.mock("../../state/workspace", () => ({ |
| 39 | + useWorkspaceEnvironments: () => [ |
| 40 | + { |
| 41 | + environmentId: "environment", |
| 42 | + connectionState: mocks.connected ? "connected" : "offline", |
| 43 | + }, |
| 44 | + ], |
| 45 | +})); |
| 46 | + |
| 47 | +import { usePullRequestMaintainerEdits } from "./usePullRequestMaintainerEdits"; |
| 48 | + |
| 49 | +function setup() { |
| 50 | + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 51 | + mocks.connected = true; |
| 52 | + mocks.update.mockReset().mockResolvedValue({ _tag: "Success" }); |
| 53 | + mocks.permission = Atom.make(true); |
| 54 | + mocks.detail = Atom.make(null); |
| 55 | + const registry = AtomRegistry.make(); |
| 56 | + const detail: PullRequestDetail = { |
| 57 | + provider: SourceControlProviderKind.make("github"), |
| 58 | + projectId: ProjectId.make("project"), |
| 59 | + projectTitle: "Project", |
| 60 | + workspaceRoot: "/repo", |
| 61 | + repository: "owner/repo", |
| 62 | + number: 7, |
| 63 | + title: "A change", |
| 64 | + body: "", |
| 65 | + url: "https://github.com/owner/repo/pull/7", |
| 66 | + author: { login: "author", name: null, avatarUrl: null }, |
| 67 | + state: "open", |
| 68 | + isDraft: false, |
| 69 | + mergeability: "unknown", |
| 70 | + additions: 0, |
| 71 | + deletions: 0, |
| 72 | + changedFiles: 0, |
| 73 | + headBranch: "feature", |
| 74 | + baseBranch: "main", |
| 75 | + createdAt: "2026-01-01T00:00:00Z", |
| 76 | + updatedAt: "2026-01-01T00:00:00Z", |
| 77 | + mergedAt: null, |
| 78 | + closedAt: null, |
| 79 | + reviewers: [], |
| 80 | + labels: [], |
| 81 | + checks: [], |
| 82 | + mergeCapabilities: { merge: false, squash: false, rebase: false }, |
| 83 | + capabilities: { |
| 84 | + diff: true, |
| 85 | + comment: true, |
| 86 | + search: true, |
| 87 | + actions: [], |
| 88 | + mergeMethods: [], |
| 89 | + review: { inlineComment: false, reply: false, resolve: false, verdicts: [] }, |
| 90 | + reviewers: { request: false, listCandidates: false }, |
| 91 | + edit: { changeRequest: true, comment: true, maintainerEdits: true }, |
| 92 | + }, |
| 93 | + viewerPermissions: { |
| 94 | + actions: [], |
| 95 | + comment: true, |
| 96 | + resolve: false, |
| 97 | + verdicts: [], |
| 98 | + requestReviewers: false, |
| 99 | + maintainerEdits: true, |
| 100 | + }, |
| 101 | + maintainerCanModify: false, |
| 102 | + }; |
| 103 | + const root = createRoot(document.createElement("div")); |
| 104 | + let current!: ReturnType<typeof usePullRequestMaintainerEdits>; |
| 105 | + function Probe() { |
| 106 | + const result = usePullRequestMaintainerEdits( |
| 107 | + { |
| 108 | + environmentId: "environment", |
| 109 | + projectId: "project", |
| 110 | + repository: "owner/repo", |
| 111 | + number: 7, |
| 112 | + }, |
| 113 | + detail, |
| 114 | + ); |
| 115 | + useLayoutEffect(() => { |
| 116 | + current = result; |
| 117 | + }); |
| 118 | + return null; |
| 119 | + } |
| 120 | + const render = () => |
| 121 | + act(() => |
| 122 | + root.render( |
| 123 | + <RegistryContext.Provider value={registry}> |
| 124 | + <Probe /> |
| 125 | + </RegistryContext.Provider>, |
| 126 | + ), |
| 127 | + ); |
| 128 | + render(); |
| 129 | + return { |
| 130 | + detail, |
| 131 | + render, |
| 132 | + registry, |
| 133 | + get current() { |
| 134 | + return current; |
| 135 | + }, |
| 136 | + [Symbol.dispose]() { |
| 137 | + act(() => root.unmount()); |
| 138 | + registry.dispose(); |
| 139 | + }, |
| 140 | + }; |
| 141 | +} |
| 142 | + |
| 143 | +it("saves an explicit maintainer grant without a viewer login, while honoring local access", async () => { |
| 144 | + using screen = setup(); |
| 145 | + expect(canEditPullRequestChangeRequest(screen.detail)).toBe(false); |
| 146 | + expect(screen.current.visible).toBe(true); |
| 147 | + expect(screen.current.disabled).toBe(false); |
| 148 | + await act(async () => screen.current.toggle()); |
| 149 | + expect(screen.current.value).toBe(true); |
| 150 | + expect(mocks.update).toHaveBeenCalledExactlyOnceWith({ |
| 151 | + environmentId: EnvironmentId.make("environment"), |
| 152 | + input: { |
| 153 | + projectId: ProjectId.make("project"), |
| 154 | + repository: "owner/repo", |
| 155 | + number: 7, |
| 156 | + maintainerCanModify: true, |
| 157 | + }, |
| 158 | + }); |
| 159 | + act(() => screen.registry.set(mocks.permission, false)); |
| 160 | + expect(screen.current.disabled).toBe(true); |
| 161 | + await act(async () => screen.current.toggle()); |
| 162 | + act(() => screen.registry.set(mocks.permission, true)); |
| 163 | + mocks.connected = false; |
| 164 | + screen.render(); |
| 165 | + expect(screen.current.disabled).toBe(true); |
| 166 | + await act(async () => screen.current.toggle()); |
| 167 | + expect(mocks.update).toHaveBeenCalledTimes(1); |
| 168 | +}); |
0 commit comments