Skip to content

AI, fully local on your computer.

11.6 KB
Raw Blame
import { describe, it, expect } from 'vitest'
import {
  displayDevice,
  eligibleGpuProbe,
  GPU_BUMP_MIN_VRAM_MB,
  gpuUsefulForProfile,
  isUsefulDevice,
  looksIntegrated,
  primaryUsefulDevice,
  USABLE_VRAM_MB
} from '../../src/shared/gpu-rules'
import type { GpuDevice, GpuProbeResult } from '../../src/shared/types'

// The ONE "usable graphics card" rule (PR #303 audit M8 / N3, owner decisions G3 / G4): which
// device counts, which one is shown, whether a persisted probe may speak for this machine —
// shared by the profile bump, the memory class, the snapshot and the Performance screen. The
// `looksIntegrated` table lives here alone (gpu.test.ts's copy was merged in by the 2026-10
// runtime-ladder test campaign).

const dev = (id: string, name: string, totalMb: number): GpuDevice => ({ id, name, totalMb, freeMb: totalMb })
const IRIS = dev('Vulkan0', 'Intel(R) Iris(R) Xe Graphics', 16_384)
const RTX = dev('Vulkan1', 'NVIDIA GeForce RTX 3090', 24_576)
const RX = dev('Vulkan0', 'AMD Radeon RX 6700 XT', 12_272)
const GTX = dev('Vulkan0', 'NVIDIA GeForce GTX 1650', 4096)
// A real hybrid laptop (Ryzen 7 5800H, 2026-09-07): the iGPU is listed first, and the "6 GB"
// card reports 5,994 MiB on the Vulkan backend. That was under the old 6,144 gate; since #321
// lowered it to 5,120 the card is USEFUL, which is the whole point of the change.
const RADEON_IGPU = dev('Vulkan0', 'AMD Radeon(TM) Graphics', 8886)
const RTX3060L = dev('Vulkan1', 'NVIDIA GeForce RTX 3060 Laptop GPU', 5994)

describe('looksIntegrated', () => {
  it.each([
    // Integrated → true (the bump must NOT fire)
    ['Intel(R) Iris(R) Xe Graphics', true],
    ['Intel(R) UHD Graphics 630', true],
    ['Intel(R) HD Graphics 520', true],
    ['AMD Radeon(TM) Graphics', true],
    ['AMD Radeon Vega 8', true],
    // Audit fix: names real Linux/RADV + Meteor-Lake drivers report (these used to
    // slip through and could bump the profile on shared-memory APUs).
    ['AMD Radeon Graphics (RADV REMBRANDT)', true],
    ['AMD Radeon(TM) 780M Graphics', true],
    ['AMD Radeon Vega 8 Graphics (RADV RAVEN)', true],
    ['Intel(R) Arc(TM) Graphics', true],
    // PR #308 audit (decision 9, R6): the current-generation names the pinned Vulkan build
    // reports for integrated parts — none matched before, so a hybrid laptop's iGPU (11–36 GiB
    // of SHARED memory) read as a discrete card on every consumer.
    ['Intel(R) Graphics (ARL)', true],
    ['Intel(R) Graphics (LNL)', true],
    ['Intel(R) Arc(TM) 140V GPU (16GB)', true],
    ['Intel(R) Iris(R) Xe Graphics (TGL GT2)', true],
    ['Intel(R) UHD Graphics 770', true], // also the iGPU of the real b11146 --list-devices capture (gpu.test.ts)
    ['AMD Radeon 780M Graphics (RADV PHOENIX)', true],
    ['AMD Radeon Graphics (RADV RENOIR)', true],
    ['AMD Radeon 890M Graphics (RADV GFX1150)', true],
    ['AMD Radeon 8060S Graphics (RADV GFX1151)', true],
    // #320, owner decision 2026-09-07: the BARE Arrow/Lunar-Lake name, no platform code. It
    // used to read discrete, so a 16–32 GiB shared-memory iGPU could become the budget device
    // — a false negative, the one direction the module's bias note forbids. The alternative is
    // anchored to the whole trimmed string, so the model-numbered discrete Arcs below stay out.
    ['Intel(R) Graphics', true],
    ['  Intel(R) Graphics  ', true],
    // Discrete → false (eligible for the bump)
    ['NVIDIA GeForce RTX 3080 Ti', false],
    ['AMD Radeon RX 6700 XT', false],
    ['NVIDIA GeForce GTX 1660', false],
    ['AMD Radeon RX 7800 XT (RADV NAVI32)', false],
    // Re-asserted against the bare-name alternative above (#320): a discrete Arc carries its
    // model number, so the anchor never reaches it.
    ['Intel(R) Arc(TM) A770 Graphics', false],
    ['Intel(R) Arc(TM) B580 Graphics', false],
    // PR #308 audit: the discrete forms the new patterns must keep OUT — the model-numbered Arc
    // A-series, the laptop "RX …S", the RTX line, and Apple's Metal device.
    ['Intel(R) Arc(tm) A770 Graphics (DG2)', false],
    ['Intel(R) Arc(TM) A750 Graphics', false],
    ['AMD Radeon RX 7700S (RADV NAVI33)', false],
    ['AMD Radeon RX 7700S', false],
    ['AMD Radeon RX 7900 XTX (RADV NAVI31)', false],
    ['NVIDIA GeForce RTX 5060', false],
    ['NVIDIA GeForce RTX 3090', false],
    ['Apple M2', false]
  ])('%s → %s', (name, integrated) => {
    expect(looksIntegrated(name)).toBe(integrated)
  })
})

