Merge pull request 'docs/start: verbatim drift check for C# and TS first-call blocks' (#7) from libby/start-drift-checks into main
Some checks failed
CultMesh QUIC native / Build win32-x64 (push) Has been skipped
CultMesh QUIC native / Build linux-x64 (push) Has been skipped
CultMesh QUIC native / Release native binaries (push) Has been skipped
CultMesh portability and performance / ubuntu-latest one-command multiplayer onboarding (push) Has been cancelled
CultMesh portability and performance / windows-latest one-command multiplayer onboarding (push) Has been cancelled
CultMesh portability and performance / ubuntu-latest reactive semantics and leases (push) Has been cancelled
CultMesh portability and performance / windows-latest reactive semantics and leases (push) Has been cancelled
CultNet cross-runtime interop / C# / TypeScript / Rust / Python / Kotlin (push) Has been cancelled
CultMesh QUIC native / Verify committed binaries match SHA256SUMS (push) Has been cancelled
Some checks failed
CultMesh QUIC native / Build win32-x64 (push) Has been skipped
CultMesh QUIC native / Build linux-x64 (push) Has been skipped
CultMesh QUIC native / Release native binaries (push) Has been skipped
CultMesh portability and performance / ubuntu-latest one-command multiplayer onboarding (push) Has been cancelled
CultMesh portability and performance / windows-latest one-command multiplayer onboarding (push) Has been cancelled
CultMesh portability and performance / ubuntu-latest reactive semantics and leases (push) Has been cancelled
CultMesh portability and performance / windows-latest reactive semantics and leases (push) Has been cancelled
CultNet cross-runtime interop / C# / TypeScript / Rust / Python / Kotlin (push) Has been cancelled
CultMesh QUIC native / Verify committed binaries match SHA256SUMS (push) Has been cancelled
This commit is contained in:
commit
cafffbf6d5
3 changed files with 129 additions and 75 deletions
|
|
@ -1,11 +1,15 @@
|
|||
import assert from "node:assert/strict";
|
||||
import { mkdtemp, rm } from "node:fs/promises";
|
||||
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import test from "node:test";
|
||||
|
||||
// The TypeScript first-call snippet that docs/start/README.md shows: define a typed document, store it
|
||||
// in a .cc file, read it back through a second cache. Keep it small; it is the front door. The docs show
|
||||
// the region between the `snippet` markers verbatim, except that the package import reads
|
||||
// "@gamecult/cultcache-ts" where this file reads "../src/index"; the docs test below fails when they differ.
|
||||
// snippet:begin
|
||||
import { z } from "zod";
|
||||
|
||||
import {
|
||||
CultCache,
|
||||
SingleFileMessagePackBackingStore,
|
||||
|
|
@ -13,8 +17,6 @@ import {
|
|||
defineDocumentType,
|
||||
} from "../src/index";
|
||||
|
||||
// The TypeScript first-call snippet that docs/start/README.md links: define a typed document,
|
||||
// store it in a .cc file, read it back through a second cache. Keep it small; it is the front door.
|
||||
const itemDocument = defineDocumentType({
|
||||
type: "item",
|
||||
schemaId: "gamecult.first_call_item.v1",
|
||||
|
|
@ -26,26 +28,43 @@ const itemDocument = defineDocumentType({
|
|||
});
|
||||
const registry = defineDocumentRegistry(itemDocument);
|
||||
|
||||
async function firstCall(path: string) {
|
||||
const cache = CultCache.builder()
|
||||
.withRegistry(registry)
|
||||
.withGenericStore(new SingleFileMessagePackBackingStore(path))
|
||||
.build();
|
||||
await cache.pullAllBackingStores();
|
||||
await cache.put(itemDocument, "item:potion", { name: "Potion", category: "Consumable", value: 50 });
|
||||
|
||||
const reopened = CultCache.builder()
|
||||
.withRegistry(registry)
|
||||
.withGenericStore(new SingleFileMessagePackBackingStore(path))
|
||||
.build();
|
||||
await reopened.pullAllBackingStores();
|
||||
const potion = reopened.get(itemDocument, "item:potion");
|
||||
const byName = reopened.getByName(itemDocument, "Potion");
|
||||
return { potion, byName };
|
||||
}
|
||||
// snippet:end
|
||||
|
||||
test("define, store and read back a typed document", async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), "cultcache-first-call-"));
|
||||
const path = join(dir, "first-call.cc");
|
||||
try {
|
||||
const cache = CultCache.builder()
|
||||
.withRegistry(registry)
|
||||
.withGenericStore(new SingleFileMessagePackBackingStore(path))
|
||||
.build();
|
||||
await cache.pullAllBackingStores();
|
||||
await cache.put(itemDocument, "item:potion", { name: "Potion", category: "Consumable", value: 50 });
|
||||
|
||||
const reopened = CultCache.builder()
|
||||
.withRegistry(registry)
|
||||
.withGenericStore(new SingleFileMessagePackBackingStore(path))
|
||||
.build();
|
||||
await reopened.pullAllBackingStores();
|
||||
|
||||
assert.equal(reopened.get(itemDocument, "item:potion")?.value, 50);
|
||||
assert.equal(reopened.getByName(itemDocument, "Potion")?.category, "Consumable");
|
||||
const { potion, byName } = await firstCall(join(dir, "first-call.cc"));
|
||||
assert.equal(potion?.value, 50);
|
||||
assert.equal(byName?.category, "Consumable");
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("docs show the tested snippet", async () => {
|
||||
// Compiled to dist-test/test/first-call.test.js, so the package root is two levels up.
|
||||
const source = await readFile(join(__dirname, "..", "..", "test", "first-call.test.ts"), "utf8");
|
||||
const snippet = source
|
||||
.split("// snippet:begin\n")[1]
|
||||
.split("// snippet:end\n")[0]
|
||||
.replace('"../src/index"', '"@gamecult/cultcache-ts"');
|
||||
const docs = (await readFile(join(__dirname, "..", "..", "..", "..", "docs", "start", "README.md"), "utf8")).replace(/\r\n/g, "\n");
|
||||
assert.ok(docs.includes("```ts\n" + snippet + "```"), "docs/start/README.md no longer shows test/first-call.test.ts");
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue