feat(canvas): batch mutable document operations with durable coverage
This commit is contained in:
@@ -166,3 +166,58 @@ test('holds acquired before start retain notified work; stop is terminal even wh
|
||||
const finalRelease = state.scheduler.hold(); state.add('never'); state.scheduler.stop(); finalRelease(); await tick();
|
||||
assert.equal(state.calls.length, 1); assert.throws(() => state.scheduler.hold(), /scheduler_closed/);
|
||||
});
|
||||
|
||||
const compileBatch = (intents, requestId) => {
|
||||
const canvasIds = [...new Set(intents.flatMap(intent => intent.changes.map(change => change.id)))].sort();
|
||||
return { canvasIds, expectedVersions: Object.fromEntries(canvasIds.map(id => [id, 1])), payload: { requestId, operations: intents.map(intent => ({ id: intent.id, changes: intent.changes })) } };
|
||||
};
|
||||
|
||||
test('mutable microtask window batches dependent document IDs behind one durable dispatch barrier', async () => {
|
||||
const barrier = deferred(), sent = [];
|
||||
const state = setup({ compileBatch, persist: () => barrier.promise, submit: async request => { sent.push(request); return { outcome: 'applied' }; } });
|
||||
state.scheduler.start(); state.add('create'); state.add('rename', ['canvas'], ['create']); state.add('move', ['canvas'], ['rename']);
|
||||
assert.equal(state.ledger().getSnapshot().requests.size, 0);
|
||||
await tick();
|
||||
const frozen = [...state.ledger().getSnapshot().requests.values()][0];
|
||||
assert.deepEqual(frozen.operationIds, ['create', 'rename', 'move']); assert.equal(frozen.phase, 'dispatch-marked'); assert.equal(sent.length, 0);
|
||||
assert.deepEqual(JSON.parse(frozen.body).operations.map(op => op.id), frozen.operationIds);
|
||||
barrier.resolve(); await state.scheduler.settle();
|
||||
assert.equal(sent.length, 1); assert.equal(state.scheduler.getClaims().size, 0);
|
||||
for (const id of frozen.operationIds) assert.equal(state.ledger().getSnapshot().operations.get(id).phase, 'resolved');
|
||||
state.scheduler.stop();
|
||||
});
|
||||
|
||||
test('batching retains independent lanes and does not cross an external dependency or task observer', async () => {
|
||||
const pending = deferred(), sent = [], state = setup({ compileBatch, submit: async request => { sent.push(request); return pending.promise; } });
|
||||
state.add('a1', ['a']); state.add('b1', ['b']); state.add('a2', ['a'], ['a1']);
|
||||
state.scheduler.start(); await tick();
|
||||
assert.deepEqual(sent.map(request => request.operationIds), [['a1'], ['b1']]);
|
||||
pending.resolve({ outcome: 'applied' }); await state.scheduler.settle();
|
||||
assert.deepEqual(sent.map(request => request.operationIds), [['a1'], ['b1'], ['a2']]); state.scheduler.stop();
|
||||
|
||||
const observed = setup({ compileBatch });
|
||||
observed.add('before');
|
||||
observed.ledger().add({ id: 'run', operationType: 'run.start', operationSchemaVersion: 1, sequence: 2, origin: 'user', changes: [], dependencies: ['before'] }, {});
|
||||
observed.ledger().block('run', ['awaiting-input']); observed.scheduler.notify(['run']);
|
||||
observed.add('after', ['canvas'], ['before']); observed.scheduler.start(); await observed.scheduler.settle();
|
||||
assert.deepEqual(observed.calls.map(([, request]) => request.operationIds), [['before'], ['after']]); observed.scheduler.stop();
|
||||
});
|
||||
|
||||
test('unknown batch preserves all IDs, claims, and exact bytes across query and retry; later edits stay mutable', async () => {
|
||||
const sent = [], state = setup({ compileBatch, submit: async request => { sent.push(request); return { outcome: 'unresolved' }; }, query: async () => ({ outcome: 'unresolved' }) });
|
||||
state.add('one'); state.add('two', ['canvas'], ['one']); state.scheduler.start(); await state.scheduler.settle();
|
||||
const original = sent[0]; state.add('three', ['canvas'], ['two']); await state.scheduler.settle();
|
||||
assert.equal(sent.length, 1); assert.equal(state.ledger().getSnapshot().operations.get('three').phase, 'pending');
|
||||
await state.advance(100); await state.advance(100);
|
||||
assert.equal(sent.length, 2); assert.equal(sent[1].id, original.id); assert.equal(sent[1].body, original.body); assert.deepEqual(sent[1].operationIds, ['one', 'two']);
|
||||
assert.equal(state.scheduler.getClaims().get('canvas'), original.id); state.scheduler.stop();
|
||||
});
|
||||
|
||||
test('bounded batches drain through settle without waiting for fake timers and holds suppress scheduled admission', async () => {
|
||||
const state = setup({ compileBatch }); state.scheduler.start(); const release = state.scheduler.hold();
|
||||
for (let i = 0; i < 130; i++) state.add(`op-${i}`, ['canvas'], i ? [`op-${i - 1}`] : []);
|
||||
await state.scheduler.settle(); assert.equal(state.calls.length, 0);
|
||||
release(); await state.scheduler.settle();
|
||||
assert.deepEqual(state.calls.map(([, request]) => request.operationIds.length), [64, 64, 2]);
|
||||
assert.equal(state.ledger().getSnapshot().operations.get('op-129').phase, 'resolved'); state.scheduler.stop();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user