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.

156 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import type { FileDiff } from "@g1t/contracts";
5
6import { diffContent, firstScreens, htmlOfFile, linkedFile, withHtml } from "./diff.ts";
7import { highlightFile } from "./shiki.ts";
8
9const FILE: FileDiff = {
10 path: "src/lib.rs",
11 status: "modified",
12 additions: 2,
13 deletions: 1,
14 binary: false,
15 hunks: [
16 {
17 lines: [
18 { kind: "context", old: 1, new: 1, text: "fn main() {" },
19 { kind: "delete", old: 2, new: null, text: ' println!("old");' },
20 { kind: "add", old: null, new: 2, text: ' println!("new");' },
21 { kind: "add", old: null, new: 3, text: "" },
22 { kind: "context", old: 3, new: 4, text: "}" },
23 ],
24 },
25 { lines: [{ kind: "add", old: null, new: 10, text: "// end" }] },
26 ],
27};
28
29test("a highlighted file's HTML, kept apart and given back, is the same highlighted file", async () => {
30 const highlighted = await highlightFile(FILE);
31 assert.ok(highlighted);
32 const html = htmlOfFile(highlighted);
33 assert.equal(html.length, 2);
34 assert.ok(html[0]!.every((row) => row !== null));
35 // Through the data centre's cache it travels as JSON.
36 const kept = JSON.parse(JSON.stringify(html));
37 assert.deepEqual(withHtml(FILE, kept), highlighted);
38});
39
40test("lines without HTML stay without it", () => {
41 const html = [[null, "<b>x</b>", null, "", null], [null]];
42 const file = withHtml(FILE, html);
43 assert.equal("html" in file.hunks[0]!.lines[0]!, false);
44 assert.equal(file.hunks[0]!.lines[1]!.html, "<b>x</b>");
45 // An empty line's HTML is kept: it is not the same as none.
46 assert.equal(file.hunks[0]!.lines[3]!.html, "");
47 assert.equal("html" in file.hunks[1]!.lines[0]!, false);
48 // The diff given in is not changed.
49 assert.equal("html" in FILE.hunks[0]!.lines[1]!, false);
50});
51
52test("what a file's colours depend on: each line's side and text, not its numbers or path", () => {
53 const moved: FileDiff = {
54 ...FILE,
55 path: "other/lib.rs",
56 hunks: FILE.hunks.map((hunk) => ({ lines: hunk.lines.map((line) => ({ ...line, old: 99, new: 99 })) })),
57 };
58 assert.equal(diffContent(moved), diffContent(FILE));
59 const sideChanged: FileDiff = {
60 ...FILE,
61 hunks: [{ lines: FILE.hunks[0]!.lines.map((line, n) => (n === 0 ? { ...line, kind: "add" as const } : line)) }, FILE.hunks[1]!],
62 };
63 assert.notEqual(diffContent(sideChanged), diffContent(FILE));
64 // A line split differently is different content.
65 const one: FileDiff = { ...FILE, hunks: [{ lines: [{ kind: "add", old: null, new: 1, text: "a\nb" }] }] };
66 const two: FileDiff = {
67 ...FILE,
68 hunks: [
69 {
70 lines: [
71 { kind: "add", old: null, new: 1, text: "a" },
72 { kind: "add", old: null, new: 2, text: "b" },
73 ],
74 },
75 ],
76 };
77 assert.notEqual(diffContent(one), diffContent(two));
78});
79
80/** A file of `lines` added lines. */
81function sized(path: string, lines: number): FileDiff {
82 return {
83 path,
84 status: "added",
85 additions: lines,
86 deletions: 0,
87 binary: false,
88 hunks: [{ lines: Array.from({ length: lines }, (_, n) => ({ kind: "add" as const, old: null, new: n + 1, text: `line ${n}` })) }],
89 };
90}
91
92const comparisonOf = (files: FileDiff[]) => ({ base: "a".repeat(40), head: "b".repeat(40), files, truncated: false });
93
94test("a small change is sent whole", () => {
95 const files = [sized("a", 10), sized("b", 300), FILE];
96 const sent = firstScreens(comparisonOf(files));
97 assert.deepEqual(sent.files, files);
98 assert.ok(sent.files.every((file) => !file.deferred));
99});
100
101test("past the first screens' lines, files are sent with their counts but not their lines", () => {
102 const files = Array.from({ length: 10 }, (_, n) => sized(`f${n}`, 400));
103 const sent = firstScreens(comparisonOf(files), { budget: 2_000 });
104 // 5 × 400 lines fit in 2,000; the rest wait, in order.
105 assert.deepEqual(
106 sent.files.map((file) => file.hunks.length > 0),
107 [true, true, true, true, true, false, false, false, false, false],
108 );
109 const later = sent.files[7]!;
110 assert.deepEqual(later.deferred, { lines: 400, large: false });
111 assert.equal(later.additions, 400);
112 assert.equal(later.path, "f7");
113 // Once one does not fit, a smaller one after it waits too: what is drawn is the top of the change.
114 const mixed = firstScreens(comparisonOf([sized("a", 800), sized("b", 900), sized("c", 10)]), { budget: 1_000 });
115 assert.deepEqual(
116 mixed.files.map((file) => file.deferred?.lines ?? null),
117 [null, 900, 10],
118 );
119});
120
121test("a very large file waits to be asked for, and does not hold back the files after it", () => {
122 const sent = firstScreens(comparisonOf([sized("package-lock.json", 5_000), sized("src/a.ts", 20)]));
123 assert.deepEqual(sent.files[0]!.deferred, { lines: 5_000, large: true });
124 assert.equal(sent.files[0]!.hunks.length, 0);
125 assert.equal(sent.files[1]!.deferred, undefined);
126 assert.equal(sent.files[1]!.hunks.length, 1);
127});
128
129test("files with comments keep their lines wherever they are, however large", () => {
130 const files = [sized("a", 900), sized("b", 400), sized("lock", 3_000), sized("c", 400)];
131 const sent = firstScreens(comparisonOf(files), { budget: 1_000, eager: ["c", "lock"] });
132 assert.deepEqual(
133 sent.files.map((file) => file.deferred ?? null),
134 [null, { lines: 400, large: false }, null, null],
135 );
136});
137
138test("files the comparison left out wait with no counts; binary files are as they were", () => {
139 const omitted: FileDiff = { ...sized("z", 0), hunks: [], additions: 0, omitted: true };
140 const binary: FileDiff = { ...sized("logo.png", 0), hunks: [], binary: true };
141 const sent = firstScreens(comparisonOf([omitted, binary]));
142 assert.deepEqual(sent.files[0]!.deferred, { lines: null, large: false });
143 assert.equal(sent.files[1]!.deferred, undefined);
144 assert.equal(sent.files[1]!.binary, true);
145});
146
147test("a link to a file names it by path, encoded or not, and only a file of the change", () => {
148 const files = [{ path: "src/a b.ts" }, { path: "README.md" }];
149 assert.equal(linkedFile("#file-README.md", files), "README.md");
150 assert.equal(linkedFile("#diff-src/a%20b.ts", files), "src/a b.ts");
151 assert.equal(linkedFile("#file-src/a b.ts", files), "src/a b.ts");
152 assert.equal(linkedFile("#file-missing.ts", files), null);
153 assert.equal(linkedFile("#readme", files), null);
154 assert.equal(linkedFile("", files), null);
155 assert.equal(linkedFile("#file-%E0%A4%A", files), null);
156});