feat: machine translation resumes after a page reload
The run is recorded at every page and picked up from the translator's finished job; one run across tabs via Web Locks. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7fa88e4025
commit
68b66a1d94
5 files changed
+200
-30
No files matched your search
@@ -138,3 +138,39 @@ test("addresses are completed and cleaned", () => {
|
||||
assert.equal(normalizeUrl(" 127.0.0.1:8787/ "), "http://127.0.0.1:8787");
|
||||
assert.equal(normalizeUrl("https://t.example//"), "https://t.example");
|
||||
});
|
||||
|
||||
test("a run that was interrupted picks up the page the translator finished meanwhile", async () => {
|
||||
const server = fakeServer(["a", "b", "c", "d", "e"]);
|
||||
const translator = fakeTranslator();
|
||||
// the page "a","b" was sent before the reload; the translator completed it by itself
|
||||
const sent = await translator.createJob("p", [{ id: "0", text: "a" }, { id: "1", text: "b" }]);
|
||||
const checkpoints = [];
|
||||
const outcome = await run(translator, server, {
|
||||
initial: { total: 5, done: 0, failed: 0, skip: 0 },
|
||||
resume: { jobId: sent.id, keys: ["k0", "k1"] },
|
||||
onCheckpoint: (c) => checkpoints.push(c),
|
||||
});
|
||||
assert.equal(outcome, "finished");
|
||||
assert.deepEqual(Object.keys(server.saved), ["k0", "k1", "k2", "k3", "k4"]);
|
||||
// the first page was not sent twice: the resumed job plus two new pages
|
||||
assert.equal(translator.created.length, 3);
|
||||
const last = checkpoints.at(-1);
|
||||
assert.deepEqual([last.jobId, last.done, last.failed], [null, 5, 0]);
|
||||
});
|
||||
|
||||
test("a job the translator no longer knows is skipped and its lines come back with the list", async () => {
|
||||
const server = fakeServer(["a", "b", "c"]);
|
||||
const translator = { ...fakeTranslator(), async job() { throw new Error("404"); } };
|
||||
// `job` is also used while waiting, but the resumed job fails at once, so nothing waits
|
||||
const created = [];
|
||||
const client = { ...translator, async createJob(...args) { const j = await fakeTranslator().createJob(...args); created.push(j); return j; } };
|
||||
const outcome = await run(client, server, { pageSize: 3, resume: { jobId: "gone", keys: ["k0"] } });
|
||||
assert.equal(outcome, "finished");
|
||||
assert.deepEqual(Object.keys(server.saved), ["k0", "k1", "k2"]);
|
||||
});
|
||||
|
||||
test("checkpoints say which page is out and when it is handled", async () => {
|
||||
const marks = [];
|
||||
await run(fakeTranslator(), fakeServer(["a", "b", "c"]), { pageSize: 2, onCheckpoint: (c) => marks.push([c.jobId ? "sent" : "done", c.keys.length, c.skip]) });
|
||||
assert.deepEqual(marks, [["sent", 2, 0], ["done", 0, 0], ["sent", 1, 0], ["done", 0, 0]]);
|
||||
});
|
||||
Reference in new issue
Block a user