|
| 1 | +import { describe, it, expect, beforeEach, vi, Mock } from 'vitest'; |
| 2 | +import { RefreshPolicy } from './refresh-policy'; |
| 3 | +import { quotaMonitor } from './quota-monitor'; |
| 4 | + |
| 5 | +// Mock the quota monitor to control high-contrast "override" states |
| 6 | +vi.mock('./quota-monitor', () => ({ |
| 7 | + quotaMonitor: { |
| 8 | + isQuotaLow: vi.fn(() => false), |
| 9 | + incrementRefreshCount: vi.fn(), |
| 10 | + }, |
| 11 | +})); |
| 12 | + |
| 13 | +describe('RefreshPolicy - Theme Contrast Equivalent (Mode Cohesion)', () => { |
| 14 | + let policy: RefreshPolicy; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + policy = RefreshPolicy.getInstance(); |
| 18 | + policy.reset(); |
| 19 | + vi.clearAllMocks(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('Dual Environment Setup: seamlessly swaps between standard (light) and aggressive (dark) modes', () => { |
| 23 | + // Light Mode (Standard Cooldown) |
| 24 | + policy.setCooldown(300000); |
| 25 | + expect(policy.getRemainingCooldown('new_user')).toBe(0); |
| 26 | + |
| 27 | + // Dark Mode (Aggressive Cooldown) |
| 28 | + policy.setCooldown(600000); |
| 29 | + expect(policy.getRemainingCooldown('new_user')).toBe(0); |
| 30 | + |
| 31 | + // Swap back |
| 32 | + policy.setCooldown(300000); |
| 33 | + expect(policy.getRemainingCooldown('new_user')).toBe(0); |
| 34 | + }); |
| 35 | + |
| 36 | + it('Behavior Adaptation Cohesion: dynamically adapts allow/deny status based on active mode', () => { |
| 37 | + // Record user in Standard Mode (300000ms) |
| 38 | + policy.setCooldown(300000); |
| 39 | + policy.recordRefresh('mode_user'); |
| 40 | + |
| 41 | + // Simulate exactly 400000ms passing by manipulating Date.now |
| 42 | + const originalDateNow = Date.now; |
| 43 | + const futureTime = originalDateNow() + 400000; |
| 44 | + vi.spyOn(Date, 'now').mockReturnValue(futureTime); |
| 45 | + |
| 46 | + // In Standard Mode, 400000 > 300000, so refresh is allowed |
| 47 | + expect(policy.isRefreshAllowed('mode_user')).toBe(true); |
| 48 | + |
| 49 | + // Swap to Aggressive Mode (600000ms) |
| 50 | + policy.setCooldown(600000); |
| 51 | + |
| 52 | + // In Aggressive Mode, 400000 < 600000, so refresh is now denied! |
| 53 | + expect(policy.isRefreshAllowed('mode_user')).toBe(false); |
| 54 | + |
| 55 | + // Restore |
| 56 | + vi.restoreAllMocks(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('Contrast Verification (Overrides): quota lockouts uniformly block across all configuration modes', () => { |
| 60 | + // Activate high-contrast global override |
| 61 | + (quotaMonitor.isQuotaLow as Mock).mockReturnValue(true); |
| 62 | + |
| 63 | + policy.setCooldown(100); |
| 64 | + expect(policy.isRefreshAllowed('user_1')).toBe(false); |
| 65 | + |
| 66 | + policy.setCooldown(9999999); |
| 67 | + expect(policy.isRefreshAllowed('user_1')).toBe(false); |
| 68 | + |
| 69 | + policy.setCooldown(0); |
| 70 | + expect(policy.isRefreshAllowed('user_1')).toBe(false); |
| 71 | + |
| 72 | + // Deactivate override |
| 73 | + (quotaMonitor.isQuotaLow as Mock).mockReturnValue(false); |
| 74 | + }); |
| 75 | + |
| 76 | + it('Configuration Preservation: swapping modes preserves internal caching data integrity', () => { |
| 77 | + // Record in mode A |
| 78 | + policy.setCooldown(300000); |
| 79 | + policy.recordRefresh('state_user'); |
| 80 | + |
| 81 | + // Check remaining cooldown (should be roughly 300000) |
| 82 | + const remainingA = policy.getRemainingCooldown('state_user'); |
| 83 | + |
| 84 | + // Swap to mode B |
| 85 | + policy.setCooldown(600000); |
| 86 | + |
| 87 | + // The timestamp state is still intact, just the boundary shifted |
| 88 | + const remainingB = policy.getRemainingCooldown('state_user'); |
| 89 | + |
| 90 | + expect(remainingB).toBeGreaterThan(remainingA); |
| 91 | + // Delta should be exactly 300000 |
| 92 | + expect(Math.abs(remainingB - remainingA - 300000)).toBeLessThan(50); |
| 93 | + }); |
| 94 | + |
| 95 | + it('Foreground/Background Cohesion: getRemainingCooldown calculates foreground values flawlessly against active background mode', () => { |
| 96 | + policy.recordRefresh('fg_bg_user'); |
| 97 | + |
| 98 | + // Background Mode 1 |
| 99 | + policy.setCooldown(1000); |
| 100 | + const bg1 = policy.getRemainingCooldown('fg_bg_user'); |
| 101 | + expect(bg1).toBeGreaterThan(0); |
| 102 | + expect(bg1).toBeLessThanOrEqual(1000); |
| 103 | + |
| 104 | + // Background Mode 2 |
| 105 | + policy.setCooldown(5000); |
| 106 | + const bg2 = policy.getRemainingCooldown('fg_bg_user'); |
| 107 | + expect(bg2).toBeGreaterThan(1000); |
| 108 | + expect(bg2).toBeLessThanOrEqual(5000); |
| 109 | + |
| 110 | + // Background Mode 3 (Disabled) |
| 111 | + policy.setCooldown(0); |
| 112 | + const bg3 = policy.getRemainingCooldown('fg_bg_user'); |
| 113 | + expect(bg3).toBe(0); |
| 114 | + }); |
| 115 | +}); |
0 commit comments