Skip to content

refactor(test): migrate 4 remaining multi-backend test files to createTestDb helper#3520

Merged
Yeraze merged 1 commit into
mainfrom
feature/3501-multibackend
Jun 17, 2026
Merged

refactor(test): migrate 4 remaining multi-backend test files to createTestDb helper#3520
Yeraze merged 1 commit into
mainfrom
feature/3501-multibackend

Conversation

@Yeraze

@Yeraze Yeraze commented Jun 17, 2026

Copy link
Copy Markdown
Owner

Summary

  • Migrates misc.test.ts, neighbors.test.ts, notifications.test.ts, and traceroutes.test.ts from hand-rolled SQLITE_CREATE DDL to createTestDb() (the real migration-built in-memory SQLite schema)
  • Removes ~250 lines of copy-pasted DDL that had already drifted from the production schema
  • Part of issue Tech debt (B2): migrate remaining 32 test files to the shared createTestDb() helper #3501 — all multi-backend repository test files now use createTestDb() for their SQLite path

Notable fixes

  • insertNodeSql in notifications.test.ts: adds nodeId (NOT NULL) and sourceId (composite PK component) required by the real schema
  • insertMessageSql in notifications.test.ts: adds createdAt (NOT NULL in real schema)
  • misc.test.ts: seeds a user before saveUserNewsStatus tests because migration 041 leaves foreign_keys=ON and the real schema has a FK from user_news_status.userId → users.id

Test plan

  • All 4 migrated test files pass locally: vitest run src/db/repositories/{misc,neighbors,notifications,traceroutes}.test.ts
  • Full suite: 438 test files passed, 0 failed
  • CI green

🤖 Generated with Claude Code

…lper

Replaces hand-rolled SQLITE_CREATE DDL with createTestDb() in
misc.test.ts, neighbors.test.ts, notifications.test.ts, and
traceroutes.test.ts so their SQLite path runs against the real
migration-built schema rather than a stale copy.

Fixed: insertNodeSql for SQLite now includes nodeId (NOT NULL) and
sourceId (composite PK); insertMessageSql includes createdAt (NOT NULL);
user_news_status tests seed a FK-required user since migration 041
leaves foreign_keys=ON.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jun 17, 2026

Copy link
Copy Markdown

Claude finished @Yeraze's task —— View job


PR Review — refactor(test): migrate 4 remaining multi-backend test files to createTestDb helper

  • Read changed files
  • Check createTestDb helper pattern
  • Analyze code quality and correctness
  • Post review feedback

This is a clean, well-structured refactor. The approach is consistent with the other 25 files migrated earlier and the code quality is good. A few observations:

Positive Highlights

  • SQLite path is now correct-by-construction. Using createTestDb() (which runs the real migration registry) means the SQLite backend tests will always match production schema. This is exactly the right fix — the drifted hand-rolled DDL was the root cause of feat(map): position-history markers at every fix, points-only mode, hover tooltip (#3492) #3495-style silent failures.
  • Foreign key seeding in misc.test.ts is thoughtful. The beforeEach try/catch for inserting user id=1 handles the real FK constraint from migration 041 while gracefully skipping PG/MySQL where the test DDL doesn't include the users table.
  • insertNodeSql in notifications.test.ts correctly adds nodeId and sourceId for the SQLite path, matching the real schema's NOT NULL and composite PK requirements.

Issues / Concerns

1. notifications.test.tsmessages schema divergence for PG/MySQL (minor, existing pattern)

The PG and MySQL POSTGRES_CREATE/MYSQL_CREATE blocks in notifications.test.ts define a messages table without a sourceId column (lines 125–145, 261–281). The production messages schema has sourceId. This PR doesn't introduce this gap, but it's worth noting as a follow-up: the PG/MySQL DDL for notifications.test.ts may drift again for the same reasons the SQLite DDL did. This pattern is already an accepted trade-off for PG/MySQL since those run only in CI with test containers.

2. notifications.test.tsinsertMessageSql SQLite vs PG/MySQL asymmetry (functional concern)

insertMessageSql (line 333) includes createdAt for SQLite only:

// SQLite:
`... timestamp, createdAt) VALUES (... ${timestamp}, ${timestamp})`
// PG/MySQL:
`... timestamp) VALUES (... ${timestamp})`

The PG messages DDL (line 136) has no createdAt column and no NOT NULL constraint for it, so PG tests pass. But if someone adds createdAt NOT NULL to the PG/MySQL DDL in the future, these inserts will silently fail. Not a bug today, just a flag for future maintainers.

3. notifications.test.tsmarkChannelMessagesAsRead userId=null test (latent risk)

The test at line 741–754 inserts a message but passes userId = null to markChannelMessagesAsRead, with a comment that "For SQLite, userId 0 doesn't need to exist (no FK constraint enforced by default)". With createTestDb(), foreign_keys=ON is actually enabled by migration 041 — if read_messages has an FK on userId → users.id, inserting with userId=0 when there's no user with id=0 could fail or be silently masked. The current test uses expect(typeof count).toBe('number') which only checks no exception is thrown, but this tolerance could hide a real behavioral change. Worth a closer look at whether migration 041 adds that FK on read_messages.

4. misc.test.tsALL_TABLES cleanup list no longer needed for SQLite (minor, no harm)

The ALL_TABLES constant (lines 153–160) is used only in the PG/MySQL beforeEach for clearTable(). The SQLite backend now creates a fresh createTestDb() instance per test (lines 585–593), so ALL_TABLES isn't referenced in the SQLite path. This is correct behavior — just a note that the constant's scope is PG/MySQL only.

5. neighbors.test.ts — no schema drift concerns

The simplest of the four; clean migration with no notable issues.

6. traceroutes.test.ts — no schema drift concerns

Also clean. The DbTraceroute/DbRouteSegment type imports reinforce that the repository contract is being tested, not just raw SQL.


Summary

The core change is correct and beneficial. The SQLite path for all four repositories now uses the real migration-built schema, eliminating future drift. The PG/MySQL paths are unchanged (still hand-rolled DDL in POSTGRES_CREATE/MYSQL_CREATE constants), which is the accepted pattern for this codebase.

The one item worth a follow-up is point 3 above — verifying the userId=null behavior in the markChannelMessagesAsRead test works correctly now that foreign_keys=ON is active via createTestDb().

Verdict: Approve with optional follow-up on point 3.


@Yeraze Yeraze merged commit d6c50b0 into main Jun 17, 2026
19 checks passed
@Yeraze Yeraze deleted the feature/3501-multibackend branch June 17, 2026 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant