Skip to content
Myr

folder.test.ts

This file

Path

apps/web/test/folder.test.ts

182 lines · 5.2 KiB · TypeScript

Last changed by

Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248

History of this file

Take it

Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215

lines 1–12why, one step back
1import { describe, expect, it } from "@effect/vitest";
2import { Client, Contracts } from "@repo/protocol/contracts";3import type { FileView, TreeListing } from "@repo/protocol/domain/code";4import { BlobSha, CommitSha, ProjectName, RepoPath } from "@repo/protocol/ids";5import { UnknownPath } from "@repo/protocol/refusal";6import { asTester, unanswered } from "@repo/protocol/testing";7import { Deferred, Duration, Effect, Layer, Option, Ref } from "effect";8import type { Types } from "effect";9import { RpcTest } from "effect/rpc";1011import { readFolderWithReadme } from "../src/server/folder.ts";12

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 13why, one step back
13const project = ProjectName.make("oscabriel/datekit");

Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215

lines 14–182why, one step back
14
15const landed = CommitSha.make("a".repeat(40));1617const moved = CommitSha.make("b".repeat(40));1819const fileAt = (path: string, at = landed): FileView => ({20 at,21 binary: false,22 path: RepoPath.make(path),23 sha: BlobSha.make("c".repeat(40)),24 size: 5,25 text: `# ${path}`,26 truncated: false,27});2829const folder = (readme?: string, path?: string): TreeListing => {30 const listing: Types.Mutable<TreeListing> = {31 at: landed,32 entries: [],33 };3435 if (path !== undefined) {36 listing.path = RepoPath.make(path);37 }3839 if (readme !== undefined) {40 listing.readme = {41 path: RepoPath.make(readme),42 sha: BlobSha.make("c".repeat(40)),43 };44 }4546 return listing;47};4849const platform = (input: {50 readonly asked: Ref.Ref<readonly string[]>;51 readonly files: ReadonlyMap<string, FileView>;52 readonly listing: TreeListing;53 readonly readStarted?: Deferred.Deferred<string>;54 readonly treeWaits?: Effect.Effect<void>;55}) =>56 Layer.effect(Client, RpcTest.makeClient(Contracts)).pipe(57 Layer.provide([58 Layer.mergeAll(59 unanswered,60 Contracts.toLayerHandler("readTree", () =>61 Effect.as(input.treeWaits ?? Effect.void, input.listing)62 ),63 Contracts.toLayerHandler("readFile", ({ path }) =>64 Effect.gen(function* () {65 yield* Ref.update(input.asked, (all) => [...all, path]);6667 if (input.readStarted !== undefined) {68 yield* Deferred.succeed(input.readStarted, path);69 }7071 const file = input.files.get(path);7273 return file ?? (yield* new UnknownPath({ commit: landed, path }));74 })75 )76 ),77 asTester,78 ])79 );8081const readWith = (82 input: Omit<Parameters<typeof platform>[0], "asked">,83 path?: string84) =>85 Effect.gen(function* () {86 const asked = yield* Ref.make<readonly string[]>([]);8788 const { readme } = yield* readFolderWithReadme({89 path: Option.map(Option.fromNullishOr(path), (at) => RepoPath.make(at)),90 project,91 rev: "main",92 }).pipe(Effect.provide(platform({ ...input, asked })));9394 return {95 asked: yield* Ref.get(asked),96 readme: Option.getOrUndefined(97 Option.map(98 readme,99 ({ at, path: found }) => `${found}@${at.slice(0, 1)}`100 )101 ),102 };103 });104105describe(readFolderWithReadme, () => {106 it.live("asks for the README while the folder is still being read", () =>107 Effect.gen(function* () {108 const readStarted = yield* Deferred.make<string>();109110 // The folder answers only once a README was asked for: read one after111 // the other, the read would never end.112 const done = yield* readWith({113 files: new Map([["README.md", fileAt("README.md")]]),114 listing: folder("README.md"),115 readStarted,116 treeWaits: Deferred.await(readStarted),117 }).pipe(Effect.timeoutOption(Duration.seconds(2)));118119 expect(Option.getOrUndefined(done)).toStrictEqual({120 asked: ["README.md"],121 readme: "README.md@a",122 });123 })124 );125126 it.effect("reads the README the folder names when it is not README.md", () =>127 Effect.gen(function* () {128 expect(129 yield* readWith({130 files: new Map([["readme.markdown", fileAt("readme.markdown")]]),131 listing: folder("readme.markdown"),132 })133 ).toStrictEqual({134 asked: ["README.md", "readme.markdown"],135 readme: "readme.markdown@a",136 });137 })138 );139140 it.effect(141 "reads the folder's README again when the guess was read at another commit",142 () =>143 Effect.gen(function* () {144 expect(145 yield* readWith({146 files: new Map([["README.md", fileAt("README.md", moved)]]),147 listing: folder("README.md"),148 })149 ).toStrictEqual({150 asked: ["README.md", "README.md"],151 readme: "README.md@b",152 });153 })154 );155156 it.effect("guesses the README inside the folder it reads", () =>157 Effect.gen(function* () {158 expect(159 yield* readWith(160 {161 files: new Map([162 ["packages/parse/README.md", fileAt("packages/parse/README.md")],163 ]),164 listing: folder("packages/parse/README.md", "packages/parse"),165 },166 "packages/parse"167 )168 ).toStrictEqual({169 asked: ["packages/parse/README.md"],170 readme: "packages/parse/README.md@a",171 });172 })173 );174175 it.effect("leaves a folder with no README without one", () =>176 Effect.gen(function* () {177 expect(178 yield* readWith({ files: new Map(), listing: folder() })179 ).toStrictEqual({ asked: ["README.md"], readme: undefined });180 })181 );182});