test: add integration tests for /api/v1/prices routes#56
Open
cLamberti wants to merge 1 commit into
Open
Conversation
Closes SmartDropLabs#9 Adds 13 supertest integration tests covering GET /api/v1/prices/:asset_code and GET /api/v1/prices/:asset_code/refresh. All tests mock priceOracle, apiKeys, cache, and logger at module level for speed and isolation. Suite runs in ~2s. Prerequisite fixes required to make the test suite runnable: - package.json: missing comma between "zod" and "winston-daily-rotate-file" caused JSONError on every npm test invocation - src/index.js: `const server` declared inside if block made `module.exports = { app, server }` throw ReferenceError when imported in tests; moved to module scope Collateral fixes in existing test files: - test/airdrops.test.js: `require('../src/index')` was assigning the whole module object instead of the Express app - test/alerts-routes.test.js: `server.close(done)` threw TypeError since server is undefined in test context; added null guard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #9
Good afternoon maintainer, thanks for the opportunity to contribute.
Here's what I did:
Summary
Adds 13 supertest integration tests for
GET /api/v1/prices/:asset_codeand
GET /api/v1/prices/:asset_code/refresh. All external dependencies(priceOracle, apiKeys, cache, logger) are mocked at module level so tests
are fast, deterministic, and require no running Redis or network.
Test results
13/13 passing in ~2s.
Test cases
GET /api/v1/prices/:asset_codeis_stale: trueand non-emptystale_warningprice_usd: null) — 404 (see discrepancy note below)nullissuer?issuer=query param — passed through to oracleredis_unavailable: true(graceful degradation)GET /api/v1/prices/:asset_code/refreshDiscrepancy noted
Issue #9 describes unknown assets as returning
200withprice_usd: null.The actual route (
src/routes/prices.jslines 42–48) returns404whenpriceData.price_usd === null. The test reflects real route behavior andincludes an inline comment documenting the discrepancy.
Prerequisite fixes
Two bugs prevented
npm testfrom running at all before this PR:package.json: missing comma between"zod"and"winston-daily-rotate-file"caused a JSONError on everynpm testinvocation before a single test could run.src/index.js:const serverwas declared inside theif (require.main === module)block, makingmodule.exports = { app, server }throw aReferenceErrorwhen imported in tests.Pre-existing test failures (not introduced by this PR), I'm happy to help with it, though. Just create an issue I'll apply and you can assign it to me.
test/airdrops.test.js(14 failures) andtest/alerts-routes.test.js(1 failure)were already broken before this branch — they were simply undetectable because
npm testcrashed on the JSON syntax error before running any test file.The root cause is a routing issue in
src/index.js:requireApiKey()registeredvia
app.use('/api/v1', requireApiKey(), alertsRouter)intercepts all subsequent/api/v1/*requests, returning 401 for routes that don't require auth.These failures are pre-existing and out of scope for this PR.
Collateral fixes applied to unblock the full suite run:
test/airdrops.test.js: corrected import fromrequire('../src/index')torequire('../src/index').apptest/alerts-routes.test.js: added null guard onserver.close(done)to prevent TypeError in test context