Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

94 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import {
5 bodyCitations,
6 citationHref,
7 citationMarkdown,
8 citationsFromMarkdown,
9 cleanCitation,
10 cleanDescribes,
11 cleanPath,
12 touchedPaths,
13 touches,
14} from "./citations.ts";
15
16test("paths are kept tidy, and unsafe ones refused", () => {
17 assert.equal(cleanPath("/src//export.ts/"), "src/export.ts");
18 assert.equal(cleanPath("./docs/a.md"), "docs/a.md");
19 assert.equal(cleanPath("src/../secrets"), null);
20 assert.equal(cleanPath(""), null);
21});
22
23test("a citation names a repository and a path; a label only for what it names", () => {
24 assert.deepEqual(cleanCitation({ repo: "Acme/Web", path: "src/export.ts", kind: "symbol", label: " exportCsv ", ref: "ABCDEF1" }, "body"), {
25 repo: "acme/web",
26 path: "src/export.ts",
27 kind: "symbol",
28 label: "exportCsv",
29 ref: "abcdef1",
30 source: "body",
31 });
32 assert.equal(cleanCitation({ repo: "acme/web", path: "src/a.ts", kind: "path", label: "ignored" }, "body")?.label, null);
33 assert.equal(cleanCitation({ repo: "nope", path: "src/a.ts" }, "body"), null);
34 assert.equal(cleanCitation({ repo: "acme/web", path: "src/a.ts", kind: "weird" as never }, "body")?.kind, "path");
35});
36
37test("a change touches a cited file, anything under a cited folder, and what a glob matches", () => {
38 assert.ok(touches("src/export.ts", "src/export.ts"));
39 assert.ok(!touches("src/export.ts", "src/export.tsx"));
40 assert.ok(touches("src/export", "src/export/csv.ts"));
41 assert.ok(!touches("src/export", "src/exports/csv.ts"));
42 assert.ok(touches("src/*.ts", "src/a.ts"));
43 assert.ok(!touches("src/*.ts", "src/deep/a.ts"));
44 assert.ok(touches("src/**/*.sql", "src/a.sql"));
45 assert.ok(touches("src/**/*.sql", "src/db/migrations/0001.sql"));
46 assert.ok(touches("api/**", "api/v1/routes.rs"));
47 assert.ok(touches("docs/?.md", "docs/a.md"));
48 assert.ok(!touches("docs/?.md", "docs/ab.md"));
49 assert.ok(!touches("a.b/*.ts", "aXb/x.ts"));
50 assert.deepEqual(touchedPaths([{ path: "src/export" }, { path: "*.toml" }], ["README.md", "src/export/csv.ts", "Cargo.toml"]), [
51 "src/export/csv.ts",
52 "Cargo.toml",
53 ]);
54});
55
56test("a citation links to the code at the commit it was cited at", () => {
57 assert.equal(citationHref({ repo: "acme/web", path: "src/export.ts", ref: "abc1234" }), "/acme/web/blob/abc1234/src/export.ts");
58 assert.equal(citationHref({ repo: "acme/web", path: "src/export", ref: null }), "/acme/web/tree/HEAD/src/export");
59 assert.equal(citationHref({ repo: "acme/web", path: "src/**/*.sql", ref: null }), "/acme/web/tree/HEAD/src");
60 assert.equal(
61 citationMarkdown({ repo: "acme/web", path: "src/export.ts", ref: "abc1234", kind: "env", label: "EXPORT_BUCKET" }),
62 "[`EXPORT_BUCKET`](/acme/web/blob/abc1234/src/export.ts)",
63 );
64});
65
66test("links to code in a page are citations too, a chip's own link once", () => {
67 const md =
68 "See [`exportCsv`](/acme/web/blob/abc1234/src/export.ts) and [the folder](/acme/web/tree/main/src/jobs) and [elsewhere](https://example.com/a/b/blob/x/y.ts) and [on g1t](https://g1t.sh/acme/api/blob/main/src/lib.rs#L10).";
69 assert.deepEqual(
70 citationsFromMarkdown(md).map((c) => [c.repo, c.path, c.ref]),
71 [
72 ["acme/web", "src/export.ts", "abc1234"],
73 ["acme/web", "src/jobs", "main"],
74 ["acme/api", "src/lib.rs", "main"],
75 ],
76 );
77 const chips = [{ repo: "acme/web", path: "src/export.ts", kind: "symbol" as const, label: "exportCsv", ref: "abc1234" }];
78 const all = bodyCitations(chips, md);
79 assert.deepEqual(
80 all.map((c) => [c.path, c.kind]),
81 [
82 ["src/export.ts", "symbol"],
83 ["src/jobs", "path"],
84 ["src/lib.rs", "path"],
85 ],
86 );
87});
88
89test("the header's Describes list is tidy and short", () => {
90 assert.deepEqual(cleanDescribes([{ repo: "Acme/Web", path: "/src/export/" }, { repo: "acme/web", path: "src/export" }, { repo: "x", path: "y" }, "nope"]), [
91 { repo: "acme/web", path: "src/export" },
92 ]);
93 assert.equal(cleanDescribes(Array.from({ length: 40 }, (_, i) => ({ repo: "acme/web", path: `f${i}` }))).length, 20);
94});