Skip to content
Myr

code-diff.test.ts

This file

Path

apps/web/test/code-diff.test.ts

231 lines · 6.9 KiB · TypeScript

Last changed by

Merge build/1211-code-browseroscabrielbefore adoptiona6808d3

History of this file

Take it

Merge build/1211-code-browseroscabrielbefore adoptiona6808d3

lines 1–231why, one step back
1/**
2 * A commit's diff as the server sends it and the browser draws it: both3 * parse a file's two texts with the drawing's own parser, so the plain4 * text holds the drawing's hunks line for line; a file's counts, which5 * files start folded, the order a tree lists paths in, the stats split by6 * the ask's territory, and the keys that move between files.7 */8import { describe, expect, it } from "@effect/vitest";9import { ChangedFile } from "@repo/protocol/domain/code";10import { Option, Schema } from "effect";1112import {13 countsOf,14 drawnFiles,15 fileDiffOf,16 globTest,17 isTestPath,18 splitRows,19 statsOf,20 treeOrder,21 unifiedRows,22} from "../src/features/code/diff-model.ts";23import { permalinkPress } from "../src/features/code/file-header.tsx";24import { stepFrom } from "../src/features/code/file-keys.ts";2526const decode = Schema.decodeSync(ChangedFile);2728/** Lines `from` to `to`, as a file's text. */29const lines = (from: number, to: number) =>30 `${Array.from(31 { length: to - from + 1 },32 (_, index) => `line ${from + index}`33 ).join("\n")}\n`;3435const thirty = lines(1, 30);3637const changed = decode({38 after: "a".repeat(40),39 before: "b".repeat(40),40 binary: false,41 newText: thirty.replace("line 15\n", "line fifteen\nline 15b\n"),42 oldText: thirty,43 path: "src/parse.ts",44 status: "modified",45});4647describe("a changed file's diff", () => {48 it("is parsed into the hunks the drawing draws, keyed by both blobs", () => {49 const diff = fileDiffOf(changed);5051 expect(Option.map(diff, ({ hunks }) => hunks.length)).toStrictEqual(52 Option.some(1)53 );54 expect(Option.map(diff, ({ cacheKey }) => cacheKey)).toStrictEqual(55 Option.some(`diff:${"b".repeat(40)}:${"a".repeat(40)}`)56 );57 expect(countsOf(diff)).toStrictEqual({ additions: 2, deletions: 1 });58 });5960 it("reads as unified rows: the unchanged lines skipped before and after, each line numbered on its side", () => {61 const rows = Option.match(fileDiffOf(changed), {62 onNone: () => [],63 onSome: unifiedRows,64 });6566 expect(rows[0]).toStrictEqual({ kind: "separator", lines: 10 });67 expect(rows.at(-1)).toStrictEqual({ kind: "separator", lines: 11 });68 expect(69 rows.filter((row) => row.kind !== "separator" && row.kind !== "context")70 ).toStrictEqual([71 { kind: "deletion", number: 15, text: "line 15" },72 { kind: "addition", number: 15, text: "line fifteen" },73 { kind: "addition", number: 16, text: "line 15b" },74 ]);75 });7677 it("reads as split rows: the deletion beside the first addition, the second against nothing", () => {78 const rows = Option.match(fileDiffOf(changed), {79 onNone: () => [],80 onSome: splitRows,81 });8283 const pairs = rows.filter(84 (row) =>85 row.kind === "pair" &&86 (row.left?.kind !== "context" || row.right === undefined)87 );8889 expect(pairs).toStrictEqual([90 {91 kind: "pair",92 left: { kind: "deletion", number: 15, text: "line 15" },93 right: { kind: "addition", number: 15, text: "line fifteen" },94 },95 {96 kind: "pair",97 right: { kind: "addition", number: 16, text: "line 15b" },98 },99 ]);100 });101102 it("has no lines for a binary file, or one whose text was not sent", () => {103 expect(104 fileDiffOf(decode({ binary: true, path: "logo.png", status: "added" }))105 ).toStrictEqual(Option.none());106 expect(107 fileDiffOf(108 decode({ binary: false, path: "big.json", status: "modified" })109 )110 ).toStrictEqual(Option.none());111 });112});113114describe("a commit's files", () => {115 const files = [116 decode({117 binary: false,118 newText: lines(1, 81),119 path: "data/zones.ts",120 status: "added",121 }),122 decode({123 binary: false,124 oldText: lines(1, 80),125 path: "data/old.ts",126 status: "deleted",127 }),128 changed,129 decode({130 binary: false,131 newText: "it\n",132 path: "test/parse.test.ts",133 status: "added",134 }),135 decode({136 binary: false,137 newText: "x\n",138 path: "README.md",139 status: "added",140 }),141 ];142143 it("are listed in the tree's order: folders before files, then by name", () => {144 expect(drawnFiles(files).map(({ file }) => file.path)).toStrictEqual([145 "data/old.ts",146 "data/zones.ts",147 "src/parse.ts",148 "test/parse.test.ts",149 "README.md",150 ]);151 expect(["b", "a/z", "a/b/c"].toSorted(treeOrder)).toStrictEqual([152 "a/b/c",153 "a/z",154 "b",155 ]);156 });157158 it("start folded when added or deleted past 80 lines", () => {159 expect(160 drawnFiles(files)161 .filter(({ folded }) => folded)162 .map(({ file }) => file.path)163 ).toStrictEqual(["data/zones.ts"]);164 });165166 it("count by path: the ask's territory, its tests, the rest", () => {167 const stats = statsOf(drawnFiles(files), ["src/**"]);168169 expect([stats.files, stats.additions, stats.deletions]).toStrictEqual([170 5, 85, 81,171 ]);172 expect(stats.parts).toStrictEqual([173 { additions: 2, deletions: 1, files: 1, label: "territory" },174 { additions: 1, deletions: 0, files: 1, label: "tests" },175 { additions: 82, deletions: 80, files: 3, label: "other" },176 ]);177 });178179 it("read a territory's globs as git paths", () => {180 expect(globTest("src/**")("src/a/b.ts")).toBeTruthy();181 expect(globTest("src/*.ts")("src/a/b.ts")).toBeFalsy();182 expect(globTest("packages/parse")("packages/parse/index.ts")).toBeTruthy();183 expect(globTest("**/*.ts")("a.ts")).toBeTruthy();184 });185186 it("know a test file by its folder or its name", () => {187 expect(isTestPath("src/parse.test.ts")).toBeTruthy();188 expect(isTestPath("test/asks/x.ts")).toBeTruthy();189 expect(isTestPath("src/testing.ts")).toBeFalsy();190 });191});192193describe("the keys between files", () => {194 const paths = ["a.ts", "b.ts", "c.ts"];195196 it("move from the page's own file, and stop at either end", () => {197 expect(stepFrom(paths, "b.ts", 1)).toBe("c.ts");198 expect(stepFrom(paths, "b.ts", -1)).toBe("a.ts");199 expect(stepFrom(paths, "c.ts", 1)).toBe("c.ts");200 expect(stepFrom(paths, "a.ts", -1)).toBe("a.ts");201 });202203 it("start at the first file, or the last going back, from none", () => {204 expect(stepFrom(paths, undefined, 1)).toBe("a.ts");205 expect(stepFrom(paths, undefined, -1)).toBe("c.ts");206 expect(stepFrom([], undefined, 1)).toBeUndefined();207 });208});209210describe("the permalink key", () => {211 const press = {212 altKey: false,213 ctrlKey: false,214 inField: false,215 key: "y",216 metaKey: false,217 };218219 it("takes y to the page at its commit while the single keys are on", () => {220 expect(permalinkPress(press, true)).toBeTruthy();221 });222223 it("does nothing once the single keys are switched off, in a field, or with a modifier", () => {224 expect([225 permalinkPress(press, false),226 permalinkPress({ ...press, inField: true }, true),227 permalinkPress({ ...press, ctrlKey: true }, true),228 permalinkPress({ ...press, key: "Y" }, true),229 ]).toStrictEqual([false, false, false, false]);230 });231});