|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { |
| 4 | + AGE_MAX_RED_DAYS, |
| 5 | + ageColorStyle, |
| 6 | + ageRowStyle, |
| 7 | + daysSince, |
| 8 | + getActualLatestTag, |
| 9 | + isOutdated, |
| 10 | +} from './latest-image-utils'; |
| 11 | + |
| 12 | +const lightnessOf = (s: string) => Number(s.match(/oklch\(([\d.]+)/u)?.[1] ?? Number.NaN); |
| 13 | +const alphaOf = (s: string) => Number(s.match(/\/ ([\d.]+)\)/u)?.[1] ?? Number.NaN); |
| 14 | + |
| 15 | +describe('daysSince', () => { |
| 16 | + it('returns 0 for today', () => { |
| 17 | + const today = new Date('2026-05-27T12:34:56Z'); |
| 18 | + expect(daysSince('2026-05-27', today)).toBe(0); |
| 19 | + }); |
| 20 | + |
| 21 | + it('returns whole days for dates strictly in the past', () => { |
| 22 | + const today = new Date('2026-05-27T00:00:00Z'); |
| 23 | + expect(daysSince('2026-05-26', today)).toBe(1); |
| 24 | + expect(daysSince('2026-05-20', today)).toBe(7); |
| 25 | + expect(daysSince('2026-03-28', today)).toBe(60); |
| 26 | + }); |
| 27 | + |
| 28 | + it('floors to whole days regardless of intraday hour offset', () => { |
| 29 | + // The submitted-day anchor is 00:00Z; an early-morning "today" still |
| 30 | + // belongs to the same UTC day, so 23:59 vs 00:01 must round identically. |
| 31 | + const today = new Date('2026-05-28T00:01:00Z'); |
| 32 | + expect(daysSince('2026-05-27', today)).toBe(1); |
| 33 | + const lateToday = new Date('2026-05-28T23:59:00Z'); |
| 34 | + expect(daysSince('2026-05-27', lateToday)).toBe(1); |
| 35 | + }); |
| 36 | + |
| 37 | + it('clamps at 0 for future-dated submissions (never returns negative)', () => { |
| 38 | + const today = new Date('2026-05-27T00:00:00Z'); |
| 39 | + expect(daysSince('2026-06-01', today)).toBe(0); |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe('ageColorStyle', () => { |
| 44 | + it('returns undefined for 0-day rows so the muted-foreground class wins', () => { |
| 45 | + expect(ageColorStyle(0)).toBeUndefined(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('returns an oklch color for 1d and ramps toward darker red at AGE_MAX_RED_DAYS', () => { |
| 49 | + const oneDay = ageColorStyle(1); |
| 50 | + const maxDay = ageColorStyle(AGE_MAX_RED_DAYS); |
| 51 | + expect(oneDay?.color).toMatch(/^oklch\(/u); |
| 52 | + expect(maxDay?.color).toMatch(/^oklch\(/u); |
| 53 | + // Lightness L drops as days grow; AGE_MAX_RED_DAYS sits at L≈0.50, 1d at L≈0.77. |
| 54 | + expect(lightnessOf(maxDay!.color as string)).toBeLessThan(lightnessOf(oneDay!.color as string)); |
| 55 | + }); |
| 56 | + |
| 57 | + it('clamps anything past AGE_MAX_RED_DAYS to the same color (no extrapolation past the ramp)', () => { |
| 58 | + const at60 = ageColorStyle(AGE_MAX_RED_DAYS); |
| 59 | + const at365 = ageColorStyle(365); |
| 60 | + expect(at60).toEqual(at365); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +describe('ageRowStyle', () => { |
| 65 | + it('returns undefined for 0-day rows', () => { |
| 66 | + expect(ageRowStyle(0)).toBeUndefined(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('returns a low-alpha oklch background that ramps from ~0.04 to ~0.28', () => { |
| 70 | + const oneDay = ageRowStyle(1); |
| 71 | + const maxDay = ageRowStyle(AGE_MAX_RED_DAYS); |
| 72 | + const a1 = alphaOf(oneDay!.backgroundColor as string); |
| 73 | + const aMax = alphaOf(maxDay!.backgroundColor as string); |
| 74 | + expect(a1).toBeGreaterThan(0); |
| 75 | + expect(a1).toBeLessThan(0.1); |
| 76 | + expect(aMax).toBeGreaterThan(0.2); |
| 77 | + expect(aMax).toBeLessThanOrEqual(0.3); |
| 78 | + }); |
| 79 | + |
| 80 | + it('clamps past AGE_MAX_RED_DAYS so a 1y-old row looks identical to a 60d row', () => { |
| 81 | + expect(ageRowStyle(AGE_MAX_RED_DAYS)).toEqual(ageRowStyle(365)); |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +describe('isOutdated', () => { |
| 86 | + it('is false when image contains the latest tag', () => { |
| 87 | + expect(isOutdated('sgl-project/sglang:v0.5.12-cu130', 'v0.5.12')).toBe(false); |
| 88 | + }); |
| 89 | + |
| 90 | + it('is true when image does not contain the latest tag', () => { |
| 91 | + expect(isOutdated('sgl-project/sglang:v0.5.10-cu130', 'v0.5.12')).toBe(true); |
| 92 | + }); |
| 93 | + |
| 94 | + it('is true for nightly tags regardless of latest', () => { |
| 95 | + expect(isOutdated('sgl-project/sglang:nightly', 'v0.5.12')).toBe(true); |
| 96 | + expect(isOutdated('sgl-project/sglang:nightly', null)).toBe(true); |
| 97 | + }); |
| 98 | + |
| 99 | + it('is true for rocm/sgl-dev and sglang-rocm patterns (case-insensitive)', () => { |
| 100 | + expect(isOutdated('rocm/sgl-dev:latest', 'v0.5.12')).toBe(true); |
| 101 | + expect(isOutdated('ROCM/SGL-DEV:foo', 'v0.5.12')).toBe(true); |
| 102 | + expect(isOutdated('myreg/sglang-rocm:bar', 'v0.5.12')).toBe(true); |
| 103 | + }); |
| 104 | + |
| 105 | + it('is false when actualLatest is null and tag is not in UNSTABLE_PATTERNS', () => { |
| 106 | + // No release data → can't classify as outdated. Avoid red-flagging a row |
| 107 | + // we have no ground truth for. |
| 108 | + expect(isOutdated('vllm/vllm-openai:v0.21.0', null)).toBe(false); |
| 109 | + }); |
| 110 | +}); |
| 111 | + |
| 112 | +describe('getActualLatestTag', () => { |
| 113 | + it('looks up the base framework from FRAMEWORK_TO_BASE and returns its release', () => { |
| 114 | + const releases = { vllm: 'v0.21.0', sglang: 'v0.5.12' }; |
| 115 | + expect(getActualLatestTag('vllm', releases)).toBe('v0.21.0'); |
| 116 | + expect(getActualLatestTag('sglang', releases)).toBe('v0.5.12'); |
| 117 | + expect(getActualLatestTag('dynamo-sglang', releases)).toBe('v0.5.12'); |
| 118 | + expect(getActualLatestTag('mori-sglang', releases)).toBe('v0.5.12'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('returns null when releases is undefined (API still loading)', () => { |
| 122 | + expect(getActualLatestTag('vllm', undefined)).toBeNull(); |
| 123 | + }); |
| 124 | + |
| 125 | + it('returns null for an unknown framework (no base mapping)', () => { |
| 126 | + expect(getActualLatestTag('trt', { vllm: 'v0.21.0', sglang: 'v0.5.12' })).toBeNull(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('returns null when the base release is missing from the releases map', () => { |
| 130 | + expect(getActualLatestTag('vllm', { sglang: 'v0.5.12' })).toBeNull(); |
| 131 | + }); |
| 132 | +}); |
0 commit comments