Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
import { describe, it, expect, vi, beforeEach } from 'vitest'
const { mockT } = vi.hoisted(() => ({
mockT: vi.fn((key: string, opts?: Record<string, unknown>) =>
opts ? `${key}|${JSON.stringify(opts)}` : key
),
}))
vi.mock('@/i18n/react-i18next-compat', () => ({
i18n: { t: mockT },
}))
import {
parseEngineError,
describeEngineError,
engineFailure,
formatLoadDiagnostics,
stripLocalPaths,
ENGINE_ERROR_CODES,
} from '../engineError'
describe('parseEngineError', () => {
it('reads a Tauri-serialized engine error', () => {
const parsed = parseEngineError({
code: 'MISSING_SHARED_LIBRARY',
message: 'A library this backend depends on is missing.',
details: 'libnccl.so.2: cannot open shared object file',
missing_libraries: ['libnccl.so.2'],
})
expect(parsed).toEqual({
code: 'MISSING_SHARED_LIBRARY',
message: 'A library this backend depends on is missing.',
details: 'libnccl.so.2: cannot open shared object file',
missingLibraries: ['libnccl.so.2'],
})
})
it('returns undefined for things that are not engine errors', () => {
expect(parseEngineError(new Error('plain'))).toBeUndefined()
expect(parseEngineError('string')).toBeUndefined()
expect(parseEngineError(null)).toBeUndefined()
expect(parseEngineError(undefined)).toBeUndefined()
expect(parseEngineError({ message: 'no code' })).toBeUndefined()
})
// An unrecognized code from a newer engine must not be treated as a known one.
it('rejects an unknown code', () => {
expect(parseEngineError({ code: 'SOMETHING_NEW' })).toBeUndefined()
})
it('unwraps an engine error nested behind a message string', () => {
const inner = JSON.stringify({
code: 'OUT_OF_MEMORY',
message: 'Out of memory.',
})
const parsed = parseEngineError(new Error(`Failed to start model: ${inner}`))
expect(parsed?.code).toBe('OUT_OF_MEMORY')
})
it('tolerates a non-array missing_libraries', () => {
const parsed = parseEngineError({
code: 'MISSING_SHARED_LIBRARY',
missing_libraries: 'libnccl.so.2',
})
expect(parsed?.missingLibraries).toBeUndefined()
})
// #9270: start_engine hands the wrong-processor failure over as JSON inside
// the error text, and the UI must still recover its code from it.
it('recovers a wrong-processor engine start from the error text', () => {
const parsed = parseEngineError(
new Error(
'Failed to start model: {"code":"UNSUPPORTED_CPU","message":"The llama.cpp engine in this Jan build does not support this computer\'s processor.","details":"Bad CPU type in executable (os error 86)"}'
)
)
expect(parsed?.code).toBe('UNSUPPORTED_CPU')
expect(parsed?.details).toBe('Bad CPU type in executable (os error 86)')
})
})
describe('describeEngineError', () => {
beforeEach(() => {
mockT.mockClear()
})
it('translates a known code instead of showing the Rust message', () => {
const text = describeEngineError({
code: 'GPU_DRIVER_TOO_OLD',
message: 'The installed GPU driver is too old for this backend.',
})
expect(mockT).toHaveBeenCalledWith('model-errors:engine.GPU_DRIVER_TOO_OLD')
expect(text).toBe('model-errors:engine.GPU_DRIVER_TOO_OLD')
expect(text).not.toContain('backend')
})
it('appends the missing library names, which are not translatable', () => {
const text = describeEngineError({
code: 'MISSING_SHARED_LIBRARY',
missing_libraries: ['libnccl.so.2', 'libcublas.so.12'],
})
expect(mockT).toHaveBeenCalledWith('model-errors:engineMissingLibraries', {
libraries: 'libnccl.so.2, libcublas.so.12',
})
expect(text).toContain('libnccl.so.2, libcublas.so.12')
})
// The localized sentence is advice; the engine's own text is the evidence.
// Dropping it left an OOM indistinguishable from any other load failure.
it('appends the engine reason so the cause is visible, not just the advice', () => {
const text = describeEngineError({
code: 'OUT_OF_MEMORY',
message: 'Out of memory.',
details:
'could not start the llama.cpp engine: failed to load model; cudaMalloc failed: out of memory',
})
expect(mockT).toHaveBeenCalledWith('model-errors:engineReportedDetail', {
detail:
'could not start the llama.cpp engine: failed to load model; cudaMalloc failed: out of memory',
})
expect(text).toContain('cudaMalloc failed: out of memory')
})
// jan-internal#487: fit finding no plan is its own advice, not the generic
// OOM one that suggests lowering a context fit already lowered.
it('describes a model fit could not place as not fitting this device', () => {
describeEngineError({
code: 'MODEL_DOES_NOT_FIT',
details: 'common_fit_params: failed to allocate a plan',
})
expect(mockT).toHaveBeenCalledWith('model-errors:engine.MODEL_DOES_NOT_FIT')
})
it('collapses whitespace and clips a log-sized detail', () => {
const text = describeEngineError({
code: 'MODEL_LOAD_FAILED',
details: `line one\n line two${' padding'.repeat(60)}`,
})
const detail = mockT.mock.calls.find(
([key]) => key === 'model-errors:engineReportedDetail'
)?.[1]?.detail as string
expect(detail).toContain('line one line two')
expect(detail).not.toContain('\n')
expect(detail.length).toBeLessThan(250)
expect(detail.endsWith('...')).toBe(true)
})
// `find_session_by_model` and `get_loaded_models` return String errors, so
// the plugin sends the structured error as JSON inside the string, and the
// extension rethrows it as `new Error(String(e))`. It has to come out as the
// localized sentence plus the evidence, not as the raw JSON.
it('describes an engine-stopped error that arrived inside an Error message', () => {
const fromRust = JSON.stringify({
code: 'ENGINE_STOPPED',
message: 'The llama.cpp engine stopped unexpectedly (exit code 0xC0000005).',
details: 'exit code 0xC0000005. Last output: CUDA error: unspecified launch failure',
})
const text = describeEngineError(new Error(fromRust))
expect(mockT).toHaveBeenCalledWith('model-errors:engine.ENGINE_STOPPED')
expect(text).toContain('0xC0000005')
expect(text).not.toContain('"code"')
})
it('describes a live but unreachable engine from its code', () => {
const text = describeEngineError(
JSON.stringify({
code: 'ENGINE_UNREACHABLE',
details: '127.0.0.1:51825: error sending request: connection refused',
})
)
expect(mockT).toHaveBeenCalledWith('model-errors:engine.ENGINE_UNREACHABLE')
expect(text).toContain('127.0.0.1:51825')
})
it('omits the detail wrapper when there is no detail', () => {
describeEngineError({ code: 'IO_ERROR' })
expect(mockT).not.toHaveBeenCalledWith(
'model-errors:engineReportedDetail',
expect.anything()
)
})
it('has a key for every code the engine can emit', () => {
for (const code of ENGINE_ERROR_CODES) {
mockT.mockClear()
describeEngineError({ code })
expect(mockT).toHaveBeenCalledWith(`model-errors:engine.${code}`)
}
})
// Never surface raw JSON, which is what the old path did for a plain object.
it('falls back to a translated generic message for an unknown shape', () => {
const text = describeEngineError({ weird: true })
expect(text).toBe('model-errors:engine.unknown')
expect(text).not.toContain('{')
})
it('keeps a plain Error message, which is already human-readable', () => {
expect(describeEngineError(new Error('No running session found'))).toBe(
'No running session found'
)
})
it('keeps a plain string error', () => {
expect(describeEngineError('something broke')).toBe('something broke')
})
it('never returns an empty string', () => {
for (const input of [new Error(''), '', ' ', {}, null, undefined]) {
expect(describeEngineError(input).trim().length).toBeGreaterThan(0)
}
})
})
describe('engineFailure', () => {
beforeEach(() => {
mockT.mockClear()
})
it('keeps the structured error reachable so an outer layer describes it once', () => {
const engine = { code: 'OUT_OF_MEMORY', details: 'cudaMalloc failed' }
const inner = engineFailure('model-errors:startModelFailed', engine)
const outer = engineFailure('model-errors:createModelFailed', inner)
expect(parseEngineError(outer)?.code).toBe('OUT_OF_MEMORY')
expect(describeEngineError(outer)).toContain(
'model-errors:engine.OUT_OF_MEMORY'
)
expect(describeEngineError(outer)).not.toContain(
'model-errors:startModelFailed'
)
})
it('describes a non-engine cause from its own message', () => {
const wrapped = engineFailure(
'model-errors:startModelFailed',
new Error('GPU fail')
)
expect(wrapped.message).toContain('GPU fail')
})
})
describe('formatLoadDiagnostics', () => {
const failure = {
code: 'MODEL_LOAD_FAILED',
details: 'failed to load model; open /home/bob/models/qwen.gguf failed',
diagnostics: {
model_file: 'qwen.gguf',
architecture: 'qwen3',
quantization: 'Q4_K_M',
request: { ctx_size: '8192', n_gpu_layers: '20', flash_attn: null },
gpus: [
{
name: 'NVIDIA RTX 4090',
driver_version: '560.35',
total_mib: 24564,
free_mib: 20000,
},
],
backends: ['CUDA (libggml-cuda.so)'],
engine: { version: '0.9.0', tag: 'b12110', commit: 'a'.repeat(40) },
log: 'load_tensors: loading /home/bob/models/qwen.gguf\nC:\\Users\\bob\\x.dll failed',
log_truncated: true,
},
}
it('reports the request, devices, engine and log as markdown', () => {
const text = formatLoadDiagnostics(failure, {
appVersion: '0.9.0',
os: 'linux',
})!
expect(text).toContain('- Model file: qwen.gguf')
expect(text).toContain('- Architecture: qwen3')
expect(text).toContain('- ctx_size: 8192')
expect(text).not.toContain('flash_attn')
expect(text).toContain(
'NVIDIA RTX 4090 (driver 560.35, 24564 MiB, 20000 MiB free)'
)
expect(text).toContain('- CUDA (libggml-cuda.so)')
expect(text).toContain('- llama.cpp: 0.9.0 (b12110, aaaaaaaaaaaa)')
expect(text).toContain('Engine log (older lines dropped)')
})
it('leaves no home directory or user name in the copied text', () => {
const text = formatLoadDiagnostics(failure)!
expect(text).not.toContain('bob')
expect(text).toContain('~/models/qwen.gguf')
})
it('has nothing to report for an error without diagnostics', () => {
expect(formatLoadDiagnostics({ code: 'MODEL_LOAD_FAILED' })).toBeUndefined()
expect(formatLoadDiagnostics(new Error('plain'))).toBeUndefined()
})
it('reads diagnostics from an error that was stringified or wrapped', () => {
const wrapped = new Error('outer') as Error & { cause?: unknown }
wrapped.cause = failure
expect(formatLoadDiagnostics(wrapped)).toContain('Model load failure')
expect(formatLoadDiagnostics(new Error(JSON.stringify(failure)))).toContain(
'qwen.gguf'
)
})
})
describe('stripLocalPaths', () => {
it('replaces home directories on every platform', () => {
expect(
stripLocalPaths('/home/alice/a /Users/bob/b C:\\Users\\carol\\c')
).toBe('~/a ~/b ~\\c')
})
it('still strips a home directory with nothing after it', () => {
expect(stripLocalPaths('cannot open /home/alice')).toBe('cannot open ~')
})
// The shape-guessing fallback stops at whitespace, so it can neither eat
// the prose between two paths nor turn an unrelated path into one under
// the home directory.
it('never reaches across prose into another path', () => {
expect(
stripLocalPaths('cannot open /home/bob and /usr/lib/libggml.so')
).toBe('cannot open ~ and /usr/lib/libggml.so')
expect(stripLocalPaths('cannot open /home/bob and models/x.gguf')).toBe(
'cannot open ~ and models/x.gguf'
)
expect(
stripLocalPaths('C:\\Users\\bob and C:\\Windows\\System32\\x.dll')
).toBe('~ and C:\\Windows\\System32\\x.dll')
})
// Knowing the directory is what makes a name with a space safe: there is no
// rule over the text alone that tells `/home/Jo Smith/lib` apart from
// `/home/bob and models/x.gguf`.
it('takes a spaced or non-ASCII profile name when the home is known', () => {
expect(
stripLocalPaths(
'C:\\Users\\Jo Smith\\AppData\\jan',
'C:\\Users\\Jo Smith'
)
).toBe('~\\AppData\\jan')
expect(
stripLocalPaths('/Users/Jos\u00e9 Garc\u00eda/Library/jan', '/Users/Jos\u00e9 Garc\u00eda')
).toBe('~/Library/jan')
expect(
stripLocalPaths("/home/Se\u00e1n O'Brien/models", "/home/Se\u00e1n O'Brien/")
).toBe('~/models')
})
// A bare substring replace turned `/home/bobby` into `~by`, and having
// eaten the `/home/` the fallback could no longer recognise what was left.
it('does not match a longer name that merely starts with the home', () => {
expect(stripLocalPaths('/home/bobby/x.gguf', '/home/bob')).toBe('~/x.gguf')
expect(
stripLocalPaths('C:\\Users\\bobby\\x.gguf', 'C:\\Users\\bob')
).toBe('~\\x.gguf')
})
it('matches the home directory written with either separator', () => {
expect(
stripLocalPaths('C:/Users/Jo Smith/jan', 'C:\\Users\\Jo Smith')
).toBe('~/jan')
})
it('leaves the prose alone even when a home is known', () => {
expect(
stripLocalPaths('cannot open /home/Jo Smith and models/x.gguf', '/home/Jo Smith')
).toBe('cannot open ~ and models/x.gguf')
})
})