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
Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 1–284why, one step back1import { describe, expect, it } from "@effect/vitest";2import { RepoHostMemory } from "@repo/artifacts/repo-host/memory";3import { PROJECT_DESCRIPTION_CAP } from "@repo/protocol/domain/front-page";4import type { ProjectListing } from "@repo/protocol/domain/front-page";5import { keyOf, LandedSha, ProjectName, RepoName } from "@repo/protocol/ids";6import { DateTime, Effect, Layer, Logger, Option, Ref } from "effect";78import type { CodeProject } from "../src/code.ts";9import { descriptionOf, withDescriptions } from "../src/description.ts";10import type { LandedProject } from "../src/description.ts";11import { ProjectDescriptionsMemory } from "../src/description/memory.ts";12import { Content, writer } from "./code-fixtures.ts";1314const lines = (...text: readonly string[]) => text.join("\n");1516describe(descriptionOf, () => {17 it("is the README's first paragraph under its title", () => {18 expect(19 descriptionOf(20 lines(21 "# datekit",22 "",23 "Small, dependency-free date helpers: parse, format, add and diff.",24 "",25 "A fictional project: it and its people are invented.",26 ""27 )28 )29 ).toStrictEqual(30 Option.some(31 "Small, dependency-free date helpers: parse, format, add and diff."32 )33 );34 });3536 it("passes over a centred banner, badges and images to the first prose", () => {37 expect(38 descriptionOf(39 lines(40 '<p align="center">',41 ' <img src="logo.svg" alt="ky" width="200">',42 "</p>",43 "",44 "[](https://ci.example) ",45 "",46 "> Tiny and elegant HTTP client based on the [Fetch API](https://fetch.spec.whatwg.org)",47 ""48 )49 )50 ).toStrictEqual(51 Option.some("Tiny and elegant HTTP client based on the Fetch API")52 );53 });5455 it("reads a wrapped paragraph as one line, its markup as plain text", () => {56 expect(57 descriptionOf(58 lines(59 "Click",60 "=====",61 "",62 "Click is a **Python** package for creating",63 "beautiful command line interfaces with `as little code`",64 "as necessary.<br>",65 "",66 "```sh",67 "pip install click",68 "```"69 )70 )71 ).toStrictEqual(72 Option.some(73 "Click is a Python package for creating beautiful command line interfaces with as little code as necessary."74 )75 );76 });7778 it("cuts a long paragraph at a word, within the cap", () => {79 const long = descriptionOf(80 `${"A word that repeats ".repeat(20)}until the end.`81 ).pipe(Option.getOrThrow);8283 expect(long.length).toBeLessThanOrEqual(PROJECT_DESCRIPTION_CAP);84 expect(long).toMatch(/^A word that repeats .+ repeats…$/u);85 });8687 it("cuts a long run with no space by whole characters, never half of one", () => {88 // The cap falls just after a whole pair, which a cut of one code unit would split.89 const cut = descriptionOf(`ab${"\u{1F501}".repeat(100)}`).pipe(90 Option.getOrThrow91 );9293 expect(cut.length).toBeLessThanOrEqual(PROJECT_DESCRIPTION_CAP);94 expect(cut).not.toMatch(/\p{Cs}/u);95 expect(cut).toMatch(/^ab(?:\u{1F501})+…$/u);96 });9798 it("is none for a README of headings, badges and code alone", () => {99 expect(100 descriptionOf(101 lines(102 "# empty",103 "",104 "[](https://ci.example)",105 "",106 "## Install",107 "",108 " npm install empty",109 "",110 "---"111 )112 )113 ).toStrictEqual(Option.none());114 });115});116117const datekit = ProjectName.make("alice/datekit");118119const burrow = ProjectName.make("alice/burrow");120121const landedAt = (iso: string) => DateTime.makeUnsafe(iso);122123const listingOf = (project: ProjectName, landed?: string): ProjectListing =>124 landed === undefined125 ? { openAsks: 1, posture: "open", project }126 : { landedAt: landedAt(landed), openAsks: 1, posture: "open", project };127128/** A repo whose main holds one README and three landings, read through a record that counts its reads. */129const withReadme = (project: ProjectName, readme: string) =>130 Effect.gen(function* () {131 const git = yield* writer;132 const repo = RepoName.make(keyOf(project));133134 const sha = yield* git.commit(135 "Start",136 [["README.md", Content.Text({ text: readme })]],137 [],138 1139 );140141 yield* git.push(repo, [["refs/heads/main", sha]]);142143 const reads = yield* Ref.make(0);144145 const code: CodeProject = {146 forks: Effect.succeed([]),147 head: Effect.as(148 Ref.update(reads, (n) => n + 1),149 Option.some(LandedSha.make(sha))150 ),151 landings: () => Effect.succeed([]),152 project,153 repo,154 };155156 const landed: LandedProject = { code, landingCount: Effect.succeed(3) };157158 return { landed, reads };159 });160161const described = (listed: readonly ProjectListing[]) =>162 Object.fromEntries(163 listed.map(({ description, landings, project }) => [164 project,165 { description, landings },166 ])167 );168169describe(withDescriptions, () => {170 it.effect(171 "gives each landed project its README's first paragraph and its count of landings, and one not yet landed neither",172 () =>173 Effect.gen(function* () {174 const { landed } = yield* withReadme(175 datekit,176 "# datekit\n\nDate helpers.\n"177 );178179 const listed = yield* withDescriptions(180 [listingOf(datekit, "2026-10-12T12:00:00Z"), listingOf(burrow)],181 (project) =>182 project === datekit183 ? Effect.succeed(landed)184 : Effect.die("never read")185 );186187 expect(described(listed)).toStrictEqual({188 [burrow]: { description: undefined, landings: undefined },189 [datekit]: { description: "Date helpers.", landings: 3 },190 });191 }).pipe(Effect.provide(RepoHostMemory))192 );193194 it.effect(195 "reads a project once while its last landing stands, and again after the next",196 () =>197 Effect.gen(function* () {198 const { landed, reads } = yield* withReadme(199 datekit,200 "# datekit\n\nDate helpers.\n"201 );202203 const codeOf = () => Effect.succeed(landed);204205 yield* withDescriptions(206 [listingOf(datekit, "2026-10-12T12:00:00Z")],207 codeOf208 );209210 const kept = yield* withDescriptions(211 [listingOf(datekit, "2026-10-12T12:00:00Z")],212 codeOf213 );214215 const readOnce = yield* Ref.get(reads);216217 yield* withDescriptions(218 [listingOf(datekit, "2026-10-12T13:00:00Z")],219 codeOf220 );221222 expect(described(kept)).toStrictEqual({223 [datekit]: { description: "Date helpers.", landings: 3 },224 });225 expect([readOnce, yield* Ref.get(reads)]).toStrictEqual([1, 2]);226 }).pipe(227 Effect.provide(Layer.merge(RepoHostMemory, ProjectDescriptionsMemory))228 )229 );230231 it.effect(232 "lists a project whose record cannot be reached, with no description and no count",233 () =>234 Effect.gen(function* () {235 const listed = yield* withDescriptions(236 [listingOf(datekit, "2026-10-12T12:00:00Z")],237 () => Effect.die("the record is gone")238 ).pipe(Effect.provide(Logger.layer([])));239240 expect(listed).toStrictEqual([241 listingOf(datekit, "2026-10-12T12:00:00Z"),242 ]);243 }).pipe(Effect.provide(RepoHostMemory))244 );245246 it.effect(247 "lists a project whose landings cannot be counted with neither, and keeps nothing",248 () =>249 Effect.gen(function* () {250 const { landed, reads } = yield* withReadme(251 datekit,252 "# datekit\n\nDate helpers.\n"253 );254255 const uncounted: LandedProject = {256 ...landed,257 landingCount: Effect.die("the record's storage is down"),258 };259260 const first = yield* withDescriptions(261 [listingOf(datekit, "2026-10-12T12:00:00Z")],262 () => Effect.succeed(uncounted)263 );264265 yield* withDescriptions(266 [listingOf(datekit, "2026-10-12T12:00:00Z")],267 () => Effect.succeed(uncounted)268 );269270 expect(first).toStrictEqual([271 listingOf(datekit, "2026-10-12T12:00:00Z"),272 ]);273 expect(yield* Ref.get(reads)).toBe(2);274 }).pipe(275 Effect.provide(276 Layer.mergeAll(277 RepoHostMemory,278 ProjectDescriptionsMemory,279 Logger.layer([])280 )281 )282 )283 );284});