domain.test.ts
This file
Path
packages/protocol/test/domain.test.ts
363 lines · 10.6 KiB · TypeScript
Last changed by
Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
History of this fileTake 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–17why, one step back1import { describe, expect, it } from "@effect/vitest";2import { Brand } from "@repo/brand";3import { Result, Schema } from "effect";45import { AnswerReceipt, AnswerView, Verdict } from "../src/domain/answer.ts";6import { Ask, AskListing, Brief } from "../src/domain/ask.ts";7import { CheckSpec, RatchetSpec } from "../src/domain/check.ts";8import { ClaimView } from "../src/domain/claim.ts";9import { Evidence } from "../src/domain/common.ts";10import { AgentTrailer, Credit } from "../src/domain/credit.ts";11import { Decision, Today } from "../src/domain/decision.ts";12import { EnvSchemaView } from "../src/domain/env.ts";13import { ProjectEvent } from "../src/domain/event.ts";14import { FrontPageEntry, ProjectListing } from "../src/domain/front-page.ts";15import { AskKind } from "../src/domain/kind.ts";16import { LandingView } from "../src/domain/landing.ts";17import { Outcome } from "../src/domain/outcome.ts";Name the ask an opening opened, say why a policy has not evaluated, and give code history a durable storeoscabrielbefore adoption51f046c
lines 18–24why, one step back18import {19 AskInput,20 Policy,21 PolicyInput,22 PolicyPending,23} from "../src/domain/policy.ts";24import { DraftNote, Proposal } from "../src/domain/proposal.ts";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 25–96why, one step back25import { CreditSummary } from "../src/domain/record.ts";26import { RunBlock } from "../src/domain/run.ts";27import { Territory } from "../src/domain/territory.ts";28import {29 LayerManifest,30 Ledger,31 VisibilityPolicy,32} from "../src/domain/visibility.ts";33import { Envelope } from "../src/envelope.ts";34import {35 AreaName,36 AskId,37 BlobSha,38 CheckVersion,39 DecisionId,40 EnvKey,41 Handle,42 KindName,43 LandedSha,44 LandingId,45 LaneName,46 LayerName,47 PatchId,48 ProjectName,49 ProposalId,50 RatchetName,51 RepoName,52 RequestId,53 SuiteRoot,54 TreeSha,55 sameHandle,56} from "../src/ids.ts";57import { Anonymous, Person, Principal } from "../src/principal.ts";58import wire from "./fixtures/domain.json" with { type: "json" };5960const Fixture = Schema.Struct({61 refused: Schema.Record(62 Schema.String,63 Schema.Struct({ schema: Schema.NonEmptyString, value: Schema.Json })64 ),65 roundTrip: Schema.Record(Schema.String, Schema.NonEmptyArray(Schema.Json)),66 withoutDefaults: Schema.Json,67});6869const fixture = Schema.decodeUnknownSync(Fixture)(wire);7071const Listed = Envelope(Schema.Struct({ asks: Schema.Array(AskListing) }));7273const schemas = {74 AgentTrailer,75 AnswerReceipt,76 AnswerView,77 Ask,78 AskInput,79 AskKind,80 AskListing,81 Brief,82 CheckSpec,83 ClaimView,84 Credit,85 CreditSummary,86 Decision,87 EnvSchemaView,88 Envelope: Listed,89 Evidence,90 FrontPageEntry,91 LandingView,92 LayerManifest,93 Ledger,94 Outcome,95 Policy,96 PolicyInput,Name the ask an opening opened, say why a policy has not evaluated, and give code history a durable storeoscabrielbefore adoption51f046c
line 97why, one step back97 PolicyPending,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 98–164why, one step back98 Principal,99 ProjectEvent,100 ProjectListing,101 Proposal,102 RatchetSpec,103 RunBlock,104 Territory,105 Today,106 Verdict,107 VisibilityPolicy,108} as const;109110const schemaNamed = new Map<string, Schema.Codec<unknown, unknown>>(111 Object.entries(schemas)112);113114const examples = Object.entries(schemas).flatMap(([name, schema]) =>115 (fixture.roundTrip[name] ?? []).map((example, index) => ({116 example,117 name: `${name} #${index + 1}`,118 schema,119 }))120);121122const refusals = Object.entries(fixture.refused).map(123 ([description, { schema, value }]) => ({124 description,125 schema: schemaNamed.get(schema),126 value,127 })128);129130const refuses = (schema: Schema.Codec<unknown, unknown>, input: Schema.Json) =>131 Result.isFailure(Schema.decodeResult(schema)(input));132133describe("the domain schemas", () => {134 it("have examples for every schema, and a schema for every example", () => {135 expect(Object.keys(fixture.roundTrip).toSorted()).toStrictEqual(136 Object.keys(schemas).toSorted()137 );138 expect(139 refusals.flatMap(({ description, schema }) =>140 schema === undefined ? [description] : []141 )142 ).toStrictEqual([]);143 });144145 it.each(examples)(146 "$name decodes its example and encodes it back unchanged",147 ({ example, schema }) => {148 expect(149 Schema.encodeSync(schema)(Schema.decodeUnknownSync(schema)(example))150 ).toStrictEqual(example);151 }152 );153154 it.each(refusals)("refuse $description", ({ schema, value }) => {155 expect(schema !== undefined && refuses(schema, value)).toBeTruthy();156 });157158 it.each([159 ["Policy", Policy],160 ["Verdict", Verdict],161 ["Outcome", Outcome],162 ["ProjectEvent", ProjectEvent],163 ["VisibilityPolicy", VisibilityPolicy],164 ["LayerManifest", LayerManifest],Name the ask an opening opened, say why a policy has not evaluated, and give code history a durable storeoscabrielbefore adoption51f046c
lines 165–167why, one step back165 ["LandingView", LandingView],166 ["PolicyPending", PolicyPending],167 ["Proposal", Proposal],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 168–175why, one step back168 ] as const)(169 "%s has a JSON Schema with no non-finite numbers and no nulls",170 (_name, schema) => {171 const document = JSON.stringify(Schema.toJsonSchemaDocument(schema));172173 expect(document).not.toMatch(/Infinity|NaN|"null"/u);174 }175 );Name the ask an opening opened, say why a policy has not evaluated, and give code history a durable storeoscabrielbefore adoption51f046c
lines 176–182why, one step back176177 it("keep a draft's note to 2,000 characters", () => {178 expect([179 refuses(DraftNote, "x".repeat(2000)),180 refuses(DraftNote, "x".repeat(2001)),181 ]).toStrictEqual([false, true]);182 });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 183–262why, one step back183184 it("fill the defaults for every key the config folder may leave out", () => {185 expect(186 Schema.decodeUnknownSync(PolicyInput)(fixture.withoutDefaults)187 ).toMatchObject({188 agents: "trailer",189 budget: { decisionsPerDay: 5, expireDays: 14 },190 caps: { claimsPerPerson: 3, proposalsPerPerson: 2 },191 claims: { lapseAfterMs: 86_400_000, warnAfterMs: 7_200_000 },192 record: { countsAfterDays: 30, windowDays: 365 },193 vouch: { file: `${Brand.configDir}/vouched.td`, mode: "forCode" },194 });195 });196197 // A span the config folder writes as `"-1 days"` is a valid string to the198 // sdk; the policy is where a count or a span that can't hold is refused.199 const [kind] = wire.withoutDefaults.kinds;200201 it.each([202 ["budget", { budget: { decisionsPerDay: 0, expireDays: 14 } }],203 ["budget", { budget: { decisionsPerDay: 5, expireDays: -1 } }],204 ["caps", { caps: { claimsPerPerson: 0, proposalsPerPerson: 2 } }],205 ["caps", { caps: { claimsPerPerson: 3, proposalsPerPerson: -1 } }],206 ["claims", { claims: { lapseAfterMs: 0, warnAfterMs: 7_200_000 } }],207 ["claims", { claims: { lapseAfterMs: 86_400_000, warnAfterMs: -1 } }],208 ["record", { record: { countsAfterDays: -1, windowDays: 365 } }],209 ["record", { record: { countsAfterDays: 30, windowDays: 0 } }],210 ["a kind's race", { kinds: [{ ...kind, race: 0 }] }],211 [212 "a kind's record rule",213 {214 kinds: [{ ...kind, claimers: { record: { area: "x", atLeast: -1 } } }],215 },216 ],217 ] as const)("refuse %s below what can hold", (_name, change) => {218 expect(219 refuses(PolicyInput, { ...wire.withoutDefaults, ...change })220 ).toBeTruthy();221 });222223 it("let a project turn proposals off and count a landing at once", () => {224 expect(225 refuses(PolicyInput, {226 ...wire.withoutDefaults,227 caps: { claimsPerPerson: 1, proposalsPerPerson: 0 },228 record: { countsAfterDays: 0, windowDays: 1 },229 })230 ).toBeFalsy();231 });232});233234describe("a project's visibility", () => {235 const [stated] = wire.roundTrip.VisibilityPolicy;236237 const [policy] = wire.roundTrip.Policy;238239 it("rides on the policy at a landed commit", () => {240 const withVisibility = { ...policy, visibility: stated };241242 expect(243 Schema.encodeSync(Policy)(244 Schema.decodeUnknownSync(Policy)(withVisibility)245 )246 ).toStrictEqual(withVisibility);247 });248249 it("is absent from a policy whose config folder declares none", () => {250 expect(Schema.decodeUnknownSync(Policy)(policy)).not.toHaveProperty(251 "visibility"252 );253 });254});255256describe("names and ids", () => {257 it("tell two handles apart without case, as sign-in does", () => {258 expect(sameHandle(Handle.make("Alice"), Handle.make("alice"))).toBeTruthy();259 expect(sameHandle(Handle.make("alice"), Handle.make("alicia"))).toBeFalsy();260 });261262 it.each(["a--b", "-a", "a-", "A", "a_b", ""])(Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 263why, one step back263 "an ask may not be named %j",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 264–269why, one step back264 (name) => {265 expect(refuses(AskId, name)).toBeTruthy();266 }267 );268269 it.each(["a", "a-b", "iso-week-2", "datekit"])(Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 270why, one step back270 "an ask may be named %j",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 271–275why, one step back271 (name) => {272 expect(refuses(AskId, name)).toBeFalsy();273 }274 );275Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
lines 276–282why, one step back276 it.each(["a", "a-b", "iso-week-2"])(277 "an owner may make a project named %j",278 (name) => {279 expect(refuses(ProjectName, `oscabriel/${name}`)).toBeFalsy();280 }281 );282Merge 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 283–286why, one step back283 it.each(["me", "layer", "open", "flag"])(284 "an ask may not be named %j",285 (name) => {286 expect(refuses(AskId, name)).toBeTruthy();Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 287why, one step back287 expect(refuses(ProjectName, `oscabriel/${name}`)).toBeFalsy();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 288–290why, one step back288 }289 );290Merge build/1107 into mainoscabrielbefore adoption0ac60f3
lines 291–295why, one step back291 it.each([292 "agents",293 "assets",294 "auth",295 "icons",Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 296why, one step back296 "import",Merge build/1107 into mainoscabrielbefore adoption0ac60f3
lines 297–301why, one step back297 "people",298 "releases",299 "rpc",300 "today",301 ])(Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 302why, one step back302 "a project may not be owned by %j, a path the web takes for itself",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
line 303why, one step back303 (name) => {Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 304why, one step back304 expect(refuses(ProjectName, `${name}/foo`)).toBeTruthy();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 305–336why, one step back305 expect(refuses(AskId, name)).toBeFalsy();306 }307 );308309 const tree = "5555555555555555555555555555555555555555";310311 const patchId = "6".repeat(64);312313 const suite = `sha256:${"7".repeat(64)}`;314315 it.each([316 ["LandedSha", "4".repeat(40), "4444", LandedSha],317 ["TreeSha", tree, "g".repeat(40), TreeSha],318 ["BlobSha", tree, `${tree}0`, BlobSha],319 ["PatchId", patchId, tree, PatchId],320 ["RequestId", "push-33333333", "short", RequestId],321 ["ProposalId", "prop-000001", "prop-1", ProposalId],322 ["DecisionId", "dec-0a1b2c", "dec_0a1b2c", DecisionId],323 ["LandingId", "land-000001", "-000001", LandingId],324 ["KindName", "lintToZero", "1fix", KindName],325 ["LayerName", "maintainers", "Maintainers", LayerName],326 ["AreaName", "parse", "parse/iso", AreaName],327 ["LaneName", "unit", "unit tests", LaneName],328 ["RatchetName", "anyCount", "any_count", RatchetName],329 ["EnvKey", "PAYMENTS_KEY", "PAYMENTS-KEY", EnvKey],330 ["SuiteRoot", suite, patchId, SuiteRoot],331 ["CheckVersion", "redThenGreen@0.0.0+abc", "", CheckVersion],332 ["RepoName", "datekit--iso-week--alice", "datekit/iso", RepoName],333 ] as const)("%s takes %j and refuses %j", (_name, good, bad, schema) => {334 expect(refuses(schema, good)).toBeFalsy();335 expect(refuses(schema, bad)).toBeTruthy();336 });Close W7: cross-compile the release again, guard pi's edits, and keep the swarm out of this checkoutoscabrielbefore adoption842f042
lines 337–349why, one step back337338 it("names a layer in an ask's words, so its repo never reads as a claim fork", () => {339 expect(340 ["security-review", "maintainers2"].map((name) =>341 refuses(LayerName, name)342 )343 ).toStrictEqual([false, false]);344 expect(345 ["a--b", "triageTeam", "fix-", "x".repeat(0)].map((name) =>346 refuses(LayerName, name)347 )348 ).toStrictEqual([true, true, true, true]);349 });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 350–363why, one step back350});351352describe("the caller", () => {353 it("is a person named by their handle, or anyone at all", () => {354 expect(355 (fixture.roundTrip.Principal ?? []).map((principal) =>356 Schema.decodeUnknownSync(Principal)(principal)357 )358 ).toStrictEqual([359 Person.make({ handle: Handle.make("alice") }),360 Anonymous.make({}),361 ]);362 });363});