test(server): seed and read test data through ORM factories

The server suite seeds and reads its database through some 5,500 raw
better-sqlite3 statements on the sync handle. They restate the schema by
hand, skip the repositories the app goes through and run on SQLite only,
so nothing in the suite could ever exercise the dialect layer on another
engine.

tests/helpers/factories/ adds typed factories per domain (users, trips and
days, places, assignments, bookings, budget, packing, to-dos, files,
journeys, tours, collections, notifications, settings, plugins, tokens,
atlas, vacay, photos, collab) and generic readers (findRow, findRows,
countRows, updateRows, deleteRows) that go through MikroORM in a request
context of their own, against the app's ORM or a createTestOrm handle.
They refuse a write to a read-only foreign-key twin, which MikroORM would
otherwise drop without a word. The README there is the conversion guide.

The categories integration suite and the tags and calendar-feed e2e suites
are converted in full as worked examples; the tags suite also moves from
hand-rolled tables to the migrated snapshot.

lint:test-sql (scripts/test-sql-ratchet.mjs) holds the raw statements left
to a per-file baseline: growth and stale entries fail, files whose subject
is raw SQL are exempt with a reason, and a single statement can be allowed
with a test-sql-allow comment. CI runs it next to lint:boundaries.
This commit is contained in:
Maurice
2026-10-08 15:13:04 +02:00
parent be751ad521
commit 1bba65634c
37 changed files with 2760 additions and 87 deletions
+354
View File
@@ -0,0 +1,354 @@
{
"exempt": {
"tests/helpers/legacy-fixture.ts": "Loads and fingerprints the legacy SQL fixtures, databases the retired positional runner built before any ORM schema existed.",
"tests/helpers/legacy-upgrade-suite.ts": "Reads legacy databases before and after the legacy baseline adopts them; the bookkeeping tables it checks (schema_version, mikro_orm_migrations) are the subject.",
"tests/integration/legacy-upgrade-v244.test.ts": "Compares a v4.3.3 database column by column before and after the upgrade, through pragma_table_info, on a copy no ORM is bound to.",
"tests/unit/db/dialect/sql-functions.test.ts": "Pins the SQL text and the engine result of every dialect function in src/db/dialect/sql-functions.ts; the raw statement is what is under test.",
"tests/unit/db/entity-schema-parity.test.ts": "Compares PRAGMA table_info, foreign_key_list and index_list of the migrated schema against the entity metadata.",
"tests/unit/db/foreign-key-indexes.test.ts": "Reads PRAGMA foreign_key_list, index_list and index_info and EXPLAIN QUERY PLAN to prove every foreign key is indexed.",
"tests/unit/db/migrate-encryption-parity.test.ts": "Seeds and reads a bare database for scripts/migrate-encryption.ts, which runs on the raw handle without the ORM.",
"tests/unit/db/nul-safe-sqlite-platform.test.ts": "Pins how the SQLite driver binds and returns the literals the platform has to escape (NUL, NaN, infinities).",
"tests/unit/db/pre-migrate-snapshot.test.ts": "Inspects the VACUUM INTO copy written before the migrator runs, a file no ORM is bound to.",
"tests/unit/db/restore-migrates-forward.test.ts": "Probes a restored database file before any ORM boots on it.",
"tests/unit/db/test-orm.test.ts": "Proves the test ORM shares the raw handle it was given; the raw statements are the subject."
},
"counts": {
"tests/e2e/addons.e2e.test.ts": 10,
"tests/e2e/admin.e2e.test.ts": 7,
"tests/e2e/assignments.e2e.test.ts": 54,
"tests/e2e/atlas.e2e.test.ts": 14,
"tests/e2e/auth.e2e.test.ts": 10,
"tests/e2e/booking-import.e2e.test.ts": 3,
"tests/e2e/budget.e2e.test.ts": 27,
"tests/e2e/categories.e2e.test.ts": 3,
"tests/e2e/collab.e2e.test.ts": 26,
"tests/e2e/collections.e2e.test.ts": 10,
"tests/e2e/dawarich.e2e.test.ts": 14,
"tests/e2e/days.e2e.test.ts": 42,
"tests/e2e/doc-sync.e2e.test.ts": 11,
"tests/e2e/feeds.e2e.test.ts": 2,
"tests/e2e/files.e2e.test.ts": 6,
"tests/e2e/google-quota.e2e.test.ts": 3,
"tests/e2e/harness.ts": 1,
"tests/e2e/mailer.e2e.test.ts": 1,
"tests/e2e/notifications.e2e.test.ts": 4,
"tests/e2e/packing.e2e.test.ts": 24,
"tests/e2e/place-enrichment.e2e.test.ts": 3,
"tests/e2e/place-shadow.e2e.test.ts": 6,
"tests/e2e/places.e2e.test.ts": 48,
"tests/e2e/plugin-pois.e2e.test.ts": 2,
"tests/e2e/plugin-search.e2e.test.ts": 1,
"tests/e2e/public-api.e2e.test.ts": 28,
"tests/e2e/push.e2e.test.ts": 7,
"tests/e2e/receipt-scan.e2e.test.ts": 9,
"tests/e2e/reservations.e2e.test.ts": 36,
"tests/e2e/roadtrip.e2e.test.ts": 20,
"tests/e2e/school-holidays.e2e.test.ts": 4,
"tests/e2e/settings.e2e.test.ts": 11,
"tests/e2e/share.e2e.test.ts": 5,
"tests/e2e/storage-admin.e2e.test.ts": 7,
"tests/e2e/todo.e2e.test.ts": 8,
"tests/e2e/tours.e2e.test.ts": 34,
"tests/e2e/transit-provider.e2e.test.ts": 9,
"tests/e2e/trip-invite.e2e.test.ts": 6,
"tests/e2e/trips.e2e.test.ts": 23,
"tests/e2e/vacay.e2e.test.ts": 2,
"tests/helpers/db-mock.ts": 4,
"tests/helpers/factories.ts": 62,
"tests/helpers/test-db.ts": 5,
"tests/helpers/tours-repos.ts": 1,
"tests/integration/admin.test.ts": 62,
"tests/integration/airtrail-sync-request-context.test.ts": 2,
"tests/integration/assignments.test.ts": 5,
"tests/integration/async-closure-truthiness-guards.test.ts": 1,
"tests/integration/atlas-geocode-request-context.test.ts": 3,
"tests/integration/atlas.test.ts": 5,
"tests/integration/auth.test.ts": 75,
"tests/integration/budget.test.ts": 10,
"tests/integration/collections.test.ts": 3,
"tests/integration/dayReorder.test.ts": 5,
"tests/integration/days.test.ts": 46,
"tests/integration/doc-sync-webhook-request-context.test.ts": 2,
"tests/integration/feeds-request-context.test.ts": 3,
"tests/integration/files.test.ts": 2,
"tests/integration/immich.test.ts": 22,
"tests/integration/journey-place-order.test.ts": 6,
"tests/integration/journey.test.ts": 5,
"tests/integration/leg-mode-incoming.test.ts": 4,
"tests/integration/mcp.test.ts": 28,
"tests/integration/memories-immich.test.ts": 11,
"tests/integration/memories-synology.test.ts": 25,
"tests/integration/memories-unified.test.ts": 10,
"tests/integration/notifications.test.ts": 8,
"tests/integration/oauth-authorize.test.ts": 1,
"tests/integration/oauth.test.ts": 2,
"tests/integration/oidc.test.ts": 5,
"tests/integration/orm-request-context-seams.test.ts": 2,
"tests/integration/packing.test.ts": 5,
"tests/integration/passkey.test.ts": 4,
"tests/integration/places.test.ts": 32,
"tests/integration/plugins/boot-registry-order.test.ts": 5,
"tests/integration/plugins/dev-link.test.ts": 2,
"tests/integration/plugins/plugin-runtime.task2-conversion.test.ts": 23,
"tests/integration/plugins/plugin-runtime.test.ts": 67,
"tests/integration/plugins/plugin-scheduler-sweep.test.ts": 9,
"tests/integration/plugins/registry.test.ts": 18,
"tests/integration/profile.test.ts": 3,
"tests/integration/reservations.test.ts": 22,
"tests/integration/security.test.ts": 5,
"tests/integration/share.test.ts": 17,
"tests/integration/storage-restore-reload.test.ts": 2,
"tests/integration/systemNotices.test.ts": 23,
"tests/integration/todo.test.ts": 1,
"tests/integration/trip-access-primitives.test.ts": 5,
"tests/integration/trips.test.ts": 59,
"tests/integration/uploads-photo-request-context.test.ts": 3,
"tests/integration/vacay.test.ts": 2,
"tests/unit/db/booked-night-reseat.test.ts": 11,
"tests/unit/db/connection.test.ts": 3,
"tests/unit/db/durability.test.ts": 3,
"tests/unit/db/entities/primary-relations.spike.test.ts": 3,
"tests/unit/db/entity-class-fields.test.ts": 2,
"tests/unit/db/place-regions-follow-place.test.ts": 15,
"tests/unit/db/repositories/_shared/atlas-travowns-consumers.parity.test.ts": 8,
"tests/unit/db/repositories/_shared/collection-role.parity.test.ts": 2,
"tests/unit/db/repositories/_shared/owned-lookup.test.ts": 1,
"tests/unit/db/repositories/_shared/packing-visibility.parity.test.ts": 8,
"tests/unit/db/repositories/_shared/reservation-travelers-owns.parity.test.ts": 2,
"tests/unit/db/repositories/_shared/reservation-visibility.test.ts": 37,
"tests/unit/db/repositories/addons-config-encryption.test.ts": 1,
"tests/unit/db/repositories/Addons.repository.test.ts": 5,
"tests/unit/db/repositories/AppSettings.repository.test.ts": 6,
"tests/unit/db/repositories/AssignmentParticipants.repository.test.ts": 6,
"tests/unit/db/repositories/AuditLog.repository.test.ts": 10,
"tests/unit/db/repositories/bucket-list-duplicate.parity.test.ts": 2,
"tests/unit/db/repositories/BucketList.repository.test.ts": 12,
"tests/unit/db/repositories/BudgetItems.repository.test.ts": 5,
"tests/unit/db/repositories/Categories.repository.test.ts": 4,
"tests/unit/db/repositories/CollabMessages.repository.test.ts": 3,
"tests/unit/db/repositories/DawarichConnections.repository.test.ts": 4,
"tests/unit/db/repositories/DawarichVisitSuggestions.repository.test.ts": 11,
"tests/unit/db/repositories/DayAccommodations.repository.test.ts": 12,
"tests/unit/db/repositories/DayAssignments.repository.test.ts": 60,
"tests/unit/db/repositories/DayNotes.repository.test.ts": 10,
"tests/unit/db/repositories/Days.repository.test.ts": 9,
"tests/unit/db/repositories/DemoRepository.test.ts": 21,
"tests/unit/db/repositories/DocumentProviders.repository.test.ts": 3,
"tests/unit/db/repositories/DocumentSyncItems.repository.test.ts": 14,
"tests/unit/db/repositories/GooglePlacePhotoMeta.repository.test.ts": 12,
"tests/unit/db/repositories/HiddenRegions.repository.test.ts": 4,
"tests/unit/db/repositories/IdempotencyKeys.repository.test.ts": 9,
"tests/unit/db/repositories/identity-map-writeback.test.ts": 3,
"tests/unit/db/repositories/InviteTokens.repository.test.ts": 5,
"tests/unit/db/repositories/MaintenanceRepository.test.ts": 4,
"tests/unit/db/repositories/McpTokens.repository.test.ts": 11,
"tests/unit/db/repositories/NotificationChannelPreferences.repository.test.ts": 10,
"tests/unit/db/repositories/Notifications.repository.test.ts": 23,
"tests/unit/db/repositories/OauthClients.repository.test.ts": 7,
"tests/unit/db/repositories/OauthConsents.repository.test.ts": 9,
"tests/unit/db/repositories/OauthTokens.repository.test.ts": 31,
"tests/unit/db/repositories/PackingItems.repository.test.ts": 8,
"tests/unit/db/repositories/PasswordResetTokens.repository.test.ts": 4,
"tests/unit/db/repositories/PhotoProviderFields.repository.test.ts": 2,
"tests/unit/db/repositories/PhotoProviders.repository.test.ts": 4,
"tests/unit/db/repositories/PlaceDetailsCache.repository.test.ts": 8,
"tests/unit/db/repositories/PlaceRatings.repository.test.ts": 9,
"tests/unit/db/repositories/PlaceRegions.repository.test.ts": 7,
"tests/unit/db/repositories/Places.repository.test.ts": 94,
"tests/unit/db/repositories/PlaceShadowPicks.repository.test.ts": 12,
"tests/unit/db/repositories/PluginOauthTokens.repository.test.ts": 1,
"tests/unit/db/repositories/ReservationDayPositions.repository.test.ts": 4,
"tests/unit/db/repositories/ReservationEndpoints.repository.test.ts": 5,
"tests/unit/db/repositories/Reservations.repository.test.ts": 76,
"tests/unit/db/repositories/ReservationTravelers.repository.test.ts": 7,
"tests/unit/db/repositories/RoadtripDayTracks.repository.test.ts": 1,
"tests/unit/db/repositories/Settings.repository.test.ts": 8,
"tests/unit/db/repositories/ShareTokens.repository.test.ts": 10,
"tests/unit/db/repositories/Tags.repository.test.ts": 13,
"tests/unit/db/repositories/TodoItems.repository.test.ts": 3,
"tests/unit/db/repositories/Tours.repository.test.ts": 2,
"tests/unit/db/repositories/trek-repository.test.ts": 4,
"tests/unit/db/repositories/TripInviteTokens.repository.test.ts": 7,
"tests/unit/db/repositories/TripMembers.repository.test.ts": 34,
"tests/unit/db/repositories/Trips.repository.test.ts": 71,
"tests/unit/db/repositories/UserNoticeDismissals.repository.test.ts": 9,
"tests/unit/db/repositories/Users.repository.test.ts": 109,
"tests/unit/db/repositories/vacay-branch-coverage.test.ts": 10,
"tests/unit/db/repositories/VacayCompanyHolidays.repository.test.ts": 5,
"tests/unit/db/repositories/VacayHolidayCalendars.repository.test.ts": 4,
"tests/unit/db/repositories/VacayPlanMembers.repository.test.ts": 6,
"tests/unit/db/repositories/VacayPlans.repository.test.ts": 3,
"tests/unit/db/repositories/VacayShares.repository.test.ts": 11,
"tests/unit/db/repositories/VacayUserSettings.repository.test.ts": 4,
"tests/unit/db/repositories/VacayUserYears.repository.test.ts": 5,
"tests/unit/db/repositories/VacayYears.repository.test.ts": 5,
"tests/unit/db/repositories/VisitedCountries.repository.test.ts": 7,
"tests/unit/db/repositories/VisitedRegions.repository.test.ts": 9,
"tests/unit/db/repositories/WebauthnChallenges.repository.test.ts": 5,
"tests/unit/db/repositories/WebauthnCredentials.repository.test.ts": 6,
"tests/unit/db/seeds-admin.test.ts": 6,
"tests/unit/db/transaction-yield.test.ts": 8,
"tests/unit/db/trip-access.test.ts": 8,
"tests/unit/demo/demo-seed.test.ts": 6,
"tests/unit/helpers/test-uow.test.ts": 1,
"tests/unit/idempotency-cleanup.test.ts": 6,
"tests/unit/mcp/tools-airtrail.test.ts": 2,
"tests/unit/mcp/tools-assignments-reservations-extra.test.ts": 2,
"tests/unit/mcp/tools-assignments.test.ts": 21,
"tests/unit/mcp/tools-atlas-expanded.test.ts": 21,
"tests/unit/mcp/tools-atlas.test.ts": 5,
"tests/unit/mcp/tools-budget-advanced.test.ts": 13,
"tests/unit/mcp/tools-budget.test.ts": 30,
"tests/unit/mcp/tools-categories.test.ts": 19,
"tests/unit/mcp/tools-collab-polls-chat.test.ts": 11,
"tests/unit/mcp/tools-collections.test.ts": 16,
"tests/unit/mcp/tools-days-accommodations.test.ts": 20,
"tests/unit/mcp/tools-days.test.ts": 8,
"tests/unit/mcp/tools-feeds-invite.test.ts": 12,
"tests/unit/mcp/tools-files.test.ts": 18,
"tests/unit/mcp/tools-help-addons.test.ts": 4,
"tests/unit/mcp/tools-journey.test.ts": 3,
"tests/unit/mcp/tools-leg-transport-mode.test.ts": 5,
"tests/unit/mcp/tools-memories.test.ts": 6,
"tests/unit/mcp/tools-notes.test.ts": 22,
"tests/unit/mcp/tools-notifications.test.ts": 7,
"tests/unit/mcp/tools-packing-advanced.test.ts": 17,
"tests/unit/mcp/tools-packing.test.ts": 24,
"tests/unit/mcp/tools-places.test.ts": 34,
"tests/unit/mcp/tools-plugin-pois.test.ts": 2,
"tests/unit/mcp/tools-prompts.test.ts": 6,
"tests/unit/mcp/tools-reservations.test.ts": 16,
"tests/unit/mcp/tools-roadtrip.test.ts": 8,
"tests/unit/mcp/tools-settings.test.ts": 5,
"tests/unit/mcp/tools-share.test.ts": 2,
"tests/unit/mcp/tools-tags-maps-weather.test.ts": 9,
"tests/unit/mcp/tools-todos.test.ts": 7,
"tests/unit/mcp/tools-transit.test.ts": 11,
"tests/unit/mcp/tools-transports.test.ts": 22,
"tests/unit/mcp/tools-trip-members.test.ts": 32,
"tests/unit/mcp/tools-trips.test.ts": 49,
"tests/unit/mcp/tools-vacay.test.ts": 18,
"tests/unit/nest/accommodations.service.test.ts": 68,
"tests/unit/nest/addons.service.test.ts": 12,
"tests/unit/nest/admin.service.test.ts": 30,
"tests/unit/nest/admin.version-notification.test.ts": 5,
"tests/unit/nest/airtrail.service.test.ts": 1,
"tests/unit/nest/assignments.service.test.ts": 36,
"tests/unit/nest/atlas.service.test.ts": 46,
"tests/unit/nest/audit.service.test.ts": 6,
"tests/unit/nest/auth.service.test.ts": 73,
"tests/unit/nest/backup-archive-version.test.ts": 1,
"tests/unit/nest/boot-restore.test.ts": 2,
"tests/unit/nest/budget.service.calc.test.ts": 11,
"tests/unit/nest/budget.service.db.test.ts": 108,
"tests/unit/nest/budget.service.test.ts": 3,
"tests/unit/nest/calendar.service.test.ts": 88,
"tests/unit/nest/channel-override.test.ts": 1,
"tests/unit/nest/collab.service.test.ts": 35,
"tests/unit/nest/collections.service.test.ts": 62,
"tests/unit/nest/database/unit-of-work.test.ts": 1,
"tests/unit/nest/dawarich-suggestions.service.test.ts": 17,
"tests/unit/nest/dawarich-sync.service.test.ts": 27,
"tests/unit/nest/dawarich.service.test.ts": 2,
"tests/unit/nest/day-boundaries.test.ts": 1,
"tests/unit/nest/day-notes.service.test.ts": 4,
"tests/unit/nest/day-removal.service.test.ts": 24,
"tests/unit/nest/days.service.test.ts": 66,
"tests/unit/nest/doc-sync/doc-sync-config.service.test.ts": 23,
"tests/unit/nest/doc-sync/doc-sync.controller.test.ts": 18,
"tests/unit/nest/doc-sync/doc-sync.job.test.ts": 4,
"tests/unit/nest/doc-sync/doc-sync.service.test.ts": 50,
"tests/unit/nest/feeds.service.test.ts": 4,
"tests/unit/nest/files.service.test.ts": 26,
"tests/unit/nest/google-quota.service.test.ts": 7,
"tests/unit/nest/google-route.test.ts": 2,
"tests/unit/nest/idempotency.interceptor.test.ts": 2,
"tests/unit/nest/immich.service.test.ts": 5,
"tests/unit/nest/journey-book.service.test.ts": 8,
"tests/unit/nest/journey-domain.service.test.ts": 173,
"tests/unit/nest/journey-share.service.test.ts": 34,
"tests/unit/nest/journey-stats.service.test.ts": 7,
"tests/unit/nest/mailer.service.test.ts": 1,
"tests/unit/nest/mcp-shared.service.test.ts": 6,
"tests/unit/nest/memories-access.service.test.ts": 42,
"tests/unit/nest/notification-preferences.service.test.ts": 12,
"tests/unit/nest/notification-transports.test.ts": 3,
"tests/unit/nest/notifications.inapp-prefs.test.ts": 17,
"tests/unit/nest/notifications.service.test.ts": 45,
"tests/unit/nest/notifications/push-subscriptions.service.test.ts": 4,
"tests/unit/nest/notifications/vapid-keys.service.test.ts": 14,
"tests/unit/nest/notifications/web-push-dispatch.test.ts": 7,
"tests/unit/nest/notifications/web-push.service.test.ts": 7,
"tests/unit/nest/oauth.service.test.ts": 6,
"tests/unit/nest/oidc.service.test.ts": 48,
"tests/unit/nest/packing.service.test.ts": 56,
"tests/unit/nest/passkey.service.test.ts": 18,
"tests/unit/nest/permissions.service.test.ts": 12,
"tests/unit/nest/photo-resolver.service.test.ts": 4,
"tests/unit/nest/place-enrichment.service.test.ts": 1,
"tests/unit/nest/place-photos/place-photo-cache.service.test.ts": 14,
"tests/unit/nest/place-regions-repair.test.ts": 7,
"tests/unit/nest/place-shadow.service.test.ts": 9,
"tests/unit/nest/places.directions.test.ts": 1,
"tests/unit/nest/places.service.test.ts": 98,
"tests/unit/nest/platform.controller.test.ts": 3,
"tests/unit/nest/public-api-scopes.test.ts": 6,
"tests/unit/nest/query-helpers.service.test.ts": 4,
"tests/unit/nest/registration-invites.service.test.ts": 4,
"tests/unit/nest/reminder-jobs.service.test.ts": 15,
"tests/unit/nest/reservations.service.test.ts": 135,
"tests/unit/nest/roadtrip-plan.service.test.ts": 30,
"tests/unit/nest/roadtrip.service.test.ts": 2,
"tests/unit/nest/route-usage.service.test.ts": 10,
"tests/unit/nest/school-holidays.service.test.ts": 11,
"tests/unit/nest/settings.service.test.ts": 26,
"tests/unit/nest/settings/instance-api-keys.test.ts": 8,
"tests/unit/nest/share.service.test.ts": 49,
"tests/unit/nest/storage/storage-admin.service.test.ts": 3,
"tests/unit/nest/storage/storage-jobs.service.test.ts": 3,
"tests/unit/nest/storage/storage-registry.service.test.ts": 11,
"tests/unit/nest/storage/storage-stats.service.test.ts": 3,
"tests/unit/nest/synology.service.test.ts": 4,
"tests/unit/nest/thumbnail.service.test.ts": 3,
"tests/unit/nest/todo.service.test.ts": 7,
"tests/unit/nest/token.service.test.ts": 12,
"tests/unit/nest/tours.gpx.atomic.test.ts": 8,
"tests/unit/nest/tours.service.test.ts": 12,
"tests/unit/nest/travel-stats.service.test.ts": 6,
"tests/unit/nest/trek-photo-cache.service.test.ts": 13,
"tests/unit/nest/trek-photo-registration.service.test.ts": 9,
"tests/unit/nest/trip-invite.service.test.ts": 3,
"tests/unit/nest/trip-members.service.test.ts": 19,
"tests/unit/nest/trip-membership/trip-membership.service.test.ts": 8,
"tests/unit/nest/trip-read-model.service.test.ts": 10,
"tests/unit/nest/trips.service.test.ts": 208,
"tests/unit/nest/unified-memories.service.test.ts": 29,
"tests/unit/nest/user-cleanup.service.test.ts": 48,
"tests/unit/nest/user-profile.service.test.ts": 16,
"tests/unit/nest/vacay.service.test.ts": 93,
"tests/unit/plugins/discovery.test.ts": 12,
"tests/unit/plugins/host-rpc.test.ts": 38,
"tests/unit/plugins/operator-egress.test.ts": 5,
"tests/unit/plugins/plugin-audit.test.ts": 11,
"tests/unit/plugins/plugin-guards.test.ts": 2,
"tests/unit/plugins/plugin-host-state.test.ts": 1,
"tests/unit/plugins/plugin-instance-settings.test.ts": 17,
"tests/unit/plugins/plugin-oauth.service.test.ts": 13,
"tests/unit/plugins/plugin-pois.test.ts": 4,
"tests/unit/plugins/plugin-repository-sql-parity.test.ts": 20,
"tests/unit/plugins/plugin-route-normalize.test.ts": 2,
"tests/unit/plugins/plugin-routes.controller.test.ts": 1,
"tests/unit/plugins/plugin-runtime.boot-no-orm.test.ts": 1,
"tests/unit/plugins/plugin-user-settings.test.ts": 4,
"tests/unit/plugins/plugins-service.test.ts": 29,
"tests/unit/plugins/request-context.test.ts": 1,
"tests/unit/plugins/rpc-fallbacks.test.ts": 1,
"tests/unit/plugins/rpc-host-inv-strip.test.ts": 1,
"tests/unit/plugins/settings-isolation.test.ts": 9,
"tests/unit/services/airtrailImport.test.ts": 4,
"tests/unit/services/conflictUpdate.test.ts": 3,
"tests/unit/services/trekPhotoMedia.test.ts": 2,
"tests/unit/trips/trips.rpc.test.ts": 1,
"tests/websocket/connection.test.ts": 7
}
}
+201
View File
@@ -0,0 +1,201 @@
#!/usr/bin/env node
/*
* lint:test-sql: raw better-sqlite3 statements in the server tests may only shrink.
*
* The suite used to seed and read its database through `.prepare()` on the
* synchronous better-sqlite3 handle: thousands of hand-written SQLite
* statements that no other driver can serve, which welds every test to one
* engine and bypasses the repositories the app goes through. The factories
* and readers in tests/helpers/factories/ do the same work through MikroORM
* (see the README there); this check makes sure the old way only recedes.
*
* Every `.prepare(` under tests/ is counted per file, mentions in comments
* included. A file may hold at most its entry in the `counts` of
* scripts/test-sql-baseline.json, and a file without an entry none at all.
* An entry above what its file holds now, or for a file that is gone, fails
* too until --update lowers it, so a converted file cannot grow back unseen.
*
* Two ways out, both explicit and reviewed:
* - a file whose subject is raw SQL itself (migrations, the legacy
* baseline, the dialect functions, PRAGMA introspection) is listed under
* `exempt` with the reason; it is not counted, and an exemption for a
* file that is gone fails;
* - a single statement that has to stay raw carries a comment with
* `test-sql-allow:` and a reason, on its own line or the line above.
* A marker with no reason does not count.
*
* npm run lint:test-sql check against the baseline (CI)
* npm run lint:test-sql -- --update lower the counts to what the files hold now;
* it never raises an entry, never adds one,
* and never touches the exemptions
*
* --dir=<path> points the check at another server root (the unit tests use it).
*/
import { existsSync, readdirSync, readFileSync, realpathSync, statSync, writeFileSync } from 'node:fs';
import { join, relative, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
/** The tree the check walks, relative to the server root. */
export const ROOT = 'tests';
/** The baseline, relative to the server root. */
export const BASELINE = 'scripts/test-sql-baseline.json';
const SOURCE = /\.(?:[cm]?[jt]s)$/;
const PREPARE = /\.prepare\(/g;
const ALLOW = /test-sql-allow:\s*\S/;
/**
* The raw statements in one file's text: every `.prepare(` except those on a
* line that carries an allow marker with a reason, or right below one.
*/
export function countPrepares(text) {
const lines = text.replace(/\r\n?/g, '\n').split('\n');
let total = 0;
lines.forEach((line, i) => {
const hits = line.match(PREPARE)?.length ?? 0;
if (hits === 0) return;
if (ALLOW.test(line) || (i > 0 && ALLOW.test(lines[i - 1]))) return;
total += hits;
});
return total;
}
function walk(dir, files = []) {
for (const name of readdirSync(dir)) {
if (name === 'node_modules') continue;
const path = join(dir, name);
if (statSync(path).isDirectory()) walk(path, files);
else if (SOURCE.test(name)) files.push(path);
}
return files;
}
/** Raw statement counts keyed by the server-relative POSIX path, files without any left out. */
export function scan(serverDir) {
const dir = join(serverDir, ROOT);
if (!existsSync(dir) || !statSync(dir).isDirectory()) {
throw new Error(`${ROOT}/ does not exist under ${serverDir}: the check would pass without looking at anything`);
}
const counts = {};
for (const path of walk(dir)) {
const n = countPrepares(readFileSync(path, 'utf8'));
if (n > 0) counts[relative(serverDir, path).split('\\').join('/')] = n;
}
return counts;
}
/**
* The baseline as committed. Missing, unreadable or malformed stops the run:
* read as empty, every file would fail with no hint at the cause, and
* --update would replace it with an empty one.
*/
export function readBaseline(path) {
let parsed;
try {
parsed = JSON.parse(readFileSync(path, 'utf8'));
} catch (err) {
throw new Error(`${BASELINE} cannot be read: ${err.message}`);
}
const isObject = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
if (!isObject(parsed) || !isObject(parsed.exempt) || !isObject(parsed.counts)) {
throw new Error(`${BASELINE} must be an object with an "exempt" and a "counts" object`);
}
for (const [file, reason] of Object.entries(parsed.exempt)) {
if (typeof reason !== 'string' || reason.trim() === '') {
throw new Error(`${BASELINE}: the exemption for ${file} needs a reason`);
}
if (file in parsed.counts) {
throw new Error(`${BASELINE}: ${file} is both exempt and counted`);
}
}
for (const [file, n] of Object.entries(parsed.counts)) {
if (!Number.isInteger(n) || n <= 0) {
throw new Error(`${BASELINE}: ${file} holds ${JSON.stringify(n)}, expected a whole number above 0`);
}
}
return parsed;
}
/** The counts lowered to what the files hold now. Never raises an entry, never adds one. */
export function lowerCounts(baseline, counts) {
const lowered = {};
for (const [file, allowed] of Object.entries(baseline.counts)) {
const now = counts[file] ?? 0;
if (now > 0) lowered[file] = Math.min(allowed, now);
}
return Object.fromEntries(Object.entries(lowered).sort(([a], [b]) => a.localeCompare(b)));
}
/**
* Files over their allowance; count entries above what their file holds now
* (exactly the ones lowerCounts would lower or drop); exemptions for files
* that are gone.
*/
export function compare(baseline, counts, serverDir) {
const exempt = baseline.exempt;
const grown = Object.entries(counts).filter(([file, n]) => !(file in exempt) && n > (baseline.counts[file] ?? 0));
const lowered = lowerCounts(baseline, counts);
const stale = Object.entries(baseline.counts).filter(([file, n]) => lowered[file] !== n);
const goneExempt = Object.keys(exempt).filter((file) => !existsSync(join(serverDir, file)));
return { grown, stale, goneExempt };
}
function main(argv) {
const dirArg = argv.find((a) => a.startsWith('--dir='));
const serverDir = dirArg ? resolve(dirArg.slice('--dir='.length)) : fileURLToPath(new URL('..', import.meta.url));
const baselinePath = join(serverDir, BASELINE);
const update = argv.includes('--update');
const counts = scan(serverDir);
const baseline = readBaseline(baselinePath);
if (update) {
baseline.counts = lowerCounts(baseline, counts);
writeFileSync(baselinePath, JSON.stringify(baseline, null, 2) + '\n');
}
const { grown, stale, goneExempt } = compare(baseline, counts, serverDir);
for (const [file, n] of grown) {
const entry = baseline.counts[file];
console.error(
`FAIL ${file}: ${n} raw statement(s), ` +
(entry ? `its baseline is ${entry}. ` : 'a file without a baseline entry may hold none. ') +
'Seed and read through tests/helpers/factories/ instead (see the README there).',
);
}
for (const [file, entry] of stale) {
console.error(
`FAIL ${file} is held at ${entry} in ${BASELINE}, ` +
(file in counts ? `but it has ${counts[file]} now.` : 'but it has none left (or the file is gone).'),
);
}
for (const file of goneExempt) {
console.error(`FAIL ${file} is exempt in ${BASELINE}, but the file is gone: drop the exemption.`);
}
if (stale.length) {
console.error(
'Run npm run lint:test-sql -- --update to lower the baseline with the change that made it smaller: ' +
'an entry above the file lets it grow back unseen.',
);
}
const total = Object.entries(counts)
.filter(([file]) => !(file in baseline.exempt))
.reduce((sum, [, n]) => sum + n, 0);
console.log(
`test-sql: ${total} raw statement(s) in ${Object.keys(baseline.counts).length} baselined file(s), ` +
`${Object.keys(baseline.exempt).length} file(s) exempt`,
);
return grown.length || stale.length || goneExempt.length ? 1 : 0;
}
// Compared by real path, so the check still runs when the script is started through a symlink.
const isCli =
Boolean(process.argv[1]) && existsSync(process.argv[1]) && realpathSync(process.argv[1]) === fileURLToPath(import.meta.url);
if (isCli) {
try {
process.exitCode = main(process.argv.slice(2));
} catch (err) {
console.error(`FAIL ${err.message}`);
process.exitCode = 1;
}
}