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

definition.test.ts

80 lines3.4 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { definition, parsePullRequestUrl } from "./definition.ts";
4​
5describe("TanStack Source definition", () => {
6 it("reads pull requests and commits only on its own host", () => {
7 expect(
8 definition.parseCommitUrl?.(
9 new URL(
10 "https://source.tanstack.com/@TanStack/router/commit/cebaf84a4fe3776f27359ef1d978f0650d6b0e51",
11 ),
12 ),
13 ).toMatchObject({ repository: "tanstack/router" });
14 for (const url of [
15 "https://github.com/TanStack/router/pull/1",
16 "https://source.tanstack.com.evil.test/@TanStack/router/pull/1",
17 "https://source.tanstack.com/@TanStack/router/pull/0",
18 ])
19 expect(parsePullRequestUrl(url), url).toBeNull();
20 // Some clients send the owner's `@` percent-encoded.
21 expect(
22 definition.parseIssueUrl?.(new URL("https://source.tanstack.com/%40TanStack/ai/issues/12")),
23 ).toEqual({ host: "source.tanstack.com", scope: "tanstack/ai", key: "12" });
24 });
25​
26 it("builds links with the owner's @ and reads src's checkout command", () => {
27 expect(
28 definition.changeRequestUrl({
29 host: "source.tanstack.com",
30 repository: "TanStack/router",
31 number: 7,
32 }),
33 ).toBe("https://source.tanstack.com/@TanStack/router/pull/7");
34 // A stored link or an older client may name the repository as the web spells it.
35 for (const repository of ["@TanStack/router", "%40TanStack/router"])
36 expect(
37 definition.changeRequestUrl({ host: "source.tanstack.com", repository, number: 7 }),
38 repository,
39 ).toBe("https://source.tanstack.com/@TanStack/router/pull/7");
40 expect(definition.checkoutCommandArgument("src pr checkout 7")).toBe("7");
41 expect(definition.checkoutCommandArgument("gh pr checkout 7")).toBeNull();
42 });
43​
44 it.each(["tanstack/router", "@tanstack/router", "%40tanstack/router"])(
45 "keys %s as the API's owner/name",
46 (repository) => {
47 expect(definition.canonicalRepositoryKey?.(`source.tanstack.com/${repository}`)).toBe(
48 "source.tanstack.com/tanstack/router",
49 );
50 expect(definition.canonicalRepositoryKey?.(`github.com/${repository}`)).toBeNull();
51 },
52 );
53​
54 it("claims repository assets on its own host, so private ones load through the server", () => {
55 const media = (url: string) => definition.mediaUrl?.(new URL(url)) ?? null;
56 const expected = {
57 fetchUrl: "https://source.tanstack.com/acme/app.private/assets/shots/home.png",
58 fileName: "home.png",
59 };
60 expect(media("https://source.tanstack.com/@acme/app.private/assets/shots/home.png")).toEqual(
61 expected,
62 );
63 expect(media("https://SOURCE.tanstack.com/%40acme/app.private/assets/shots/home.png")).toEqual(
64 expected,
65 );
66 expect(media("https://source.tanstack.com/acme/app/assets/hero.png?v=3&x=1")).toEqual({
67 fetchUrl: "https://source.tanstack.com/acme/app/assets/hero.png?v=3",
68 fileName: "hero.png",
69 });
70 for (const url of [
71 "http://source.tanstack.com/acme/app/assets/hero.png",
72 "https://source.tanstack.com:8443/acme/app/assets/hero.png",
73 "https://source.tanstack.com.evil.test/acme/app/assets/hero.png",
74 "https://github.com/acme/app/assets/hero.png",
75 "https://source.tanstack.com/acme/app/blob/main/hero.png",
76 "https://source.tanstack.com/acme/app/assets/",
77 ])
78 expect(media(url), url).toBeNull();
79 });
80});