From 0460b08a79221373c2b1b64de8d87e6371d07ece Mon Sep 17 00:00:00 2001 From: Maurice Date: Tue, 17 Nov 2026 21:42:09 +0100 Subject: [PATCH] test: send the reset and seeding steps one by one or together, not awaited in a loop resetDatabase empties tables and puts back the default categories one after another through the repo's oneByOne helper, where the order can matter, and the default addons and photo providers together; the MCP suites switch their addons on together and create ordered stops through oneByOne. The warnings this brings below the baseline, and the no-explicit-any and no-unused-vars counts earlier commits lowered, are lowered in scripts/eslint-baseline.json. --- server/scripts/eslint-baseline.json | 6 +- server/tests/helpers/reset-database.ts | 60 ++++++++++--------- .../tests/unit/helpers/reset-database.test.ts | 12 +++- server/tests/unit/mcp/demo-tool-gate.test.ts | 2 +- .../tests/unit/mcp/tools-addon-gating.test.ts | 6 +- .../tests/unit/mcp/tools-assignments.test.ts | 24 ++++---- .../tests/unit/mcp/tools-help-addons.test.ts | 6 +- 7 files changed, 65 insertions(+), 51 deletions(-) diff --git a/server/scripts/eslint-baseline.json b/server/scripts/eslint-baseline.json index a5e090f71..f02cd93d8 100644 --- a/server/scripts/eslint-baseline.json +++ b/server/scripts/eslint-baseline.json @@ -16,12 +16,12 @@ "no-misleading-character-class": 1 }, "tests": { - "@typescript-eslint/no-explicit-any": 1553, + "@typescript-eslint/no-explicit-any": 1549, "@typescript-eslint/no-non-null-assertion": 1747, "@typescript-eslint/no-require-imports": 25, "@typescript-eslint/no-unsafe-function-type": 7, - "@typescript-eslint/no-unused-vars": 96, - "no-await-in-loop": 452, + "@typescript-eslint/no-unused-vars": 87, + "no-await-in-loop": 451, "no-console": 18, "no-empty": 2, "no-useless-escape": 1, diff --git a/server/tests/helpers/reset-database.ts b/server/tests/helpers/reset-database.ts index e1055740d..ef4796e6e 100644 --- a/server/tests/helpers/reset-database.ts +++ b/server/tests/helpers/reset-database.ts @@ -32,6 +32,7 @@ import { Categories } from '../../src/db/entities/Categories.entity'; import { PhotoProviders } from '../../src/db/entities/PhotoProviders.entity'; import { inContext, type FactoryOrm } from './factories/context'; import { insertRowIgnoringConflict } from './factories/rows'; +import { oneByOne } from './one-by-one'; import { DEFAULT_ADDONS, DEFAULT_CATEGORIES, DEFAULT_PHOTO_PROVIDERS, KEEP_TABLES } from './test-db'; import type { EntityManager } from '@mikro-orm/core'; @@ -80,7 +81,7 @@ async function clearUserTables(em: EntityManager, restartUsers: boolean): Promis const tables = rows.map((r) => r.name).filter((name) => !name.startsWith('sqlite_') && !KEEP_TABLES.has(name)); await connection.execute('PRAGMA foreign_keys = OFF', [], 'run'); try { - for (const table of tables) await connection.execute(`DELETE FROM ${quote(table)}`, [], 'run'); + await oneByOne(tables, (table: string) => connection.execute(`DELETE FROM ${quote(table)}`, [], 'run')); if (restartUsers) await connection.execute("DELETE FROM sqlite_sequence WHERE name = 'users'", [], 'run'); } finally { await connection.execute('PRAGMA foreign_keys = ON', [], 'run'); @@ -109,7 +110,8 @@ async function clearUserTables(em: EntityManager, restartUsers: boolean): Promis await em.transactional(async (tx) => { const run = (sql: string) => tx.getConnection().execute(sql, [], 'run', tx.getTransactionContext()); if (truncated.length > 0) await run(`TRUNCATE ${truncated.map(quote).join(', ')}`); - for (const table of deleted) await run(`DELETE FROM ${quote(table)}`); + // One after another: a held table may reference another held table. + await oneByOne(deleted, (table: string) => run(`DELETE FROM ${quote(table)}`)); if (restartUsers) await run("select setval(pg_get_serial_sequence('users', 'id'), 1, false)"); }); return; @@ -125,32 +127,36 @@ async function clearUserTables(em: EntityManager, restartUsers: boolean): Promis * an addon a test switched off included. */ async function seedDefaultRows(orm: FactoryOrm): Promise { - await inContext(orm, async (em) => { - for (const cat of DEFAULT_CATEGORIES) { + // The categories in list order, so a fresh database numbers them as + // seedDefaults does; addons and providers carry their own ids. + await inContext(orm, (em) => + oneByOne(DEFAULT_CATEGORIES, async (cat) => { if ((await em.count(Categories, { name: cat.name })) === 0) { await em.insert(Categories, { name: cat.name, color: cat.color, icon: cat.icon }); } - } - }); - for (const a of DEFAULT_ADDONS) { - await insertRowIgnoringConflict(orm, Addons, { - id: a.id, - name: a.name, - description: a.description, - type: a.type, - icon: a.icon, - enabled: a.enabled === 1, - sort_order: a.sort_order, - }); - } - for (const p of DEFAULT_PHOTO_PROVIDERS) { - await insertRowIgnoringConflict(orm, PhotoProviders, { - id: p.id, - name: p.name, - description: p.id, - icon: 'Image', - enabled: p.enabled, - sort_order: 0, - }); - } + }), + ); + await Promise.all([ + ...DEFAULT_ADDONS.map((a) => + insertRowIgnoringConflict(orm, Addons, { + id: a.id, + name: a.name, + description: a.description, + type: a.type, + icon: a.icon, + enabled: a.enabled === 1, + sort_order: a.sort_order, + }), + ), + ...DEFAULT_PHOTO_PROVIDERS.map((p) => + insertRowIgnoringConflict(orm, PhotoProviders, { + id: p.id, + name: p.name, + description: p.id, + icon: 'Image', + enabled: p.enabled, + sort_order: 0, + }), + ), + ]); } diff --git a/server/tests/unit/helpers/reset-database.test.ts b/server/tests/unit/helpers/reset-database.test.ts index a13155667..50efeabc2 100644 --- a/server/tests/unit/helpers/reset-database.test.ts +++ b/server/tests/unit/helpers/reset-database.test.ts @@ -82,9 +82,15 @@ describe('resetDatabase', () => { await resetDatabase(orm); await resetDatabase(orm); - for (const cat of DEFAULT_CATEGORIES) expect(await countRows(orm, Categories, { name: cat.name })).toBe(1); - for (const addon of DEFAULT_ADDONS) expect(await countRows(orm, Addons, { id: addon.id })).toBe(1); - for (const p of DEFAULT_PHOTO_PROVIDERS) expect(await countRows(orm, PhotoProviders, { id: p.id })).toBe(1); + const once = (keys: readonly string[]) => Object.fromEntries(keys.map((key) => [key, 1])); + const counted = async (keys: readonly string[], count: (key: string) => Promise) => + Object.fromEntries(await Promise.all(keys.map(async (key) => [key, await count(key)]))); + const categories = DEFAULT_CATEGORIES.map((cat) => cat.name); + const addons = DEFAULT_ADDONS.map((addon) => addon.id); + const providers = DEFAULT_PHOTO_PROVIDERS.map((p) => p.id); + expect(await counted(categories, (name) => countRows(orm, Categories, { name }))).toEqual(once(categories)); + expect(await counted(addons, (id) => countRows(orm, Addons, { id }))).toEqual(once(addons)); + expect(await counted(providers, (id) => countRows(orm, PhotoProviders, { id }))).toEqual(once(providers)); const addon = await findRow(orm, Addons, { id: DEFAULT_ADDONS[0].id }); expect(addon?.name).toBe(DEFAULT_ADDONS[0].name); expect(Boolean(addon?.enabled)).toBe(DEFAULT_ADDONS[0].enabled === 1); diff --git a/server/tests/unit/mcp/demo-tool-gate.test.ts b/server/tests/unit/mcp/demo-tool-gate.test.ts index 741cb87e3..70f96ee21 100644 --- a/server/tests/unit/mcp/demo-tool-gate.test.ts +++ b/server/tests/unit/mcp/demo-tool-gate.test.ts @@ -71,7 +71,7 @@ beforeAll(async () => { beforeEach(async () => { await resetDatabase(orm); - for (const id of Object.values(ADDON_IDS)) await setAddonEnabled(orm, id, true); + await Promise.all(Object.values(ADDON_IDS).map((id) => setAddonEnabled(orm, id, true))); // The wanderer tools show only while wanderer is allowed under Tours. await setAppSetting(orm, 'tours_wanderer_enabled', 'true'); }); diff --git a/server/tests/unit/mcp/tools-addon-gating.test.ts b/server/tests/unit/mcp/tools-addon-gating.test.ts index 398eae693..2987ec8bb 100644 --- a/server/tests/unit/mcp/tools-addon-gating.test.ts +++ b/server/tests/unit/mcp/tools-addon-gating.test.ts @@ -29,9 +29,9 @@ beforeEach(async () => { // The summary reads these three off the injected AddonsService, so the case // that wants one off writes the row the admin panel writes. resetDatabase // puts back the default addon rows, so each case restates what it needs. - for (const id of [ADDON_IDS.BUDGET, ADDON_IDS.PACKING, ADDON_IDS.COLLAB]) { - await setAddonEnabled(orm, id, true); - } + await Promise.all( + [ADDON_IDS.BUDGET, ADDON_IDS.PACKING, ADDON_IDS.COLLAB].map((id) => setAddonEnabled(orm, id, true)), + ); }); afterAll(async () => { diff --git a/server/tests/unit/mcp/tools-assignments.test.ts b/server/tests/unit/mcp/tools-assignments.test.ts index a944ae877..d590e0521 100644 --- a/server/tests/unit/mcp/tools-assignments.test.ts +++ b/server/tests/unit/mcp/tools-assignments.test.ts @@ -15,6 +15,7 @@ import { makeDay, makeTrip } from '../../helpers/factories/trips'; import { makeUser } from '../../helpers/factories/users'; import { FakeRealtimeService } from '../../helpers/fake-realtime'; import { createMcpHarness, parseToolResult, type McpHarness } from '../../helpers/mcp-harness'; +import { oneByOne } from '../../helpers/one-by-one'; import { resetDatabase } from '../../helpers/reset-database'; import { bootTestApp } from '../../helpers/test-app'; import { MikroORM } from '@mikro-orm/core'; @@ -36,6 +37,13 @@ async function dayOrder(dayId: number): Promise { return (await findRows(orm, DayAssignments, { day: dayId }, { order_index: 'asc' })).map((r) => r.id); } +/** `count` stops of the place on the day, order_index 0 upwards; their ids in that order. */ +async function stops(dayId: number, placeId: number, count: number): Promise { + const indexes = Array.from({ length: count }, (_, i) => i); + const rows = await oneByOne(indexes, (i) => makeDayAssignment(orm, dayId, placeId, { order_index: i })); + return rows.map((row) => row.id); +} + async function viaAfterIndex(id: number) { const row = await findRow(orm, RoadtripVias, { id }); return row ? { after_order_index: row.after_order_index } : undefined; @@ -85,13 +93,13 @@ describe('Tool: assign_place_to_day', () => { const place = await makePlace(orm, trip.id); const visit = await makeDayAssignment(orm, day.id, place.id); await withHarness(user.id, async (h) => { - for (const end_day of [true, false]) { + await oneByOne([true, false], async (end_day) => { const changed = await h.client.callTool({ name: 'set_assignment_end_day', arguments: { tripId: trip.id, assignmentId: visit.id, end_day }, }); expect(parseToolResult(changed)).toMatchObject({ assignment: { end_day } }); - } + }); const other = await makeTrip(orm, user.id); const refused = await h.client.callTool({ name: 'set_assignment_end_day', @@ -579,9 +587,7 @@ describe('Tool: update_assignment_time', () => { const trip = await makeTrip(orm, user.id); const day = await makeDay(orm, trip.id); const place = await makePlace(orm, trip.id); - const ids: number[] = []; - for (const i of [0, 1, 2, 3]) ids.push((await makeDayAssignment(orm, day.id, place.id, { order_index: i })).id); - const [a, b, c, d] = ids; + const [a, b, c, d] = await stops(day.id, place.id, 4); await updateRows(orm, DayAssignments, { id: b }, { assignment_time: '15:00' }); const via = await insertRow(orm, RoadtripVias, { day: day.id, @@ -624,9 +630,7 @@ describe('Tool: update_assignment_time', () => { const trip = await makeTrip(orm, user.id); const day = await makeDay(orm, trip.id); const place = await makePlace(orm, trip.id); - const ids: number[] = []; - for (const i of [0, 1, 2]) ids.push((await makeDayAssignment(orm, day.id, place.id, { order_index: i })).id); - const [a, b, c] = ids; + const [a, b, c] = await stops(day.id, place.id, 3); await withHarness(user.id, async (h) => { await h.client.callTool({ @@ -646,9 +650,7 @@ describe('Tool: update_assignment_time', () => { const day = await makeDay(orm, trip.id); const place = await makePlace(orm, trip.id); // B starts before A and was put behind it on purpose. - const ids: number[] = []; - for (const i of [0, 1]) ids.push((await makeDayAssignment(orm, day.id, place.id, { order_index: i })).id); - const [a, b] = ids; + const [a, b] = await stops(day.id, place.id, 2); await updateRows(orm, DayAssignments, { id: a }, { assignment_time: '14:00' }); await updateRows(orm, DayAssignments, { id: b }, { assignment_time: '10:00' }); const via = await insertRow(orm, RoadtripVias, { diff --git a/server/tests/unit/mcp/tools-help-addons.test.ts b/server/tests/unit/mcp/tools-help-addons.test.ts index 2011d812d..3f605ba43 100644 --- a/server/tests/unit/mcp/tools-help-addons.test.ts +++ b/server/tests/unit/mcp/tools-help-addons.test.ts @@ -119,9 +119,9 @@ beforeEach(async () => { }); // resetDatabase leaves the addons table alone, so a case that flips a toggle // would otherwise leak into the next one. - for (const id of [ADDON_IDS.BUDGET, ADDON_IDS.PACKING, ADDON_IDS.COLLAB]) { - await setAddonEnabled(orm, id, true); - } + await Promise.all( + [ADDON_IDS.BUDGET, ADDON_IDS.PACKING, ADDON_IDS.COLLAB].map((id) => setAddonEnabled(orm, id, true)), + ); }); let orm: MikroORM;