Files
leleka/engine/strata/apps/host/tests/offline-start.test.mjs
T
Pavlo RatushnyiandClaude Sonnet 5.5 87e2633afe engine/strata: bring the Strata subtree to upstream 41cf05a0 (engine 0.1.50) with the local patches re-applied
Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree
(570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base =
the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main).

Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR
experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details,
conflict table and left-out files are in docs/upstream.md.

Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61),
docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out.

scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run").
Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR
macros on. No GPU run, no quality gate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 12:49:15 +01:00

72 lines
4.6 KiB
JavaScript

import test from 'node:test';
import { ROOMY_HOST } from './helpers/hardware.mjs';
import assert from 'node:assert/strict';
import { spawn } from 'node:child_process';
import { existsSync, readFileSync } from 'node:fs';
import { mkdtemp, rm } from 'node:fs/promises';
import { createServer } from 'node:net';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
/**
* Strata works without internet. The host is started with every non-loopback fetch failing (and logged), the way a PC with the cable
* pulled behaves. Starting, reading the screens' data and loading a model make no outside call. The update check is the one
* thing that reaches out, and only when it is asked for: opening Settings does not ask.
*/
const root = fileURLToPath(new URL('..', import.meta.url));
const preload = pathToFileURL(fileURLToPath(new URL('./helpers/offline-preload.mjs', import.meta.url))).href;
const delay = ms => new Promise(r => setTimeout(r, ms));
async function freePort() { const s = createServer(); await new Promise(r => s.listen(0, '127.0.0.1', r)); const p = s.address().port; await new Promise(r => s.close(r)); return p; }
async function startOfflineHost() {
const home = await mkdtemp(join(tmpdir(), 'strata-offline-'));
const log = join(home, 'net.log');
const port = await freePort();
let output = '';
const host = spawn(process.execPath, ['--import', preload, 'server/index.mjs'], {
cwd: root, stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env, STRATA_HOME: home, STRATA_MODELS_DIR: join(home, 'models'), STRATA_PORT: String(port), STRATA_SHARE_PORT: String(await freePort()), STRATA_TUNNEL_PORT: String(await freePort()), STRATA_PACKAGED: '1', STRATA_NET_LOG: log, STRATA_NO_ACQUIRE: '1', STRATA_REAL_ENGINE: '0', STRATA_TEST_HARDWARE: ROOMY_HOST },
});
host.stdout.on('data', d => { output += d; }); host.stderr.on('data', d => { output += d; });
let up = false;
for (let i = 0; i < 150 && !up; i++) { try { up = (await fetch(`http://127.0.0.1:${port}/api/health`)).ok; } catch { await delay(100); } }
assert.ok(up, `host started\n${output}`);
const calls = () => (existsSync(log) ? readFileSync(log, 'utf8').split('\n').filter(Boolean) : []);
const get = async path => { const r = await fetch(`http://127.0.0.1:${port}${path}`); return { status: r.status, data: await r.json().catch(() => null) }; };
const post = async path => { const r = await fetch(`http://127.0.0.1:${port}${path}`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: '{}' }); return { status: r.status, data: await r.json().catch(() => null) }; };
return { host, get, post, calls, stop: async () => { host.kill(); await new Promise(r => host.once('exit', r)); await rm(home, { recursive: true, force: true }).catch(() => {}); } };
}
test('with no internet the host starts, serves its screens and loads a model without one outside call', async t => {
const h = await startOfflineHost();
t.after(h.stop);
assert.deepEqual(h.calls(), [], 'nothing reached out while starting');
for (const path of ['/api/native', '/api/setup', '/api/models', '/api/engine', '/api/health']) {
const r = await h.get(path);
assert.equal(r.status, 200, `${path} answers`);
}
const install = await h.post('/api/models/qwen38-q4_k_m/install');
assert.equal(install.status, 200);
let installed = false;
for (let i = 0; i < 150 && !installed; i++) { installed = (await h.get('/api/models')).data.variants.find(v => v.id === 'qwen38-q4_k_m').state === 'installed'; if (!installed) await delay(100); }
assert.ok(installed, 'the model is on this computer');
const load = await h.post('/api/models/qwen38-q4_k_m/load');
assert.equal(load.status, 200, JSON.stringify(load.data));
assert.equal(load.data.loaded, 'qwen38-q4_k_m');
assert.deepEqual(h.calls(), [], 'opening the screens and loading a model made no outside call');
});
test('the update check reaches out only when it is asked for, and offline it answers in plain words', async t => {
const h = await startOfflineHost();
t.after(h.stop);
await h.get('/api/setup'); await h.get('/api/native'); await h.get('/api/models');
assert.equal(h.calls().length, 0, 'opening the screens does not check');
const checked = await h.get('/api/updates/check');
assert.equal(checked.status, 200);
assert.equal(h.calls().length, 1, 'one check, from the click');
assert.equal(checked.data.strata.status, 'error');
assert.equal(checked.data.strata.message, "No internet connection. Try again when you're online.");
assert.doesNotMatch(JSON.stringify(checked.data), /fetch failed/);
});