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

issueLinks.test.ts

149 lines5.4 KB

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