describe('isUsefulDevice — the single predicate', () => {
  it('needs BOTH the VRAM floor and a discrete-looking name', () => {
    expect(isUsefulDevice(RTX)).toBe(true)
    expect(isUsefulDevice(RX)).toBe(true)
    // 16 GB of SHARED memory is not a card (M8.1).
    expect(isUsefulDevice(IRIS)).toBe(false)
    // A discrete card below the gate is not usable either — a 4 GB GTX 1650 since #321.
    expect(isUsefulDevice(GTX)).toBe(false)
  })

  it('the gate is inclusive at exactly 5 GiB and is the profile bump’s constant (#321)', () => {
    expect(USABLE_VRAM_MB).toBe(5120)
    expect(GPU_BUMP_MIN_VRAM_MB).toBe(USABLE_VRAM_MB)
    expect(isUsefulDevice({ name: 'NVIDIA GeForce RTX 2060', totalMb: 5120 })).toBe(true)
    expect(isUsefulDevice({ name: 'NVIDIA GeForce RTX 2060', totalMb: 5119 })).toBe(false)
    // The three real sub-6,144 cards the decision was made for (#318 leg 4 + the #308 audit):
    // all three are now in, which is the entire point of lowering the gate.
    expect(isUsefulDevice({ name: 'NVIDIA GeForce GTX 1660 SUPER', totalMb: 5746 })).toBe(true)
    expect(isUsefulDevice({ name: 'NVIDIA GeForce RTX 4050 Laptop GPU', totalMb: 5921 })).toBe(true)
    expect(isUsefulDevice(RTX3060L)).toBe(true)
    // …and a 4 GB card stays out. NOT because nothing ranked fits it — the E2B needs 2,271 MiB
    // and would fit — but because it is the only ranked model that fits the budget such a card
    // actually produces (≈ 4,096 − the 768 MiB idle reserve measured on every probed card = 3,328;
    // the 4B's 3,838 does not clear it), so admitting such a card would star the E2B at every RAM
    // size with no measurement behind the demotion (#321, restated reason, arithmetic corrected by
    // #413; `model-benchmarks.md` §6.6 N8 "Why 5,120 — RESTATED"). (#321: the floor is a
    // DECISION, not an arithmetic consequence — it did not move when the estimate fixes made a
    // 4 GB card able to hold a ranked model.)
    expect(isUsefulDevice({ name: 'NVIDIA GeForce GTX 1650', totalMb: 4096 })).toBe(false)
  })
})

describe('gpuUsefulForProfile — unchanged semantics (G4)', () => {
  it('answers "some device is useful"', () => {
    expect(gpuUsefulForProfile([])).toBe(false)
    expect(gpuUsefulForProfile([IRIS])).toBe(false)
    expect(gpuUsefulForProfile([GTX])).toBe(false)
    expect(gpuUsefulForProfile([RTX])).toBe(true)
    expect(gpuUsefulForProfile([IRIS, RTX])).toBe(true)
  })
})

describe('primaryUsefulDevice — the device every figure comes from (the budget device)', () => {
  it('is null with no device, an integrated-only list, or a small card', () => {
    expect(primaryUsefulDevice([])).toBeNull()
    expect(primaryUsefulDevice([IRIS])).toBeNull()
    expect(primaryUsefulDevice([GTX])).toBeNull()
  })

  it('is the discrete card, and on a hybrid [iGPU, dGPU] box the dGPU — never devices[0] (M8.2)', () => {
    expect(primaryUsefulDevice([RTX])).toBe(RTX)
    expect(primaryUsefulDevice([IRIS, RTX])).toBe(RTX)
    expect(primaryUsefulDevice([GTX, IRIS, RTX])).toBe(RTX)
  })

  it('with two usable cards the LARGER one wins, whichever the driver listed first (PR #308 decision 9)', () => {
    // The #303 P5 rule took the first useful device; unified with #308's `selectBudgetDevice`
    // at the merge of the two, so the tile, the record, the budget and the Models ★ agree.
    expect(primaryUsefulDevice([RX, RTX])).toBe(RTX)
    expect(primaryUsefulDevice([RTX, RX])).toBe(RTX)
  })
})

