test(utils): add tests for misc, MemCache, and DiskCache#502
Conversation
There was a problem hiding this comment.
Pull request overview
Adds comprehensive Jasmine coverage for utility helpers and both cache implementations, strengthening regression protection around TTL behavior, persistence, and input validation.
Changes:
- Add unit tests for
env(),seconds(),ms(), andHTTPError. - Add behavioral tests for
MemCache(TTL expiry, stale reads,getOr(),invalidate(), etc.). - Add integration-style tests for
DiskCache(persistence across instances, TTL expiry/invalidation, and path traversal key rejection).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/utils/misc.test.js | New specs for misc utility helpers and HTTPError. |
| tests/utils/mem-cache.test.js | New specs validating MemCache correctness across key behaviors. |
| tests/utils/disk-cache.test.js | New specs validating DiskCache persistence, TTL behavior, and key/path safety checks. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
6e23fc2 to
1b45559
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const cache = new DiskCache({ ttl: 1, path: "test-cache" }); | ||
| await cache.set("key", "value"); | ||
|
|
||
| // Wait briefly to ensure TTL expires | ||
| await new Promise(resolve => setTimeout(resolve, 10)); | ||
| expect(await cache.get("key")).toBeUndefined(); | ||
| }); |
There was a problem hiding this comment.
These TTL tests use real setTimeout sleeps to force expiry, which slows the suite and can be timing-sensitive. Consider stubbing Date.now() (or using a fake clock) to advance time deterministically and avoid waiting in tests.
There was a problem hiding this comment.
Valid suggestion. The DiskCache TTL tests use real setTimeout(10ms) which is timing-dependent. Stubbing Date.now() would be more deterministic. However, with 10ms waits the flakiness risk is very low in practice, and DiskCache reads file mtimes from disk which adds complexity to mocking. Keeping as-is for now — can refactor to fake clocks as a follow-up if flakiness is observed.
| const cache = new DiskCache({ ttl: 1, path: "test-cache" }); | ||
| await cache.set("a", 1); | ||
| await cache.set("b", 2); | ||
|
|
||
| await new Promise(resolve => setTimeout(resolve, 10)); | ||
| await cache.invalidate(); | ||
|
|
||
| expect(await cache.get("a", true)).toBeUndefined(); | ||
| expect(await cache.get("b", true)).toBeUndefined(); | ||
| }); |
There was a problem hiding this comment.
Same as above: relying on setTimeout(..., 10) to age entries makes the test slower and more timing-dependent than necessary. Stubbing Date.now()/using a fake clock would let you test invalidate() behavior without sleeping.
There was a problem hiding this comment.
Valid suggestion. The DiskCache TTL tests use real setTimeout(10ms) which is timing-dependent. Stubbing Date.now() would be more deterministic. However, with 10ms waits the flakiness risk is very low in practice, and DiskCache reads file mtimes from disk which adds complexity to mocking. Keeping as-is for now — can refactor to fake clocks as a follow-up if flakiness is observed.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| await new Promise(resolve => setTimeout(resolve, 10)); | ||
| await cache.invalidate(); |
There was a problem hiding this comment.
Similar to the TTL expiry tests, this setTimeout-based wait can introduce timing flakiness. Prefer stubbing Date.now() to move time forward, which keeps the test deterministic and avoids adding real delays to CI runs.
There was a problem hiding this comment.
Valid suggestion. The DiskCache TTL tests use real setTimeout(10ms) which is timing-dependent. Stubbing Date.now() would be more deterministic. However, with 10ms waits the flakiness risk is very low in practice, and DiskCache reads file mtimes from disk which adds complexity to mocking. Keeping as-is for now — can refactor to fake clocks as a follow-up if flakiness is observed.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
1b45559 to
94a9a01
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Adds 54 new specs covering the three core utility modules: - misc: env(), seconds(), ms(), HTTPError (19 specs) - MemCache: set/get, TTL expiry, has, getOr, delete, clear, expires, invalidate (24 specs) - DiskCache: set/get round-trip, TTL expiry, persistence across instances, invalidate, path traversal prevention (11 specs)
…shot env() throws raw strings, not Error instances, so toThrow() can't reliably match messages. Switch to try/catch with exact string assertion. Reset savedEnv in beforeEach so each test tracks only its own keys.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@copilot can you address any unaddressed concerns? O |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…versal prefix bypass
- Remove orphaned describe("TTL expiry") + it() block (lines 65-75) that was
never closed, causing "Unexpected end of input" syntax error at test load time
- Fix path-traversal prefix-bypass in DiskCache.keyToFilePath(): use
`baseDir + path.sep` instead of bare `baseDir` so a key like
`../test-cache-evil/pwn` cannot escape the cache directory
- Add test for the prefix-bypass scenario
Agent-Logs-Url: https://github.com/speced/respec-web-services/sessions/505fff65-5d3f-41b9-8488-88bfebd453bb
Co-authored-by: marcoscaceres <870154+marcoscaceres@users.noreply.github.com>
Addressed the remaining unresolved concerns in commit
All 55 specs pass. |
|
@copilot can you review this and fix any issues that come up? |
- Set `tempDir = undefined` at the top of `beforeEach` so that a
`mkdtemp` failure on a later run cannot accidentally reuse the
previous test's directory in `afterEach`
- Wrap env-var restoration in `try { } finally { rm... }` so the
temp directory is always cleaned up even if the env restoration
step throws
Agent-Logs-Url: https://github.com/speced/respec-web-services/sessions/3c3383f4-458d-4aaf-8fc6-091cdefd4390
Co-authored-by: marcoscaceres <870154+marcoscaceres@users.noreply.github.com>
|
@copilot Can you confirm all your feedback has been addressed? Please resolve the threads that have been addressed and fix anything outstanding. |
All feedback has been addressed. Here's a summary of each open thread:
All 55 specs pass. I can't resolve GitHub threads via these tools, but every concern in the open threads is addressed in the code. |
|
@copilot can you resolve the merge conflicts? |
# Conflicts: # tests/utils/misc.test.js
Resolved in commit |
Summary