droboticflow/tests/frontend/annotation-persistence.test.mjs

164 lines
5.9 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { createAnnotationPersistence } from "../../src/lib/annotation-persistence.js";
import {
createAuthoritativeMutationContext,
createAuthoritativeMutationScope,
} from "../../src/lib/annotation-authoritative-mutation.js";
const deferred = () => {
let resolve, reject;
const promise = new Promise((yes, no) => {
resolve = yes;
reject = no;
});
return { promise, resolve, reject };
};
function fixture() {
const scope = createAuthoritativeMutationScope();
const context = createAuthoritativeMutationContext(scope, { taskId: "a", userId: "u" });
const events = [],
state = { version: 3, writable: true, wait: async () => true, read: async () => ({ version: state.version }) };
const persistence = createAnnotationPersistence({
scope,
context: context.current,
adapter: {
waitForPending: () => state.wait(),
version: () => state.version,
item: () => ({ id: "item-a", version: state.version, display: { name: "kept" } }),
read: (taskId) => state.read(taskId),
},
view: {
accept: (value) => events.push(value),
canWrite: () => state.writable,
begin: () => events.push("begin"),
idle: () => events.push("idle"),
blocked: () => events.push("blocked"),
},
});
return { persistence, scope, context, events, state };
}
test("explicit operations wait for autosave and use its newly confirmed version", async () => {
const f = fixture(),
pending = deferred();
f.state.wait = () => pending.promise;
const beginning = f.persistence.begin("a");
assert.equal(await f.persistence.begin("a"), null);
f.state.version = 4;
pending.resolve(true);
const token = await beginning;
assert.equal(token.version, 4);
f.persistence.end(token);
assert.deepEqual(f.events, ["begin", "idle"]);
});
test("uncertain or failed autosave never writes and releases the explicit-operation lease", async () => {
for (const wait of [
async () => false,
async () => {
throw new Error("network");
},
]) {
const f = fixture();
f.state.wait = wait;
assert.equal(await f.persistence.begin("a"), null);
assert.equal(f.scope.busy(), false);
assert.deepEqual(f.events, ["begin", "idle", "blocked"]);
}
});
test("task/account changes or revoked editing during autosave cannot acquire a stale write", async () => {
for (const change of [
(f) => f.context.switchTo({ taskId: "b", userId: "u" }),
(f) => f.context.switchTo({ taskId: "a", userId: "v" }),
(f) => {
f.state.writable = false;
},
]) {
const f = fixture(),
pending = deferred();
f.state.wait = () => pending.promise;
const beginning = f.persistence.begin("a");
change(f);
pending.resolve(true);
assert.equal(await beginning, null);
assert.equal(f.scope.busy(), false);
}
const f = fixture();
assert.equal(await f.persistence.begin("other"), null);
assert.deepEqual(f.events, []);
});
test("the same persistence seam handles successful, lost, rejected and unknown outcomes", async () => {
for (const mode of ["success", "lost", "rejected", "unknown"]) {
const f = fixture(),
token = await f.persistence.begin("a");
let reads = 0,
writes = 0;
f.state.read = async (taskId) => {
assert.equal(taskId, "a");
reads++;
if (mode === "unknown") throw new Error("offline");
return { applied: mode === "lost" };
};
const result = await f.persistence.settle(token, {
write: async () => {
writes++;
if (mode !== "success") throw new Error("response lost");
return { version: 4 };
},
isApplied: (value) => value.applied,
});
assert.equal(result.status, ["success", "lost"].includes(mode) ? "confirmed" : mode);
assert.equal(writes, 1);
assert.equal(reads, mode === "success" ? 0 : 1);
f.persistence.end(token);
assert.equal(f.scope.busy(), false);
}
});
test("late responses and cleanup cannot release a newer task's operation", async () => {
const f = fixture(),
token = await f.persistence.begin("a"),
response = deferred();
const write = f.persistence.settle(token, { write: () => response.promise, isApplied: () => true });
f.context.switchTo({ taskId: "b", userId: "u" });
const newer = await f.persistence.begin("b");
response.resolve({ version: 4 });
assert.equal((await write).status, "stale");
f.persistence.end(token);
assert.equal(f.scope.busy(), true);
f.persistence.end(newer);
assert.equal(f.scope.busy(), false);
});
test("authoritative adoption is scoped, monotonic, normalized and keeps display metadata", () => {
const f = fixture();
const workspace = { sliceSegments: [{ id: "s", start: 0, end: 10 }], segments: null, operations: null };
const item = { id: "item-a", version: 4 };
for (const other of [
{ taskId: "b" },
{ userId: "other" },
{ item: { id: "different", version: 4 } },
{ item: { ...item, version: 2 } },
]) {
assert.equal(f.persistence.accept({ taskId: "a", userId: "u", item, workspace, ...other }), null);
}
assert.deepEqual(f.events, []);
const accepted = f.persistence.accept({ taskId: "a", userId: "u", item, workspace, mode: "draft" });
assert.deepEqual(accepted.segments, []);
assert.equal(f.events[0].mode, "draft");
assert.deepEqual(f.events[0].item.display, { name: "kept" });
assert.throws(() =>
f.persistence.accept({ taskId: "a", userId: "u", item, workspace: { ...workspace, segments: "broken" } }),
);
assert.equal(f.events.length, 1);
});
test("a lease invalidated before dispatch never sends its explicit write", async () => {
const f = fixture(),
token = await f.persistence.begin("a");
f.context.switchTo({ taskId: "b", userId: "u" });
const result = await f.persistence.settle(token, {
write: async () => assert.fail("stale write dispatched"),
isApplied: () => true,
});
assert.equal(result.status, "stale");
});