Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

issueLinks.test.ts

169 lines6.2 KB

1import {
2 EnvironmentId,
3 IssueTrackerAccountId,
4 IssueTrackerKind,
5 ProjectId,
6 SourceControlProviderKind,
7} from "@t3tools/contracts";
8import { describe, expect, it } from "vite-plus/test";
9​
10import { resolveIssueLinkTarget } from "./issueLinks.ts";
11​
12const project = (id: string, repository: string, origin?: string) =>
13 ({
14 id: ProjectId.make(id),
15 environmentId: EnvironmentId.make("env"),
16 repositoryIdentity: {
17 canonicalKey: `github.com/${repository}`,
18 provider: SourceControlProviderKind.make("github"),
19 displayName: repository,
20 owner: repository.split("/")[0],
21 name: repository.split("/")[1],
22 ...(origin ? { origin: { canonicalKey: `github.com/${origin}`, displayName: origin } } : {}),
23 },
24 }) as never;
25​
26describe("resolveIssueLinkTarget", () => {
27 const projects = [project("p1", "acme/api"), project("p2", "acme/web")];
28​
29 it.each([
30 {
31 name: "reads an issue through the project bound to its repository",
32 url: "https://github.com/acme/web/issues/7",
33 expected: { projectId: "p2", scope: "acme/web", key: "7" },
34 },
35 {
36 name: "lends a project on the same host to a repository no project is bound to",
37 url: "https://github.com/other/repo/issues/9",
38 expected: { projectId: "p1", scope: "other/repo", key: "9" },
39 },
40 ])("$name", ({ url, expected }) => {
41 const target = resolveIssueLinkTarget({ url, projects });
42 expect(target?.ref).toMatchObject({ tracker: "github", host: "github.com", ...expected });
43 });
44​
45 it("reads a fork's own issue through the fork, not as a borrowed repository", () => {
46 const target = resolveIssueLinkTarget({
47 url: "https://github.com/me/web/issues/3",
48 projects: [project("p1", "acme/api"), project("fork", "acme/web", "me/web")],
49 });
50 expect(target?.ref).toMatchObject({ projectId: "fork", scope: "me/web", key: "3" });
51 });
52​
53 it("uses the current repository identity after a project changes repositories", () => {
54 const url = "https://github.com/acme/api/issues/7";
55 const original = project("p1", "acme/api");
56 const replacement = project("p1", "acme/mobile");
57 const lender = project("p3", "acme/web");
58 expect(resolveIssueLinkTarget({ url, projects: [lender, original] })?.ref.projectId).toBe("p1");
59 expect(resolveIssueLinkTarget({ url, projects: [lender, replacement] })?.ref.projectId).toBe(
60 "p3",
61 );
62 });
63​
64 it("prefers the project bound to the link's own port over one on the bare hostname", () => {
65 const enterprise = (id: string, host: string) =>
66 ({
67 id: ProjectId.make(id),
68 environmentId: EnvironmentId.make("env"),
69 repositoryIdentity: {
70 canonicalKey: `${host}/acme/web`,
71 provider: SourceControlProviderKind.make("github"),
72 displayName: "acme/web",
73 owner: "acme",
74 name: "web",
75 },
76 }) as never;
77 const target = resolveIssueLinkTarget({
78 url: "https://github.example.com:8443/acme/web/issues/7",
79 projects: [
80 enterprise("bare", "github.example.com"),
81 enterprise("ported", "github.example.com:8443"),
82 ],
83 });
84 expect(target?.ref.projectId).toBe("ported");
85​
86 // An Enterprise Server's hostname need not say "github"; its project vouches for it.
87 const custom = resolveIssueLinkTarget({
88 url: "https://code.acme.test/acme/web/issues/7",
89 projects: [enterprise("custom", "code.acme.test")],
90 });
91 expect(custom?.ref).toMatchObject({ projectId: "custom", host: "code.acme.test", key: "7" });
92 });
93​
94 it("reads a Linear link through the project bound to its team, else any bound to Linear", () => {
95 const trackers = {
96 trackers: [
97 {
98 tracker: IssueTrackerKind.make("linear"),
99 displayName: "Linear",
100 host: "linear.app",
101 scopeNoun: "team",
102 credential: { label: "Personal API key", helpUrl: null },
103 connected: true,
104 account: "julius",
105 detail: null,
106 workspace: "acme",
107 scopes: [],
108 },
109 ],
110 bindings: {
111 [ProjectId.make("p1")]: { tracker: IssueTrackerKind.make("linear"), scope: "API" },
112 [ProjectId.make("p2")]: {
113 tracker: IssueTrackerKind.make("linear"),
114 scope: "WEB",
115 additionalScopes: ["DOCS"],
116 },
117 },
118 };
119 const resolve = (url: string) => resolveIssueLinkTarget({ url, projects, trackers });
120 expect(resolve("https://linear.app/acme/issue/WEB-17/fix-it")?.ref).toEqual({
121 projectId: "p2",
122 tracker: "linear",
123 host: "linear.app",
124 scope: "WEB",
125 key: "WEB-17",
126 });
127 expect(resolve("https://linear.app/acme/issue/DOCS-4")?.ref).toMatchObject({
128 projectId: "p2",
129 scope: "DOCS",
130 });
131 expect(resolve("https://linear.app/acme/issue/OPS-3")?.ref).toMatchObject({
132 projectId: "p1",
133 scope: "OPS",
134 key: "OPS-3",
135 });
136 expect(resolve("https://linear.app.evil.example/acme/issue/WEB-17")).toBeNull();
137 // Team keys repeat across workspaces; only the connected one's links are its issues.
138 expect(resolve("https://linear.app/othercorp/issue/WEB-17")).toBeNull();
139 expect(
140 resolveIssueLinkTarget({ url: "https://linear.app/acme/issue/WEB-17", projects }),
141 ).toBeNull();
142​
143 // A second account reaches its own workspace, through only the projects bound with it.
144 const accountId = IssueTrackerAccountId.make("second");
145 const [p1, p2] = [ProjectId.make("p1"), ProjectId.make("p2")];
146 const both = (url: string) =>
147 resolveIssueLinkTarget({
148 url,
149 projects,
150 trackers: {
151 ...trackers,
152 accounts: [{ ...trackers.trackers[0]!, accountId, workspace: "othercorp" }],
153 bindings: { [p1]: trackers.bindings[p1]! },
154 accountBindings: { [p2]: { ...trackers.bindings[p2]!, accountId } },
155 },
156 })?.ref.projectId;
157 expect([
158 both("https://linear.app/othercorp/issue/WEB-17"),
159 both("https://linear.app/acme/issue/WEB-17"),
160 ]).toEqual(["p2", "p1"]);
161 });
162​
163 it.each([
164 "https://github.com.evil.example/acme/web/issues/7",
165 "https://github.com/acme/web/pull/7",
166 ])("leaves %s an ordinary link", (url) => {
167 expect(resolveIssueLinkTarget({ url, projects })).toBeNull();
168 });
169});