place-contrib: settle a sent value only while its queue row still holds it

A value saved while the request for its predecessor was out rewrote the same
outbox row with a new cid; the answer for the old value then deleted the row,
or charged it an attempt, and the newer value was never sent. The settle
writes now match the cid that was sent as well as the id, so such a row stays
queued for the next run. A replaced value also counts its age from the
replacement, so a long pause cannot expire a value queued days ago.
This commit is contained in:
Maurice
2026-10-30 23:14:32 +00:00
parent c10a698b9f
commit aa41c67a20
4 changed files with 95 additions and 30 deletions
@@ -66,10 +66,7 @@ describe('PlaceContribOutboxRepository', () => {
it('PCOUTREPO-001: a newer value for the same key and field replaces the queued one and starts its attempts over', async () => {
const placeId = await aPlace();
await queue(placeId);
await outbox.scheduleRetry(
(await outbox.dueFor('osm:node/1', NOW)).map((row) => row.id),
PAST,
);
await outbox.scheduleRetry(await outbox.dueFor('osm:node/1', NOW), PAST);
await queue(placeId, { value: 'https://luna.example', cid: 'cid-website-0002' });
const rows = await outbox.dueFor('osm:node/1', NOW);
expect(rows).toEqual([
@@ -94,9 +91,9 @@ describe('PlaceContribOutboxRepository', () => {
await queue(placeId);
const [row] = await outbox.dueFor('osm:node/1', NOW);
if (!row) throw new Error('the queued value is missing');
await outbox.scheduleRetry([row.id], LATER);
await outbox.scheduleRetry([row], LATER);
expect(await outbox.dueFor('osm:node/1', NOW)).toEqual([]);
await outbox.postpone([row.id], PAST);
await outbox.postpone([row], PAST);
const [again] = await outbox.dueFor('osm:node/1', NOW);
expect(again).toEqual(expect.objectContaining({ attempts: 1 }));
await outbox.scheduleRetry([], LATER);
@@ -132,12 +129,11 @@ describe('PlaceContribOutboxRepository', () => {
expect(await outbox.deleteForPlace(one, [])).toBe(0);
expect(await outbox.deleteFields([])).toBe(0);
expect(await outbox.deleteIds([])).toBe(0);
expect(await outbox.deleteSent([])).toBe(0);
expect(await outbox.deleteForPlace(one, ['phone'])).toBe(1);
expect(await outbox.deleteFields(['email'])).toBe(1);
const left = (await outbox.dueFor('osm:node/2', NOW)).map((row) => row.id);
expect(await outbox.deleteIds(left)).toBe(1);
expect(await outbox.deleteSent(await outbox.dueFor('osm:node/2', NOW))).toBe(1);
expect(await outbox.deleteAll()).toBe(1);
expect(await countRows(t, PlaceContribOutbox)).toBe(0);
});
@@ -162,4 +158,37 @@ describe('PlaceContribOutboxRepository', () => {
]),
);
});
it('PCOUTREPO-007: a value replaced after the sender read it is neither deleted nor charged nor postponed', async () => {
const placeId = await aPlace();
await queue(placeId);
const read = await outbox.dueFor('osm:node/1', NOW);
await queue(placeId, { value: 'https://luna.example', cid: 'cid-website-0002' });
await outbox.scheduleRetry(read, LATER);
await outbox.postpone(read, LATER);
expect(await outbox.deleteSent(read)).toBe(0);
expect(await outbox.dueFor('osm:node/1', NOW)).toEqual([
expect.objectContaining({ value: 'https://luna.example', cid: 'cid-website-0002', attempts: 0 }),
]);
});
it('PCOUTREPO-008: a replaced value counts its age from the replacement, not from the value it replaced', async () => {
const placeId = await aPlace();
await insertRow(t, PlaceContribOutbox, {
contrib_key: 'osm:node/1',
field: 'website',
value: 'https://cafe-luna.de',
place: placeId,
place_name: 'Café Luna',
cid: 'cid-website-0001',
eligible_at: PAST,
next_attempt_at: PAST,
created_at: '2026-01-01 00:00:00',
});
expect(await outbox.queuedBefore('2026-06-01 00:00:00', 10)).toHaveLength(1);
await queue(placeId, { value: 'https://luna.example', cid: 'cid-website-0002' });
expect(await outbox.queuedBefore('2026-06-01 00:00:00', 10)).toEqual([]);
});
});
@@ -292,6 +292,26 @@ describe('a place request', () => {
expect(client.servedPlace).not.toHaveBeenCalled();
expect(await countRows(orm(), PlaceContribOutbox)).toBe(2);
});
it('PCSD-015: a value the user replaces while its request is out stays queued for the next run', async () => {
await seed({ website: 'https://cafe-luna.de' });
client.postPlace.mockImplementation(async (body) => {
// The user saves a newer website while the request for the old one is out.
await updateRows(
orm(),
PlaceContribOutbox,
{ field: 'website' },
{ value: 'https://luna.example', cid: 'cid-website-0002' },
);
return accepted(resultsFor(body, 'published'));
});
await sender().run(NOW);
// The old value went out and is logged; the newer one waits for its own request.
expect(await logLines()).toEqual([{ field: 'website', result: 'accepted', reason: 'published' }]);
expect(await findRows(orm(), PlaceContribOutbox, {})).toEqual([
expect.objectContaining({ value: 'https://luna.example', cid: 'cid-website-0002', attempts: 0 }),
]);
});
});
describe('refusals and failures', () => {