45 lines
1.9 KiB
JavaScript
45 lines
1.9 KiB
JavaScript
const assert = require("node:assert/strict");
|
|
const sync = require("../static/sync.js");
|
|
|
|
// A second client advances the shared revision while this client is saving.
|
|
// The in-flight local value must not be replaced by the older server value.
|
|
const local = { "crop-parsnip": true, "fish-eel": false };
|
|
const remoteWhileSaving = {
|
|
revision: 6,
|
|
checks: { "crop-parsnip": false, "fish-eel": true }
|
|
};
|
|
const merged = sync.mergeSnapshot(local, new Set(["crop-parsnip"]), 5, remoteWhileSaving);
|
|
assert.equal(merged.applied, true);
|
|
assert.equal(merged.checks["crop-parsnip"], true);
|
|
assert.equal(merged.checks["fish-eel"], true);
|
|
|
|
// A response already superseded by a later write must never roll the client back.
|
|
const stale = sync.mergeSnapshot(merged.checks, new Set(), 7, remoteWhileSaving);
|
|
assert.equal(stale.applied, false);
|
|
assert.equal(stale.revision, 7);
|
|
|
|
// The write response is authoritative for its own item even after an interleaved poll.
|
|
const confirmed = sync.confirmWrite(merged.checks, 6, {
|
|
itemId: "crop-parsnip",
|
|
checked: true,
|
|
revision: 7
|
|
});
|
|
assert.equal(confirmed.checks["crop-parsnip"], true);
|
|
assert.equal(confirmed.revision, 7);
|
|
|
|
console.log("sync race tests passed");
|
|
|
|
// Plantings follow the same rules: an in-flight local planting (or removal) wins.
|
|
const springFive = { year: 1, season: "spring", day: 5 };
|
|
const plantMerge = sync.mergeSnapshot({}, new Set(), 7, {
|
|
revision: 8,
|
|
checks: {},
|
|
plantings: { "crop-parsnip": { year: 1, season: "spring", day: 1 }, "crop-potato": springFive }
|
|
}, { "crop-parsnip": springFive }, new Set(["crop-parsnip", "crop-potato"]));
|
|
assert.deepEqual(plantMerge.plantings, { "crop-parsnip": springFive });
|
|
|
|
const unplanted = sync.confirmPlanting(plantMerge.plantings, 8, { itemId: "crop-parsnip", planted: null, revision: 9 });
|
|
assert.deepEqual(unplanted.plantings, {});
|
|
assert.equal(unplanted.revision, 9);
|
|
|
|
console.log("planting sync tests passed");
|