description.test.ts
This file
Path
packages/project/test/description.test.ts
284 lines · 8.4 KiB · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285import { describe, expect, it } from "@effect/vitest";import { RepoHostMemory } from "@repo/artifacts/repo-host/memory";import { PROJECT_DESCRIPTION_CAP } from "@repo/protocol/domain/front-page";import type { ProjectListing } from "@repo/protocol/domain/front-page";import { keyOf, LandedSha, ProjectName, RepoName } from "@repo/protocol/ids";import { DateTime, Effect, Layer, Logger, Option, Ref } from "effect";
import type { CodeProject } from "../src/code.ts";import { descriptionOf, withDescriptions } from "../src/description.ts";import type { LandedProject } from "../src/description.ts";import { ProjectDescriptionsMemory } from "../src/description/memory.ts";import { Content, writer } from "./code-fixtures.ts";
const lines = (...text: readonly string[]) => text.join("\n");
describe(descriptionOf, () => { it("is the README's first paragraph under its title", () => { expect( descriptionOf( lines( "# datekit", "", "Small, dependency-free date helpers: parse, format, add and diff.", "", "A fictional project: it and its people are invented.", "" ) ) ).toStrictEqual( Option.some( "Small, dependency-free date helpers: parse, format, add and diff." ) ); });
it("passes over a centred banner, badges and images to the first prose", () => { expect( descriptionOf( lines( '<p align="center">', ' <img src="logo.svg" alt="ky" width="200">', "</p>", "", "[](https://ci.example) ", "", "> Tiny and elegant HTTP client based on the [Fetch API](https://fetch.spec.whatwg.org)", "" ) ) ).toStrictEqual( Option.some("Tiny and elegant HTTP client based on the Fetch API") ); });
it("reads a wrapped paragraph as one line, its markup as plain text", () => { expect( descriptionOf( lines( "Click", "=====", "", "Click is a **Python** package for creating", "beautiful command line interfaces with `as little code`", "as necessary.<br>", "", "```sh", "pip install click", "```" ) ) ).toStrictEqual( Option.some( "Click is a Python package for creating beautiful command line interfaces with as little code as necessary." ) ); });
it("cuts a long paragraph at a word, within the cap", () => { const long = descriptionOf( `${"A word that repeats ".repeat(20)}until the end.` ).pipe(Option.getOrThrow);
expect(long.length).toBeLessThanOrEqual(PROJECT_DESCRIPTION_CAP); expect(long).toMatch(/^A word that repeats .+ repeats…$/u); });
it("cuts a long run with no space by whole characters, never half of one", () => { // The cap falls just after a whole pair, which a cut of one code unit would split. const cut = descriptionOf(`ab${"\u{1F501}".repeat(100)}`).pipe( Option.getOrThrow );
expect(cut.length).toBeLessThanOrEqual(PROJECT_DESCRIPTION_CAP); expect(cut).not.toMatch(/\p{Cs}/u); expect(cut).toMatch(/^ab(?:\u{1F501})+…$/u); });
it("is none for a README of headings, badges and code alone", () => { expect( descriptionOf( lines( "# empty", "", "[](https://ci.example)", "", "## Install", "", " npm install empty", "", "---" ) ) ).toStrictEqual(Option.none()); });});
const datekit = ProjectName.make("alice/datekit");
const burrow = ProjectName.make("alice/burrow");
const landedAt = (iso: string) => DateTime.makeUnsafe(iso);
const listingOf = (project: ProjectName, landed?: string): ProjectListing => landed === undefined ? { openAsks: 1, posture: "open", project } : { landedAt: landedAt(landed), openAsks: 1, posture: "open", project };
/** A repo whose main holds one README and three landings, read through a record that counts its reads. */const withReadme = (project: ProjectName, readme: string) => Effect.gen(function* () { const git = yield* writer; const repo = RepoName.make(keyOf(project));
const sha = yield* git.commit( "Start", [["README.md", Content.Text({ text: readme })]], [], 1 );
yield* git.push(repo, [["refs/heads/main", sha]]);
const reads = yield* Ref.make(0);
const code: CodeProject = { forks: Effect.succeed([]), head: Effect.as( Ref.update(reads, (n) => n + 1), Option.some(LandedSha.make(sha)) ), landings: () => Effect.succeed([]), project, repo, };
const landed: LandedProject = { code, landingCount: Effect.succeed(3) };
return { landed, reads }; });
const described = (listed: readonly ProjectListing[]) => Object.fromEntries( listed.map(({ description, landings, project }) => [ project, { description, landings }, ]) );
describe(withDescriptions, () => { it.effect( "gives each landed project its README's first paragraph and its count of landings, and one not yet landed neither", () => Effect.gen(function* () { const { landed } = yield* withReadme( datekit, "# datekit\n\nDate helpers.\n" );
const listed = yield* withDescriptions( [listingOf(datekit, "2026-10-12T12:00:00Z"), listingOf(burrow)], (project) => project === datekit ? Effect.succeed(landed) : Effect.die("never read") );
expect(described(listed)).toStrictEqual({ [burrow]: { description: undefined, landings: undefined }, [datekit]: { description: "Date helpers.", landings: 3 }, }); }).pipe(Effect.provide(RepoHostMemory)) );
it.effect( "reads a project once while its last landing stands, and again after the next", () => Effect.gen(function* () { const { landed, reads } = yield* withReadme( datekit, "# datekit\n\nDate helpers.\n" );
const codeOf = () => Effect.succeed(landed);
yield* withDescriptions( [listingOf(datekit, "2026-10-12T12:00:00Z")], codeOf );
const kept = yield* withDescriptions( [listingOf(datekit, "2026-10-12T12:00:00Z")], codeOf );
const readOnce = yield* Ref.get(reads);
yield* withDescriptions( [listingOf(datekit, "2026-10-12T13:00:00Z")], codeOf );
expect(described(kept)).toStrictEqual({ [datekit]: { description: "Date helpers.", landings: 3 }, }); expect([readOnce, yield* Ref.get(reads)]).toStrictEqual([1, 2]); }).pipe( Effect.provide(Layer.merge(RepoHostMemory, ProjectDescriptionsMemory)) ) );
it.effect( "lists a project whose record cannot be reached, with no description and no count", () => Effect.gen(function* () { const listed = yield* withDescriptions( [listingOf(datekit, "2026-10-12T12:00:00Z")], () => Effect.die("the record is gone") ).pipe(Effect.provide(Logger.layer([])));
expect(listed).toStrictEqual([ listingOf(datekit, "2026-10-12T12:00:00Z"), ]); }).pipe(Effect.provide(RepoHostMemory)) );
it.effect( "lists a project whose landings cannot be counted with neither, and keeps nothing", () => Effect.gen(function* () { const { landed, reads } = yield* withReadme( datekit, "# datekit\n\nDate helpers.\n" );
const uncounted: LandedProject = { ...landed, landingCount: Effect.die("the record's storage is down"), };
const first = yield* withDescriptions( [listingOf(datekit, "2026-10-12T12:00:00Z")], () => Effect.succeed(uncounted) );
yield* withDescriptions( [listingOf(datekit, "2026-10-12T12:00:00Z")], () => Effect.succeed(uncounted) );
expect(first).toStrictEqual([ listingOf(datekit, "2026-10-12T12:00:00Z"), ]); expect(yield* Ref.get(reads)).toBe(2); }).pipe( Effect.provide( Layer.mergeAll( RepoHostMemory, ProjectDescriptionsMemory, Logger.layer([]) ) ) ) );});