|
| 1 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 2 | +import { RefreshPolicy } from './refresh-policy'; |
| 3 | + |
| 4 | +// Mock the quota monitor to avoid interference |
| 5 | +vi.mock('./quota-monitor', () => ({ |
| 6 | + quotaMonitor: { |
| 7 | + isQuotaLow: vi.fn(() => false), |
| 8 | + incrementRefreshCount: vi.fn(), |
| 9 | + }, |
| 10 | +})); |
| 11 | + |
| 12 | +describe('RefreshPolicy - Massive Scaling & Bounds (Variation 2)', () => { |
| 13 | + let policy: RefreshPolicy; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + policy = RefreshPolicy.getInstance(); |
| 17 | + policy.reset(); |
| 18 | + vi.clearAllMocks(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('High-Volume Data Injection: rapidly inserts 10,000 distinct usernames without memory overflow', () => { |
| 22 | + const VOLUME = 10000; |
| 23 | + |
| 24 | + // Inject 10,000 users |
| 25 | + for (let i = 0; i < VOLUME; i++) { |
| 26 | + policy.recordRefresh(`user_${i}`); |
| 27 | + } |
| 28 | + |
| 29 | + // Verify a random subset to ensure map holds state correctly |
| 30 | + expect(policy.isRefreshAllowed(`user_0`)).toBe(false); |
| 31 | + expect(policy.isRefreshAllowed(`user_5000`)).toBe(false); |
| 32 | + expect(policy.isRefreshAllowed(`user_9999`)).toBe(false); |
| 33 | + expect(policy.isRefreshAllowed(`unrecorded_user`)).toBe(true); |
| 34 | + }); |
| 35 | + |
| 36 | + it('Extreme Cooldown Limits: processes Number.MAX_SAFE_INTEGER seamlessly without JS overflow', () => { |
| 37 | + policy.setCooldown(Number.MAX_SAFE_INTEGER); |
| 38 | + policy.recordRefresh('timetraveler'); |
| 39 | + |
| 40 | + // With MAX_SAFE_INTEGER, remaining cooldown should be massive but valid |
| 41 | + const remaining = policy.getRemainingCooldown('timetraveler'); |
| 42 | + |
| 43 | + // We allow a small 10000ms delta for the execution time gap between Date.now() |
| 44 | + expect(remaining).toBeGreaterThan(Number.MAX_SAFE_INTEGER - 10000); |
| 45 | + expect(policy.isRefreshAllowed('timetraveler')).toBe(false); |
| 46 | + }); |
| 47 | + |
| 48 | + it('High-Speed Query Stress Test: executes 10,000 rapid calls under 50ms total execution time', () => { |
| 49 | + const VOLUME = 10000; |
| 50 | + // Pre-populate |
| 51 | + for (let i = 0; i < VOLUME; i++) { |
| 52 | + policy.recordRefresh(`user_${i}`); |
| 53 | + } |
| 54 | + |
| 55 | + // Measure query time |
| 56 | + const start = performance.now(); |
| 57 | + for (let i = 0; i < VOLUME; i++) { |
| 58 | + policy.isRefreshAllowed(`user_${i}`); |
| 59 | + } |
| 60 | + const end = performance.now(); |
| 61 | + |
| 62 | + const executionTimeMs = end - start; |
| 63 | + // Execution time should be exceptionally fast (under 50ms for 10k Map lookups) |
| 64 | + expect(executionTimeMs).toBeLessThan(50); |
| 65 | + }); |
| 66 | + |
| 67 | + it('Massive String Input Bound: safely trims and processes a 100,000+ character string without loop crashing', () => { |
| 68 | + const massiveString = 'a'.repeat(100000); |
| 69 | + const untrimmedMassive = ` ${massiveString} `; |
| 70 | + |
| 71 | + // Should process without throwing memory or stack errors |
| 72 | + policy.recordRefresh(untrimmedMassive); |
| 73 | + |
| 74 | + // The exact trimmed string should correctly map |
| 75 | + expect(policy.isRefreshAllowed(massiveString)).toBe(false); |
| 76 | + expect(policy.getRemainingCooldown(massiveString)).toBeGreaterThan(0); |
| 77 | + }); |
| 78 | + |
| 79 | + it('Bulk Cooldown Retrieval: rapidly calculates cache-misses for 10,000+ unrecorded users without degrading', () => { |
| 80 | + const start = performance.now(); |
| 81 | + const VOLUME = 10000; |
| 82 | + |
| 83 | + let cacheMisses = 0; |
| 84 | + for (let i = 0; i < VOLUME; i++) { |
| 85 | + if (policy.getRemainingCooldown(`phantom_${i}`) === 0) { |
| 86 | + cacheMisses++; |
| 87 | + } |
| 88 | + } |
| 89 | + const end = performance.now(); |
| 90 | + |
| 91 | + expect(cacheMisses).toBe(VOLUME); |
| 92 | + // Retrieval misses should also be extremely fast (under 50ms) |
| 93 | + expect(end - start).toBeLessThan(50); |
| 94 | + }); |
| 95 | +}); |
0 commit comments