import test from 'node:test'; import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import { createRequire } from 'node:module'; import vm from 'node:vm'; const enginePath = new URL('../vendor/agent-harness/lib/qvac.js', import.meta.url); const source = readFileSync(enginePath, 'utf8'); const require = createRequire(enginePath); function engineHarness(required) { const attempts = []; const fakeSdk = { getSystemResources: async () => ({ gpus: [{ name: 'Test GPU', memory: 16e9 }], drivers: { vulkan: true }, vramBytes: 16e9 }), lookupModelSrc: async () => 'test-model.gguf', loadModel: async ({ modelConfig }) => { attempts.push({ ...modelConfig }); if (modelConfig.device === 'gpu') throw new Error('GPU allocation failed'); return 'cpu-model'; }, }; const context = vm.createContext({ require, module: { exports: {} }, process: { env: { JARVIS_GPU_REQUIRED: required ? '1' : '0' } }, console: { error() {} }, fakeSdk }); vm.runInContext(source + '\nsdk = fakeSdk;', context); return { engine: context.module.exports, attempts }; } for (const [requested, required] of [['gpu', false], ['auto', true]]) { test(`GPU-required loading does not waste a CPU reload (${requested}, required=${required})`, async () => { const { engine, attempts } = engineHarness(required); await assert.rejects(engine.load({ model: 'test-model', device: requested, vision: false }), /GPU allocation failed/); assert.equal(attempts.length, 1); assert.equal(attempts[0].gpu_layers, 99); }); } test('optional automatic CPU fallback reports its actual backend', async () => { const { engine, attempts } = engineHarness(false); const loaded = await engine.load({ model: 'test-model', device: 'auto', vision: false }); assert.deepEqual(attempts.map((config) => config.device), ['gpu', 'cpu']); assert.equal(loaded.device, 'cpu'); assert.equal(loaded.backend, 'cpu'); assert.equal(loaded.backendId, 0); });