code-diff.test.ts
This file
Path
apps/web/test/code-diff.test.ts
223 lines · 6.5 KiB · TypeScript
Last changed by
Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224import { describe, expect, it } from "@effect/vitest";import { ChangedFile } from "@repo/protocol/domain/code";import { Option, Schema } from "effect";
import { countsOf, drawnFiles, fileDiffOf, globTest, isTestPath, splitRows, statsOf, treeOrder, unifiedRows,} from "../src/features/code/diff-model.ts";import { permalinkPress } from "../src/features/code/file-header.tsx";import { stepFrom } from "../src/features/code/file-keys.ts";
const decode = Schema.decodeSync(ChangedFile);
const lines = (from: number, to: number) => `${Array.from( { length: to - from + 1 }, (_, index) => `line ${from + index}` ).join("\n")}\n`;
const thirty = lines(1, 30);
const changed = decode({ after: "a".repeat(40), before: "b".repeat(40), binary: false, newText: thirty.replace("line 15\n", "line fifteen\nline 15b\n"), oldText: thirty, path: "src/parse.ts", status: "modified",});
describe("a changed file's diff", () => { it("is parsed into the hunks the drawing draws, keyed by both blobs", () => { const diff = fileDiffOf(changed);
expect(Option.map(diff, ({ hunks }) => hunks.length)).toStrictEqual( Option.some(1) ); expect(Option.map(diff, ({ cacheKey }) => cacheKey)).toStrictEqual( Option.some(`diff:${"b".repeat(40)}:${"a".repeat(40)}`) ); expect(countsOf(diff)).toStrictEqual({ additions: 2, deletions: 1 }); });
it("reads as unified rows: the unchanged lines skipped before and after, each line numbered on its side", () => { const rows = Option.match(fileDiffOf(changed), { onNone: () => [], onSome: unifiedRows, });
expect(rows[0]).toStrictEqual({ kind: "separator", lines: 10 }); expect(rows.at(-1)).toStrictEqual({ kind: "separator", lines: 11 }); expect( rows.filter((row) => row.kind !== "separator" && row.kind !== "context") ).toStrictEqual([ { kind: "deletion", number: 15, text: "line 15" }, { kind: "addition", number: 15, text: "line fifteen" }, { kind: "addition", number: 16, text: "line 15b" }, ]); });
it("reads as split rows: the deletion beside the first addition, the second against nothing", () => { const rows = Option.match(fileDiffOf(changed), { onNone: () => [], onSome: splitRows, });
const pairs = rows.filter( (row) => row.kind === "pair" && (row.left?.kind !== "context" || row.right === undefined) );
expect(pairs).toStrictEqual([ { kind: "pair", left: { kind: "deletion", number: 15, text: "line 15" }, right: { kind: "addition", number: 15, text: "line fifteen" }, }, { kind: "pair", right: { kind: "addition", number: 16, text: "line 15b" }, }, ]); });
it("has no lines for a binary file, or one whose text was not sent", () => { expect( fileDiffOf(decode({ binary: true, path: "logo.png", status: "added" })) ).toStrictEqual(Option.none()); expect( fileDiffOf( decode({ binary: false, path: "big.json", status: "modified" }) ) ).toStrictEqual(Option.none()); });});
describe("a commit's files", () => { const files = [ decode({ binary: false, newText: lines(1, 81), path: "data/zones.ts", status: "added", }), decode({ binary: false, oldText: lines(1, 80), path: "data/old.ts", status: "deleted", }), changed, decode({ binary: false, newText: "it\n", path: "test/parse.test.ts", status: "added", }), decode({ binary: false, newText: "x\n", path: "README.md", status: "added", }), ];
it("are listed in the tree's order: folders before files, then by name", () => { expect(drawnFiles(files).map(({ file }) => file.path)).toStrictEqual([ "data/old.ts", "data/zones.ts", "src/parse.ts", "test/parse.test.ts", "README.md", ]); expect(["b", "a/z", "a/b/c"].toSorted(treeOrder)).toStrictEqual([ "a/b/c", "a/z", "b", ]); });
it("start folded when added or deleted past 80 lines", () => { expect( drawnFiles(files) .filter(({ folded }) => folded) .map(({ file }) => file.path) ).toStrictEqual(["data/zones.ts"]); });
it("count by path: the ask's territory, its tests, the rest", () => { const stats = statsOf(drawnFiles(files), ["src/**"]);
expect([stats.files, stats.additions, stats.deletions]).toStrictEqual([ 5, 85, 81, ]); expect(stats.parts).toStrictEqual([ { additions: 2, deletions: 1, files: 1, label: "territory" }, { additions: 1, deletions: 0, files: 1, label: "tests" }, { additions: 82, deletions: 80, files: 3, label: "other" }, ]); });
it("read a territory's globs as git paths", () => { expect(globTest("src/**")("src/a/b.ts")).toBeTruthy(); expect(globTest("src/*.ts")("src/a/b.ts")).toBeFalsy(); expect(globTest("packages/parse")("packages/parse/index.ts")).toBeTruthy(); expect(globTest("**/*.ts")("a.ts")).toBeTruthy(); });
it("know a test file by its folder or its name", () => { expect(isTestPath("src/parse.test.ts")).toBeTruthy(); expect(isTestPath("test/asks/x.ts")).toBeTruthy(); expect(isTestPath("src/testing.ts")).toBeFalsy(); });});
describe("the keys between files", () => { const paths = ["a.ts", "b.ts", "c.ts"];
it("move from the page's own file, and stop at either end", () => { expect(stepFrom(paths, "b.ts", 1)).toBe("c.ts"); expect(stepFrom(paths, "b.ts", -1)).toBe("a.ts"); expect(stepFrom(paths, "c.ts", 1)).toBe("c.ts"); expect(stepFrom(paths, "a.ts", -1)).toBe("a.ts"); });
it("start at the first file, or the last going back, from none", () => { expect(stepFrom(paths, undefined, 1)).toBe("a.ts"); expect(stepFrom(paths, undefined, -1)).toBe("c.ts"); expect(stepFrom([], undefined, 1)).toBeUndefined(); });});
describe("the permalink key", () => { const press = { altKey: false, ctrlKey: false, inField: false, key: "y", metaKey: false, };
it("takes y to the page at its commit while the single keys are on", () => { expect(permalinkPress(press, true)).toBeTruthy(); });
it("does nothing once the single keys are switched off, in a field, or with a modifier", () => { expect([ permalinkPress(press, false), permalinkPress({ ...press, inField: true }, true), permalinkPress({ ...press, ctrlKey: true }, true), permalinkPress({ ...press, key: "Y" }, true), ]).toStrictEqual([false, false, false, false]); });});