import { test } from "node:test"; import assert from "node:assert"; import { mkdtempSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { detectGpuVendors, driverPackagesFor, type GpuVendor } from "../src/gpu.js"; function withFakeLspci(output: string, fn: () => void) { const dir = mkdtempSync(join(tmpdir(), "fake-lspci-")); writeFileSync(join(dir, "lspci"), `#!/bin/sh\ncat <<'EOF'\n${output}\nEOF\n`, { mode: 0o755 }); const originalPath = process.env.PATH; process.env.PATH = `${dir}:${originalPath}`; try { fn(); } finally { process.env.PATH = originalPath; } } test("detects an NVIDIA + Intel machine", () => { withFakeLspci( `00:02.0 VGA compatible controller: Intel Corporation blah (rev 0a) 01:00.0 VGA compatible controller: NVIDIA Corporation GA104 [GeForce RTX 3070] (rev a1) 01:00.1 Audio device: NVIDIA Corporation GA104 High Definition Audio Controller (rev a1)`, () => { assert.deepStrictEqual(detectGpuVendors().sort(), ["intel", "nvidia"]); }, ); }); test("detects an Intel + NVIDIA hybrid/Optimus laptop", () => { withFakeLspci( `00:02.0 VGA compatible controller: Intel Corporation TigerLake-LP GT2 [Iris Xe Graphics] 01:00.0 3D controller: NVIDIA Corporation GA107M [GeForce RTX 3050 Mobile]`, () => { assert.deepStrictEqual(detectGpuVendors().sort(), ["intel", "nvidia"]); }, ); }); test("detects an AMD-only machine without false-positiving on 'Corporation'", () => { // Regression test: /amd|ati/i used to match the "ati" inside "Corporation", // flagging every vendor line (including Intel/NVIDIA ones) as AMD. withFakeLspci( `03:00.0 VGA compatible controller: Advanced Micro Devices, Inc. [AMD/ATI] Navi 23 [Radeon RX 6600]`, () => { assert.deepStrictEqual(detectGpuVendors(), ["amd"]); }, ); }); test("does not false-positive AMD on an NVIDIA-only 'Corporation' line", () => { withFakeLspci( `01:00.0 VGA compatible controller: NVIDIA Corporation GA104 [GeForce RTX 3070]`, () => { assert.deepStrictEqual(detectGpuVendors(), ["nvidia"]); }, ); }); test("returns no vendors when lspci reports no controllers", () => { withFakeLspci("", () => { assert.deepStrictEqual(detectGpuVendors(), []); }); }); test("returns no vendors when lspci is missing entirely", () => { const originalPath = process.env.PATH; process.env.PATH = "/nonexistent"; try { assert.deepStrictEqual(detectGpuVendors(), []); } finally { process.env.PATH = originalPath; } }); test("driverPackagesFor maps each vendor to its package list", () => { assert.deepStrictEqual(driverPackagesFor(["amd"]), ["mesa", "vulkan-radeon", "libva-mesa-driver"]); assert.deepStrictEqual(driverPackagesFor([]), []); const vendors: GpuVendor[] = ["nvidia", "intel"]; assert.deepStrictEqual(driverPackagesFor(vendors), [ "nvidia-open-dkms", "nvidia-utils", "egl-wayland", "mesa", "vulkan-intel", "intel-media-driver", ]); });