describe('displayDevice — what a screen may show', () => {
  it('is the useful device with useful: true', () => {
    expect(displayDevice([RTX])).toEqual({ device: RTX, useful: true })
    expect(displayDevice([IRIS, RTX])).toEqual({ device: RTX, useful: true })
  })

  it('names an integrated or small device with useful: false, so the copy never implies acceleration', () => {
    expect(displayDevice([IRIS])).toEqual({ device: IRIS, useful: false })
    expect(displayDevice([GTX])).toEqual({ device: GTX, useful: false })
    // Its name and memory stay one pair.
    expect(displayDevice([GTX, IRIS])).toEqual({ device: GTX, useful: false })
  })

  it('prefers a small DISCRETE card over an integrated one, whatever the driver order (owner decision 2026-09-07)', () => {
    // A hybrid laptop lists the iGPU first, and the tile must name THE card with its own memory,
    // never the iGPU's shared figure the old `devices[0]` fallback handed it. Below the gate the
    // card is named with `useful: false` so the copy can say "small" honestly — a 4 GB GTX 1650
    // since #321 lowered the gate to 5,120.
    expect(displayDevice([IRIS, GTX])).toEqual({ device: GTX, useful: false })
    // The same hybrid laptop's RTX 3060 Laptop (5,994 MiB) is USEFUL since #321, in either driver
    // order: named, and now also the budget device — the tile says "Usable" rather than "Small".
    expect(displayDevice([RADEON_IGPU, RTX3060L])).toEqual({ device: RTX3060L, useful: true })
    expect(displayDevice([RTX3060L, RADEON_IGPU])).toEqual({ device: RTX3060L, useful: true })
    // A useful card beside a sub-gate one: the useful one wins outright, not by size.
    expect(displayDevice([GTX, RTX3060L])).toEqual({ device: RTX3060L, useful: true })
    // The verdict follows: 5,994 ≥ 5,120, so this machine HAS a budget device now.
    expect(isUsefulDevice(RTX3060L)).toBe(true)
    expect(primaryUsefulDevice([RADEON_IGPU, RTX3060L])).toBe(RTX3060L)
  })

  it('two SUB-gate discrete cards: the larger one is named, still useful: false (#321)', () => {
    const gtx1630 = { id: 'Vulkan1', name: 'NVIDIA GeForce GTX 1630', totalMb: 4096 }
    const gtx1650ti = { id: 'Vulkan0', name: 'NVIDIA GeForce GTX 1650 Ti', totalMb: 4096 }
    expect(displayDevice([gtx1630, { ...gtx1650ti, totalMb: 5000 }])).toEqual({
      device: { ...gtx1650ti, totalMb: 5000 },
      useful: false
    })
  })

  it('is null with no device', () => {
    expect(displayDevice([])).toBeNull()
  })
})

describe('eligibleGpuProbe — which persisted probe may speak for this machine (G3)', () => {
  const here = 'win32|x64|Intel Core i7-1260P|12|16'
  const probe = (machineKey?: string | null): GpuProbeResult =>
    machineKey === undefined
      ? { devices: [RTX], probedAt: '2026-09-05T00:00:00Z' }
      : { devices: [RTX], probedAt: '2026-09-05T00:00:00Z', machineKey }

  it('no probe → null', () => {
    expect(eligibleGpuProbe(null, here)).toBeNull()
    expect(eligibleGpuProbe(undefined, here)).toBeNull()
  })

  it('an unstamped legacy probe is eligible (unverifiable until a local refresh replaces it)', () => {
    const legacy = probe()
    expect(eligibleGpuProbe(legacy, here)).toBe(legacy)
  })

  it('a probe stamped with this machine, or with an unknown identity, is eligible', () => {
    const mine = probe(here)
    expect(eligibleGpuProbe(mine, here)).toBe(mine)
    const unknown = probe(null)
    expect(eligibleGpuProbe(unknown, here)).toBe(unknown)
  })

  it('a probe stamped with ANOTHER machine supplies nothing', () => {
    expect(eligibleGpuProbe(probe('linux|x64|Some Other CPU|32|64'), here)).toBeNull()
  })

  it('an unknown local identity cannot prove foreignness, so a stamped probe stays eligible', () => {
    const stamped = probe('linux|x64|Some Other CPU|32|64')
    expect(eligibleGpuProbe(stamped, null)).toBe(stamped)
  })
})