Persist staged image uploads alongside document operations

This commit is contained in:
2026-11-07 23:24:00 +08:00
parent 8a19909716
commit 8a76e53a9f
20 changed files with 659 additions and 52 deletions
+39 -1
View File
@@ -24,7 +24,7 @@ function setup(overrides = {}, initial) {
});
return { scheduler, calls, persisted, errors, ledger: () => ledger,
add(id, canvases = ['canvas'], dependencies = []) { ledger.add({ id, operationType: 'document.change', operationSchemaVersion: 1, sequence: ledger.getSnapshot().operations.size + 1, origin: 'user', changes: canvases.map(id => ({ kind: 'canvas', id, before: null, after: { id, parent: null } })), dependencies }, {}); scheduler.notify([id]); },
async advance(ms) { time += ms; for (const [id, timer] of [...timers]) if (timer.at <= time) { timers.delete(id); timer.callback(); } await tick(); await scheduler.settle(); },
async advance(ms, settle = true) { time += ms; for (const [id, timer] of [...timers]) if (timer.at <= time) { timers.delete(id); timer.callback(); } await tick(); if (settle) await scheduler.settle(); },
timerCount: () => timers.size,
};
}
@@ -105,3 +105,41 @@ test('permission/protocol rejection keeps unknown claims without an automatic re
assert.equal(state.ledger().getSnapshot().requests.get('request-1').outcome,'unresolved');assert.equal(state.scheduler.getClaims().size,1);assert.equal(state.timerCount(),0);
await state.advance(10000);assert.equal(calls.length,1);state.scheduler.stop();
});
const mixedCompile = (intent, requestId) => ({ kind: intent.operationType, protocolVersion: 2, canvasIds: intent.operationType === 'asset.upload' ? [] : intent.changes.map(change => change.id), expectedVersions: Object.fromEntries(intent.changes.map(change => [change.id, 1])), payload: { requestId, operationId: intent.id } });
function addAsset(state, id) {
state.ledger().add({ id, operationType: 'asset.upload', operationSchemaVersion: 1, sequence: state.ledger().getSnapshot().operations.size + 1, origin: 'user', changes: [], canvasIds: [], dependencies: [], resourceId: `resource-${id}` }, {});
state.scheduler.notify([id]);
}
test('an upload burst yields without filling both network slots or starving independent edits', async () => {
const gate = deferred(), calls = [];
const state = setup({ compile: mixedCompile, submit: async request => { calls.push(request); if (request.operationIds[0] === 'asset-0') await gate.promise; return { outcome: 'applied' }; } });
for (let index = 0; index < 80; index++) addAsset(state, `asset-${index}`);
state.add('independent', ['canvas']); state.scheduler.start(); await tick();
await state.advance(0, false);
assert.deepEqual(calls.map(request => request.operationIds[0]), ['asset-0', 'independent']);
assert.equal(state.timerCount(), 0);
gate.resolve(); await state.scheduler.settle();
assert.equal(calls.length, 81); assert.equal(state.errors.length, 0); state.scheduler.stop();
});
test('a capacity-blocked upload retry does not spin or hide a later document retry deadline', async () => {
const gate = deferred(), calls = [];
const state = setup({ compile: mixedCompile,
submit: async request => {
const id = request.operationIds[0]; calls.push(['submit', id]);
if (id === 'asset-2') { await gate.promise; return { outcome: 'applied' }; }
return { outcome: 'unresolved', retryAfterMs: id === 'doc' ? 200 : 100 };
},
query: async request => { calls.push(['query', request.operationIds[0]]); return { outcome: 'applied' }; },
});
addAsset(state, 'asset-1'); addAsset(state, 'asset-2'); state.add('doc'); state.scheduler.start(); await tick();
await state.advance(100, false); assert.equal(state.timerCount(), 1);
await state.advance(100, false);
assert.ok(calls.some(([action, id]) => action === 'query' && id === 'doc'));
assert.equal(calls.some(([action, id]) => action === 'query' && id === 'asset-1'), false);
assert.equal(state.timerCount(), 0);
gate.resolve(); await state.scheduler.settle();
assert.ok(calls.some(([action, id]) => action === 'query' && id === 'asset-1')); state.scheduler.stop();
});