Skip to content

test: add unit tests for invoice and user repositories#526

Closed
CKodidela wants to merge 3 commits intocameri:mainfrom
CKodidela:test/invoice-and-user-repositories
Closed

test: add unit tests for invoice and user repositories#526
CKodidela wants to merge 3 commits intocameri:mainfrom
CKodidela:test/invoice-and-user-repositories

Conversation

@CKodidela
Copy link
Copy Markdown
Collaborator

@CKodidela CKodidela commented Apr 19, 2026

Description

Adds unit test specs for InvoiceRepository and UserRepository, covering all public methods with a stubbed DB client (no real database connections). Also fixes a pre-existing Windows incompatibility in the test:unit npm script where single-quoted globs were passed literally to cmd.exe.

New files:

  • test/unit/repositories/invoice-repository.spec.ts (15 tests)
  • test/unit/repositories/user-repository.spec.ts (16 tests)

Modified files:

  • .mocharc.js moved spec glob here so mocha handles expansion cross-platform
  • package.json simplified test:unit script to just mocha

Related Issue

Closes #491

Motivation and Context

These data-access layer components had virtually no test coverage. Poor branch coverage meant conditional paths (found/not-found, error propagation, vanish-state fallback) were completely untested.

How Has This Been Tested?

All tests use sinon sandboxes and a stubbed DatabaseClient — no real Postgres connection required.

Coverage after this PR (measured with nyc):

File Statements Branches
invoice-repository.ts 88% (was 21%) 100% (was 0%)
user-repository.ts 91% (was 21%) 100% (was 0%)

Full suite: 875 passing, 0 failing (npm run test:unit).

Types of changes

  • Non-functional change (docs, style, minor refactor)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my code changes.
  • I added a changeset, or this is docs-only and I added an empty changeset.
  • All new and existing tests passed.

Copilot AI review requested due to automatic review settings April 19, 2026 10:11
@coveralls
Copy link
Copy Markdown
Collaborator

coveralls commented Apr 19, 2026

Coverage Status

coverage: 74.014% (+0.9%) from 73.153% — CKodidela:test/invoice-and-user-repositories into cameri:main

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves unit test coverage for the data-access layer by adding repository specs for InvoiceRepository and UserRepository, and updates the unit test runner configuration to be Windows-compatible by moving the spec glob into Mocha’s config.

Changes:

  • Add unit tests for InvoiceRepository and UserRepository using stubbed DB clients (no real DB connections).
  • Move the Mocha spec glob to .mocharc.js and simplify npm run test:unit to mocha for cross-platform compatibility.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
test/unit/repositories/invoice-repository.spec.ts Adds unit tests covering InvoiceRepository public methods and mapping behavior.
test/unit/repositories/user-repository.spec.ts Adds unit tests covering UserRepository public methods and vanish/balance/admission paths.
package.json Simplifies test:unit script to rely on Mocha config for spec discovery (Windows fix).
.mocharc.js Adds spec glob so Mocha handles test file expansion consistently across shells/OSes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +103 to +107
it('generates a UUID when invoice has no id', () => {
const invoice = makeInvoice()
delete (invoice as any).id

const sql = repository.upsert(invoice).toString()
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test deletes invoice.id via delete (invoice as any).id, which bypasses the Invoice type (where id is required) to reach a code path. If UUID generation is intended public behavior, consider making id optional in the input type/signature; otherwise, prefer a dedicated input type or explicit cast at the call site rather than mutating a typed object.

Copilot uses AI. Check for mistakes.
Comment on lines +8 to +9
import { IEventRepository, IUserRepository } from '../../../src/@types/repositories'
import { UserRepository } from '../../../src/repositories/user-repository'
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IUserRepository is imported but never used. This will typically fail lint/CI due to unused imports; remove it or use it for the repository variable type if that was the intent.

Copilot uses AI. Check for mistakes.
Comment on lines +41 to +45
unit: InvoiceUnit.MSATS,
status: InvoiceStatus.PENDING,
description: 'test invoice',
confirmed_at: null as any,
expires_at: null as any,
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makeDBInvoice uses null as any for nullable-looking DB fields (amount_paid, confirmed_at, expires_at). This weakens type-safety and likely means DBInvoice should model these as nullable (e.g., bigint | null, Date | null) so the test data can avoid any casts.

Copilot uses AI. Check for mistakes.
@CKodidela CKodidela force-pushed the test/invoice-and-user-repositories branch from 61560b4 to 57e1aa5 Compare April 20, 2026 04:40
@CKodidela CKodidela closed this Apr 20, 2026
@CKodidela CKodidela deleted the test/invoice-and-user-repositories branch April 20, 2026 04:40
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.

test: add unit tests for invoice and user repositories

3 participants