AI, fully local on your computer.
TypeScript aiai-toolskiwixllama-cpplocal-ailocal-llmofflineoffline-aioffline-firstprivacy-firstragwikipediazim
import { describe, it, expect } from 'vitest'
import { mkdtempSync, mkdirSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { stringify } from 'yaml'
import { composeServices, composeTranscriberSlot, refreshTranscriberSlot } from '../../src/main/services/compose-services'
import {
engineFamilyHasHostBuild,
hostRuntimeArch,
hostRuntimeOs
} from '../../src/main/services/runtime-download'
import {
WhisperCliTranscriber,
whisperCliBinaryName,
whisperCliDir,
transcriberMissingReason,
type Transcriber
} from '../../src/main/services/transcriber'
import type { TranscriberMissing } from '../../src/shared/types'
// Issue #497 — the post-install transcriber re-selection, the transcriber twin of the issue-#40 translator hook
// (compose-translator.test.ts). `composeTranscriberSlot` is the ONE construction `composeServices` (startup) and
// `refreshTranscriberSlot` (the hook body of both mid-session installs: a speech-model download, a whisper.cpp engine
// install) share, driven here over a REAL temp drive layout (manifest YAML + binary + weight files, no mocks): null while
// binary or weights are absent, a live selection the moment both are on the drive (the restart-free activation the
// issue asks for), and since #527 the reason the slot is empty.
/** A minimal VALID transcriber manifest (JSON is YAML — discoverManifests parses it fine). */
/** #913: the start check every composition now requires; these rows are about selection, so it passes all. */
const checkModel = async (): Promise<'ok'> => 'ok'
const MANIFEST = {
id: 'whisper-test',
display_name: 'Whisper (test)',
family: 'whisper',
role: 'transcriber',
format: 'ggml',
runtime: 'whisper_cpp',
license: 'mit',
size_on_disk_gb: 0.1,
recommended_min_ram_gb: 1,
recommended_ram_gb: 2,
recommended_context_tokens: 0, // the CLI takes none — the shipped manifest says 0 too
local_path: 'models/transcriber/whisper-test.bin',
sha256: 'REPLACE_WITH_REAL_HASH',
recommended_profiles: [],
license_review: {
status: 'approved',
reviewed_by: 'test',
reviewed_at: '2026-09-21',
notes: ''
}
}
/** The drive's `model-manifests/`, and `appDir`: the app's own copy, which holds the engine pins (#842). */
function tempDrive(): { root: string; manifestsDir: string; appDir: string } {
const root = mkdtempSync(join(tmpdir(), 'hilbertraum-compose-transcriber-'))
const manifestsDir = join(root, 'model-manifests')
mkdirSync(manifestsDir, { recursive: true })
writeFileSync(join(manifestsDir, 'whisper-test.yaml'), JSON.stringify(MANIFEST))
const appDir = join(root, 'app-bundle', 'model-manifests')
mkdirSync(appDir, { recursive: true })
return { root, manifestsDir, appDir }
}
function installBinary(root: string): void {
const binDir = whisperCliDir(root)
mkdirSync(binDir, { recursive: true })
writeFileSync(join(binDir, whisperCliBinaryName()), '')
}
function installWeight(root: string): void {
const dir = join(root, 'models', 'transcriber')
mkdirSync(dir, { recursive: true })
writeFileSync(join(dir, 'whisper-test.bin'), 'ggml-bytes')
}
// #504: the shipped whisper manifest declares the Silero VAD model as a second REQUIRED file
// (`files[]`). Over a real temp layout: the transcriber stays null until BOTH files are present,
// and the composed CLI backend knows the VAD path (found by name among the required files).
describe('composeTranscriberSlot — the Silero VAD file as the second required file (#504)', () => {
const VAD_NAME = 'ggml-silero-v5.1.2.bin'
function tempDriveWithVad(): { root: string; manifestsDir: string } {
const root = mkdtempSync(join(tmpdir(), 'hilbertraum-compose-transcriber-vad-'))
const manifestsDir = join(root, 'model-manifests')
mkdirSync(manifestsDir, { recursive: true })
writeFileSync(
join(manifestsDir, 'whisper-test.yaml'),
JSON.stringify({
...MANIFEST,
files: [{ local_path: `models/transcriber/${VAD_NAME}`, sha256: 'REPLACE_WITH_REAL_HASH' }]
})
)
return { root, manifestsDir }
}
it('stays null with the weight alone, and composes with the VAD path once the second file lands', () => {
const { root, manifestsDir } = tempDriveWithVad()
installBinary(root)
installWeight(root)
expect(composeTranscriberSlot({ rootPath: root, manifestsDir, bundledManifestsDir: null, checkModel }).transcriber).toBeNull()
writeFileSync(join(root, 'models', 'transcriber', VAD_NAME), 'vad-bytes')
const transcriber = composeTranscriberSlot({ rootPath: root, manifestsDir, bundledManifestsDir: null, checkModel }).transcriber
expect(transcriber).toBeInstanceOf(WhisperCliTranscriber)
expect((transcriber as WhisperCliTranscriber).vadModelPath).toBe(join(root, 'models', 'transcriber', VAD_NAME))
})
})
/** A minimal live Transcriber fake — the interface `AppContext.transcriber` carries. */
function fakeTranscriber(): Transcriber {
return {
id: 'live-instance',
transcribe: async () => []
}
}
describe('refreshTranscriberSlot (#497 — the hook body both install paths share)', () => {
function slot(root: string, manifestsDir: string | null, transcriber: Transcriber | null = null) {
return { transcriber, paths: { rootPath: root }, manifestsDir, bundledManifestsDir: null, isDev: false, checkModel }
}
it('leaves a LIVE instance untouched — same object, no re-composition', () => {
const { root, manifestsDir } = tempDrive()
installBinary(root)
installWeight(root)
const live = fakeTranscriber()
const ctx = slot(root, manifestsDir, live)
expect(refreshTranscriberSlot(ctx)).toBe(false)
expect(ctx.transcriber).toBe(live)
})
it('never throws — a fault is swallowed and logged, so the translator refresh behind it still runs', () => {
// The download manager wraps the WHOLE onModelInstalled hook in one swallowing try/catch
// (downloads.ts), so a throw here would silently skip the issue-#40 translator refresh that
// follows in main/index.ts. A context whose paths cannot be read is the cheapest fault.
const ctx = {
transcriber: null as Transcriber | null,
get paths(): { rootPath: string } {
throw new Error('paths unreadable')
},
manifestsDir: null,
bundledManifestsDir: null,
isDev: false,
checkModel
}
expect(() => refreshTranscriberSlot(ctx)).not.toThrow()
expect(refreshTranscriberSlot(ctx)).toBe(false)
expect(ctx.transcriber).toBeNull()
})
})
// #527 — WHY the slot is empty. The selection ladder stops at the first missing rung (binary
// before weights), so on a Linux drive with the speech model installed and no engine it read as
// "no binary" while every surface blamed the model. The reason is derived independently of the
// ladder, and an engine with no build for this host is its own answer: no download can help.
describe('transcriberMissingReason (#527 — which piece is missing)', () => {
const MODEL = { id: 'w', modelPath: '/drive/models/transcriber/w.bin' }
function reason(opts: { binary: boolean; weights: boolean; fetchable: boolean; model?: typeof MODEL | null }) {
return transcriberMissingReason({
rootPath: '/drive',
model: opts.model === undefined ? MODEL : opts.model,
engineFetchable: opts.fetchable,
resolveBin: () => (opts.binary ? '/drive/runtime/whisper.cpp/x/whisper-cli' : null),
modelExists: () => opts.weights
})
}
it('is null with the engine and the weights present', () => {
expect(reason({ binary: true, weights: true, fetchable: true })).toBeNull()
// An engine on the drive is enough, even where none could be fetched (a source-built one).
expect(reason({ binary: true, weights: true, fetchable: false })).toBeNull()
})
it('names the model when only the weights are missing', () => {
expect(reason({ binary: true, weights: false, fetchable: true })).toBe('model')
expect(reason({ binary: true, weights: false, fetchable: false })).toBe('model')
// No transcriber manifest at all reads as "no speech model on this drive" too.
expect(reason({ binary: true, weights: true, fetchable: true, model: null })).toBe('model')
})
it('reports an engine with no build for this host whatever the weights’ state (the Linux report)', () => {
expect(reason({ binary: false, weights: true, fetchable: false })).toBe('engine-unsupported')
expect(reason({ binary: false, weights: false, fetchable: false })).toBe('engine-unsupported')
})
it('requires EVERY required file, like the selection ladder (#504 VAD file)', () => {
const withVad = { ...MODEL, requiredPaths: [MODEL.modelPath, '/drive/models/transcriber/ggml-silero-v5.1.2.bin'] }
const missingVad = transcriberMissingReason({
rootPath: '/drive',
model: withVad,
engineFetchable: true,
resolveBin: () => '/bin/whisper-cli',
modelExists: (p) => p === MODEL.modelPath
})
expect(missingVad).toBe('model')
})
})
/** runtime-sources.yaml with a whisper_cpp build for `os` (the host's, by default). */
function writeRuntimeSources(manifestsDir: string, os: string = hostRuntimeOs(), whisper = true): void {
const build = (family: 'llama.cpp' | 'whisper.cpp', buildOs: string) => ({
os: buildOs,
arch: hostRuntimeArch(),
backend: 'cpu',
url: `https://example.test/${family}-${buildOs}.zip`,
sha256: 'a'.repeat(64),
extract_to: `runtime/${family}/${buildOs}`
})
const sources: Record<string, unknown> = {
llama_cpp: { version: 'btest', builds: [build('llama.cpp', hostRuntimeOs())] }
}
if (whisper) sources.whisper_cpp = { version: 'wtest', builds: [build('whisper.cpp', os)] }
writeFileSync(join(manifestsDir, 'runtime-sources.yaml'), stringify(sources))
}
/** Any runtime-sources OS key that is not this host's. */
function otherOs(): string {
return hostRuntimeOs() === 'linux' ? 'win' : 'linux'
}
describe('engineFamilyHasHostBuild (#527)', () => {
it('is true only for a family with a build for this host', () => {
const { manifestsDir } = tempDrive()
writeRuntimeSources(manifestsDir)
expect(engineFamilyHasHostBuild(manifestsDir, 'whisper_cpp')).toBe(true)
writeRuntimeSources(manifestsDir, otherOs())
expect(engineFamilyHasHostBuild(manifestsDir, 'whisper_cpp')).toBe(false)
writeRuntimeSources(manifestsDir, hostRuntimeOs(), false)
expect(engineFamilyHasHostBuild(manifestsDir, 'whisper_cpp')).toBe(false)
})
it('is false without a readable yaml', () => {
const { manifestsDir } = tempDrive()
expect(engineFamilyHasHostBuild(manifestsDir, 'whisper_cpp')).toBe(false) // absent
writeFileSync(join(manifestsDir, 'runtime-sources.yaml'), 'llama_cpp: [not, a, mapping')
expect(engineFamilyHasHostBuild(manifestsDir, 'whisper_cpp')).toBe(false) // malformed
expect(engineFamilyHasHostBuild(null, 'whisper_cpp')).toBe(false)
})
it('follows the platform it is asked about — the shipped v1.8.6-era shape (win only)', () => {
const { manifestsDir } = tempDrive()
writeRuntimeSources(manifestsDir, 'win')
// The fixture's own arch, so only the OS separates these three: a hard-coded x64 failed on an arm64 host.
const arch = hostRuntimeArch()
expect(engineFamilyHasHostBuild(manifestsDir, 'whisper_cpp', 'win32', arch)).toBe(true)
expect(engineFamilyHasHostBuild(manifestsDir, 'whisper_cpp', 'linux', arch)).toBe(false)
expect(engineFamilyHasHostBuild(manifestsDir, 'whisper_cpp', 'darwin', arch)).toBe(false)
})
})
describe('composeTranscriberSlot (#527 — the slot and its reason from one resolution)', () => {
it('walks a fresh drive from "both missing" to "engine" to selected', () => {
const { root, manifestsDir, appDir } = tempDrive()
writeRuntimeSources(appDir)
const deps = { rootPath: root, manifestsDir, bundledManifestsDir: appDir, checkModel }
expect(composeTranscriberSlot(deps)).toEqual({
transcriber: null,
missing: 'model-and-engine'
})
installWeight(root)
expect(composeTranscriberSlot(deps).missing).toBe('engine')
installBinary(root)
const slot = composeTranscriberSlot(deps)
expect(slot.transcriber?.id).toBe('whisper-test')
expect(slot.missing).toBeNull()
// A weight-only manifest (drives from before #504) composes without a VAD path.
expect((slot.transcriber as WhisperCliTranscriber).vadModelPath).toBeNull()
})
it('reports the model once the engine is in place', () => {
const { root, manifestsDir, appDir } = tempDrive()
writeRuntimeSources(appDir)
installBinary(root)
expect(composeTranscriberSlot({ rootPath: root, manifestsDir, bundledManifestsDir: appDir, checkModel }).missing).toBe('model')
})
it('the issue’s drive: speech model installed, no engine, and no engine build for this OS', () => {
const { root, manifestsDir, appDir } = tempDrive()
writeRuntimeSources(appDir, otherOs())
installWeight(root)
expect(composeTranscriberSlot({ rootPath: root, manifestsDir, bundledManifestsDir: appDir, checkModel })).toEqual({
transcriber: null,
missing: 'engine-unsupported'
})
})
// #842: the reason asks the pins the in-app installer uses, the app's own, not the drive's yaml
// (on a launcher start `manifestsDir` is the drive's copy, which can predate the app).
it('asks the app’s engine pins whether the voice engine could be fetched, never the drive’s (#842)', () => {
const { root, manifestsDir, appDir } = tempDrive()
installWeight(root)
const deps = { rootPath: root, manifestsDir, bundledManifestsDir: appDir, checkModel }
writeRuntimeSources(manifestsDir, otherOs()) // the drive's yaml: no build for this system
writeRuntimeSources(appDir) // the app's: one
expect(composeTranscriberSlot(deps).missing).toBe('engine')
writeRuntimeSources(manifestsDir)
writeRuntimeSources(appDir, otherOs())
expect(composeTranscriberSlot(deps).missing).toBe('engine-unsupported')
})
it('returns null with no manifests dir (every role falls back)', () => {
const { root } = tempDrive()
installBinary(root)
installWeight(root) // both pieces ARE on the drive: only the missing manifests dir keeps the slot empty
expect(composeTranscriberSlot({ rootPath: root, manifestsDir: null, bundledManifestsDir: null, checkModel })).toEqual({ transcriber: null, missing: 'model' })
})
it('composeServices carries the reason alongside the slot', () => {
const { root, manifestsDir, appDir } = tempDrive()
writeRuntimeSources(appDir)
installWeight(root)
const services = composeServices({
rootPath: root,
manifestsDir,
bundledManifestsDir: appDir,
rerankerDevicePosture: () => 'cpu',
rerankerRequestCeiling: () => Infinity,
checkModel: async () => 'ok'
})
expect(services.transcriber).toBeNull()
expect(services.transcriberMissing).toBe('engine')
})
})
describe('refreshTranscriberSlot keeps the reason in step (#527)', () => {
function slot(root: string, manifestsDir: string | null, bundledManifestsDir: string | null) {
return {
transcriber: null as Transcriber | null,
transcriberMissing: undefined as TranscriberMissing | null | undefined,
paths: { rootPath: root },
manifestsDir,
bundledManifestsDir,
isDev: false,
checkModel
}
}
it('re-derives the reason on every refresh of a null slot, and clears it when the slot fills', () => {
const { root, manifestsDir, appDir } = tempDrive()
writeRuntimeSources(appDir)
const ctx = slot(root, manifestsDir, appDir)
// Nothing on the drive yet, and the app's pins can fetch the voice engine (#842).
expect(refreshTranscriberSlot(ctx)).toBe(false)
expect(ctx.transcriberMissing).toBe('model-and-engine')
// The voice engine install landed without the model: "both" turns into "model".
installBinary(root)
expect(refreshTranscriberSlot(ctx)).toBe(false)
expect(ctx.transcriberMissing).toBe('model')
installWeight(root)
expect(refreshTranscriberSlot(ctx)).toBe(true)
expect(ctx.transcriber?.id).toBe('whisper-test')
expect(ctx.transcriberMissing).toBeNull()
})
})