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

issueLinks.test.ts

121 lines4.2 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) =>
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 },
22 }) as never;
23​
24describe("resolveIssueLinkTarget", () => {
25 const projects = [project("p1", "acme/api"), project("p2", "acme/web")];
26​
27 it.each([
28 {
29 name: "reads an issue through the project bound to its repository",
30 url: "https://github.com/acme/web/issues/7",
31 expected: { projectId: "p2", scope: "acme/web", key: "7" },
32 },
33 {
34 name: "lends a project on the same host to a repository no project is bound to",
35 url: "https://github.com/other/repo/issues/9",
36 expected: { projectId: "p1", scope: "other/repo", key: "9" },
37 },
38 ])("$name", ({ url, expected }) => {
39 const target = resolveIssueLinkTarget({ url, projects });
40 expect(target?.ref).toMatchObject({ tracker: "github", host: "github.com", ...expected });
41 });
42​
43 it("prefers the project bound to the link's own port over one on the bare hostname", () => {
44 const enterprise = (id: string, host: string) =>
45 ({
46 id: ProjectId.make(id),
47 environmentId: EnvironmentId.make("env"),
48 repositoryIdentity: {
49 canonicalKey: `${host}/acme/web`,
50 provider: SourceControlProviderKind.make("github"),
51 displayName: "acme/web",
52 owner: "acme",
53 name: "web",
54 },
55 }) as never;
56 const target = resolveIssueLinkTarget({
57 url: "https://github.example.com:8443/acme/web/issues/7",
58 projects: [
59 enterprise("bare", "github.example.com"),
60 enterprise("ported", "github.example.com:8443"),
61 ],
62 });
63 expect(target?.ref.projectId).toBe("ported");
64​
65 // An Enterprise Server's hostname need not say "github"; its project vouches for it.
66 const custom = resolveIssueLinkTarget({
67 url: "https://code.acme.test/acme/web/issues/7",
68 projects: [enterprise("custom", "code.acme.test")],
69 });
70 expect(custom?.ref).toMatchObject({ projectId: "custom", host: "code.acme.test", key: "7" });
71 });
72​
73 it("reads a Linear link through the project bound to its team, else any bound to Linear", () => {
74 const trackers = {
75 trackers: [
76 {
77 tracker: IssueTrackerKind.make("linear"),
78 displayName: "Linear",
79 host: "linear.app",
80 scopeNoun: "team",
81 credential: { label: "Personal API key", helpUrl: null },
82 connected: true,
83 account: "julius",
84 detail: null,
85 workspace: "acme",
86 scopes: [],
87 },
88 ],
89 bindings: {
90 [ProjectId.make("p1")]: { tracker: IssueTrackerKind.make("linear"), scope: "API" },
91 [ProjectId.make("p2")]: { tracker: IssueTrackerKind.make("linear"), scope: "WEB" },
92 },
93 };
94 const resolve = (url: string) => resolveIssueLinkTarget({ url, projects, trackers });
95 expect(resolve("https://linear.app/acme/issue/WEB-17/fix-it")?.ref).toEqual({
96 projectId: "p2",
97 tracker: "linear",
98 host: "linear.app",
99 scope: "WEB",
100 key: "WEB-17",
101 });
102 expect(resolve("https://linear.app/acme/issue/OPS-3")?.ref).toMatchObject({
103 projectId: "p1",
104 scope: "OPS",
105 key: "OPS-3",
106 });
107 expect(resolve("https://linear.app.evil.example/acme/issue/WEB-17")).toBeNull();
108 // Team keys repeat across workspaces; only the connected one's links are its issues.
109 expect(resolve("https://linear.app/othercorp/issue/WEB-17")).toBeNull();
110 expect(
111 resolveIssueLinkTarget({ url: "https://linear.app/acme/issue/WEB-17", projects }),
112 ).toBeNull();
113 });
114​
115 it.each([
116 "https://github.com.evil.example/acme/web/issues/7",
117 "https://github.com/acme/web/pull/7",
118 ])("leaves %s an ordinary link", (url) => {
119 expect(resolveIssueLinkTarget({ url, projects })).toBeNull();
120 });
121});