The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | import { |
| 4 | allowedMergeMethods, |
| 5 | canPerformPullRequestAction, |
| 6 | mergeUnavailableReason, |
| 7 | pullRequestActionLabel, |
| 8 | resolveMergeMethod, |
| 9 | } from "./pullRequestActions"; |
| 10 | |
| 11 | const GITHUB = sourceControlClients.get("github").changeRequest; |
| 12 | const GITLAB = sourceControlClients.get("gitlab").changeRequest; |
| 13 | |
| 14 | import type { PullRequestAction } from "@t3tools/contracts"; |
| 15 | |
| 16 | const ALL_PR_ACTIONS: readonly PullRequestAction[] = [ |
| 17 | "merge", |
| 18 | "ready", |
| 19 | "draft", |
| 20 | "update-branch", |
| 21 | "enable-auto-merge", |
| 22 | "disable-auto-merge", |
| 23 | "approve-workflows", |
| 24 | "close", |
| 25 | "reopen", |
| 26 | "revert", |
| 27 | "delete-branch", |
| 28 | ]; |
| 29 | |
| 30 | type Detail = Parameters<typeof allowedMergeMethods>[0]; |
| 31 | |
| 32 | function detail(overrides: Partial<Detail> = {}): Detail { |
| 33 | return { |
| 34 | state: "open", |
| 35 | isDraft: false, |
| 36 | mergeability: "mergeable", |
| 37 | autoMergeEnabled: false, |
| 38 | capabilities: { |
| 39 | actions: ALL_PR_ACTIONS, |
| 40 | mergeMethods: ["merge", "squash", "rebase"], |
| 41 | updateMethods: ["merge", "rebase"], |
| 42 | }, |
| 43 | viewerPermissions: { actions: ALL_PR_ACTIONS, updateMethods: ["merge"] }, |
| 44 | mergeCapabilities: { merge: true, squash: true, rebase: false }, |
| 45 | ...overrides, |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | describe("mobile pull request actions", () => { |
| 50 | it("requires both provider support and account permission", () => { |
| 51 | expect( |
| 52 | canPerformPullRequestAction(detail({ viewerPermissions: { actions: [] } }), "merge"), |
| 53 | ).toBe(false); |
| 54 | expect( |
| 55 | canPerformPullRequestAction( |
| 56 | detail({ capabilities: { actions: [], mergeMethods: ["merge"] } }), |
| 57 | "merge", |
| 58 | ), |
| 59 | ).toBe(false); |
| 60 | expect(mergeUnavailableReason(detail(), false, GITHUB)).toContain("read-only"); |
| 61 | }); |
| 62 | |
| 63 | it("blocks a conflicting PR's immediate merge and explains why", () => { |
| 64 | const pr = detail({ mergeability: "conflicting" }); |
| 65 | expect(canPerformPullRequestAction(pr, "merge")).toBe(false); |
| 66 | expect(mergeUnavailableReason(pr, true, GITHUB)).toContain("conflicts"); |
| 67 | }); |
| 68 | |
| 69 | it("picks the chosen, project default, then last used merge method the repository allows", () => { |
| 70 | const pr = detail(); |
| 71 | expect(resolveMergeMethod(pr, null, "merge", "squash")).toBe("merge"); |
| 72 | expect(resolveMergeMethod(pr, "squash", "merge", "merge")).toBe("squash"); |
| 73 | expect(resolveMergeMethod(pr, null, "rebase", "merge")).toBe("merge"); |
| 74 | expect(resolveMergeMethod(pr, null, null, undefined)).toBe("squash"); |
| 75 | expect( |
| 76 | resolveMergeMethod( |
| 77 | detail({ mergeCapabilities: { merge: true, squash: false, rebase: false } }), |
| 78 | null, |
| 79 | null, |
| 80 | "rebase", |
| 81 | ), |
| 82 | ).toBe("merge"); |
| 83 | }); |
| 84 | |
| 85 | it("names the change request with its host's term", () => { |
| 86 | expect(pullRequestActionLabel("close", GITHUB)).toBe("Close pull request"); |
| 87 | expect(pullRequestActionLabel("close", GITLAB)).toBe("Close merge request"); |
| 88 | expect(pullRequestActionLabel("revert", GITLAB)).toBe("Create revert merge request"); |
| 89 | expect(mergeUnavailableReason(detail({ state: "merged" }), true, GITLAB)).toBe( |
| 90 | "This merge request has been merged.", |
| 91 | ); |
| 92 | }); |
| 93 | }); |