From 2ac333f8427904efb275a339ca6a9af4cfa9a310 Mon Sep 17 00:00:00 2001 From: Maurice Date: Sun, 25 Oct 2026 00:10:21 +0200 Subject: [PATCH] fix(demo): skip the example trips when the demo account owns its own The seed put the example trips in whenever the admin owned none, so a demo whose content belongs to the demo account got them back on every reopen, the hourly reset included, and the next baseline saved them along. --- server/src/demo/demo-seed.ts | 10 +++++++++ server/tests/unit/demo/demo-seed.test.ts | 26 +++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/server/src/demo/demo-seed.ts b/server/src/demo/demo-seed.ts index 9ef9f185e..853c8f755 100644 --- a/server/src/demo/demo-seed.ts +++ b/server/src/demo/demo-seed.ts @@ -69,6 +69,16 @@ async function seedDemoData(): Promise<{ adminId: number; demoId: number }> { return { adminId, demoId }; } + // A demo whose content belongs to the demo account itself (built over the + // API, then saved as the baseline) is left alone. Without this the old + // example trips would come back on every reopen, the hourly reset included, + // and the mark below would save them into the next baseline. + const demoTripCount = await demo.countTripsByUser(demoId); + if (demoTripCount > 0) { + console.log('[Demo] The demo account owns its own trips, skipping the example trips'); + return { adminId, demoId }; + } + console.log('[Demo] Seeding example trips...'); await seedExampleTrips(demo, adminId, demoId); diff --git a/server/tests/unit/demo/demo-seed.test.ts b/server/tests/unit/demo/demo-seed.test.ts index cb53896ae..7de0e1c35 100644 --- a/server/tests/unit/demo/demo-seed.test.ts +++ b/server/tests/unit/demo/demo-seed.test.ts @@ -27,7 +27,7 @@ import { takeExampleTripsSeeded } from '../../../src/demo/demo-reset'; import { seedDemoData } from '../../../src/demo/demo-seed'; import { withRequestContext } from '../../../src/nest/database/request-context'; import { createSnapshotTestDb } from '../../helpers/db-mock'; -import { countRows, findRows } from '../../helpers/factories/rows'; +import { countRows, deleteRows, findRows, updateRows } from '../../helpers/factories/rows'; import { readAppSetting } from '../../helpers/factories/settings'; import { readUser } from '../../helpers/factories/users'; import { createTestOrm } from '../../helpers/test-orm'; @@ -167,4 +167,28 @@ describe('demo seeding', () => { await seed(); expect(takeExampleTripsSeeded()).toBe(false); }); + + it('DEMOSEED-006: leaves a demo alone whose trips belong to the demo account, not the admin', async () => { + takeExampleTripsSeeded(); + const { adminId, demoId } = await seed(); + expect(takeExampleTripsSeeded()).toBe(true); + + // The demo content is rebuilt as the demo user's own: the admin's example + // trips go, one trip owned by the demo account stays. + const orm = await createTestOrm(db); + const exampleTrips = await findRows(orm, Trips, { user_id: adminId }, { id: 'asc' }); + const keep = exampleTrips[0]; + await deleteRows(orm, Trips, { id: { $in: exampleTrips.slice(1).map((t) => t.id) } }); + await updateRows(orm, Trips, { id: keep.id }, { user: demoId }); + await orm.close(); + + await seed(); + + const after = await createTestOrm(db); + const trips = await findRows(after, Trips, {}, { id: 'asc' }); + await after.close(); + expect(trips.map((t) => ({ id: t.id, user_id: t.user_id }))).toEqual([{ id: keep.id, user_id: demoId }]); + // Nothing was seeded, so no baseline is due either. + expect(takeExampleTripsSeeded()).toBe(false); + }); });