Skip to content

AI, fully local on your computer.

2.4 KB
Raw Blame
import { describe, it, expect } from 'vitest'
import { RuntimeManager } from '../../src/main/services/runtime'
import { createMockRuntime } from '../../src/main/services/runtime/mock'

describe('RuntimeManager + MockRuntime', () => {
  it('starts a model and reports healthy status', async () => {
    const mgr = new RuntimeManager(createMockRuntime)
    expect(mgr.status().running).toBe(false)

    const status = await mgr.start({
      modelId: 'qwen3-4b-instruct-q4',
      modelPath: '/models/chat/x.gguf',
      contextTokens: 4096
    })
    expect(status.running).toBe(true)
    expect(status.healthy).toBe(true)
    expect(status.modelId).toBe('qwen3-4b-instruct-q4')
    expect(mgr.activeModelId()).toBe('qwen3-4b-instruct-q4')
  })

  it('reports the launched context window on status (§L0) and clears it when stopped', async () => {
    const mgr = new RuntimeManager(createMockRuntime)
    // Not running → no window to report.
    expect(mgr.status().contextWindow).toBeUndefined()
    await mgr.start({ modelId: 'a', modelPath: '/a.gguf', contextTokens: 7777 })
    // The runtime echoes the exact value it was launched with (its --ctx-size).
    expect(mgr.status().contextWindow).toBe(7777)
    await mgr.stop()
    expect(mgr.status().contextWindow).toBeUndefined()
  })

  // #39: contract parity with the real (ladder) runtime — cold after start, warm once anything
  // has streamed, and RuntimeManager.status() carries the flag for the Chat warm-up hint.
  it('the mock runtime reports warmedUp false until its first streamed token', async () => {
    const mgr = new RuntimeManager(createMockRuntime)
    await mgr.start({ modelId: 'a', modelPath: '/a.gguf', contextTokens: 2048 })
    expect(mgr.status().warmedUp).toBe(false)
    for await (const _t of mgr.active()!.chatStream([{ role: 'user', content: 'hi' }])) break
    expect(mgr.status().warmedUp).toBe(true)
  })

  it('streams a mock reply token-by-token that echoes the user message', async () => {
    const runtime = createMockRuntime({ modelId: 'a', modelPath: '/a.gguf', contextTokens: 2048 })
    await runtime.start()
    const chunks: string[] = []
    for await (const t of runtime.chatStream([{ role: 'user', content: 'hello there' }]))
      chunks.push(t)
    // Multiple tokens (not one blob), and the reply references the user's text.
    expect(chunks.length).toBeGreaterThan(1)
    expect(chunks.join('')).toContain('hello there')
  })
})