import type { ChangeRequestProjectIdentity } from "@t3tools/source-control-core/shared/definition"; import { recognizeRemoteWith } from "@t3tools/source-control-core/shared/definition"; import { describe, expect, it } from "vite-plus/test"; import { definition } from "./definition.ts"; const parse = (url: string) => definition.parseChangeRequestUrl(new URL(url))!; const identity = ( remoteUrl: string, fields: Partial = {}, ): ChangeRequestProjectIdentity => ({ canonicalKey: "forge.example/git/team/repo", displayName: "git/team/repo", locator: { source: "git-remote", remoteName: "origin", remoteUrl }, ...fields, }); const onHost = (project: ChangeRequestProjectIdentity, link: ReturnType) => definition.isChangeRequestInRepository(project, link) || definition.canReadChangeRequestOnHost(project, link); describe("Forgejo change request URLs", () => { it("reads nested paths and keeps the server's port as the authority", () => { expect(parse("http://git.example.test:3000/Team/Repo/pulls/42/files")).toEqual({ host: "git.example.test", authority: "git.example.test:3000", repository: "team/repo", number: 42, repositoryUrl: "http://git.example.test:3000/Team/Repo", }); expect(parse("https://github.internal/team/repo/pulls/7")).toMatchObject({ host: "github.internal", authority: "github.internal", }); }); it("claims only its own route", () => { for (const url of [ "https://forge.example/team/repo/pull/7", "https://forge.example/repo/pulls/7", "https://forge.example/team/repo/pulls/abc", ]) { expect(definition.parseChangeRequestUrl(new URL(url)), url).toBeNull(); } }); it.each([ ["http://forge.example:3000/git/owner/repo.git", "http://forge.example:3000"], ["git@forge.example:git/owner/repo.git", "https://forge.example"], ["http://other.example:3000/git/owner/repo.git", "https://forge.example"], ])("keeps the matching web origin from %s", (remoteUrl, origin) => { expect( definition.changeRequestUrl({ host: "forge.example", repository: "git/owner/repo", number: 42, remoteUrl, }), ).toBe(`${origin}/git/owner/repo/pulls/42`); }); it("keeps the origin when the host already carries its port", () => { expect( definition.changeRequestUrl({ host: "forge.example:3000", repository: "team/repo", number: 42, remoteUrl: "http://forge.example:3000/team/repo.git", }), ).toBe("http://forge.example:3000/team/repo/pulls/42"); }); it("matches projects by HTTP port at repository and host level", () => { const [atThree, atFour] = [3000, 4000].map((port) => identity(`http://forge.example:${port}/git/team/repo.git`), ); const link = parse("http://forge.example:4000/git/team/repo/pulls/42"); expect(definition.isChangeRequestInRepository(atFour!, link)).toBe(true); expect(onHost(atFour!, { ...link, repository: "git/team/other" })).toBe(true); expect(definition.isChangeRequestInRepository(atThree!, link)).toBe(false); expect(onHost(atThree!, link)).toBe(false); }); it("lets tea resolve the web port for SSH remotes, and honours a resolved web URL", () => { const link = parse("http://forge.example:4000/git/team/repo/pulls/42"); expect( definition.isChangeRequestInRepository(identity("git@forge.example:git/team/repo.git"), link), ).toBe(true); const aliased = identity("git@ssh.forge.example:team/repo.git", { canonicalKey: "ssh.forge.example/team/repo", displayName: "team/repo", webUrl: "http://forge.example:4000/git/team/repo", }); expect(definition.isChangeRequestInRepository(aliased, link)).toBe(true); expect(onHost(aliased, { ...link, repository: "git/team/other" })).toBe(true); for (const url of [ "http://other.example:4000/git/team/repo/pulls/42", "http://forge.example:3000/git/team/repo/pulls/42", "http://forge.example:4000/other/team/repo/pulls/42", ]) { expect(onHost(aliased, parse(url)), url).toBe(false); } }); }); describe("Forgejo repository pages", () => { it("reads the repository from branch, tag and commit views", () => { const repositoryUrl = (url: string) => definition.repositoryUrlFromPage?.(new URL(url), true); expect(repositoryUrl("https://codeberg.org/forgejo/forgejo/src/branch/forgejo/README.md")).toBe( "https://codeberg.org/forgejo/forgejo", ); expect(repositoryUrl("http://git.example.test:3000/git/team/repo/src/tag/v1.0")).toBe( "http://git.example.test:3000/git/team/repo", ); expect(repositoryUrl("https://codeberg.org/forgejo/forgejo/src/commit/abc123")).toBe( "https://codeberg.org/forgejo/forgejo", ); expect(repositoryUrl("https://codeberg.org/forgejo/forgejo")).toBeNull(); }); }); describe("Forgejo remotes", () => { it("recognises Codeberg and Forgejo or Gitea hosts, keeping an HTTP remote's origin", () => { for (const host of ["codeberg.org", "forgejo.example.test", "gitea.example.test"]) { expect(recognizeRemoteWith(definition, `http://${host}:3000/team/repo.git`)).toEqual({ kind: "forgejo", name: "Forgejo", baseUrl: `http://${host}:3000`, }); } expect( recognizeRemoteWith(definition, "ssh://git@forgejo.example.test:2222/team/repo.git")?.baseUrl, ).toBe("https://forgejo.example.test"); expect(recognizeRemoteWith(definition, "https://git.example.test/team/repo")).toBeNull(); }); }); describe("Forgejo CLI commands", () => { it("recognizes shell merges and closes under every spelling tea accepts", () => { for (const command of [ "tea pulls merge 7", "tea pull merge 7", "tea pr m 7", "tea pull close 7", "fj pr close 7", ]) { expect(definition.changeRequestCloseCommand.test(command), command).toBe(true); } expect(definition.changeRequestCloseCommand.test("tea pull checkout 7")).toBe(false); }); it("reads the change request from every checkout spelling tea accepts", () => { for (const command of [ "tea pulls checkout 7", "tea pull checkout 7", "tea pr co 7", "tea pull co 7", ]) { expect(definition.checkoutCommandArgument(command), command).toBe("7"); } }); }); describe("Forgejo change request host", () => { it("keeps an HTTP remote's port, because a server's identity includes it", () => { const project = identity("http://forge.example:3000/git/team/repo.git"); expect(definition.changeRequestHost?.(project)).toBe("forge.example:3000"); expect( definition.changeRequestHost?.(identity("ssh://git@forge.example:2222/git/team/repo.git")), ).toBe("forge.example"); }); }); describe("Forgejo credentialed media", () => { it("claims an uploaded attachment, which a private repository serves only with a token", () => { const url = "https://codeberg.org/attachments/5b5549d9-9b57-488f-bab0-ec32f841bca8"; expect(definition.mediaUrl?.(new URL(url))).toMatchObject({ fetchUrl: url }); }); });