215 lines
5.9 KiB
JavaScript
215 lines
5.9 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import {
|
|
freezeSliceEditSession,
|
|
createSliceEditSession,
|
|
sliceEditWorkspaceChanged,
|
|
} from "../../src/lib/slice-edit-session.js";
|
|
const deferred = () => {
|
|
let resolve, reject;
|
|
const promise = new Promise((yes, no) => {
|
|
resolve = yes;
|
|
reject = no;
|
|
});
|
|
return { promise, resolve, reject };
|
|
};
|
|
function fixture() {
|
|
let identity = { taskId: "task", userId: "u" };
|
|
const live = {
|
|
taskId: "task",
|
|
slices: [{ id: "s", start: 0, end: 10 }],
|
|
actions: [{ id: "a", start: 0, end: 10, description: "kept", status: "described" }],
|
|
drafts: [],
|
|
};
|
|
const controller = createSliceEditSession({ context: () => identity }),
|
|
session = controller.freeze(live);
|
|
return {
|
|
live,
|
|
session,
|
|
controller,
|
|
switchTo(next) {
|
|
identity = next;
|
|
},
|
|
options: (apply) => ({ current: () => live, canApply: () => true, apply }),
|
|
};
|
|
}
|
|
test("frozen edits preserve original actions and complete drafts without accepting unfinished ranges", async () => {
|
|
const f = fixture();
|
|
f.live.drafts = [{ annotation: { ...f.live.actions[0], description: "new" } }];
|
|
const session = freezeSliceEditSession(f.live);
|
|
assert.equal(session.snapshot.actions[0].description, "new");
|
|
assert.equal(session.baseSnapshot.actions[0].description, "kept");
|
|
f.live.actions[0].description = "changed";
|
|
assert.equal(session.baseSnapshot.actions[0].description, "kept");
|
|
assert.equal(sliceEditWorkspaceChanged(session.baseSnapshot, f.live.slices, f.live.actions), true);
|
|
});
|
|
test("panel and quick commit share duplicate suppression until the original request completes", async () => {
|
|
const f = fixture(),
|
|
response = deferred();
|
|
let writes = 0;
|
|
const first = f.controller.commit(
|
|
f.session,
|
|
f.options(() => {
|
|
writes++;
|
|
return response.promise;
|
|
}),
|
|
);
|
|
assert.equal(
|
|
(
|
|
await f.controller.commit(
|
|
f.session,
|
|
f.options(() => {
|
|
writes++;
|
|
}),
|
|
)
|
|
).status,
|
|
"blocked",
|
|
);
|
|
assert.equal(f.controller.busy(), true);
|
|
response.resolve(true);
|
|
assert.equal((await first).status, "applied");
|
|
assert.equal(writes, 1);
|
|
assert.equal(f.controller.busy(), false);
|
|
});
|
|
test("save rejection keeps a frozen session reusable for retry", async () => {
|
|
const f = fixture();
|
|
await assert.rejects(
|
|
f.controller.commit(
|
|
f.session,
|
|
f.options(async () => {
|
|
throw new Error("offline");
|
|
}),
|
|
),
|
|
/offline/,
|
|
);
|
|
assert.equal(f.controller.busy(), false);
|
|
assert.equal(
|
|
(
|
|
await f.controller.commit(
|
|
f.session,
|
|
f.options(async () => false),
|
|
)
|
|
).status,
|
|
"blocked",
|
|
);
|
|
assert.equal(
|
|
(
|
|
await f.controller.commit(
|
|
f.session,
|
|
f.options(async () => true),
|
|
)
|
|
).status,
|
|
"applied",
|
|
);
|
|
});
|
|
test("changed drafts and unfinished endpoints cannot be consumed by an edit", async () => {
|
|
const f = fixture();
|
|
let writes = 0;
|
|
f.live.drafts = [{ annotation: { ...f.live.actions[0], description: "changed" } }];
|
|
await assert.rejects(
|
|
f.controller.commit(
|
|
f.session,
|
|
f.options(async () => {
|
|
writes++;
|
|
}),
|
|
),
|
|
/其它修改/,
|
|
);
|
|
f.live.drafts[0].selectingEnd = true;
|
|
const unfinished = f.controller.freeze(f.live);
|
|
await assert.rejects(
|
|
f.controller.commit(
|
|
unfinished,
|
|
f.options(async () => {
|
|
writes++;
|
|
}),
|
|
),
|
|
/结束点/,
|
|
);
|
|
assert.equal(writes, 0);
|
|
});
|
|
test("equal cloned content is valid; late completion after switching away and back is stale", async () => {
|
|
const f = fixture();
|
|
f.live.actions = structuredClone(f.live.actions);
|
|
const response = deferred(),
|
|
first = f.controller.commit(
|
|
f.session,
|
|
f.options(() => response.promise),
|
|
);
|
|
f.switchTo({ taskId: "other", userId: "u" });
|
|
f.switchTo({ taskId: "task", userId: "u" });
|
|
response.resolve(true);
|
|
assert.equal((await first).status, "stale");
|
|
assert.equal(f.controller.busy(), false);
|
|
assert.equal(
|
|
(
|
|
await f.controller.commit(
|
|
f.controller.freeze(f.live),
|
|
f.options(async () => true),
|
|
)
|
|
).status,
|
|
"applied",
|
|
);
|
|
});
|
|
|
|
test("sessions reject changed slice geometry and identities before sending a write", async () => {
|
|
const f = fixture();
|
|
let writes = 0;
|
|
f.live.slices = [{ ...f.live.slices[0], name: "externally renamed" }];
|
|
await assert.rejects(
|
|
f.controller.commit(
|
|
f.session,
|
|
f.options(async () => {
|
|
writes++;
|
|
}),
|
|
),
|
|
/其它修改/,
|
|
);
|
|
f.live.slices = structuredClone(f.session.baseSnapshot.slices);
|
|
f.switchTo({ taskId: "task", userId: "other" });
|
|
assert.equal(
|
|
(
|
|
await f.controller.commit(
|
|
f.session,
|
|
f.options(async () => {
|
|
writes++;
|
|
}),
|
|
)
|
|
).status,
|
|
"stale",
|
|
);
|
|
assert.equal(writes, 0);
|
|
});
|
|
test("only the current gesture preview may differ from its frozen slice geometry", async () => {
|
|
const f = fixture();
|
|
f.live.slices = [{ ...f.live.slices[0], name: "preview" }];
|
|
const result = await f.controller.commit(f.session, {
|
|
...f.options(async () => true),
|
|
previewSlices: structuredClone(f.live.slices),
|
|
});
|
|
assert.equal(result.status, "applied");
|
|
});
|
|
|
|
test("an old pending request cannot block or release a new task's commit", async () => {
|
|
const f = fixture(),
|
|
older = deferred(),
|
|
newer = deferred();
|
|
const first = f.controller.commit(
|
|
f.session,
|
|
f.options(() => older.promise),
|
|
);
|
|
f.switchTo({ taskId: "other", userId: "u" });
|
|
f.live.taskId = "other";
|
|
const second = f.controller.commit(
|
|
f.controller.freeze(f.live),
|
|
f.options(() => newer.promise),
|
|
);
|
|
assert.equal(f.controller.busy(), true);
|
|
older.resolve(true);
|
|
assert.equal((await first).status, "stale");
|
|
assert.equal(f.controller.busy(), true);
|
|
newer.resolve(true);
|
|
assert.equal((await second).status, "applied");
|
|
assert.equal(f.controller.busy(), false);
|
|
});
|