Skip to content

Commit 3dcc4c9

Browse files
authored
Add massive scaling tests for TrackUserProtection under high-volume load (JhaSourav07#3033)
## Description Fixes JhaSourav07#2964 The `TrackUserProtection` class at `services/security/track-user-protection.ts` maintains an internal `Map`-based write cooldown cache but had no tests verifying behavior under high-volume concurrent user operations. Bulk format validation, username normalization at scale, cache reset correctness after thousands of writes, and execution time bounds were all untested. ## 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 is a test-only change. ## Checklist before requesting a review: - [x] I have read the `CONTRIBUTING.md` file. - [x] I have tested these changes locally. - [x] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). - [x] I have run `npm run test` and all tests pass locally. - [x] I have run `npm run test:coverage` and branch coverage is at or above 70%. - [x] My commits follow the Conventional Commits format. - [ ] I have updated `README.md` if I added a new theme or URL parameter. - [x] I have started the repo. - [x] 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. - [ ] (Recommended) I joined the CommitPulse Discord server for faster collaboration, mentorship, and PR support.
2 parents 4521d31 + 257ace0 commit 3dcc4c9

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { describe, it, expect, vi } from 'vitest';
2+
import { TrackUserProtection } from './track-user-protection';
3+
4+
describe('TrackUserProtection massive scaling behavior', () => {
5+
it('records writes for 1000 unique usernames and enforces cooldown on all', () => {
6+
const tracker = TrackUserProtection.getInstance();
7+
tracker.reset();
8+
9+
for (let i = 0; i < 1000; i++) {
10+
tracker.recordWrite(`user${i}`);
11+
}
12+
13+
for (let i = 0; i < 1000; i++) {
14+
expect(tracker.isWriteAllowed(`user${i}`)).toBe(false);
15+
}
16+
});
17+
18+
it('validates format for 1000 usernames without throwing', () => {
19+
const tracker = TrackUserProtection.getInstance();
20+
21+
expect(() => {
22+
for (let i = 0; i < 1000; i++) {
23+
tracker.validateFormat(`user${i}`);
24+
}
25+
}).not.toThrow();
26+
});
27+
28+
it('normalizes usernames consistently under high volume', () => {
29+
const tracker = TrackUserProtection.getInstance();
30+
tracker.reset();
31+
32+
for (let i = 0; i < 500; i++) {
33+
tracker.recordWrite(` USER${i} `);
34+
}
35+
36+
for (let i = 0; i < 500; i++) {
37+
expect(tracker.isWriteAllowed(`user${i}`)).toBe(false);
38+
}
39+
});
40+
41+
it('clears the full cache on reset after bulk writes', () => {
42+
const tracker = TrackUserProtection.getInstance();
43+
tracker.reset();
44+
45+
for (let i = 0; i < 1000; i++) {
46+
tracker.recordWrite(`user${i}`);
47+
}
48+
49+
tracker.reset();
50+
51+
for (let i = 0; i < 1000; i++) {
52+
expect(tracker.isWriteAllowed(`user${i}`)).toBe(true);
53+
}
54+
});
55+
56+
it('completes format validation for 10000 usernames within acceptable time', () => {
57+
const tracker = TrackUserProtection.getInstance();
58+
59+
const start = performance.now();
60+
for (let i = 0; i < 10000; i++) {
61+
tracker.validateFormat(`user${i}-test`);
62+
}
63+
const elapsed = performance.now() - start;
64+
65+
expect(elapsed).toBeLessThan(2000);
66+
});
67+
});

0 commit comments

Comments
 (0)