🐛 Surface changes rejected by the server

The server now answers an invalid change per item instead of failing
the whole batch, and the client dropped such a change silently: the
round went through and nothing told the user that the edit never
reached the server. A rejected change now sets the sync status to
"<collection>: <error>", cut to 200 characters, so the sync button
shows its warning state. The change is not retried, as before.

The message survives the collections that still succeed in the same
round, since each of them used to clear the error. The next round
without a rejection clears it.
This commit is contained in:
2026-12-01 17:59:29 +01:00
parent 6ce66f81c7
commit bddabbf93d
2 changed files with 54 additions and 4 deletions
+43 -2
View File
@@ -8,11 +8,15 @@ const mocks = vi.hoisted(() => {
const server = new Map<string, Map<string, Record<string, unknown>>>();
const pushes: { collection: string; id: string; data: Record<string, unknown>; client_updated_at: number; deleted: boolean }[] = [];
const batches: number[] = [];
const reject = { collection: '', error: '' };
const slow = { name: '' };
let tick = 0;
return {
server,
pushes,
batches,
reject,
slow,
stamp: () => new Date(Date.UTC(2026, 0, 1) + tick++).toISOString().replace('T', ' ')
};
});
@@ -22,7 +26,10 @@ vi.mock('$lib/pb', () => ({
autoCancellation: () => undefined,
filter: (tpl: string, params: Record<string, unknown>) => tpl.replace(/\{:(\w+)\}/g, (_m, key: string) => JSON.stringify(String(params[key]))),
collection: (name: string) => ({
getFullList: async () => [...(mocks.server.get(name)?.values() ?? [])],
getFullList: async () => {
if (mocks.slow.name === name) await new Promise((resolve) => setTimeout(resolve, 5));
return [...(mocks.server.get(name)?.values() ?? [])];
},
subscribe: async () => async () => undefined
}),
send: async (path: string, options: { method?: string; body?: unknown }) => {
@@ -30,6 +37,8 @@ vi.mock('$lib/pb', () => ({
const body = options.body as { changes: (typeof mocks.pushes)[number][] };
mocks.batches.push(body.changes.length);
const results = body.changes.map((c) => {
mocks.pushes.push(c);
if (mocks.reject.collection === c.collection) return { id: c.id, collection: c.collection, status: 'invalid', error: mocks.reject.error };
const record = { id: c.id, ...c.data, client_updated_at: c.client_updated_at, rev: 1, deleted: c.deleted, updated: mocks.stamp() };
let store = mocks.server.get(c.collection);
if (!store) {
@@ -37,7 +46,6 @@ vi.mock('$lib/pb', () => ({
mocks.server.set(c.collection, store);
}
store.set(c.id, record);
mocks.pushes.push(c);
return { id: c.id, collection: c.collection, status: 'ok', record };
});
return { results };
@@ -218,4 +226,37 @@ describe('sync boundary', () => {
expect(Math.max(...mocks.batches)).toBeLessThanOrEqual(200);
expect(mocks.batches.reduce((sum, size) => sum + size, 0)).toBe(250);
}, 20000);
it('a rejected change is dropped and reported, and a later success of the round keeps the message', async () => {
status.online = true;
mocks.reject.collection = 'settings_history';
mocks.reject.error = 'hours_per_week must be at most 168';
mocks.slow.name = 'balance_adjustments';
try {
const id = await save('settings_history', null, settingsRow);
await save('balance_adjustments', null, { delta_ms: 1000, note: '', effective_at: 1 });
await syncNow();
expect(status.error).toBe('settings_history: hours_per_week must be at most 168');
mocks.reject.collection = '';
await syncNow();
expect(status.error).toBeNull();
expect(mocks.pushes.filter((c) => c.id === id)).toHaveLength(1);
} finally {
mocks.reject.collection = '';
mocks.slow.name = '';
}
});
it('a long rejection is cut to 200 characters', async () => {
status.online = true;
mocks.reject.collection = 'balance_adjustments';
mocks.reject.error = 'x'.repeat(300);
try {
await save('balance_adjustments', null, { delta_ms: 1000, note: '', effective_at: 1 });
await syncNow();
expect(status.error).toBe(`balance_adjustments: ${'x'.repeat(200 - 'balance_adjustments: '.length)}`);
} finally {
mocks.reject.collection = '';
}
});
});