The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { |
| 2 | EnvironmentId, |
| 3 | IssueTrackerKind, |
| 4 | ProjectId, |
| 5 | SourceControlProviderKind, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { describe, expect, it } from "vite-plus/test"; |
| 8 | |
| 9 | import { resolveIssueLinkTarget } from "./issueLinks.ts"; |
| 10 | |
| 11 | const 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 | |
| 25 | describe("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("prefers the project bound to the link's own port over one on the bare hostname", () => { |
| 53 | const enterprise = (id: string, host: string) => |
| 54 | ({ |
| 55 | id: ProjectId.make(id), |
| 56 | environmentId: EnvironmentId.make("env"), |
| 57 | repositoryIdentity: { |
| 58 | canonicalKey: `${host}/acme/web`, |
| 59 | provider: SourceControlProviderKind.make("github"), |
| 60 | displayName: "acme/web", |
| 61 | owner: "acme", |
| 62 | name: "web", |
| 63 | }, |
| 64 | }) as never; |
| 65 | const target = resolveIssueLinkTarget({ |
| 66 | url: "https://github.example.com:8443/acme/web/issues/7", |
| 67 | projects: [ |
| 68 | enterprise("bare", "github.example.com"), |
| 69 | enterprise("ported", "github.example.com:8443"), |
| 70 | ], |
| 71 | }); |
| 72 | expect(target?.ref.projectId).toBe("ported"); |
| 73 | |
| 74 | // An Enterprise Server's hostname need not say "github"; its project vouches for it. |
| 75 | const custom = resolveIssueLinkTarget({ |
| 76 | url: "https://code.acme.test/acme/web/issues/7", |
| 77 | projects: [enterprise("custom", "code.acme.test")], |
| 78 | }); |
| 79 | expect(custom?.ref).toMatchObject({ projectId: "custom", host: "code.acme.test", key: "7" }); |
| 80 | }); |
| 81 | |
| 82 | it("reads a Linear link through the project bound to its team, else any bound to Linear", () => { |
| 83 | const trackers = { |
| 84 | trackers: [ |
| 85 | { |
| 86 | tracker: IssueTrackerKind.make("linear"), |
| 87 | displayName: "Linear", |
| 88 | host: "linear.app", |
| 89 | scopeNoun: "team", |
| 90 | credential: { label: "Personal API key", helpUrl: null }, |
| 91 | connected: true, |
| 92 | account: "julius", |
| 93 | detail: null, |
| 94 | workspace: "acme", |
| 95 | scopes: [], |
| 96 | }, |
| 97 | ], |
| 98 | bindings: { |
| 99 | [ProjectId.make("p1")]: { tracker: IssueTrackerKind.make("linear"), scope: "API" }, |
| 100 | [ProjectId.make("p2")]: { tracker: IssueTrackerKind.make("linear"), scope: "WEB" }, |
| 101 | }, |
| 102 | }; |
| 103 | const resolve = (url: string) => resolveIssueLinkTarget({ url, projects, trackers }); |
| 104 | expect(resolve("https://linear.app/acme/issue/WEB-17/fix-it")?.ref).toEqual({ |
| 105 | projectId: "p2", |
| 106 | tracker: "linear", |
| 107 | host: "linear.app", |
| 108 | scope: "WEB", |
| 109 | key: "WEB-17", |
| 110 | }); |
| 111 | expect(resolve("https://linear.app/acme/issue/OPS-3")?.ref).toMatchObject({ |
| 112 | projectId: "p1", |
| 113 | scope: "OPS", |
| 114 | key: "OPS-3", |
| 115 | }); |
| 116 | expect(resolve("https://linear.app.evil.example/acme/issue/WEB-17")).toBeNull(); |
| 117 | // Team keys repeat across workspaces; only the connected one's links are its issues. |
| 118 | expect(resolve("https://linear.app/othercorp/issue/WEB-17")).toBeNull(); |
| 119 | expect( |
| 120 | resolveIssueLinkTarget({ url: "https://linear.app/acme/issue/WEB-17", projects }), |
| 121 | ).toBeNull(); |
| 122 | }); |
| 123 | |
| 124 | it.each([ |
| 125 | "https://github.com.evil.example/acme/web/issues/7", |
| 126 | "https://github.com/acme/web/pull/7", |
| 127 | ])("leaves %s an ordinary link", (url) => { |
| 128 | expect(resolveIssueLinkTarget({ url, projects })).toBeNull(); |
| 129 | }); |
| 130 | }); |