diff --git a/crates/thing-contract/tests/catalog_members.rs b/crates/thing-contract/tests/catalog_members.rs index 1148b35..a343883 100644 --- a/crates/thing-contract/tests/catalog_members.rs +++ b/crates/thing-contract/tests/catalog_members.rs @@ -44,17 +44,20 @@ fn rows_equal_the_csharp_file() { } #[test] -fn the_written_store_carries_the_rows() { - // goldens.rs writes a store of one record per kind and compares it byte for byte with the - // committed surface.cc, so the committed file's catalog is the catalog the crate writes. It is - // copied first: reading a store creates a lock file beside it. - let committed = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/goldens/surface.cc"); - let work = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("catalog-members"); - std::fs::create_dir_all(&work).unwrap(); - let copy = work.join("surface.cc"); - std::fs::copy(committed, ©).unwrap(); - let catalog = SingleFileMessagePackBackingStore::new(©).pull_schema_catalog().unwrap(); - let from_store = sorted(catalog.iter().map(|entry| group(&entry.members)).collect()); - assert_eq!(from_store, tsv_groups()); - let _ = std::fs::remove_dir_all(&work); +fn every_committed_store_carries_the_rows() { + // Each runtime's committed store, this crate's own included, declares the C# catalog. The Rust + // reader does not compare member type names when it opens a store, so a drifted name in a + // store another runtime wrote fails here. A store is copied first: reading it creates a lock + // file beside it. + for file in ["written-by-rs.cc", "written-by-cs.cc", "written-by-ts.cc"] { + let committed = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/thing-contract-parity").join(file); + let work = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(format!("catalog-members-{file}")); + std::fs::create_dir_all(&work).unwrap(); + let copy = work.join(file); + std::fs::copy(committed, ©).unwrap(); + let catalog = SingleFileMessagePackBackingStore::new(©).pull_schema_catalog().unwrap(); + let from_store = sorted(catalog.iter().map(|entry| group(&entry.members)).collect()); + assert_eq!(from_store, tsv_groups(), "{file}"); + let _ = std::fs::remove_dir_all(&work); + } } diff --git a/crates/thing-contract/tests/foreign_stores.rs b/crates/thing-contract/tests/foreign_stores.rs new file mode 100644 index 0000000..0c59494 --- /dev/null +++ b/crates/thing-contract/tests/foreign_stores.rs @@ -0,0 +1,63 @@ +//! The stores the other two runtimes wrote. `written-by-cs.cc` and `written-by-ts.cc` in +//! `tests/thing-contract-parity` are read here, never written: each opens to exactly the five +//! records, each re-encodes to its value golden, and each sits under the key the other runtimes +//! key it by. The file is copied first: reading a store creates a lock file beside it. + +use std::fmt::Debug; +use std::path::PathBuf; + +use cultcache_rs::DatabaseEntry; +use thing_contract::*; +use thing_testkit::{golden, store}; + +fn parity_dir() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/thing-contract-parity") +} + +fn goldens_dir() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/goldens") +} + +fn scratch(purpose: &str, bytes: &[u8]) -> PathBuf { + static SERIAL: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + let serial = SERIAL.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let work = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(format!("foreign-{purpose}-{serial}")); + std::fs::create_dir_all(&work).unwrap(); + let copy = work.join("store.cc"); + std::fs::write(©, bytes).unwrap(); + copy +} + +fn assert_record(cache: &cultcache_rs::CultCache, key: &str, golden_name: &str) { + let records = cache.get_all_with_keys::().unwrap(); + assert_eq!(records.len(), 1, "{golden_name}: one record of the kind"); + let (found, value) = &records[0]; + assert_eq!(found, key, "{golden_name}: keyed as the other runtimes key it"); + let expected = golden::read(&goldens_dir(), &format!("{golden_name}.bin")); + assert_eq!(rmp_serde::to_vec(value).unwrap(), expected, "{golden_name}: re-encodes to its golden"); +} + +fn assert_five_records(cache: &cultcache_rs::CultCache) { + assert_record::(cache, "thing:surface:provider.a.main", "surface-all-arms"); + assert_record::(cache, "thing:composition:provider.a.panel.v3", "composition-all-params"); + assert_record::(cache, "thing:invocation:3f2b8c1e-5d44-4a6b-9a07-1c2d3e4f5a6b", "invocation-all-inputs"); + assert_record::(cache, "thing:result:3f2b8c1e-5d44-4a6b-9a07-1c2d3e4f5a6b.1", "result-pending"); + assert_record::(cache, "thing:asset_catalog:provider.a.icons", "asset-catalog"); + assert_eq!(cache.snapshot().len(), 5, "no record beyond the five"); +} + +fn open_foreign(file: &str) { + let copy = scratch("foreign", &std::fs::read(parity_dir().join(file)).unwrap()); + assert_five_records(&store::open(©)); + let _ = std::fs::remove_dir_all(copy.parent().unwrap()); +} + +#[test] +fn the_store_csharp_wrote_opens_to_five_records_that_re_encode_to_the_goldens() { + open_foreign("written-by-cs.cc"); +} + +#[test] +fn the_store_typescript_wrote_opens_to_five_records_that_re_encode_to_the_goldens() { + open_foreign("written-by-ts.cc"); +} diff --git a/crates/thing-contract/tests/goldens.rs b/crates/thing-contract/tests/goldens.rs index 8a74b28..40d12bc 100644 --- a/crates/thing-contract/tests/goldens.rs +++ b/crates/thing-contract/tests/goldens.rs @@ -14,6 +14,11 @@ fn goldens_dir() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/goldens") } +/// Where the stores of all three runtimes live; this crate writes `written-by-rs.cc` there. +fn parity_dir() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/thing-contract-parity") +} + // A value that must never appear in a refusal. const CANARY: &str = "CANARY-7f3a91c2"; @@ -480,8 +485,9 @@ fn goldens_match() { // The store: one record of each kind, written under a pinned clock, byte for byte. let scratch = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("goldens-surface.cc"); write_store(&scratch); - let committed = golden::check(&goldens_dir(), "surface.cc", &std::fs::read(&scratch).unwrap()); - let reopened = goldens_dir().join("surface.cc"); + let parity = parity_dir(); + let committed = golden::check(&parity, "written-by-rs.cc", &std::fs::read(&scratch).unwrap()); + let reopened = parity.join("written-by-rs.cc"); assert_eq!(std::fs::read(&reopened).unwrap(), committed); let cache = store::open(&scratch); let surface = full_surface(); diff --git a/crates/thing-contract/tests/goldens/README.md b/crates/thing-contract/tests/goldens/README.md index f9e9f74..711f3d0 100644 --- a/crates/thing-contract/tests/goldens/README.md +++ b/crates/thing-contract/tests/goldens/README.md @@ -1,48 +1,47 @@ -# thing-contract goldens - -Written only by `crates/thing-contract/tests/goldens.rs`, with `THING_BLESS=1` set on that test. -Every other run compares byte for byte. The TypeScript and C# packages and core decode these files -and are held to the answers below; none of them writes one. - -Each `.bin` is the payload of one record: the positional slot array of its kind, as MessagePack. -A union value is `[armKey, [armSlots...]]`; a unit arm is `[key, []]`. An optional member is nil -when absent. Arm keys are fixed for v1. - -## Values (decode to the constructed value, re-encode to the same bytes) - -| File | Kind | Decodes to | -|---|---|---| -| `surface-all-arms.bin` | `gamecult.thing.surface.v1` | One surface with 13 of the 14 node arms (Box 0, Text 1, Image 2, Button 3, Toggle 4, Slider 5, TextInput 6 as both input kinds, Text 0 and Number 1, Select 7, Repeat 8, Overlay 9, Plot 10, Embed 11, Use 12; Slot 13 is in `composition-all-params`, a body being where a slot stands), a Box chain four levels deep, every text, number and bool source arm, every Format arm, both state sources, both member steps, every dimension and colour arm, and two declared commands | -| `composition-all-params.bin` | `gamecult.thing.composition.v1` | A composition with one parameter of each of the 7 kinds, whose body uses a composition with one argument of each of the 7 argument arms and places the body slot (Slot 13) last in its root's children | -| `invocation-all-inputs.bin` | `gamecult.thing.command_invocation.v1` | An invocation with a payload record of bin bytes and one captured input of each of the 4 input arms | -| `result-pending.bin` | `gamecult.thing.command_result.v1` | Outcome Pending, seq 1 | -| `result-completed.bin` | same | Outcome Completed with source version 8, drafts cleared, no effect | -| `result-completed-open-surface.bin` | same | Completed with no source version and the OpenSurface effect | -| `result-completed-open-external.bin` | same | Completed with the OpenExternal effect and a resume command | -| `result-denied.bin` | same | Denied naming a node | -| `result-denied-no-node.bin` | same | Denied with a nil node | -| `result-failed.bin` | same | Failed | -| `asset-catalog.bin` | `gamecult.thing.asset_catalog.v1` | Two assets | -| `surface.cc` | single-file store | One record of each of the five kinds, written under the pinned clock `2026-01-01T00:00:00Z`; reading it and writing it again gives the same bytes | - -## Refusals and tolerance - -| File | Payload | Expected | -|---|---|---| -| `result-thing1-flat.bin` | a MessagePack map with named members, the shape a Thing 1 provider wrote for a command result | refused when stored as a command result; the error names the kind and the record key and never a value | -| `node-unknown-arm.bin` | a surface whose root node has arm key 14, which Node does not declare | refused; the error names the kind and the record key, and decoded directly it names the union Node and the key 14, and never a value | -| `outcome-unknown-arm.bin` | a result whose outcome has arm key 4, which Outcome does not declare | refused in the same way, naming the union Outcome and the key 4 | -| `result-extra-slot.bin` | a Pending result with a fourth slot past the last one declared | read: it decodes to the Pending result with seq 1 (CultLib's extra-trailing-slots ruling: read, never written back) | -| `result-seq-zero.bin` | a result whose seq slot is 0 | refused; the error names the kind and the record key, decoded directly it names Seq, and never a value | -| `surface-version-negative.bin` | a surface whose version slot is -1 | refused in the same way, naming Version | -| `surface-label-dotted.bin` | a surface whose label is `b.c` | refused in the same way, naming Segment: a label holds no dot, so a record key splits at its last dot | -| `asset-extent-negative.bin` | an asset catalog whose first asset has width -5 | refused in the same way, naming Count | -| `rgba-channel-out-of-range.bin` | the Rgba slot array `[256, 0, 0, 255]` | refused when decoded as an Rgba: a channel is a byte, 0 to 255 | - -Domain refusals are the contract's rule, which the TypeScript and C# packages mirror: each is held to refuse these files, without the leaf set a Rust `u8` or a checked newtype has. - -## The nil vector - -`nil-positions.tsv` is written only by `crates/thing-contract/tests/nil_positions.rs` (`THING_BLESS=1`). For every element inside an array, at every depth, of each value golden above except `surface.cc` (the eleven `.bin` files from `surface-all-arms` to `asset-catalog`), the test replaces that element with nil (`0xC0`) and decodes the whole payload with the Rust decode door, `rmp_serde::from_slice` into the golden's kind. A row is `golden`, `path` (dotted array indices from the root, e.g. `3.0.1`), the element's byte `start` and `end` in the golden, and `ACCEPT` or `REFUSE`. An accepted variant re-encodes to the spliced bytes. Maps are not walked. - -A slot is required exactly when its Rust type is not optional, so `REFUSE` rows are the required slots and `ACCEPT` rows the optional ones. The Rust decode door decides every row; the TypeScript and C# packages keep no list of required slots and are held to every row. +# thing-contract goldens + +Written only by `crates/thing-contract/tests/goldens.rs`, with `THING_BLESS=1` set on that test. +Every other run compares byte for byte. The TypeScript and C# packages and core decode these files +and are held to the answers below; none of them writes one. + +Each `.bin` is the payload of one record: the positional slot array of its kind, as MessagePack. +A union value is `[armKey, [armSlots...]]`; a unit arm is `[key, []]`. An optional member is nil +when absent. Arm keys are fixed for v1. + +## Values (decode to the constructed value, re-encode to the same bytes) + +| File | Kind | Decodes to | +|---|---|---| +| `surface-all-arms.bin` | `gamecult.thing.surface.v1` | One surface with 13 of the 14 node arms (Box 0, Text 1, Image 2, Button 3, Toggle 4, Slider 5, TextInput 6 as both input kinds, Text 0 and Number 1, Select 7, Repeat 8, Overlay 9, Plot 10, Embed 11, Use 12; Slot 13 is in `composition-all-params`, a body being where a slot stands), a Box chain four levels deep, every text, number and bool source arm, every Format arm, both state sources, both member steps, every dimension and colour arm, and two declared commands | +| `composition-all-params.bin` | `gamecult.thing.composition.v1` | A composition with one parameter of each of the 7 kinds, whose body uses a composition with one argument of each of the 7 argument arms and places the body slot (Slot 13) last in its root's children | +| `invocation-all-inputs.bin` | `gamecult.thing.command_invocation.v1` | An invocation with a payload record of bin bytes and one captured input of each of the 4 input arms | +| `result-pending.bin` | `gamecult.thing.command_result.v1` | Outcome Pending, seq 1 | +| `result-completed.bin` | same | Outcome Completed with source version 8, drafts cleared, no effect | +| `result-completed-open-surface.bin` | same | Completed with no source version and the OpenSurface effect | +| `result-completed-open-external.bin` | same | Completed with the OpenExternal effect and a resume command | +| `result-denied.bin` | same | Denied naming a node | +| `result-denied-no-node.bin` | same | Denied with a nil node | +| `result-failed.bin` | same | Failed | +| `asset-catalog.bin` | `gamecult.thing.asset_catalog.v1` | Two assets | + +## Refusals and tolerance + +| File | Payload | Expected | +|---|---|---| +| `result-thing1-flat.bin` | a MessagePack map with named members, the shape a Thing 1 provider wrote for a command result | refused when stored as a command result; the error names the kind and the record key and never a value | +| `node-unknown-arm.bin` | a surface whose root node has arm key 14, which Node does not declare | refused; the error names the kind and the record key, and decoded directly it names the union Node and the key 14, and never a value | +| `outcome-unknown-arm.bin` | a result whose outcome has arm key 4, which Outcome does not declare | refused in the same way, naming the union Outcome and the key 4 | +| `result-extra-slot.bin` | a Pending result with a fourth slot past the last one declared | read: it decodes to the Pending result with seq 1 (CultLib's extra-trailing-slots ruling: read, never written back) | +| `result-seq-zero.bin` | a result whose seq slot is 0 | refused; the error names the kind and the record key, decoded directly it names Seq, and never a value | +| `surface-version-negative.bin` | a surface whose version slot is -1 | refused in the same way, naming Version | +| `surface-label-dotted.bin` | a surface whose label is `b.c` | refused in the same way, naming Segment: a label holds no dot, so a record key splits at its last dot | +| `asset-extent-negative.bin` | an asset catalog whose first asset has width -5 | refused in the same way, naming Count | +| `rgba-channel-out-of-range.bin` | the Rgba slot array `[256, 0, 0, 255]` | refused when decoded as an Rgba: a channel is a byte, 0 to 255 | + +Domain refusals are the contract's rule, which the TypeScript and C# packages mirror: each is held to refuse these files, without the leaf set a Rust `u8` or a checked newtype has. + +## The nil vector + +`nil-positions.tsv` is written only by `crates/thing-contract/tests/nil_positions.rs` (`THING_BLESS=1`). For every element inside an array, at every depth, of each value golden above (the eleven `.bin` files from `surface-all-arms` to `asset-catalog`), the test replaces that element with nil (`0xC0`) and decodes the whole payload with the Rust decode door, `rmp_serde::from_slice` into the golden's kind. A row is `golden`, `path` (dotted array indices from the root, e.g. `3.0.1`), the element's byte `start` and `end` in the golden, and `ACCEPT` or `REFUSE`. An accepted variant re-encodes to the spliced bytes. Maps are not walked. + +A slot is required exactly when its Rust type is not optional, so `REFUSE` rows are the required slots and `ACCEPT` rows the optional ones. The Rust decode door decides every row; the TypeScript and C# packages keep no list of required slots and are held to every row. diff --git a/docs/thing2-build-spec.md b/docs/thing2-build-spec.md index 3c74b51..9a10f4a 100644 --- a/docs/thing2-build-spec.md +++ b/docs/thing2-build-spec.md @@ -277,10 +277,10 @@ This spec adds no contract change. Rulings that bind the packages: - **Test (per package):** the shared golden fixtures decode and re-encode byte-identical (M1, M3); the refusal goldens (`seq` 0, extra slot, unknown union arm) are refused; a hand-built object with seq 0 is refused at encode. -- **1x cross-store check:** the C# package opens a `.cc` store the TS package wrote - and the reverse, and both open Rust's. The check lives in `thing-testkit` as a - scripted cross-runtime job run on Yggdrasil's verify stopgap - (`tools/stopgap/ygg-verify.sh`), because only that image has all three runtimes. +- **1x cross-store check:** each runtime's own suite reads the other two runtimes' + committed stores in `tests/thing-contract-parity` (`written-by-cs.cc`, + `written-by-ts.cc`, `written-by-rs.cc`), each asserting the five records re-encode + to the shared goldens under the other runtimes' keys. No scripted cross-runtime job. It proves the catalog type names agree (map cut 1 waiting line). - **Prior art:** CultCache wire parity against the C# reference is the standing CultLib rule; probe P12-P13 are the evidence **[read, map]**; golden-file diff --git a/docs/thing2-map.md b/docs/thing2-map.md index 448c512..9faeab3 100644 --- a/docs/thing2-map.md +++ b/docs/thing2-map.md @@ -188,7 +188,7 @@ refused by `rmp_serde::from_slice` ("Seq: at least 1") and by format"); seq 0 through serde's in-memory `IntoDeserializer` is refused the same way, so the rule holds for any serde format. With `CommandResult.seq: Seq` and both composition versions `Version` (an `Int32` slot widened to `Int64`), -`goldens_match` still compares byte-identical, `surface.cc` included. The +`goldens_match` still compares byte-identical, `written-by-rs.cc` included. The derive reads each slot with `seq.next_element::()` (`cultcache-rs-derive/src/lib.rs`, `slot_codec`, at `b3bd8dd5`), so a field's type is the one owner of its domain on both paths. `DatabaseEntry` requires diff --git a/packages/thing-contract/test/store.test.ts b/packages/thing-contract/test/store.test.ts index 8b27404..426cc8e 100644 --- a/packages/thing-contract/test/store.test.ts +++ b/packages/thing-contract/test/store.test.ts @@ -1,4 +1,4 @@ -// surface.cc is a single-file store the Rust crate wrote with one record of each kind under a pinned clock. Opened through +// written-by-rs.cc (tests/thing-contract-parity) is a single-file store the Rust crate wrote with one record of each kind under a pinned clock. Opened through // CultCache with the five definitions it yields five records, each re-encodes to its value golden's bytes, and each record // key equals the key keys.ts builds from the value. import assert from "node:assert/strict"; @@ -21,18 +21,18 @@ import { thingContract, } from "@gamecult/thing-contract"; -import { PINNED_CLOCK, assetCatalogValue, fullComposition, fullSurface, golden, goldensDir, invocation } from "./builders.js"; +import { PINNED_CLOCK, assetCatalogValue, fullComposition, fullSurface, golden, invocation, parityDir } from "./builders.js"; async function open() { const cache = CultCache.builder() .withRegistry(thingContract) - .withGenericStore(new SingleFileMessagePackBackingStore(join(goldensDir, "surface.cc"))) + .withGenericStore(new SingleFileMessagePackBackingStore(join(parityDir, "written-by-rs.cc"))) .build(); await cache.pullAllBackingStores(); return cache; } -test("surface.cc opens to one record of each of the five kinds", async () => { +test("written-by-rs.cc opens to one record of each of the five kinds", async () => { const cache = await open(); const snapshot = cache.snapshot(); assert.equal(snapshot.length, 5); diff --git a/tests/GameCult.Thing.Contract.Tests/GoldenTests.cs b/tests/GameCult.Thing.Contract.Tests/GoldenTests.cs index 40baebe..c6d9861 100644 --- a/tests/GameCult.Thing.Contract.Tests/GoldenTests.cs +++ b/tests/GameCult.Thing.Contract.Tests/GoldenTests.cs @@ -109,7 +109,7 @@ public sealed class GoldenTests public void StoreRead() { var scratch = Path.Combine(Path.GetTempPath(), $"thing-contract-store-{Guid.NewGuid():N}.cc"); - File.Copy(Wire.GoldenPath("surface.cc"), scratch); + File.Copy(Path.Combine(AppContext.BaseDirectory, "parity", "written-by-rs.cc"), scratch); try { using var cache = CultCacheMessagePack.Create(scratch, new CultCacheOpenOptions { Registry = Wire.Registry, ReadOnly = true }); diff --git a/tests/thing-contract-parity/README.md b/tests/thing-contract-parity/README.md index 017de16..a793464 100644 --- a/tests/thing-contract-parity/README.md +++ b/tests/thing-contract-parity/README.md @@ -6,7 +6,8 @@ The names the C# package `GameCult.Thing.Contract` gives its types. The C# packa |---|---| | `catalog-members.tsv` | One row per document slot, `schemaId`, `slot`, `memberName`, `typeName`, as `Wire.Registry`'s catalog entries print them. A list slot reads `System.Collections.Immutable.ImmutableArray`. | | `type-names.txt` | The full name of every public type in `GameCult.Thing.Contract`, sorted ordinally. | -| `written-by-cs.cc` | A single-file store the C# package's CultCache wrote from its own builders, one record of each kind. `packages/thing-contract/test/parity.test.ts` reads it. Regenerate: `THING_PARITY_WRITE=1 dotnet test tests/GameCult.Thing.Contract.Tests` (`ParityTests.cs`). | -| `written-by-ts.cc` | The same five records, written by `@gamecult/thing-contract`'s CultCache. `ParityTests.cs` reads it. Regenerate: `THING_PARITY_WRITE=1 npm test` in `packages/thing-contract` (`parity.test.ts`). | +| `written-by-cs.cc` | A single-file store the C# package's CultCache wrote from its own builders, one record of each kind. `packages/thing-contract/test/parity.test.ts` and `crates/thing-contract/tests/foreign_stores.rs` read it. Regenerate: `THING_PARITY_WRITE=1 dotnet test tests/GameCult.Thing.Contract.Tests` (`ParityTests.cs`). | +| `written-by-rs.cc` | The same five records, written by `thing-contract`'s cultcache-rs under a pinned clock. `ParityTests.cs` and `parity.test.ts` read it. Regenerate: `THING_BLESS=1 cargo test -p thing-contract` (`crates/thing-contract/tests/goldens.rs`). | +| `written-by-ts.cc` | The same five records, written by `@gamecult/thing-contract`'s CultCache. `ParityTests.cs` and `foreign_stores.rs` read it. Regenerate: `THING_PARITY_WRITE=1 npm test` in `packages/thing-contract` (`parity.test.ts`). | -`tests/GameCult.Thing.Contract.Tests/NameSetTests.cs` fails when either file disagrees with the C# types. The two name files change only in the commit that changes the C# types, and the other runtime packages read them without writing them. Each `.cc` is written by its own package and read by the other. +`tests/GameCult.Thing.Contract.Tests/NameSetTests.cs` fails when either file disagrees with the C# types. The two name files change only in the commit that changes the C# types, and the other runtime packages read them without writing them. Each `.cc` is written by its own package and read by the other two. diff --git a/crates/thing-contract/tests/goldens/surface.cc b/tests/thing-contract-parity/written-by-rs.cc similarity index 100% rename from crates/thing-contract/tests/goldens/surface.cc rename to tests/thing-contract-parity/written-by-rs.cc