Skip to content

test(#223): add unit tests for data utils#528

Open
nadavosa wants to merge 3 commits into
developfrom
223-add-data-utils-tests
Open

test(#223): add unit tests for data utils#528
nadavosa wants to merge 3 commits into
developfrom
223-add-data-utils-tests

Conversation

@nadavosa

Copy link
Copy Markdown
Collaborator

Summary

  • get-rrule.test.ts — all 7 weekdays (case-insensitive), invalid names, non-string input, whitespace edge case
  • get-start-end-dates.test.tsgetStartEndDates hour/minute output; getStartEnd label-to-hour mapping using .getHours() assertions (timezone-safe)
  • passwd.test.ts — mocks bcrypt to test call arguments and error-wrapping behaviour without slow 10-round hashing
  • fetch-json.test.ts — file-path vs URL routing, explicit fs/promises factory mock for reliable Node built-in interception

Test plan

  • Run yarn test:run src/test/data/utils/ and confirm all tests pass without a DB connection

Closes #223

🤖 Generated with Claude Code

…, passwd, fetchJsonFromUrl)

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

Copy link
Copy Markdown
Collaborator Author

Review notes:

Tests cover happy path, error cases, and edge inputs for all four utils (fetchJsonFromUrl, getRRULE, getStartEnd/getStartEndDates, hashPassword/verifyPassword).

One minor observation: the getStartEnd("8:00 - 10:00") test expects endHour === 11 even though the range string says 10:00. This is testing existing source-map behaviour rather than introducing a bug, so it's fine — but worth a comment in the test clarifying the discrepancy.

Otherwise looks correct and ready for Arturas to approve.

@arturasmckwcz

arturasmckwcz commented May 21, 2026

Copy link
Copy Markdown
Collaborator

✓ src/test/data/utils/get-rrule.test.ts (4 tests) 2ms
✓ src/test/data/utils/get-start-end-dates.test.ts (6 tests) 3ms
❯ src/test/data/utils/fetch-json.test.ts (3 tests | 2 failed) 5ms
× reads and parses JSON from a file path 3ms
✓ fetches JSON from a URL 1ms
× treats absolute paths as file system paths 1ms
❯ src/test/data/utils/passwd.test.ts (5 tests | 4 failed) 94ms
× uses 10 salt rounds and returns the resulting hash 45ms
× wraps bcrypt errors in a generic message 47ms
× returns true when bcrypt.compare resolves to true 1ms
✓ returns false when bcrypt.compare resolves to false 0ms
× wraps bcrypt errors in a generic message 1ms

@arturasmckwcz arturasmckwcz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @nadavosagetRRULE and passwd suites are accurate and well-targeted (non-string guard, no-trim whitespace, case-insensitivity; salt-rounds + both error wrappers). Reviewed each assertion against the source. A few notes, mostly nits:

1. (medium) getStartEndDates validation is dead code — the tests don't reveal it. The guard startHour < endHour && startHour < 0 && startHour > 24 && endHour < 0 && endHour > 24 is unsatisfiable, so invalid hours never throw. The suite only covers valid inputs, so it implies validation works when it doesn't. Either fix the guard (startHour > endHour || startHour < 0 || endHour > 24) and test the throw, or add a test asserting invalid input currently does not throw, so the gap is explicit. (Happy to land a tiny follow-up for the guard.)

2. (low) get-start-end-dates.test.ts codifies a wrong map entry. getStartEnd's map has incorrect values (e.g. "8:00 - 10:00" → 8..11, and "17:00-20:00" → 14..17). The test asserts the 8:00-10:00 → 8..11 value as expected — fine as characterization (and the comment is honest), but it locks the bug in. Worth fixing the source map separately and updating the expectation.

3. (low) timezone-fragile date assertion. expect(end.getFullYear()).toBe(2024) where end = new Date(\"2024-01-01\") (ISO date-only → parsed as UTC). In a UTC-negative TZ that's 2023 and the test fails. Green in Berlin/UTC (project default), but brittle. The getHours() assertions are fine (local-time date form).

4. (low) leaked global stub. fetch-json.test.ts calls vi.unstubAllGlobals() inline; if an assertion throws first, the fetch stub leaks into later tests. Move to afterEach(() => vi.unstubAllGlobals()).

5. (low) branch is behind develop (forked ~#578) — test-only, so low risk, but a rebase before merge runs it against current utils.

Approvable after the nits; #1 is the one worth addressing (it's a real source bug the tests currently paper over).

arturasmckwcz added a commit that referenced this pull request Jun 8, 2026
Two problems with the validation in getStartEndDates:
1. The guard was unsatisfiable (`startHour < 0 && startHour > 24` AND-ed), so
   invalid hours never threw. Replaced with: throw unless 0 <= startHour <
   endHour <= 24.
2. It threw a raw Error, which the global handler maps to 500. Invalid hours are
   a bad-request case, so throw BadRequestError -> 400 (per the error-handling
   convention in CLAUDE.md).

Source-only; getStartEndDates has no production callers today (surfaced while
reviewing the unit tests in #528). Happy-path inputs still pass; the throw-path
test belongs with the suite in #528.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
arturasmckwcz added a commit that referenced this pull request Jun 8, 2026
getStartEndDates had no production callers (referenced only by its own
definition and the barrel re-export) and its validation guard was dead code.
Rather than fix a dead function, remove it. Its sibling getStartEnd (4 callers)
stays in the same file.

Surfaced while reviewing the data-utils tests in #528 — the getStartEndDates
tests there should be dropped (keep the getStartEnd ones).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@arturasmckwcz

Copy link
Copy Markdown
Collaborator

Heads-up: getStartEndDates is being removed as stray dead code (no production callers) in #643. When you rebase, please drop the getStartEndDates describe block from get-start-end-dates.test.ts and keep the getStartEnd tests — otherwise the suite will fail to import a deleted function.

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.

add data utils tests

2 participants