Skip to content
Myr

code-reads.test.ts

This file

Path

apps/platform/test/code-reads.test.ts

209 lines · 6.4 KiB · TypeScript

Last changed by

Merge redesign into mainoscabrielbefore adoptioncaedd21

History of this file

Take it

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 1–2why, one step back
1import { describe, expect, it } from "@effect/vitest";
2import { Contracts } from "@repo/protocol/contracts";

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 3–4why, one step back
3import { AskId, Handle, RepoName, RepoPath } from "@repo/protocol/ids";
4import { UnknownRevision } from "@repo/protocol/refusal";

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 5–20why, one step back
5import { advanceTo } from "@repo/testkit/clock";
6import { aProject, withAsk, withVouches } from "@repo/testkit/scenarios";7import { Effect, Layer, SchemaAST } from "effect";89import { Handlers } from "../src/handlers.ts";10import { MemorySessions } from "../src/sessions/memory.ts";11import { seeded, worldOf } from "./handlers.fixtures.ts";12import { origin, rpcClient, served } from "./harness.ts";1314const datekit = aProject({ people: ["bob"] }).pipe(15 withVouches(["bob"]),16 withAsk({ id: "parse-iso", territory: "packages/parser" })17);1819const { project } = datekit;20

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 21–22why, one step back
21const BOB = "bob-session";
22

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 23–35why, one step back
23const platform = Effect.gen(function* () {
24 yield* advanceTo("2026-10-07T12:00:00Z");2526 const world = yield* Layer.build(worldOf([datekit]));2728 yield* seeded(datekit).pipe(Effect.provide(world));2930 const ports = Layer.succeedContext(world);3132 return yield* served(33 Layer.mergeAll(34 Handlers.pipe(Layer.provide(ports)),35 ports,

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 36why, one step back
36 MemorySessions.layer({ [BOB]: Handle.make("bob") })

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 37–116why, one step back
37 )
38 );39});4041const codeReads = [42 "listRefs",43 "readTree",44 "listPaths",45 "readFile",46 "log",47 "showCommit",48 "why",49];5051const fieldsIn = (52 ast: SchemaAST.AST53): readonly (readonly [string, SchemaAST.AST])[] => {54 if (SchemaAST.isObjects(ast)) {55 return ast.propertySignatures.flatMap(({ name, type }) => [56 [String(name), type] as const,57 ...fieldsIn(type),58 ]);59 }6061 return SchemaAST.isUnion(ast) ? ast.types.flatMap(fieldsIn) : [];62};6364describe("the code reads over /rpc", () => {65 it.effect(66 "answer anyone with datekit's refs, its root folder and a file",67 () =>68 Effect.gen(function* () {69 const { handler } = yield* platform;70 const anyone = yield* rpcClient(handler);71 const refs = yield* anyone.listRefs({ project });72 const root = yield* anyone.readTree({ project });7374 const file = yield* anyone.readFile({75 path: RepoPath.make("package.json"),76 project,77 });7879 expect([refs.project, refs.tags]).toStrictEqual([project, []]);80 expect(root.at).toBe(refs.landed);81 expect(root.entries.map(({ name }) => name)).toStrictEqual(82 expect.arrayContaining(["packages", "package.json"])83 );84 expect(file.text).toContain('"name": "datekit"');85 })86 );8788 it.effect("answer anyone with datekit's paths, log and first commit", () =>89 Effect.gen(function* () {90 const { handler } = yield* platform;91 const anyone = yield* rpcClient(handler);92 const { landed } = yield* anyone.listRefs({ project });93 const { commits } = yield* anyone.log({ project });94 const first = commits.at(-1)?.sha ?? "main";95 const paths = yield* anyone.listPaths({ at: first, project });9697 expect([commits[0]?.sha, commits.at(-1)?.subject]).toStrictEqual([98 landed,99 "Start",100 ]);101 expect(paths.paths).toContain("packages/parser/src/parse.ts");102103 const diff = yield* anyone.showCommit({ project, sha: paths.at });104105 expect(106 diff.files.map(({ path, status }) => [path, status])107 ).toStrictEqual(paths.paths.map((path) => [path, "added"]));108 })109 );110111 it.effect(112 "read the canonical repo whatever extra key a payload carries",113 () =>114 Effect.gen(function* () {115 const { handler } = yield* platform;116

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

lines 117–120why, one step back
117 const payload = JSON.stringify({
118 project,119 repo: "alice__datekit--me--alice",120 });

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 121–136why, one step back
121
122 const response = yield* Effect.promise(() =>123 handler(124 new Request(`${origin}/rpc`, {125 body: `{"_tag":"Request","id":"1","tag":"readTree","payload":${payload},"headers":[]}`,126 headers: { "content-type": "application/json" },127 method: "POST",128 })129 )130 );131132 const text = yield* Effect.promise(() => response.text());133 const anyone = yield* rpcClient(handler);134 const canonical = yield* anyone.readTree({ project });135136 expect(response.status).toBe(200);

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 137why, one step back
137 expect(text).not.toContain("alice__datekit--me--alice");

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 138why, one step back
138 expect(text).toContain(canonical.at);

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 139–177why, one step back
139 })
140 );141142 it.effect(143 "read bob's claim fork at its address, and no repo the project does not list as its fork",144 () =>145 Effect.gen(function* () {146 const { handler } = yield* platform;147 const anyone = yield* rpcClient(handler);148 const bob = yield* rpcClient(handler, BOB);149150 yield* bob.claim({ ask: AskId.make("parse-iso"), project });151152 const { landed } = yield* anyone.listRefs({ project });153 const tree = yield* anyone.readTree({ at: "@bob/parse-iso", project });154155 const { commit } = yield* anyone.showCommit({156 at: "@bob/parse-iso",157 project,158 sha: tree.at,159 });160161 const refused = [162 "nonsense",163 "@carol/parse-iso",164 "alice__datekit--parse-iso--bob",165 ];166167 expect([tree.at, commit.sha]).toStrictEqual([landed, landed]);168 expect(169 yield* Effect.forEach(refused, (at) =>170 Effect.flip(anyone.readTree({ at, project }))171 )172 ).toStrictEqual(refused.map((rev) => new UnknownRevision({ rev })));173 expect(174 yield* Effect.forEach(refused, (at) =>175 Effect.flip(anyone.showCommit({ at, project, sha: tree.at }))176 )177 ).toStrictEqual(refused.map((rev) => new UnknownRevision({ rev })));

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 178–209why, one step back
178 })
179 );180181 it("name no repo in any code read's payload", () => {182 const rpcs = codeReads.map(183 (tag) => [tag, Contracts.requests.get(tag)] as const184 );185186 const fields = rpcs.flatMap(([tag, rpc]) =>187 rpc === undefined188 ? []189 : fieldsIn(rpc.payloadSchema.ast).map(190 ([name, type]) => [tag, name, type] as const191 )192 );193194 expect(195 rpcs.flatMap(([tag, rpc]) => (rpc === undefined ? [tag] : []))196 ).toStrictEqual([]);197 expect(198 fields.some(199 ([tag, name, type]) =>200 tag === "readFile" && name === "path" && type === RepoPath.ast201 )202 ).toBeTruthy();203 expect(204 fields.flatMap(([tag, name, type]) =>205 type === RepoName.ast || name === "repo" ? [`${tag}.${name}`] : []206 )207 ).toStrictEqual([]);208 });209});