forked from longtao.wu/droboticflow
260 lines
8.5 KiB
JavaScript
260 lines
8.5 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import { createReviewDraftPersistence } from "../../src/lib/review-draft-persistence.js";
|
|
class HttpError extends Error {
|
|
constructor(status, code = "") {
|
|
super(code || `HTTP ${status}`);
|
|
this.status = status;
|
|
this.code = code;
|
|
}
|
|
}
|
|
const deferred = () => {
|
|
let resolve, reject;
|
|
const promise = new Promise((yes, no) => {
|
|
resolve = yes;
|
|
reject = no;
|
|
});
|
|
return { promise, resolve, reject };
|
|
};
|
|
function fixture() {
|
|
let current = true;
|
|
const calls = [],
|
|
payload = {
|
|
operationId: "op-1",
|
|
version: 0,
|
|
resultVersion: "r1",
|
|
problems: [{ id: "p", text: "saved" }],
|
|
confirmations: [{ opinionId: "old", resolved: true }],
|
|
};
|
|
const saved = { itemId: "item", version: 1, resultVersion: "r1", operationId: "op-1", problems: payload.problems };
|
|
const drafts = {
|
|
get: async () => {
|
|
calls.push(["get"]);
|
|
return { draft: saved };
|
|
},
|
|
save: async (item, p, headers) => {
|
|
calls.push(["save", item, p, headers]);
|
|
return { draft: saved };
|
|
},
|
|
};
|
|
const operations = {
|
|
get: async (...args) => {
|
|
calls.push(["receipt", ...args]);
|
|
throw new HttpError(404);
|
|
},
|
|
};
|
|
const writer = createReviewDraftPersistence({ drafts, operations, isApiError: (e) => e instanceof HttpError });
|
|
const input = {
|
|
itemId: "item",
|
|
pending: { payload },
|
|
retry: false,
|
|
isCurrent: () => current,
|
|
headers: () => ({ lease: "one" }),
|
|
isApplied: (d) => d.operationId === payload.operationId,
|
|
};
|
|
return {
|
|
drafts,
|
|
operations,
|
|
calls,
|
|
saved,
|
|
input,
|
|
write: () => writer.write(input),
|
|
invalidate: () => (current = false),
|
|
};
|
|
}
|
|
|
|
test("new writes adopt readback evidence rather than the PUT response alone", async () => {
|
|
const f = fixture();
|
|
const result = await f.write();
|
|
assert.equal(result.status, "applied");
|
|
assert.equal(result.draft, f.saved);
|
|
assert.deepEqual(
|
|
f.calls.map((c) => c[0]),
|
|
["save", "get"],
|
|
);
|
|
assert.equal(f.calls[0][2], f.input.pending.payload);
|
|
});
|
|
test("an already obsolete write issues no API request or lease lookup", async () => {
|
|
const f = fixture();
|
|
f.invalidate();
|
|
f.input.headers = () => assert.fail("stale lease lookup");
|
|
assert.equal((await f.write()).status, "stale");
|
|
assert.deepEqual(f.calls, []);
|
|
});
|
|
test("lost-response retry adopts already-applied draft without another PUT", async () => {
|
|
const f = fixture();
|
|
f.input.retry = true;
|
|
assert.equal((await f.write()).status, "applied");
|
|
assert.deepEqual(
|
|
f.calls.map((c) => c[0]),
|
|
["get"],
|
|
);
|
|
});
|
|
test("unknown-result retry reads the draft and receipt before resending the same payload", async () => {
|
|
const f = fixture();
|
|
f.input.retry = true;
|
|
let reads = 0;
|
|
f.drafts.get = async () => ({
|
|
draft: ++reads === 1 ? { ...f.saved, operationId: "different", version: 0 } : f.saved,
|
|
});
|
|
assert.equal((await f.write()).status, "applied");
|
|
assert.deepEqual(
|
|
f.calls.map((c) => c[0]),
|
|
["receipt", "save"],
|
|
);
|
|
assert.equal(f.calls[1][2], f.input.pending.payload);
|
|
assert.equal(f.calls[1][2].operationId, "op-1");
|
|
});
|
|
test("failed-before-effects receipt unlocks the obsolete pending operation", async () => {
|
|
const f = fixture();
|
|
f.input.retry = true;
|
|
f.input.isApplied = () => false;
|
|
f.operations.get = async () => ({ status: "failed_before_effects" });
|
|
const result = await f.write();
|
|
assert.equal(result.status, "blocked");
|
|
assert.equal(result.dropPending, true);
|
|
assert.equal(result.draft, f.saved);
|
|
assert.ok(!f.calls.some((c) => c[0] === "save"));
|
|
});
|
|
for (const reason of ["scope switch", "lease generation change", "unmount"]) {
|
|
test(`a receipt 404 after ${reason} cannot dispatch the old operation with a new lease`, async () => {
|
|
const f = fixture(),
|
|
wait = deferred();
|
|
f.input.retry = true;
|
|
f.input.isApplied = () => false;
|
|
f.operations.get = () => wait.promise;
|
|
const pending = f.write();
|
|
await Promise.resolve();
|
|
f.invalidate();
|
|
wait.reject(new HttpError(404));
|
|
assert.equal((await pending).status, "stale");
|
|
assert.ok(!f.calls.some((c) => c[0] === "save"));
|
|
});
|
|
}
|
|
test("receipt query failures retain pending evidence and never resend automatically", async () => {
|
|
const f = fixture();
|
|
f.input.retry = true;
|
|
f.input.isApplied = () => false;
|
|
f.operations.get = async () => {
|
|
throw new Error("offline");
|
|
};
|
|
const result = await f.write();
|
|
assert.equal(result.dropPending, false);
|
|
assert.ok(!f.calls.some((c) => c[0] === "save"));
|
|
});
|
|
for (const retry of [false, true]) {
|
|
test(`lease headers failing before dispatch have known effects only for a new operation (retry=${retry})`, async () => {
|
|
const f = fixture();
|
|
f.input.retry = retry;
|
|
f.input.isApplied = () => false;
|
|
f.input.headers = () => {
|
|
throw new Error("lease lost");
|
|
};
|
|
const result = await f.write();
|
|
assert.equal(result.dropPending, !retry);
|
|
assert.ok(!f.calls.some((c) => c[0] === "save"));
|
|
});
|
|
}
|
|
for (const [status, code, drop] of [
|
|
[400, "invalid", true],
|
|
[403, "review_session_lost", true],
|
|
[408, "timeout", false],
|
|
[409, "review_write_in_progress", false],
|
|
[500, "server", false],
|
|
]) {
|
|
test(`first PUT error ${status}/${code} has dropPending=${drop}`, async () => {
|
|
const f = fixture();
|
|
f.drafts.save = async () => {
|
|
throw new HttpError(status, code);
|
|
};
|
|
const result = await f.write();
|
|
assert.equal(result.status, "blocked");
|
|
assert.equal(result.dropPending, drop);
|
|
assert.equal(result.sessionLost, code === "review_session_lost");
|
|
});
|
|
}
|
|
test("an HTTP rejection during retry does not prove the earlier attempt had no effect", async () => {
|
|
const f = fixture();
|
|
f.input.retry = true;
|
|
f.input.isApplied = () => false;
|
|
f.drafts.save = async () => {
|
|
throw new HttpError(400);
|
|
};
|
|
assert.equal((await f.write()).dropPending, false);
|
|
});
|
|
test("PUT transport failure leaves the immutable operation unknown", async () => {
|
|
const f = fixture();
|
|
f.drafts.save = async () => {
|
|
throw new Error("lost response");
|
|
};
|
|
assert.equal((await f.write()).dropPending, false);
|
|
});
|
|
test("post-PUT readback 403 cannot be mistaken for a rejected PUT", async () => {
|
|
const f = fixture();
|
|
f.drafts.get = async () => {
|
|
throw new HttpError(403, "review_session_lost");
|
|
};
|
|
const result = await f.write();
|
|
assert.equal(result.dropPending, false);
|
|
assert.equal(result.sessionLost, true);
|
|
});
|
|
test("a successful PUT whose readback lacks evidence is not success", async () => {
|
|
const f = fixture();
|
|
f.input.isApplied = () => false;
|
|
const result = await f.write();
|
|
assert.equal(result.status, "blocked");
|
|
assert.equal(result.dropPending, false);
|
|
});
|
|
for (const retry of [false, true]) {
|
|
test(`changed result versions release the old pending operation (retry=${retry})`, async () => {
|
|
const f = fixture();
|
|
f.input.retry = retry;
|
|
f.drafts.get = async () => ({ draft: { ...f.saved, resultVersion: "r2" } });
|
|
const result = await f.write();
|
|
assert.equal(result.resultChanged, true);
|
|
assert.equal(result.dropPending, true);
|
|
if (retry) assert.deepEqual(f.calls, []);
|
|
});
|
|
}
|
|
test("CAS conflict can still be confirmed by applied readback", async () => {
|
|
const f = fixture();
|
|
f.drafts.save = async () => {
|
|
throw new HttpError(409, "review_draft_version_conflict");
|
|
};
|
|
assert.equal((await f.write()).status, "applied");
|
|
});
|
|
test("CAS conflict returns the current baseline without rewriting local text", async () => {
|
|
const f = fixture();
|
|
f.input.isApplied = () => false;
|
|
f.drafts.save = async () => {
|
|
throw new HttpError(409, "review_draft_version_conflict");
|
|
};
|
|
const result = await f.write();
|
|
assert.equal(result.dropPending, true);
|
|
assert.equal(result.draft, f.saved);
|
|
assert.match(result.error, /已被修改/);
|
|
});
|
|
test("stale conflict readback cannot affect the new scope", async () => {
|
|
const f = fixture(),
|
|
wait = deferred();
|
|
f.drafts.save = async () => {
|
|
throw new HttpError(409, "review_draft_version_conflict");
|
|
};
|
|
f.drafts.get = () => wait.promise;
|
|
const pending = f.write();
|
|
await Promise.resolve();
|
|
f.invalidate();
|
|
wait.reject(new Error("offline"));
|
|
assert.equal((await pending).status, "stale");
|
|
});
|
|
test("ownership loss after PUT stops readback and leaves adoption to the new lifetime", async () => {
|
|
const f = fixture(),
|
|
wait = deferred();
|
|
f.drafts.save = () => wait.promise;
|
|
const pending = f.write();
|
|
f.invalidate();
|
|
wait.resolve({ draft: f.saved });
|
|
assert.equal((await pending).status, "stale");
|
|
assert.deepEqual(f.calls, []);
|
|
});
|