Open History to see the last commit
1import { SourceControlProviderKind, type VcsStatusResult } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import { sourceControlClients } from "../sourceControlClients.ts";
5import {
6 buildMenuItems,
7 getGitActionDisabledReason,
8 resolveDefaultBranchActionDialogCopy,
9 resolveQuickAction,
10} from "./gitActions.ts";
11
12function status(overrides: Partial<VcsStatusResult> = {}): VcsStatusResult {
13 return {
14 isRepo: true,
15 hasPrimaryRemote: true,
16 isDefaultRef: false,
17 refName: "feature/test",
18 hasWorkingTreeChanges: false,
19 workingTree: { files: [], insertions: 0, deletions: 0 },
20 hasUpstream: true,
21 aheadCount: 0,
22 behindCount: 0,
23 pr: null,
24 ...overrides,
25 };
26}
27
28const gitLab = {
29 sourceControlProvider: {
30 kind: SourceControlProviderKind.make("gitlab"),
31 name: "GitLab",
32 baseUrl: "https://gitlab.com",
33 },
34};
35
36const openPr = {
37 number: 7,
38 title: "Feature",
39 url: "https://example.com/pr/7",
40 baseRef: "main",
41 headRef: "feature/test",
42 state: "open" as const,
43};
44
45describe("git action copy", () => {
46 it("says PR for a GitHub ref", () => {
47 expect(resolveQuickAction(status({ aheadCount: 1 }), false).label).toBe("Push & create PR");
48 expect(resolveQuickAction(status({ pr: openPr }), false).label).toBe("View PR");
49 const [, , prItem] = buildMenuItems(status(), false);
50 expect(prItem?.label).toBe("Create PR");
51 expect(
52 getGitActionDisabledReason({
53 item: prItem!,
54 gitStatus: status(),
55 isBusy: false,
56 hasOriginRemote: true,
57 }),
58 ).toBe("No local commits to include in a PR.");
59 });
60
61 it("says MR for a GitLab ref", () => {
62 expect(resolveQuickAction(status({ ...gitLab, aheadCount: 1 }), false).label).toBe(
63 "Push & create MR",
64 );
65 expect(
66 resolveQuickAction(status({ ...gitLab, hasWorkingTreeChanges: true }), false).label,
67 ).toBe("Commit, push & MR");
68 expect(resolveQuickAction(status({ ...gitLab, pr: openPr }), false).label).toBe("View MR");
69 const [, , prItem] = buildMenuItems(status(gitLab), false);
70 expect(prItem?.label).toBe("Create MR");
71 expect(
72 getGitActionDisabledReason({
73 item: prItem!,
74 gitStatus: status(gitLab),
75 isBusy: false,
76 hasOriginRemote: true,
77 }),
78 ).toBe("No local commits to include in an MR.");
79 });
80
81 it("offers creating a PR after the feature branch has been fully pushed", () => {
82 const pushed = status({ aheadCount: 0, behindCount: 0, aheadOfDefaultCount: 1 });
83 expect(resolveQuickAction(pushed, false)).toMatchObject({
84 disabled: false,
85 kind: "run_action",
86 action: "create_pr",
87 });
88 expect(resolveQuickAction({ ...pushed, isDefaultRef: true }, false, true).disabled).toBe(true);
89 });
90
91 it.each([
92 { tracking: "with an upstream", hasUpstream: true },
93 { tracking: "without an upstream", hasUpstream: false },
94 ])("pushes existing commits from a clean default branch $tracking", ({ hasUpstream }) => {
95 const cleanDefaultBranch = status({
96 refName: "main",
97 isDefaultRef: true,
98 hasUpstream,
99 aheadCount: 1,
100 });
101 expect(resolveQuickAction(cleanDefaultBranch, false, true)).toMatchObject({
102 disabled: false,
103 kind: "run_action",
104 action: "push",
105 });
106 });
107
108 it("names the host's change request in the default-branch dialog", () => {
109 const input = { action: "commit_push_pr", branchName: "main", includesCommit: true } as const;
110 expect(
111 resolveDefaultBranchActionDialogCopy({
112 ...input,
113 terminology: sourceControlClients.get("github").changeRequest,
114 }).title,
115 ).toBe("Commit, push & create PR from default branch?");
116 expect(
117 resolveDefaultBranchActionDialogCopy({
118 ...input,
119 terminology: sourceControlClients.get("gitlab").changeRequest,
120 }).continueLabel,
121 ).toBe("Commit, push & create MR");
122 });
123});