Skip to content

Commit 56fbc35

Browse files
authored
test(cache): reject empty cache keys (JhaSourav07#1762)
## Description Fixes JhaSourav07#1392 Added a focused boundary test for `TTLCache` to verify that empty string cache keys are rejected. Also added minimal validation in `TTLCache.set()` so an empty key cannot be stored. ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [ ] 🕐 Pillar 3 — Timezone Logic Optimization - [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview Not applicable. This PR only updates cache validation and unit test coverage. ## Checklist before requesting a review: - [x] I have read the `CONTRIBUTING.md` file. - [ ] I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). - [x] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). - [x] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). - [x] I have updated `README.md` if I added a new theme or URL parameter. - [ ] I have started the repo. - [ ] I have made sure that i have only one commit to merge in this PR. - [x] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). - [ ] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents d7f912f + 3b13b59 commit 56fbc35

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

lib/cache.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,15 @@ describe('TTLCache', () => {
410410
cache.destroy();
411411
});
412412

413+
it('rejects an empty string cache key', () => {
414+
const cache = new TTLCache<string>();
415+
416+
expect(() => cache.set('', 'value', 60_000)).toThrow('Cache key cannot be empty');
417+
expect(cache.has('')).toBe(false);
418+
419+
cache.destroy();
420+
});
421+
413422
it('handles rapid get/set/delete cycles', () => {
414423
const cache = new TTLCache<number>();
415424
for (let i = 0; i < 100; i++) {

lib/cache.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export class TTLCache<T> {
141141
}
142142

143143
set(key: string, value: T, ttlMs: number): void {
144+
if (key === '') throw new Error('Cache key cannot be empty');
144145
if (ttlMs <= 0) throw new RangeError(`ttlMs must be positive, got ${ttlMs}`);
145146

146147
const maxSize = this.maxSize;

0 commit comments

Comments
 (0)