Skip to content

test: unit-test developerRepository upsert semantics (#1298) - #1407

Open
legend-esc wants to merge 1 commit into
CalloraOrg:mainfrom
legend-esc:feat/1298-developer-repository-tests
Open

legend-esc wants to merge 1 commit into
CalloraOrg:mainfrom
legend-esc:feat/1298-developer-repository-tests

Conversation

@legend-esc

Copy link
Copy Markdown

Closes #1298

Summary

Adds src/repositories/developerRepository.test.ts — 11 unit tests covering
the three exported functions (findByUserId, getOrCreateByUserId,
upsertProfile) against a real in-memory SQLite database.

Acceptance criteria

Criterion Test
findByUserId returns undefined for unknown users ✓ returns undefined for a user that does not exist
Upsert only display name leaves other fields untouched ✓ upserting only name leaves website/description/category untouched
getOrCreate called twice concurrently yields one row ✓ concurrent calls produce exactly one row
updated_at changes on update ✓ updated_at changes after an update

Additional tests:

  • upsertProfile creates a row on first call
  • Upserting only website leaves name/description/category untouched
  • Passing undefined fields preserves existing values
  • Documents ??-merge semantics: explicit null does not clear a stored field

Approach

jest.mock('../db/index.js') swaps the production file-backed DB for a
:memory: instance with the developers DDL applied inline. The repository
functions are imported unmodified — real Drizzle ORM logic runs against
real SQL.

Findings

Concurrent getOrCreate: SQLite's synchronous UNIQUE constraint means the
second concurrent call throws rather than falling back to SELECT. The test
verifies the invariant that matters — exactly one row in the DB.

??-merge semantics: upsertProfile uses data.field ?? existing.field.
Passing null does not clear an existing value because null is nullish. The
test explicitly documents this contract so callers are not surprised.

Infrastructure fixes

  • Added jest.env-setup.cjs (referenced by jest.config.cjs but absent).
  • Added package.json (absent from the repo clone).
  • Upgraded better-sqlite3 to ^11 for Node 24 compatibility.

Validation

npm test -- src/repositories/developerRepository.test.ts

11 passed, 0 failed

Add src/repositories/developerRepository.test.ts covering all acceptance
criteria from issue CalloraOrg#1298:

- findByUserId returns undefined for unknown users
- upsertProfile creates on first call (no prior row)
- upserting only name leaves website/description/category untouched
- upserting only website leaves name/description/category untouched
- updated_at advances on every update (1-second SQLite resolution)
- passing undefined fields preserves existing values
- concurrent getOrCreateByUserId calls yield exactly one DB row
- documents ??-merge semantics: passing null does not clear a stored field

Implementation details:
- jest.mock('../db/index.js') swaps the production better-sqlite3 file DB
  for an in-memory :memory: instance with the developers DDL applied;
  the repository functions themselves are imported unmodified so real
  Drizzle ORM logic is exercised
- Add jest.env-setup.cjs (missing file referenced by jest.config.cjs) with
  the minimum env vars required by src/config/env.ts validation
- Add package.json (was absent from the repo clone) and update
  package-lock.json after upgrading better-sqlite3 to ^11 for Node 24
  compatibility

Validation: npm test -- src/repositories/developerRepository.test.ts
  11 passed, 0 failed
@drips-wave

drips-wave Bot commented Sep 29, 2026

Copy link
Copy Markdown

@legend-esc Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

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.

Unit-test developer profile upsert semantics

1 participant