feat(journey): keep the route recorded on a journey day from Dawarich, previewed first, with its gaps

A day header in the journey timeline now carries the Dawarich mark for an
editor whose Dawarich answers (dimmed with the reason while offline). It opens
a dialog that names the journey and the day and reads that one local day from
the caller's own connection: a sketch of the route, distance, first and last
time, pieces, gaps without recording, modes and whether tracks or thinned
points answered. The day is cut in the zone Dawarich records in where its
answer shows one, else in this device's, and a picker cuts it in any other.
Nothing is kept before "Keep route". On a phone the same sheet opens from the
journey's More options for the day of the card shown.

What is kept is the preview that was seen: the save reads the day again and
refuses with recording_changed when its hash moved, and the dialog shows the
new version. The same day taken over twice keeps one version and announces
nothing; a different version is replaced only on "Replace", never touching an
entry or text. A failed read throws before any write, so the kept version
stays. Gaps stay gaps: points are split where silence ends elsewhere, and no
day is joined to the one before. The dialog says how many other people will
see the route; editors may keep their own recording.

Kept days show in the day header (distance, whose recording, since when,
still running) and on the desktop and phone journey maps in the colour of
their day's pins, winning over the live trail of their date, and stay drawn
without Dawarich. A trip's GPX track is now drawn dashed as the plan, recorded
routes solid. Shared links do not show recorded days. Remove route asks first
and works without a connection.

Server: migration 046160 adds journey_recorded_days (one row per journey,
local day and source, segments as JSON, zone, window, fetch time, hash,
importer kept past their account). The journey domain owns the rows through
JourneyRecordedDaysService; the new journey-recordings domain takes a day
over through DawarichTracksService.fetchDay, which reads past the cache, asks
a window wide enough for every zone and refuses with DomainErrors. REST under
/api/journeys/:id/recordings (list, preview, PUT and DELETE /:date) with
@trek/shared contracts, and MCP list_journey_recorded_days,
preview_journey_recorded_day, save_journey_recorded_day and
delete_journey_recorded_day on the same service and access. A new user event
journey:recording:updated keeps every open map current. JourneyTrack gains an
optional kind. All 27 locales carry the strings; the Journey and Dawarich wiki
pages and the MCP tool list say what it does.
This commit is contained in:
Maurice
2026-10-21 15:05:21 +02:00
parent cbca3b2851
commit 65bb2d95eb
99 changed files with 6359 additions and 51 deletions
@@ -0,0 +1,295 @@
/**
* Recorded days a journey keeps (JRN-01), through the real AddonGuard and
* JwtAuthGuard against a migrated and seeded temp SQLite db, every answer
* checked against its response contract. The Dawarich client is the one
* thing replaced: a test never opens a socket to a location archive. The
* broadcasts go to a FakeRealtimeService.
*
* Fixtures: user 1 owns journey 7 and has a Dawarich connection, user 2 is an
* editor of it with no connection, user 3 a viewer, user 4 has no access.
*
* The walk: preview, keep, read the day back once the connection is gone,
* take the same day over again without a second copy, replace it on purpose,
* keep the version when Dawarich fails, and drop it.
*/
import { DawarichConnections } from '../../src/db/entities/DawarichConnections.entity';
import { JourneyRecordedDays } from '../../src/db/entities/JourneyRecordedDays.entity';
import { encrypt_api_key } from '../../src/nest/common/crypto/apiKeyCrypto';
import { TrekExceptionFilter } from '../../src/nest/common/trek-exception.filter';
import { ZodValidationPipe } from '../../src/nest/common/zod-validation.pipe';
import { DawarichClient, DawarichError } from '../../src/nest/integrations/dawarich.client';
import { JourneyRecordingsModule } from '../../src/nest/journey-recordings/journey-recordings.module';
import { RealtimeModule } from '../../src/nest/realtime/realtime.module';
import { RealtimeService } from '../../src/nest/realtime/realtime.service';
import { createSnapshotTestDb } from '../helpers/db-mock';
import { addJourneyContributor, makeJourney } from '../helpers/factories/journeys';
import { countRows, deleteRows, insertRow } from '../helpers/factories/rows';
import { setAddonEnabled } from '../helpers/factories/settings';
import { makeUser } from '../helpers/factories/users';
import { FakeRealtimeService } from '../helpers/fake-realtime';
import { createTestMikroOrmModule, createTestOrm, type TestOrm } from '../helpers/test-orm';
import { TestUnitOfWorkModule } from '../helpers/test-uow';
import { sessionCookie } from './harness';
import { Test } from '@nestjs/testing';
import cookieParser from 'cookie-parser';
import type { Server } from 'http';
import request from 'supertest';
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
vi.mock('../../src/nest/audit/audit-log.logger', () => ({
LOG_LEVEL: 'error',
logInfo: vi.fn(),
logDebug: vi.fn(),
logError: vi.fn(),
logWarn: vi.fn(),
}));
const db = createSnapshotTestDb();
const realtime = new FakeRealtimeService();
const client = {
probe: vi.fn(),
listVisits: vi.fn(),
listTracks: vi.fn(),
listPoints: vi.fn(),
listVisitedCities: vi.fn(),
findVisitsNear: vi.fn(),
};
let orm: TestOrm;
let journeyId: number;
const OWNER = 1;
const EDITOR = 2;
const VIEWER = 3;
const OUTSIDER = 4;
const DAY = '2026-05-14';
/** A walk across Venice on the 14th, in the zone Dawarich records in. */
function walk(endLng = 12.3218) {
return {
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: [
[12.3388, 45.434],
[endLng, 45.4364],
],
},
properties: {
start_at: `${DAY}T09:00:00+02:00`,
end_at: `${DAY}T09:40:00+02:00`,
dominant_mode: 'walking',
distance: 1500,
},
};
}
describe('Journey recordings e2e (real guards + temp SQLite)', () => {
let server: Server;
let app: Awaited<ReturnType<typeof build>>;
async function build() {
const moduleRef = await Test.createTestingModule({
imports: [
await TestUnitOfWorkModule.forRoot(db),
await createTestMikroOrmModule(db),
RealtimeModule,
JourneyRecordingsModule,
],
})
.overrideProvider(RealtimeService)
.useValue(realtime)
.overrideProvider(DawarichClient)
.useValue(client)
.compile();
const nest = moduleRef.createNestApplication();
nest.use(cookieParser());
nest.useGlobalPipes(new ZodValidationPipe());
nest.useGlobalFilters(new TrekExceptionFilter());
await nest.init();
return nest;
}
const as = (userId: number) => ({ Cookie: sessionCookie(userId) });
const base = () => `/api/journeys/${journeyId}/recordings`;
const preview = (userId = OWNER) =>
request(server)
.post(`${base()}/preview`)
.set(as(userId))
.send({ date: DAY, utcOffsetMinutes: 0, useSourceOffset: true });
beforeAll(async () => {
orm = await createTestOrm(db);
// Pinned ids: the session cookies sign for exactly these users.
const user = (id: number, username: string) =>
makeUser(orm, { id, username, email: `${username}@example.test`, role: 'user', password_version: 0 });
await user(OWNER, 'marta');
await user(EDITOR, 'jonas');
await user(VIEWER, 'viewer');
await user(OUTSIDER, 'outsider');
const journey = await makeJourney(orm, OWNER, { title: 'Venice' });
journeyId = journey.id;
await addJourneyContributor(orm, journeyId, EDITOR, 'editor');
await addJourneyContributor(orm, journeyId, VIEWER, 'viewer');
app = await build();
server = app.getHttpServer();
});
beforeEach(async () => {
await setAddonEnabled(orm, 'journey', true);
await setAddonEnabled(orm, 'dawarich', true);
await deleteRows(orm, JourneyRecordedDays);
await deleteRows(orm, DawarichConnections);
await insertRow(orm, DawarichConnections, {
user: OWNER,
url: 'https://dawarich.example',
api_key: encrypt_api_key('secret-key'),
});
for (const fn of Object.values(client)) fn.mockReset();
client.listTracks.mockResolvedValue({ features: [walk()], truncated: false });
client.listPoints.mockResolvedValue({ points: [], truncated: false });
realtime.broadcastToUserMock.mockClear();
});
afterAll(async () => {
await app.close();
await orm.close();
});
it('answers 404 with the Journey addon off before asking who the caller is, and 401 without a session', async () => {
await setAddonEnabled(orm, 'journey', false);
expect((await request(server).get(base())).status).toBe(404);
await setAddonEnabled(orm, 'journey', true);
expect((await request(server).get(base())).status).toBe(401);
});
it('previews the day for the journey without keeping it, then keeps it on the word of the traveller', async () => {
const shown = await preview();
expect(shown.status).toBe(200);
expect(shown.body).toMatchObject({
journey: { id: journeyId, title: 'Venice' },
date: DAY,
utcOffsetMinutes: 120,
offsetSource: 'source',
window: { from: '2026-05-13T22:00:00.000Z', to: '2026-05-14T22:00:00.000Z' },
sourceKind: 'tracks',
stats: { distanceMeters: 1500, segmentCount: 1, gapCount: 0, modes: ['walking'] },
stored: null,
audience: 2,
});
expect(await countRows(orm, JourneyRecordedDays)).toBe(0);
const kept = await request(server)
.put(`${base()}/${DAY}`)
.set(as(OWNER))
.set('X-Socket-Id', '11')
.send({ utcOffsetMinutes: 120, expectedHash: shown.body.contentHash });
expect(kept.status).toBe(200);
expect(kept.body).toMatchObject({ outcome: 'created', day: { date: DAY, importedBy: { username: 'marta' } } });
expect(realtime.broadcastToUserMock).toHaveBeenCalledWith(
VIEWER,
{ type: 'journey:recording:updated', journeyId, date: DAY },
'11',
);
});
it('keeps the day readable for every member once the connection is gone, and never copies it twice', async () => {
const shown = await preview();
const body = { utcOffsetMinutes: 120, expectedHash: shown.body.contentHash };
await request(server).put(`${base()}/${DAY}`).set(as(OWNER)).send(body);
const again = await request(server).put(`${base()}/${DAY}`).set(as(OWNER)).send(body);
expect(again.status).toBe(200);
expect(again.body.outcome).toBe('unchanged');
expect(await countRows(orm, JourneyRecordedDays)).toBe(1);
await deleteRows(orm, DawarichConnections);
await setAddonEnabled(orm, 'dawarich', false);
client.listTracks.mockReset();
const listed = await request(server).get(base()).set(as(VIEWER));
expect(listed.status).toBe(200);
expect(listed.body.days).toHaveLength(1);
expect(listed.body.days[0]).toMatchObject({ date: DAY, segments: [{ mode: 'walking' }] });
expect(client.listTracks).not.toHaveBeenCalled();
expect((await request(server).get(base()).set(as(OUTSIDER))).status).toBe(404);
});
it('replaces a kept day only when asked, and a failing Dawarich leaves the kept version as it was', async () => {
const first = await preview();
await request(server)
.put(`${base()}/${DAY}`)
.set(as(OWNER))
.send({ utcOffsetMinutes: 120, expectedHash: first.body.contentHash });
client.listTracks.mockResolvedValue({ features: [walk(12.3), walk(12.31)], truncated: false });
const grown = await preview();
expect(grown.body).toMatchObject({ sameAsStored: false, stored: { stats: { segmentCount: 1 } } });
const body = { utcOffsetMinutes: 120, expectedHash: grown.body.contentHash };
const refused = await request(server).put(`${base()}/${DAY}`).set(as(OWNER)).send(body);
expect(refused.status).toBe(409);
expect(refused.body).toEqual({
error: 'The journey already keeps another recording of that day',
code: 'recording_exists',
});
client.listTracks.mockRejectedValue(new DawarichError('server_error', 'Dawarich answered HTTP 500', 500));
client.listPoints.mockRejectedValue(new DawarichError('server_error', 'Dawarich answered HTTP 500', 500));
const failed = await request(server)
.put(`${base()}/${DAY}`)
.set(as(OWNER))
.send({ ...body, replace: true });
expect(failed.status).toBe(502);
expect(failed.body).toMatchObject({ code: 'server_error' });
const unchanged = await request(server).get(base()).set(as(OWNER));
expect(unchanged.body.days[0].contentHash).toBe(first.body.contentHash);
client.listTracks.mockResolvedValue({ features: [walk(12.3), walk(12.31)], truncated: false });
const replaced = await request(server)
.put(`${base()}/${DAY}`)
.set(as(OWNER))
.send({ ...body, replace: true });
expect(replaced.status).toBe(200);
expect(replaced.body.outcome).toBe('replaced');
expect(await countRows(orm, JourneyRecordedDays)).toBe(1);
});
it('an editor without a connection is told so, a viewer may not preview, and a bad body or day is a 400', async () => {
const unconnected = await preview(EDITOR);
expect(unconnected.status).toBe(409);
expect(unconnected.body).toEqual({ error: 'Dawarich is not connected', code: 'not_connected' });
expect((await preview(VIEWER)).status).toBe(403);
expect((await preview(OUTSIDER)).status).toBe(404);
const badBody = await request(server)
.post(`${base()}/preview`)
.set(as(OWNER))
.send({ date: DAY, utcOffsetMinutes: 2000 });
expect(badBody.status).toBe(400);
const badDay = await request(server)
.put(`${base()}/2026-13-01`)
.set(as(OWNER))
.send({ utcOffsetMinutes: 0, expectedHash: 'a'.repeat(64) });
expect(badDay.status).toBe(400);
expect(badDay.body).toEqual({ error: 'Invalid date' });
});
it('drops a kept day for an editor, with Dawarich off, and a second drop is a 404', async () => {
const shown = await preview();
await request(server)
.put(`${base()}/${DAY}`)
.set(as(OWNER))
.send({ utcOffsetMinutes: 120, expectedHash: shown.body.contentHash });
await setAddonEnabled(orm, 'dawarich', false);
expect((await request(server).delete(`${base()}/${DAY}`).set(as(VIEWER))).status).toBe(403);
const dropped = await request(server).delete(`${base()}/${DAY}`).set(as(EDITOR));
expect(dropped.status).toBe(200);
expect(dropped.body).toEqual({ success: true });
expect(await countRows(orm, JourneyRecordedDays)).toBe(0);
expect((await request(server).delete(`${base()}/${DAY}`).set(as(EDITOR))).status).toBe(404);
});
});