|
| 1 | +import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 2 | +import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts"; |
| 3 | +import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 4 | + |
| 5 | +const state = vi.hoisted(() => ({ |
| 6 | + openInApp: true, |
| 7 | + threadExists: true, |
| 8 | + repositoryKey: "github.com/pingdotgg/t3code", |
| 9 | + projectEnvironmentId: "local", |
| 10 | + navigate: vi.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock("@tanstack/react-router", () => ({ useNavigate: () => state.navigate })); |
| 14 | +vi.mock("../hooks/useSettings", () => ({ |
| 15 | + useClientSettings: (select: (settings: { pullRequestLinksOpenInApp: boolean }) => unknown) => |
| 16 | + select({ pullRequestLinksOpenInApp: state.openInApp }), |
| 17 | +})); |
| 18 | +vi.mock("../state/environments", () => ({ usePrimaryEnvironmentId: () => "local" })); |
| 19 | +vi.mock("../rpc/atomRegistry", () => ({ |
| 20 | + appAtomRegistry: { |
| 21 | + get: () => new Map([["local", { environment: { capabilities: { pullRequests: true } } }]]), |
| 22 | + }, |
| 23 | +})); |
| 24 | +vi.mock("../state/server", () => ({ environmentServerConfigsAtom: {}, serverEnvironment: {} })); |
| 25 | +vi.mock("../state/entities", () => ({ |
| 26 | + readThreadShell: (ref: { environmentId: string; threadId: string }) => |
| 27 | + state.threadExists && ref.environmentId === "local" && ref.threadId === "thread" |
| 28 | + ? { environmentId: "local", projectId: "project" } |
| 29 | + : null, |
| 30 | + readProject: (ref: { environmentId: string; projectId: string }) => |
| 31 | + ref.environmentId === state.projectEnvironmentId && ref.projectId === "project" |
| 32 | + ? project() |
| 33 | + : null, |
| 34 | + readProjects: () => [project()], |
| 35 | +})); |
| 36 | + |
| 37 | +import { selectThreadDiffPanelSelection, useDiffPanelStore } from "../diffPanelStore"; |
| 38 | +import { selectActiveRightPanel, useRightPanelStore } from "../rightPanelStore"; |
| 39 | +import { useOpenChangeRequestLink } from "./openPullRequestLink"; |
| 40 | + |
| 41 | +const ref = scopeThreadRef(EnvironmentId.make("local"), ThreadId.make("thread")); |
| 42 | +const sha = "5235a682169a69d18261c8bf4f1017dfb01b28fe"; |
| 43 | +const commitUrl = `https://github.com/pingdotgg/t3code/pull/19941/commits/${sha}`; |
| 44 | + |
| 45 | +function project() { |
| 46 | + return { |
| 47 | + id: ProjectId.make("project"), |
| 48 | + environmentId: EnvironmentId.make(state.projectEnvironmentId), |
| 49 | + repositoryIdentity: { |
| 50 | + provider: "github", |
| 51 | + canonicalKey: state.repositoryKey, |
| 52 | + displayName: state.repositoryKey.split("/").slice(1).join("/"), |
| 53 | + locator: { remoteUrl: `https://${state.repositoryKey}.git` }, |
| 54 | + }, |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +function click(modifiers = {}) { |
| 59 | + return { |
| 60 | + metaKey: false, |
| 61 | + ctrlKey: false, |
| 62 | + button: 0, |
| 63 | + defaultPrevented: false, |
| 64 | + preventDefault() { |
| 65 | + this.defaultPrevented = true; |
| 66 | + }, |
| 67 | + stopPropagation() {}, |
| 68 | + ...modifiers, |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +beforeEach(() => { |
| 73 | + state.openInApp = true; |
| 74 | + state.threadExists = true; |
| 75 | + state.repositoryKey = "github.com/pingdotgg/t3code"; |
| 76 | + state.projectEnvironmentId = "local"; |
| 77 | + state.navigate.mockReset(); |
| 78 | + useDiffPanelStore.setState({ byThreadKey: {}, branchBaseRefByThreadKey: {} }); |
| 79 | + useRightPanelStore.setState({ byThreadKey: {} }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe("commit link opening", () => { |
| 83 | + it("opens a port-bearing GitHub Enterprise URL for its normalized repository key", () => { |
| 84 | + state.repositoryKey = "github.acme.test/team/repo"; |
| 85 | + expect( |
| 86 | + useOpenChangeRequestLink(ref)( |
| 87 | + click(), |
| 88 | + `https://github.acme.test:8443/team/repo/commit/${sha}`, |
| 89 | + ), |
| 90 | + ).toBe(true); |
| 91 | + expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, ref)).toBe("diff"); |
| 92 | + expect(selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, ref)).toEqual({ |
| 93 | + kind: "commit", |
| 94 | + sha, |
| 95 | + baseRef: null, |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it.each([commitUrl, `https://github.com/pingdotgg/t3code/commit/${sha}`])( |
| 100 | + "opens the Diff panel with the commit and saved base: %s", |
| 101 | + (url) => { |
| 102 | + useDiffPanelStore.getState().selectBranchBaseRef(ref, "origin/main"); |
| 103 | + const event = click(); |
| 104 | + expect(useOpenChangeRequestLink(ref)(event, url)).toBe(true); |
| 105 | + expect(event.defaultPrevented).toBe(true); |
| 106 | + expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, ref)).toBe("diff"); |
| 107 | + expect(selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, ref)).toEqual( |
| 108 | + { |
| 109 | + kind: "commit", |
| 110 | + sha, |
| 111 | + baseRef: "origin/main", |
| 112 | + }, |
| 113 | + ); |
| 114 | + }, |
| 115 | + ); |
| 116 | + |
| 117 | + it.each([{ metaKey: true }, { ctrlKey: true }, { button: 1 }])( |
| 118 | + "leaves modified clicks to the original URL: %s", |
| 119 | + (modifiers) => { |
| 120 | + const event = click(modifiers); |
| 121 | + expect(useOpenChangeRequestLink(ref)(event, commitUrl)).toBe(false); |
| 122 | + expect(event.defaultPrevented).toBe(false); |
| 123 | + expect(useRightPanelStore.getState().byThreadKey).toEqual({}); |
| 124 | + expect(useDiffPanelStore.getState().byThreadKey).toEqual({}); |
| 125 | + }, |
| 126 | + ); |
| 127 | + |
| 128 | + it.each([ |
| 129 | + "preference off", |
| 130 | + "missing thread", |
| 131 | + "wrong repository", |
| 132 | + "wrong host", |
| 133 | + "wrong environment", |
| 134 | + ])("falls back without changing either panel for %s", (scenario) => { |
| 135 | + if (scenario === "preference off") state.openInApp = false; |
| 136 | + if (scenario === "missing thread") state.threadExists = false; |
| 137 | + if (scenario === "wrong repository") state.repositoryKey = "github.com/pingdotgg/other"; |
| 138 | + if (scenario === "wrong host") state.repositoryKey = "github.internal/pingdotgg/t3code"; |
| 139 | + if (scenario === "wrong environment") state.projectEnvironmentId = "remote"; |
| 140 | + const event = click(); |
| 141 | + expect(useOpenChangeRequestLink(ref)(event, commitUrl)).toBe(false); |
| 142 | + expect(event.defaultPrevented).toBe(false); |
| 143 | + expect(useRightPanelStore.getState().byThreadKey).toEqual({}); |
| 144 | + expect(useDiffPanelStore.getState().byThreadKey).toEqual({}); |
| 145 | + }); |
| 146 | + |
| 147 | + it("does not use a synthetic PR panel or a matching project without a real thread", () => { |
| 148 | + const event = click(); |
| 149 | + expect(useOpenChangeRequestLink(undefined, ref)(event, commitUrl)).toBe(false); |
| 150 | + expect(event.defaultPrevented).toBe(false); |
| 151 | + expect(useRightPanelStore.getState().byThreadKey).toEqual({}); |
| 152 | + expect(useDiffPanelStore.getState().byThreadKey).toEqual({}); |
| 153 | + expect(state.navigate).not.toHaveBeenCalled(); |
| 154 | + }); |
| 155 | + |
| 156 | + it("opens a matching real thread supplied by the desktop link handler", () => { |
| 157 | + expect(useOpenChangeRequestLink()(click(), commitUrl, ref)).toBe(true); |
| 158 | + expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, ref)).toBe("diff"); |
| 159 | + expect(selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, ref)).toEqual({ |
| 160 | + kind: "commit", |
| 161 | + sha, |
| 162 | + baseRef: null, |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + it("still opens ordinary PR links in the PR panel", () => { |
| 167 | + expect( |
| 168 | + useOpenChangeRequestLink(ref)(click(), "https://github.com/pingdotgg/t3code/pull/19941"), |
| 169 | + ).toBe(true); |
| 170 | + expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, ref)).toBe( |
| 171 | + "pull-request", |
| 172 | + ); |
| 173 | + expect(useDiffPanelStore.getState().byThreadKey).toEqual({}); |
| 174 | + }); |
| 175 | +}); |
0 commit comments