fix(journey): refuse the second of two saves of a recorded day at once unless it asked to replace

This commit is contained in:
Maurice
2026-10-21 15:18:01 +02:00
parent 4f60cc5c98
commit 5116f7be2c
5 changed files with 89 additions and 17 deletions
@@ -104,6 +104,19 @@ describe('JourneyRecordedDaysRepository', () => {
});
});
it('JRDAYS-007: a write that keeps only while none is kept leaves the version there first and says so', async () => {
const { user, journey: j } = await journey();
const first = await days.insertDayIfAbsent(day(j.id, user.id));
expect(first).toEqual(expect.any(Number));
expect(await days.insertDayIfAbsent(day(j.id, user.id, { content_hash: 'b'.repeat(64) }))).toBeUndefined();
expect(await countRows(t, JourneyRecordedDays, {})).toBe(1);
expect(await days.findDay(j.id, '2026-05-14', 'dawarich')).toMatchObject({
id: first,
content_hash: 'a'.repeat(64),
});
});
it('JRDAYS-003: lists the days of one journey earliest first, and only its own', async () => {
const { user, journey: j } = await journey();
const other = await makeJourney(t, user.id);
@@ -259,6 +259,29 @@ describe('JourneyRecordingsService.save', () => {
});
});
it('JRNREC-SVC-013: of two editors saving the day at once without Replace, the second is refused, not silently kept', async () => {
const f = await fixture();
fetchDay.mockResolvedValue(recorded());
await svc.save(f.editor.id, f.journey.id, '2026-05-14', await previewed(f.editor.id, f.journey.id));
t.realtime.reset();
fetchDay.mockResolvedValue(recorded({ segments: [WALK, { ...WALK, startedAt: '2026-05-14T15:00:00+02:00' }] }));
const body = await previewed(f.owner.id, f.journey.id);
// The owner read the day before the editor's write landed.
const staleRead = vi.spyOn(kept, 'find').mockResolvedValueOnce(null);
expect(await refusal(svc.save(f.owner.id, f.journey.id, '2026-05-14', body))).toEqual({
status: 409,
body: { error: 'The journey already keeps another recording of that day', code: 'recording_exists' },
});
staleRead.mockRestore();
expect(await kept.find(f.journey.id, '2026-05-14')).toMatchObject({
importedBy: { id: f.editor.id, username: 'jonas' },
stats: { segmentCount: 1 },
});
expect(t.realtime.broadcastToUserMock).not.toHaveBeenCalled();
});
it('JRNREC-SVC-008: a recording that changed since the preview is not kept, and the kept version stays', async () => {
const f = await fixture();
fetchDay.mockResolvedValue(recorded());