Skip to content
Myr

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 file

Take it

Merge build/1211-code-browseroscabrielbefore adoptiona6808d3

lines 1–223why, one step back
1import { describe, expect, it } from "@effect/vitest";
2import { ChangedFile } from "@repo/protocol/domain/code";3import { Option, Schema } from "effect";45import {6 countsOf,7 drawnFiles,8 fileDiffOf,9 globTest,10 isTestPath,11 splitRows,12 statsOf,13 treeOrder,14 unifiedRows,15} from "../src/features/code/diff-model.ts";16import { permalinkPress } from "../src/features/code/file-header.tsx";17import { stepFrom } from "../src/features/code/file-keys.ts";1819const decode = Schema.decodeSync(ChangedFile);2021const lines = (from: number, to: number) =>22 `${Array.from(23 { length: to - from + 1 },24 (_, index) => `line ${from + index}`25 ).join("\n")}\n`;2627const thirty = lines(1, 30);2829const changed = decode({30 after: "a".repeat(40),31 before: "b".repeat(40),32 binary: false,33 newText: thirty.replace("line 15\n", "line fifteen\nline 15b\n"),34 oldText: thirty,35 path: "src/parse.ts",36 status: "modified",37});3839describe("a changed file's diff", () => {40 it("is parsed into the hunks the drawing draws, keyed by both blobs", () => {41 const diff = fileDiffOf(changed);4243 expect(Option.map(diff, ({ hunks }) => hunks.length)).toStrictEqual(44 Option.some(1)45 );46 expect(Option.map(diff, ({ cacheKey }) => cacheKey)).toStrictEqual(47 Option.some(`diff:${"b".repeat(40)}:${"a".repeat(40)}`)48 );49 expect(countsOf(diff)).toStrictEqual({ additions: 2, deletions: 1 });50 });5152 it("reads as unified rows: the unchanged lines skipped before and after, each line numbered on its side", () => {53 const rows = Option.match(fileDiffOf(changed), {54 onNone: () => [],55 onSome: unifiedRows,56 });5758 expect(rows[0]).toStrictEqual({ kind: "separator", lines: 10 });59 expect(rows.at(-1)).toStrictEqual({ kind: "separator", lines: 11 });60 expect(61 rows.filter((row) => row.kind !== "separator" && row.kind !== "context")62 ).toStrictEqual([63 { kind: "deletion", number: 15, text: "line 15" },64 { kind: "addition", number: 15, text: "line fifteen" },65 { kind: "addition", number: 16, text: "line 15b" },66 ]);67 });6869 it("reads as split rows: the deletion beside the first addition, the second against nothing", () => {70 const rows = Option.match(fileDiffOf(changed), {71 onNone: () => [],72 onSome: splitRows,73 });7475 const pairs = rows.filter(76 (row) =>77 row.kind === "pair" &&78 (row.left?.kind !== "context" || row.right === undefined)79 );8081 expect(pairs).toStrictEqual([82 {83 kind: "pair",84 left: { kind: "deletion", number: 15, text: "line 15" },85 right: { kind: "addition", number: 15, text: "line fifteen" },86 },87 {88 kind: "pair",89 right: { kind: "addition", number: 16, text: "line 15b" },90 },91 ]);92 });9394 it("has no lines for a binary file, or one whose text was not sent", () => {95 expect(96 fileDiffOf(decode({ binary: true, path: "logo.png", status: "added" }))97 ).toStrictEqual(Option.none());98 expect(99 fileDiffOf(100 decode({ binary: false, path: "big.json", status: "modified" })101 )102 ).toStrictEqual(Option.none());103 });104});105106describe("a commit's files", () => {107 const files = [108 decode({109 binary: false,110 newText: lines(1, 81),111 path: "data/zones.ts",112 status: "added",113 }),114 decode({115 binary: false,116 oldText: lines(1, 80),117 path: "data/old.ts",118 status: "deleted",119 }),120 changed,121 decode({122 binary: false,123 newText: "it\n",124 path: "test/parse.test.ts",125 status: "added",126 }),127 decode({128 binary: false,129 newText: "x\n",130 path: "README.md",131 status: "added",132 }),133 ];134135 it("are listed in the tree's order: folders before files, then by name", () => {136 expect(drawnFiles(files).map(({ file }) => file.path)).toStrictEqual([137 "data/old.ts",138 "data/zones.ts",139 "src/parse.ts",140 "test/parse.test.ts",141 "README.md",142 ]);143 expect(["b", "a/z", "a/b/c"].toSorted(treeOrder)).toStrictEqual([144 "a/b/c",145 "a/z",146 "b",147 ]);148 });149150 it("start folded when added or deleted past 80 lines", () => {151 expect(152 drawnFiles(files)153 .filter(({ folded }) => folded)154 .map(({ file }) => file.path)155 ).toStrictEqual(["data/zones.ts"]);156 });157158 it("count by path: the ask's territory, its tests, the rest", () => {159 const stats = statsOf(drawnFiles(files), ["src/**"]);160161 expect([stats.files, stats.additions, stats.deletions]).toStrictEqual([162 5, 85, 81,163 ]);164 expect(stats.parts).toStrictEqual([165 { additions: 2, deletions: 1, files: 1, label: "territory" },166 { additions: 1, deletions: 0, files: 1, label: "tests" },167 { additions: 82, deletions: 80, files: 3, label: "other" },168 ]);169 });170171 it("read a territory's globs as git paths", () => {172 expect(globTest("src/**")("src/a/b.ts")).toBeTruthy();173 expect(globTest("src/*.ts")("src/a/b.ts")).toBeFalsy();174 expect(globTest("packages/parse")("packages/parse/index.ts")).toBeTruthy();175 expect(globTest("**/*.ts")("a.ts")).toBeTruthy();176 });177178 it("know a test file by its folder or its name", () => {179 expect(isTestPath("src/parse.test.ts")).toBeTruthy();180 expect(isTestPath("test/asks/x.ts")).toBeTruthy();181 expect(isTestPath("src/testing.ts")).toBeFalsy();182 });183});184185describe("the keys between files", () => {186 const paths = ["a.ts", "b.ts", "c.ts"];187188 it("move from the page's own file, and stop at either end", () => {189 expect(stepFrom(paths, "b.ts", 1)).toBe("c.ts");190 expect(stepFrom(paths, "b.ts", -1)).toBe("a.ts");191 expect(stepFrom(paths, "c.ts", 1)).toBe("c.ts");192 expect(stepFrom(paths, "a.ts", -1)).toBe("a.ts");193 });194195 it("start at the first file, or the last going back, from none", () => {196 expect(stepFrom(paths, undefined, 1)).toBe("a.ts");197 expect(stepFrom(paths, undefined, -1)).toBe("c.ts");198 expect(stepFrom([], undefined, 1)).toBeUndefined();199 });200});201202describe("the permalink key", () => {203 const press = {204 altKey: false,205 ctrlKey: false,206 inField: false,207 key: "y",208 metaKey: false,209 };210211 it("takes y to the page at its commit while the single keys are on", () => {212 expect(permalinkPress(press, true)).toBeTruthy();213 });214215 it("does nothing once the single keys are switched off, in a field, or with a modifier", () => {216 expect([217 permalinkPress(press, false),218 permalinkPress({ ...press, inField: true }, true),219 permalinkPress({ ...press, ctrlKey: true }, true),220 permalinkPress({ ...press, key: "Y" }, true),221 ]).toStrictEqual([false, false, false, false]);222 });223});