|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { formatTimestamp, getConfig, buildTooltip } = require('../src/extension'); |
| 4 | + |
| 5 | +// --------------------------------------------------------------------------- |
| 6 | +// formatTimestamp |
| 7 | +// --------------------------------------------------------------------------- |
| 8 | + |
| 9 | +describe('formatTimestamp', () => { |
| 10 | + it('returns HH:mm:ss only when the date is today', () => { |
| 11 | + const now = new Date(); |
| 12 | + const result = formatTimestamp(now); |
| 13 | + // Should match HH:mm:ss (no date prefix) |
| 14 | + expect(result).toMatch(/^\d{2}:\d{2}:\d{2}$/); |
| 15 | + }); |
| 16 | + |
| 17 | + it('returns YYYY-MM-DD HH:mm:ss when the date is a different day', () => { |
| 18 | + const past = new Date('2025-01-15T09:05:03'); |
| 19 | + const result = formatTimestamp(past); |
| 20 | + expect(result).toBe('2025-01-15 09:05:03'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('zero-pads single-digit hours, minutes, seconds', () => { |
| 24 | + const past = new Date('2024-03-07T01:02:03'); |
| 25 | + const result = formatTimestamp(past); |
| 26 | + expect(result).toBe('2024-03-07 01:02:03'); |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +// --------------------------------------------------------------------------- |
| 31 | +// getConfig – numeric coercion and clamping (tests the NaN fix) |
| 32 | +// --------------------------------------------------------------------------- |
| 33 | + |
| 34 | +describe('getConfig', () => { |
| 35 | + function mockCfg(values) { |
| 36 | + const vscode = require('vscode'); |
| 37 | + vscode.workspace.getConfiguration = () => ({ |
| 38 | + get: (key, defaultValue) => (key in values ? values[key] : defaultValue), |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + // Restore default: always returns the default value |
| 44 | + const vscode = require('vscode'); |
| 45 | + vscode.workspace.getConfiguration = () => ({ get: (_key, def) => def }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('returns numeric thresholds from valid config', () => { |
| 49 | + mockCfg({ 'threshold.warning': 60, 'threshold.critical': 85 }); |
| 50 | + const cfg = getConfig(); |
| 51 | + expect(cfg.thresholdWarning).toBe(60); |
| 52 | + expect(cfg.thresholdCritical).toBe(85); |
| 53 | + }); |
| 54 | + |
| 55 | + it('falls back to defaults when threshold values are non-numeric strings', () => { |
| 56 | + mockCfg({ 'threshold.warning': 'off', 'threshold.critical': 'off' }); |
| 57 | + const cfg = getConfig(); |
| 58 | + // Both fall back to defaults (75, 90) |
| 59 | + expect(cfg.thresholdWarning).toBe(75); |
| 60 | + expect(cfg.thresholdCritical).toBe(90); |
| 61 | + }); |
| 62 | + |
| 63 | + it('falls back to default 90 for critical when only critical is a string', () => { |
| 64 | + mockCfg({ 'threshold.warning': 70, 'threshold.critical': 'invalid' }); |
| 65 | + const cfg = getConfig(); |
| 66 | + expect(cfg.thresholdCritical).toBe(90); |
| 67 | + expect(cfg.thresholdWarning).toBe(70); // valid warning, and 70 < 90 |
| 68 | + }); |
| 69 | + |
| 70 | + it('clamps warning to at most critical when warning > critical', () => { |
| 71 | + mockCfg({ 'threshold.warning': 95, 'threshold.critical': 80 }); |
| 72 | + const cfg = getConfig(); |
| 73 | + expect(cfg.thresholdWarning).toBe(80); // clamped to critical |
| 74 | + expect(cfg.thresholdCritical).toBe(80); |
| 75 | + }); |
| 76 | + |
| 77 | + it('accepts numeric-string values by coercing them', () => { |
| 78 | + mockCfg({ 'threshold.warning': '60', 'threshold.critical': '85' }); |
| 79 | + const cfg = getConfig(); |
| 80 | + expect(cfg.thresholdWarning).toBe(60); |
| 81 | + expect(cfg.thresholdCritical).toBe(85); |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +// --------------------------------------------------------------------------- |
| 86 | +// buildTooltip |
| 87 | +// --------------------------------------------------------------------------- |
| 88 | + |
| 89 | +describe('buildTooltip', () => { |
| 90 | + const BASE_DATA = { |
| 91 | + plan: 'Pro', |
| 92 | + unlimited: false, |
| 93 | + noData: false, |
| 94 | + used: 90, |
| 95 | + quota: 300, |
| 96 | + usedPct: 30, |
| 97 | + overageEnabled: false, |
| 98 | + overageUsed: 0, |
| 99 | + resetDate: new Date('2026-04-01T00:00:00Z'), |
| 100 | + }; |
| 101 | + |
| 102 | + it('shows plan name and used/quota/pct for normal quota', () => { |
| 103 | + const md = buildTooltip(BASE_DATA, false); |
| 104 | + expect(md.value).toContain('Pro'); |
| 105 | + expect(md.value).toContain('90 / 300 (30%)'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('shows Unlimited for unlimited plan', () => { |
| 109 | + const md = buildTooltip({ ...BASE_DATA, unlimited: true }, false); |
| 110 | + expect(md.value).toContain('Unlimited'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('includes rate-limit notice when isRateLimited is true', () => { |
| 114 | + const md = buildTooltip(BASE_DATA, true); |
| 115 | + expect(md.value).toContain('Rate limited'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('does not include rate-limit notice when isRateLimited is false', () => { |
| 119 | + const md = buildTooltip(BASE_DATA, false); |
| 120 | + expect(md.value).not.toContain('Rate limited'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('includes overage count when overageEnabled and overageUsed > 0', () => { |
| 124 | + const data = { ...BASE_DATA, overageEnabled: true, overageUsed: 5 }; |
| 125 | + const md = buildTooltip(data, false); |
| 126 | + expect(md.value).toContain('Overage: 5 requests'); |
| 127 | + }); |
| 128 | + |
| 129 | + it('omits overage section when overageUsed is 0', () => { |
| 130 | + const md = buildTooltip(BASE_DATA, false); |
| 131 | + expect(md.value).not.toContain('Overage'); |
| 132 | + }); |
| 133 | +}); |
0 commit comments