code-why.test.ts
This file
Path
packages/project/test/code-why.test.ts
347 lines · 10.1 KiB · TypeScript
Last changed by
Merge build/comments-project into mainoscabrielbefore adoption49efd2a
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348import { describe, expect, it } from "@effect/vitest";import { RepoHost } from "@repo/artifacts/repo-host";import { RepoHostMemory } from "@repo/artifacts/repo-host/memory";import type { LandingView } from "@repo/protocol/domain/landing";import { AskId, Handle, LandedSha, ProjectName, RepoName, keyOf, RepoPath,} from "@repo/protocol/ids";import type { CommitSha } from "@repo/protocol/ids";import { UnknownPath } from "@repo/protocol/refusal";import { DateTime, Effect, Exit, Layer, Option, Ref } from "effect";
import { whyOf } from "../src/code.ts";import type { CodeProject } from "../src/code.ts";import { CodeCacheLive } from "../src/code/cache.ts";import { READS_AT_ONCE } from "../src/code/tree.ts";import { whyView } from "../src/code/why.ts";import { Content, growingHistory, longHistory, png, overlappingTrees, writer,} from "./code-fixtures.ts";
const project = ProjectName.make("alice/datekit");
const repo = RepoName.make(keyOf(project));
const format = RepoPath.make("src/format.ts");
const main = Option.none();
const text = (value: string) => Content.Text({ text: value });
const landingOf = ( sha: CommitSha, ask: Option.Option<string>, parent: Option.Option<CommitSha>): LandingView => ({ assisted: [], author: Handle.make("bob"), credits: [], landedAt: DateTime.makeUnsafe("2026-10-07T12:00:00Z"), project, sha: LandedSha.make(sha), ...Option.match(ask, { onNone: () => ({}), onSome: (id) => ({ ask: AskId.make(id) }), }), ...Option.match(parent, { onNone: () => ({}), onSome: (at) => ({ parent: LandedSha.make(at) }), }),});
const history = Effect.gen(function* () { const git = yield* writer;
const adopted = yield* git.commit( "Adopt datekit", [ ["README.md", text("# datekit\n")], ["logo.png", Content.Bytes({ bytes: png })], ["src/format.ts", text("export const a = 1;\nexport const b = 2;\n")], ], [], 1 );
const readme = yield* git.commit( "Tidy the readme", [ ["README.md", text("# datekit, tidied\n")], ["logo.png", Content.Bytes({ bytes: png })], ["src/format.ts", text("export const a = 1;\nexport const b = 2;\n")], ], [adopted], 2 );
const iso = yield* git.commit( "Land format-iso", [ ["README.md", text("# datekit, tidied\n")], ["logo.png", Content.Bytes({ bytes: png })], [ "src/format.ts", text( "export const a = 1;\nexport const iso = 3;\nexport const b = 2;\n" ), ], ], [readme], 3 );
const week = yield* git.commit( "Land format-week", [ ["README.md", text("# datekit, tidied\n")], ["logo.png", Content.Bytes({ bytes: png })], [ "src/format.ts", text( "export const a = 1;\nexport const iso = 3;\nexport const b = 2;\nexport const week = 4;\n" ), ], ], [iso], 4 );
yield* git.push(repo, [["refs/heads/main", week]]);
const landings = [ landingOf(adopted, Option.none(), Option.none()), landingOf(iso, Option.some("format-iso"), Option.some(readme)), landingOf(week, Option.some("format-week"), Option.some(iso)), ];
const asked = yield* Ref.make<readonly (readonly CommitSha[])[]>([]);
const code: CodeProject = { forks: Effect.succeed([]), head: Effect.succeedSome(LandedSha.make(week)), landings: (shas) => Ref.update(asked, (all) => [...all, shas]).pipe( Effect.as( shas.flatMap((sha) => landings.filter((landing) => landing.sha === sha) ) ) ), project, repo, };
return { adopted, asked, code, iso, landings, readme, week };});
const countedBy = (reads: Ref.Ref<readonly string[]>) => Layer.effect( RepoHost, Effect.gen(function* () { const host = yield* RepoHost;
return RepoHost.of({ ...host, readBlob: (name, blob) => Ref.update(reads, (all) => [...all, blob]).pipe( Effect.andThen(host.readBlob(name, blob)) ), readFile: (name, at, file) => Ref.update(reads, (all) => [...all, file]).pipe( Effect.andThen(host.readFile(name, at, file)) ), }); }) ).pipe(Layer.provide(RepoHostMemory));
describe("why each line is there", () => { it.effect( "points each landing's lines at its landing, and the adopted lines at the adoption", () => Effect.gen(function* () { const { adopted, code, iso, week } = yield* history; const view = yield* whyOf(code, main, format);
expect([view.at, view.path, view.stoppedAt]).toStrictEqual([ week, format, undefined, ]); expect(view.ranges).toStrictEqual([ { end: 1, sha: adopted, start: 1 }, { end: 2, sha: iso, start: 2 }, { end: 3, sha: adopted, start: 3 }, { end: 4, sha: week, start: 4 }, ]); expect( view.commits.map(({ landing, sha, subject }) => [ sha, subject, landing, ]) ).toStrictEqual([ [week, "Land format-week", undefined], [iso, "Land format-iso", undefined], [adopted, "Adopt datekit", undefined], ]); }).pipe(Effect.provide(RepoHostMemory)) );
it.effect( "returns the landings among those commits, with their asks, from one batch of the record", () => Effect.gen(function* () { const { adopted, asked, code, iso, landings, week } = yield* history; const view = yield* whyOf(code, main, format);
expect(view.landings.map(({ ask, sha }) => [sha, ask])).toStrictEqual([ [week, "format-week"], [iso, "format-iso"], [adopted, undefined], ]); expect(view.landings).toStrictEqual(landings.toReversed()); expect(yield* Ref.get(asked)).toHaveLength(1); }).pipe(Effect.provide(RepoHostMemory)) );
it.effect( "stops at the cap, giving the lines it did not reach to the commit it stopped at", () => Effect.gen(function* () { const { code, week } = yield* history; const view = yield* whyView(code, main, format, 1);
expect(view.stoppedAt).toBe(week); expect(view.ranges).toStrictEqual([{ end: 4, sha: week, start: 1 }]); expect(view.commits.map(({ sha }) => sha)).toStrictEqual([week]); }).pipe(Effect.provide(RepoHostMemory)) );
it.effect( "reads no blob or file the second time: the view is kept by (at, path)", () => Effect.gen(function* () { const reads = yield* Ref.make<readonly string[]>([]);
yield* Effect.gen(function* () { const { code, iso } = yield* history; const first = yield* whyOf(code, Option.some(iso), format); const counted = (yield* Ref.get(reads)).length;
// The file read whole at `iso` and at its two first parents. expect(counted).toBe(3);
const again = yield* whyOf(code, Option.some(iso), format);
expect(again).toStrictEqual(first); expect(yield* Ref.get(reads)).toHaveLength(counted); }).pipe( Effect.provide(Layer.merge(countedBy(reads), CodeCacheLive(100))) ); }) );
it.live( "reads the file in each commit several at a time, not one after another", () => Effect.gen(function* () { const reading = yield* Ref.make(false); const peak = yield* Ref.make(0);
yield* Effect.gen(function* () { const { code, shas } = yield* longHistory(64);
yield* Ref.set(reading, true);
// `once.txt` never changes, so the walk reads it whole in all 64 // commits. const { ranges } = yield* whyView( code, main, RepoPath.make("once.txt") );
expect(ranges).toStrictEqual([{ end: 1, sha: shas[0], start: 1 }]); }).pipe(Effect.provide(overlappingTrees(reading, peak, "file")));
// `READS_AT_ONCE` lookups under way, and the head's own beside them. expect(yield* Ref.get(peak)).toBeGreaterThanOrEqual(32); expect(yield* Ref.get(peak)).toBeLessThanOrEqual(READS_AT_ONCE + 1); }), { timeout: 30_000 } );
it.live( "reads the file's versions several at a time, not one after another", () => Effect.gen(function* () { const reading = yield* Ref.make(false); const peak = yield* Ref.make(0);
yield* Effect.gen(function* () { const { code, shas } = yield* growingHistory(40);
yield* Ref.set(reading, true);
// Each of the 40 lines came with a commit of its own, so the // blame takes every older version of `grow.txt` from the walk's // reads of it. const { ranges } = yield* whyView( code, main, RepoPath.make("grow.txt") );
expect(ranges).toStrictEqual( shas.map((sha, at) => ({ end: at + 1, sha, start: at + 1 })) ); }).pipe(Effect.provide(overlappingTrees(reading, peak, "file")));
// `READS_AT_ONCE` lookups under way, and the head's own beside them. expect(yield* Ref.get(peak)).toBeGreaterThanOrEqual(32); expect(yield* Ref.get(peak)).toBeLessThanOrEqual(READS_AT_ONCE + 1); }), { timeout: 30_000 } );
it.effect("answers no ranges for a binary file, at its commit", () => Effect.gen(function* () { const { code, week } = yield* history;
expect(yield* whyOf(code, main, RepoPath.make("logo.png"))).toStrictEqual( { at: week, commits: [], landings: [], path: RepoPath.make("logo.png"), ranges: [], } ); }).pipe(Effect.provide(RepoHostMemory)) );
it.effect("is UnknownPath for a missing path or a folder", () => Effect.gen(function* () { const { code, week } = yield* history;
for (const missing of ["nope.ts", "src"]) { const path = RepoPath.make(missing);
expect(yield* Effect.exit(whyOf(code, main, path))).toStrictEqual( Exit.fail(new UnknownPath({ commit: week, path })) ); } }).pipe(Effect.provide(RepoHostMemory)) );});