Skip to content
Myr

notes.test.ts

This file

Path

packages/git/test/notes.test.ts

275 lines · 7.7 KiB · TypeScript

Last changed by

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–53why, one step back
1import { NodeServices } from "@effect/platform-node";
2import { describe, expect, it } from "@effect/vitest";3import {4 ObjectId,5 RefName,6 RefOutcome,7 RepoHost,8 RepoName,9} from "@repo/artifacts/repo-host";10import { RepoHostMemory } from "@repo/artifacts/repo-host/memory";11import { memoryFs } from "@repo/artifacts/repo-host/memory-fs";12import { Brand } from "@repo/brand";13import { Verdict } from "@repo/protocol/domain/answer";14import { Outcome } from "@repo/protocol/domain/outcome";15import {16 AskId,17 CheckVersion,18 CommitSha,19 LaneName,20 PatchId,21 ProjectName,22 TreeSha,23} from "@repo/protocol/ids";24import { gitOk } from "@repo/testkit/git";25import { gitClient } from "@repo/testkit/git-client";26import { DateTime, Effect, FileSystem, Layer, Option, Schema } from "effect";2728import { noteCommit, notesRef } from "../src/notes.ts";29import type { ScratchSource } from "../src/notes.ts";30import { committerLine } from "../src/trailers.ts";3132const repo = RepoName.make("demo");3334const main = RefName.make("refs/heads/main");3536const verdictOn = (head: CommitSha) =>37 Verdict.make({38 ask: AskId.make("week-at-year-end"),39 at: DateTime.makeUnsafe("2026-10-07T12:00:00Z"),40 base: CommitSha.make("1".repeat(40)),41 checkVersion: CheckVersion.make("redThenGreen@0.0.0+abc"),42 head,43 heldBack: [],44 lanes: [45 {46 command: "bun test",47 durationMs: 1200,48 lane: LaneName.make("unit"),49 result: "passed",50 },51 ],52 outcome: Outcome.cases.Checking.make({ stage: "checks" }),53 patchId: PatchId.make("2".repeat(64)),

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 54why, one step back
54 project: ProjectName.make("oscabriel/datekit"),

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 55–275why, one step back
55 stage: "checks",
56 tree: TreeSha.make("3".repeat(40)),57 verifier: "platform",58 });5960const encodeVerdict = Schema.encodeEffect(Verdict);6162const reencoded = (note: Option.Option<string>) =>63 Schema.decodeEffect(Schema.fromJsonString(Verdict))(64 Option.getOrElse(note, () => "")65 ).pipe(Effect.flatMap(encodeVerdict));6667const now = DateTime.makeUnsafe("2026-10-07T12:30:00Z");6869const targets = Effect.gen(function* () {70 const repos = yield* RepoHost;71 const client = yield* gitClient(memoryFs());72 const first = yield* client.commit("first", [["a.ts", "1\n"]]);73 const second = yield* client.commit("second", [["a.ts", "2\n"]], [first]);7475 yield* repos.create(repo);76 yield* repos.push(repo, client.source, [77 { from: Option.none(), ref: main, to: second },78 ]);7980 return { first, second };81});8283const notesThroughPort = (target: CommitSha) =>84 Effect.gen(function* () {85 const repos = yield* RepoHost;86 const tip = (yield* repos.refs(repo)).get(notesRef);8788 if (tip === undefined) {89 return yield* Effect.die(`${notesRef} is not on ${repo}`);90 }9192 const commit = Option.getOrThrow(yield* repos.readCommit(repo, tip));9394 const entries = Option.getOrThrow(yield* repos.readTree(repo, commit.tree));9596 const entry = entries.find(({ name }) => name === target);9798 const blob =99 entry === undefined100 ? Option.none()101 : yield* repos.readBlob(repo, ObjectId.make(entry.sha));102103 return {104 commit,105 names: entries.map(({ name }) => name),106 note: Option.map(blob, (bytes) => new TextDecoder().decode(bytes)),107 };108 });109110const onDisk = (source: ScratchSource) =>111 Effect.gen(function* () {112 const fs = yield* FileSystem.FileSystem;113 const root = yield* fs.makeTempDirectoryScoped().pipe(Effect.orDie);114 const { promises } = source.fs;115116 const copy = (path: string): Effect.Effect<void> =>117 Effect.gen(function* () {118 const stat = yield* Effect.promise(() => promises.stat(path));119120 if (stat.isDirectory()) {121 const names = yield* Effect.promise(() => promises.readdir(path));122123 yield* fs124 .makeDirectory(`${root}${path}`, { recursive: true })125 .pipe(Effect.orDie);126 yield* Effect.forEach(names, (name) => copy(`${path}/${name}`));127128 return;129 }130131 const bytes = yield* Effect.promise(() => promises.readFile(path));132133 yield* fs134 .writeFile(135 `${root}${path}`,136 bytes instanceof Uint8Array137 ? bytes138 : new TextEncoder().encode(bytes)139 )140 .pipe(Effect.orDie);141 });142143 yield* copy(source.gitdir);144145 return `${root}${source.gitdir}`;146 });147148describe("verdicts as git notes", () => {149 it.live(150 "writes a verdict as a JSON note that the port and git both read",151 () =>152 Effect.gen(function* () {153 const repos = yield* RepoHost;154 const { first } = yield* targets;155 const verdict = verdictOn(first);156157 const { commit, source } = yield* noteCommit({158 notesParent: Option.none(),159 now,160 repo,161 target: first,162 verdict,163 });164165 expect(notesRef).toBe(`refs/notes/${Brand.slug}`);166 expect(167 yield* repos.push(repo, source, [168 { from: Option.none(), ref: notesRef, to: commit },169 ])170 ).toStrictEqual([RefOutcome.Updated({ ref: notesRef })]);171172 const read = yield* notesThroughPort(first);173174 expect({175 author: read.commit.author,176 committer: read.commit.committer,177 names: read.names,178 parents: read.commit.parents,179 sha: read.commit.sha,180 }).toStrictEqual({181 author: committerLine,182 committer: committerLine,183 names: [first],184 parents: [],185 sha: commit,186 });187 expect(yield* reencoded(read.note)).toStrictEqual(188 yield* encodeVerdict(verdict)189 );190191 const gitdir = yield* onDisk(source);192193 expect(194 yield* gitOk(195 gitdir,196 `--git-dir=${gitdir}`,197 "notes",198 `--ref=${Brand.slug}`,199 "show",200 first201 )202 ).toBe(Option.getOrElse(read.note, () => "").trim());203 }).pipe(204 Effect.scoped,205 Effect.provide(Layer.mergeAll(RepoHostMemory, NodeServices.layer))206 ),207 20_000208 );209210 it.live(211 "keeps every earlier note when it writes the next",212 () =>213 Effect.gen(function* () {214 const repos = yield* RepoHost;215 const { first, second } = yield* targets;216217 const one = yield* noteCommit({218 notesParent: Option.none(),219 now,220 repo,221 target: first,222 verdict: verdictOn(first),223 });224225 yield* repos.push(repo, one.source, [226 { from: Option.none(), ref: notesRef, to: one.commit },227 ]);228229 const two = yield* noteCommit({230 notesParent: Option.some(one.commit),231 now,232 repo,233 target: second,234 verdict: verdictOn(second),235 });236237 expect(238 yield* repos.push(repo, two.source, [239 { from: Option.some(one.commit), ref: notesRef, to: two.commit },240 ])241 ).toStrictEqual([RefOutcome.Updated({ ref: notesRef })]);242243 const read = yield* notesThroughPort(first);244245 expect({246 names: new Set(read.names),247 parents: read.commit.parents,248 }).toStrictEqual({249 names: new Set([first, second]),250 parents: [one.commit],251 });252 expect(yield* reencoded(read.note)).toStrictEqual(253 yield* encodeVerdict(verdictOn(first))254 );255256 const gitdir = yield* onDisk(two.source);257258 const listed = yield* gitOk(259 gitdir,260 `--git-dir=${gitdir}`,261 "notes",262 `--ref=${Brand.slug}`,263 "list"264 );265266 expect(267 new Set(listed.split("\n").map((line) => line.split(" ")[1]))268 ).toStrictEqual(new Set([first, second]));269 }).pipe(270 Effect.scoped,271 Effect.provide(Layer.mergeAll(RepoHostMemory, NodeServices.layer))272 ),273 20_000274 );275});