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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364import { describe, expect, it } from "@effect/vitest";import { Brand } from "@repo/brand";import { Result, Schema } from "effect";
import { AnswerReceipt, AnswerView, Verdict } from "../src/domain/answer.ts";import { Ask, AskListing, Brief } from "../src/domain/ask.ts";import { CheckSpec, RatchetSpec } from "../src/domain/check.ts";import { ClaimView } from "../src/domain/claim.ts";import { Evidence } from "../src/domain/common.ts";import { AgentTrailer, Credit } from "../src/domain/credit.ts";import { Decision, Today } from "../src/domain/decision.ts";import { EnvSchemaView } from "../src/domain/env.ts";import { ProjectEvent } from "../src/domain/event.ts";import { FrontPageEntry, ProjectListing } from "../src/domain/front-page.ts";import { AskKind } from "../src/domain/kind.ts";import { LandingView } from "../src/domain/landing.ts";import { Outcome } from "../src/domain/outcome.ts";import { AskInput, Policy, PolicyInput, PolicyPending,} from "../src/domain/policy.ts";import { DraftNote, Proposal } from "../src/domain/proposal.ts";import { CreditSummary } from "../src/domain/record.ts";import { RunBlock } from "../src/domain/run.ts";import { Territory } from "../src/domain/territory.ts";import { LayerManifest, Ledger, VisibilityPolicy,} from "../src/domain/visibility.ts";import { Envelope } from "../src/envelope.ts";import { AreaName, AskId, BlobSha, CheckVersion, DecisionId, EnvKey, Handle, KindName, LandedSha, LandingId, LaneName, LayerName, PatchId, ProjectName, ProposalId, RatchetName, RepoName, RequestId, SuiteRoot, TreeSha, sameHandle,} from "../src/ids.ts";import { Anonymous, Person, Principal } from "../src/principal.ts";import wire from "./fixtures/domain.json" with { type: "json" };
const Fixture = Schema.Struct({ refused: Schema.Record( Schema.String, Schema.Struct({ schema: Schema.NonEmptyString, value: Schema.Json }) ), roundTrip: Schema.Record(Schema.String, Schema.NonEmptyArray(Schema.Json)), withoutDefaults: Schema.Json,});
const fixture = Schema.decodeUnknownSync(Fixture)(wire);
const Listed = Envelope(Schema.Struct({ asks: Schema.Array(AskListing) }));
const schemas = { AgentTrailer, AnswerReceipt, AnswerView, Ask, AskInput, AskKind, AskListing, Brief, CheckSpec, ClaimView, Credit, CreditSummary, Decision, EnvSchemaView, Envelope: Listed, Evidence, FrontPageEntry, LandingView, LayerManifest, Ledger, Outcome, Policy, PolicyInput, PolicyPending, Principal, ProjectEvent, ProjectListing, Proposal, RatchetSpec, RunBlock, Territory, Today, Verdict, VisibilityPolicy,} as const;
const schemaNamed = new Map<string, Schema.Codec<unknown, unknown>>( Object.entries(schemas));
const examples = Object.entries(schemas).flatMap(([name, schema]) => (fixture.roundTrip[name] ?? []).map((example, index) => ({ example, name: `${name} #${index + 1}`, schema, })));
const refusals = Object.entries(fixture.refused).map( ([description, { schema, value }]) => ({ description, schema: schemaNamed.get(schema), value, }));
const refuses = (schema: Schema.Codec<unknown, unknown>, input: Schema.Json) => Result.isFailure(Schema.decodeResult(schema)(input));
describe("the domain schemas", () => { it("have examples for every schema, and a schema for every example", () => { expect(Object.keys(fixture.roundTrip).toSorted()).toStrictEqual( Object.keys(schemas).toSorted() ); expect( refusals.flatMap(({ description, schema }) => schema === undefined ? [description] : [] ) ).toStrictEqual([]); });
it.each(examples)( "$name decodes its example and encodes it back unchanged", ({ example, schema }) => { expect( Schema.encodeSync(schema)(Schema.decodeUnknownSync(schema)(example)) ).toStrictEqual(example); } );
it.each(refusals)("refuse $description", ({ schema, value }) => { expect(schema !== undefined && refuses(schema, value)).toBeTruthy(); });
it.each([ ["Policy", Policy], ["Verdict", Verdict], ["Outcome", Outcome], ["ProjectEvent", ProjectEvent], ["VisibilityPolicy", VisibilityPolicy], ["LayerManifest", LayerManifest], ["LandingView", LandingView], ["PolicyPending", PolicyPending], ["Proposal", Proposal], ] as const)( "%s has a JSON Schema with no non-finite numbers and no nulls", (_name, schema) => { const document = JSON.stringify(Schema.toJsonSchemaDocument(schema));
expect(document).not.toMatch(/Infinity|NaN|"null"/u); } );
it("keep a draft's note to 2,000 characters", () => { expect([ refuses(DraftNote, "x".repeat(2000)), refuses(DraftNote, "x".repeat(2001)), ]).toStrictEqual([false, true]); });
it("fill the defaults for every key the config folder may leave out", () => { expect( Schema.decodeUnknownSync(PolicyInput)(fixture.withoutDefaults) ).toMatchObject({ agents: "trailer", budget: { decisionsPerDay: 5, expireDays: 14 }, caps: { claimsPerPerson: 3, proposalsPerPerson: 2 }, claims: { lapseAfterMs: 86_400_000, warnAfterMs: 7_200_000 }, record: { countsAfterDays: 30, windowDays: 365 }, vouch: { file: `${Brand.configDir}/vouched.td`, mode: "forCode" }, }); });
// A span the config folder writes as `"-1 days"` is a valid string to the // sdk; the policy is where a count or a span that can't hold is refused. const [kind] = wire.withoutDefaults.kinds;
it.each([ ["budget", { budget: { decisionsPerDay: 0, expireDays: 14 } }], ["budget", { budget: { decisionsPerDay: 5, expireDays: -1 } }], ["caps", { caps: { claimsPerPerson: 0, proposalsPerPerson: 2 } }], ["caps", { caps: { claimsPerPerson: 3, proposalsPerPerson: -1 } }], ["claims", { claims: { lapseAfterMs: 0, warnAfterMs: 7_200_000 } }], ["claims", { claims: { lapseAfterMs: 86_400_000, warnAfterMs: -1 } }], ["record", { record: { countsAfterDays: -1, windowDays: 365 } }], ["record", { record: { countsAfterDays: 30, windowDays: 0 } }], ["a kind's race", { kinds: [{ ...kind, race: 0 }] }], [ "a kind's record rule", { kinds: [{ ...kind, claimers: { record: { area: "x", atLeast: -1 } } }], }, ], ] as const)("refuse %s below what can hold", (_name, change) => { expect( refuses(PolicyInput, { ...wire.withoutDefaults, ...change }) ).toBeTruthy(); });
it("let a project turn proposals off and count a landing at once", () => { expect( refuses(PolicyInput, { ...wire.withoutDefaults, caps: { claimsPerPerson: 1, proposalsPerPerson: 0 }, record: { countsAfterDays: 0, windowDays: 1 }, }) ).toBeFalsy(); });});
describe("a project's visibility", () => { const [stated] = wire.roundTrip.VisibilityPolicy;
const [policy] = wire.roundTrip.Policy;
it("rides on the policy at a landed commit", () => { const withVisibility = { ...policy, visibility: stated };
expect( Schema.encodeSync(Policy)( Schema.decodeUnknownSync(Policy)(withVisibility) ) ).toStrictEqual(withVisibility); });
it("is absent from a policy whose config folder declares none", () => { expect(Schema.decodeUnknownSync(Policy)(policy)).not.toHaveProperty( "visibility" ); });});
describe("names and ids", () => { it("tell two handles apart without case, as sign-in does", () => { expect(sameHandle(Handle.make("Alice"), Handle.make("alice"))).toBeTruthy(); expect(sameHandle(Handle.make("alice"), Handle.make("alicia"))).toBeFalsy(); });
it.each(["a--b", "-a", "a-", "A", "a_b", ""])( "an ask may not be named %j", (name) => { expect(refuses(AskId, name)).toBeTruthy(); } );
it.each(["a", "a-b", "iso-week-2", "datekit"])( "an ask may be named %j", (name) => { expect(refuses(AskId, name)).toBeFalsy(); } );
it.each(["a", "a-b", "iso-week-2"])( "an owner may make a project named %j", (name) => { expect(refuses(ProjectName, `oscabriel/${name}`)).toBeFalsy(); } );
it.each(["me", "layer", "open", "flag"])( "an ask may not be named %j", (name) => { expect(refuses(AskId, name)).toBeTruthy(); expect(refuses(ProjectName, `oscabriel/${name}`)).toBeFalsy(); } );
it.each([ "agents", "assets", "auth", "icons", "import", "people", "releases", "rpc", "today", ])( "a project may not be owned by %j, a path the web takes for itself", (name) => { expect(refuses(ProjectName, `${name}/foo`)).toBeTruthy(); expect(refuses(AskId, name)).toBeFalsy(); } );
const tree = "5555555555555555555555555555555555555555";
const patchId = "6".repeat(64);
const suite = `sha256:${"7".repeat(64)}`;
it.each([ ["LandedSha", "4".repeat(40), "4444", LandedSha], ["TreeSha", tree, "g".repeat(40), TreeSha], ["BlobSha", tree, `${tree}0`, BlobSha], ["PatchId", patchId, tree, PatchId], ["RequestId", "push-33333333", "short", RequestId], ["ProposalId", "prop-000001", "prop-1", ProposalId], ["DecisionId", "dec-0a1b2c", "dec_0a1b2c", DecisionId], ["LandingId", "land-000001", "-000001", LandingId], ["KindName", "lintToZero", "1fix", KindName], ["LayerName", "maintainers", "Maintainers", LayerName], ["AreaName", "parse", "parse/iso", AreaName], ["LaneName", "unit", "unit tests", LaneName], ["RatchetName", "anyCount", "any_count", RatchetName], ["EnvKey", "PAYMENTS_KEY", "PAYMENTS-KEY", EnvKey], ["SuiteRoot", suite, patchId, SuiteRoot], ["CheckVersion", "redThenGreen@0.0.0+abc", "", CheckVersion], ["RepoName", "datekit--iso-week--alice", "datekit/iso", RepoName], ] as const)("%s takes %j and refuses %j", (_name, good, bad, schema) => { expect(refuses(schema, good)).toBeFalsy(); expect(refuses(schema, bad)).toBeTruthy(); });
it("names a layer in an ask's words, so its repo never reads as a claim fork", () => { expect( ["security-review", "maintainers2"].map((name) => refuses(LayerName, name) ) ).toStrictEqual([false, false]); expect( ["a--b", "triageTeam", "fix-", "x".repeat(0)].map((name) => refuses(LayerName, name) ) ).toStrictEqual([true, true, true, true]); });});
describe("the caller", () => { it("is a person named by their handle, or anyone at all", () => { expect( (fixture.roundTrip.Principal ?? []).map((principal) => Schema.decodeUnknownSync(Principal)(principal) ) ).toStrictEqual([ Person.make({ handle: Handle.make("alice") }), Anonymous.make({}), ]); });});