|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import { computeGaugeSegments, type GaugeSegments } from './ram-gauge-utils' |
| 3 | +import type { SystemMemoryInfo } from '$lib/tauri-commands' |
| 4 | + |
| 5 | +const GB = 1024 * 1024 * 1024 |
| 6 | + |
| 7 | +/** Helper: builds a SystemMemoryInfo where segments sum to total. */ |
| 8 | +function mem(totalGb: number, wiredGb: number, appGb: number, freeGb: number): SystemMemoryInfo { |
| 9 | + return { totalBytes: totalGb * GB, wiredBytes: wiredGb * GB, appBytes: appGb * GB, freeBytes: freeGb * GB } |
| 10 | +} |
| 11 | + |
| 12 | +/** Asserts the result is non-null and returns it typed. */ |
| 13 | +function expectSegments(memory: SystemMemoryInfo, currentAi: number, projectedAi: number): GaugeSegments { |
| 14 | + const result = computeGaugeSegments(memory, currentAi, projectedAi) |
| 15 | + expect(result).not.toBeNull() |
| 16 | + return result as GaugeSegments |
| 17 | +} |
| 18 | + |
| 19 | +describe('computeGaugeSegments', () => { |
| 20 | + it('returns null when total is 0', () => { |
| 21 | + expect(computeGaugeSegments(mem(0, 0, 0, 0), 0, 0)).toBeNull() |
| 22 | + }) |
| 23 | + |
| 24 | + it('segments add up to <= 100%', () => { |
| 25 | + const result = expectSegments(mem(64, 5, 30, 29), 3.5 * GB, 3.5 * GB) |
| 26 | + const sum = |
| 27 | + result.systemPercent + |
| 28 | + result.otherAppsPercent + |
| 29 | + result.retainedAiPercent + |
| 30 | + result.addedPercent + |
| 31 | + result.freedPercent |
| 32 | + expect(sum).toBeLessThanOrEqual(100.01) // floating point tolerance |
| 33 | + expect(sum).toBeGreaterThan(0) |
| 34 | + }) |
| 35 | + |
| 36 | + it('system segment uses wired bytes (non-zero for real systems)', () => { |
| 37 | + const result = expectSegments(mem(64, 5, 30, 29), 3.5 * GB, 3.5 * GB) |
| 38 | + expect(result.systemBytes).toBe(5 * GB) |
| 39 | + expect(result.systemPercent).toBeCloseTo((5 / 64) * 100, 1) |
| 40 | + }) |
| 41 | + |
| 42 | + it('other apps = app memory minus AI memory', () => { |
| 43 | + const result = expectSegments(mem(64, 5, 30, 29), 3.5 * GB, 3.5 * GB) |
| 44 | + expect(result.otherAppsBytes).toBe(26.5 * GB) |
| 45 | + }) |
| 46 | + |
| 47 | + it('free bytes come directly from system memory', () => { |
| 48 | + const result = expectSegments(mem(64, 5, 30, 29), 3.5 * GB, 3.5 * GB) |
| 49 | + expect(result.freeBytes).toBe(29 * GB) |
| 50 | + }) |
| 51 | + |
| 52 | + it('other apps bytes are clamped to 0 when AI estimate exceeds app memory', () => { |
| 53 | + // AI estimate larger than reported app memory (edge case during model load) |
| 54 | + const result = expectSegments(mem(64, 5, 2, 57), 3.5 * GB, 3.5 * GB) |
| 55 | + expect(result.otherAppsBytes).toBe(0) |
| 56 | + }) |
| 57 | + |
| 58 | + it('shows added segment when projected > current (growing)', () => { |
| 59 | + const current = 2 * GB |
| 60 | + const projected = 4 * GB |
| 61 | + const result = expectSegments(mem(64, 5, 30, 29), current, projected) |
| 62 | + expect(result.addedPercent).toBeGreaterThan(0) |
| 63 | + expect(result.freedPercent).toBe(0) |
| 64 | + expect(result.retainedAiPercent).toBeCloseTo((2 / 64) * 100, 1) |
| 65 | + expect(result.addedPercent).toBeCloseTo((2 / 64) * 100, 1) |
| 66 | + }) |
| 67 | + |
| 68 | + it('shows freed segment when projected < current (shrinking)', () => { |
| 69 | + const current = 4 * GB |
| 70 | + const projected = 2 * GB |
| 71 | + const result = expectSegments(mem(64, 5, 30, 29), current, projected) |
| 72 | + expect(result.freedPercent).toBeGreaterThan(0) |
| 73 | + expect(result.addedPercent).toBe(0) |
| 74 | + expect(result.retainedAiPercent).toBeCloseTo((2 / 64) * 100, 1) |
| 75 | + expect(result.freedPercent).toBeCloseTo((2 / 64) * 100, 1) |
| 76 | + }) |
| 77 | + |
| 78 | + it('no change segments when projected == current', () => { |
| 79 | + const result = expectSegments(mem(64, 5, 30, 29), 3 * GB, 3 * GB) |
| 80 | + expect(result.addedPercent).toBe(0) |
| 81 | + expect(result.freedPercent).toBe(0) |
| 82 | + }) |
| 83 | + |
| 84 | + it('clamps to 100% when segments would overflow', () => { |
| 85 | + // Extreme case: all memory categories are huge relative to total |
| 86 | + const result = expectSegments(mem(16, 4, 10, 2), 6 * GB, 10 * GB) |
| 87 | + const sum = |
| 88 | + result.systemPercent + |
| 89 | + result.otherAppsPercent + |
| 90 | + result.retainedAiPercent + |
| 91 | + result.addedPercent + |
| 92 | + result.freedPercent |
| 93 | + expect(sum).toBeLessThanOrEqual(100.01) |
| 94 | + }) |
| 95 | + |
| 96 | + it('totalProjectedUsageRatio reflects projected AI, not current', () => { |
| 97 | + const current = 2 * GB |
| 98 | + const projected = 6 * GB |
| 99 | + const result = expectSegments(mem(64, 5, 30, 29), current, projected) |
| 100 | + // system(5) + otherApps(30-2=28) + projected(6) = 39 / 64 ≈ 0.609 |
| 101 | + expect(result.totalProjectedUsageRatio).toBeCloseTo(39 / 64, 2) |
| 102 | + }) |
| 103 | + |
| 104 | + it('AI server not running (0 current) shows only projected as added', () => { |
| 105 | + const result = expectSegments(mem(64, 5, 30, 29), 0, 3.5 * GB) |
| 106 | + expect(result.retainedAiPercent).toBe(0) |
| 107 | + expect(result.addedPercent).toBeCloseTo((3.5 / 64) * 100, 1) |
| 108 | + // Other apps = full app memory since AI current is 0 |
| 109 | + expect(result.otherAppsBytes).toBe(30 * GB) |
| 110 | + }) |
| 111 | +}) |
0 commit comments