cultnet-ts: database_change_raw carries and validates commitId and commitCount
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
9b6253660b
commit
9a42823075
2 changed files with 40 additions and 0 deletions
|
|
@ -424,6 +424,10 @@ export interface CultNetDatabaseChangeRawMessage {
|
|||
document?: CultNetRawDocumentRecord;
|
||||
recordKey?: string;
|
||||
schemaId?: string;
|
||||
/** Opaque, one subscription: changes sharing a commitId are the changes of one commit. */
|
||||
commitId?: string;
|
||||
/** How many changes the commit carries, at least two. */
|
||||
commitCount?: number;
|
||||
}
|
||||
|
||||
export interface CultNetSchemaDescriptor {
|
||||
|
|
@ -1397,6 +1401,15 @@ function validateDatabaseChangeRawMessage(
|
|||
);
|
||||
}
|
||||
|
||||
if (candidate.commitId !== undefined && candidate.commitId !== null &&
|
||||
(typeof candidate.commitId !== "string" || candidate.commitId.length === 0)) {
|
||||
throw new Error("Validation failed for cultnet.database_change_raw.v0: /commitId: must be a non-empty string");
|
||||
}
|
||||
if (candidate.commitCount !== undefined && candidate.commitCount !== null &&
|
||||
(typeof candidate.commitCount !== "number" || !Number.isInteger(candidate.commitCount) || candidate.commitCount < 2)) {
|
||||
throw new Error("Validation failed for cultnet.database_change_raw.v0: /commitCount: must be an integer of at least 2");
|
||||
}
|
||||
|
||||
if (candidate.changeKind === "removed") {
|
||||
requireNonEmptyString(candidate.recordKey, "/recordKey");
|
||||
requireNonEmptyString(candidate.schemaId, "/schemaId");
|
||||
|
|
|
|||
|
|
@ -54,6 +54,7 @@ import {
|
|||
validateGhostlightAgentStateGenerated,
|
||||
validateGhostlightAgentState,
|
||||
shardServes,
|
||||
type CultNetDatabaseChangeRawMessage,
|
||||
type CultNetLoginMessage,
|
||||
type GhostlightAgentStateShape,
|
||||
type GhostlightAgentStateDocument,
|
||||
|
|
@ -1688,6 +1689,32 @@ test("database subscription contracts match the C# live document lane", () => {
|
|||
assert.equal(removed.schemaVersion, "cultnet.database_change_raw.v0");
|
||||
assert.equal(removed.recordKey, "counter:main");
|
||||
|
||||
const grouped = parseCultNetMessage({
|
||||
schemaVersion: "cultnet.database_change_raw.v0",
|
||||
messageId: "change-counter-3",
|
||||
subscriptionId: "counter:browser-1",
|
||||
changeKind: "removed",
|
||||
recordKey: "counter:main",
|
||||
schemaId: "sample.counter_state.v1",
|
||||
commitId: "c1",
|
||||
commitCount: 2,
|
||||
});
|
||||
assert.equal((grouped as CultNetDatabaseChangeRawMessage).commitId, "c1");
|
||||
assert.equal((grouped as CultNetDatabaseChangeRawMessage).commitCount, 2);
|
||||
assert.deepEqual(encodeCultNetMessageForWire(grouped), grouped, "both commit fields travel");
|
||||
assert.equal((removed as CultNetDatabaseChangeRawMessage).commitId, undefined, "a change without them parses as before");
|
||||
const changeWith = (extra: Record<string, unknown>) => () => parseCultNetMessage({
|
||||
schemaVersion: "cultnet.database_change_raw.v0",
|
||||
messageId: "change-counter-4",
|
||||
subscriptionId: "counter:browser-1",
|
||||
changeKind: "removed",
|
||||
recordKey: "counter:main",
|
||||
schemaId: "sample.counter_state.v1",
|
||||
...extra,
|
||||
});
|
||||
for (const commitId of ["", 5]) assert.throws(changeWith({ commitId }), /\/commitId/, `commitId ${JSON.stringify(commitId)}`);
|
||||
for (const commitCount of [1, 0, 2.5, "two", -3]) assert.throws(changeWith({ commitCount }), /\/commitCount/, `commitCount ${commitCount}`);
|
||||
|
||||
const unsubscribe = parseCultNetMessage({
|
||||
schemaVersion: "cultnet.database_unsubscribe.v0",
|
||||
messageId: "unsubscribe-counter",
|
||||
|
|
|
|||
Loading…
Reference in a new issue