File size: 3,359 Bytes
0ed8124
36a170e
0ed8124
20134b6
21ad36a
36a170e
20134b6
21ad36a
c3633b1
20134b6
 
 
 
 
0ed8124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2b7fe3
33cf820
 
 
 
21ad36a
 
 
 
 
 
36a170e
 
 
 
 
 
21ad36a
33cf820
 
20134b6
 
 
 
 
 
 
 
c2b7fe3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3633b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { describe, expect, it } from 'vitest';
import releaseManifest from '../../public/manifest/models.json';
import { EngineClientError } from '../engine';
import {
  DEFAULT_CONTEXT_SIZE,
  DEFAULT_MAX_TOKENS,
  DEFAULT_MODEL_ID,
  modelArchitectureLabel,
  publishBackendReport,
  reconcileCompletionTokens,
  reconcileTotalTokens,
  reportsModelWeightProgress,
  requiresModelReload,
} from './App';

function engineError(code: string): EngineClientError {
  return new EngineClientError({ code, message: code });
}

describe('App model invalidation policy', () => {
  it.each([
    'WEBGPU_DEVICE_LOST',
    'ENGINE_WORKER_FAILED',
    'MODEL_NOT_LOADED',
  ])('requires an explicit reload after %s', (code) => {
    expect(requiresModelReload(engineError(code))).toBe(true);
  });

  it('keeps an otherwise healthy loaded model after a responsive abort', () => {
    expect(requiresModelReload(engineError('ABORTED'))).toBe(false);
  });
});

describe('App release defaults', () => {
  it('opens on the flagship tier required by the product contract', () => {
    expect(DEFAULT_MODEL_ID).toBe('bonsai-27b');
  });

  it('shows manifest architecture instead of invented model aliases', () => {
    expect(modelArchitectureLabel('qwen3')).toBe('Qwen3 dense');
    expect(modelArchitectureLabel('qwen35')).toBe('Qwen3.5 hybrid');
  });

  it('uses one 4K ceiling for context allocation and completion requests', () => {
    expect(DEFAULT_CONTEXT_SIZE).toBe(4096);
    expect(DEFAULT_MAX_TOKENS).toBe(4096);
    expect(DEFAULT_CONTEXT_SIZE).toBe(DEFAULT_MAX_TOKENS);
    expect(releaseManifest.models.find((model) => model.id === '27b')?.defaultContext)
      .toBe(DEFAULT_CONTEXT_SIZE);
  });
});

describe('App model-load progress', () => {
  it('does not turn pinned manifest metadata reads into model-weight download state', () => {
    expect(reportsModelWeightProgress('manifest')).toBe(false);
    expect(reportsModelWeightProgress('download')).toBe(true);
    expect(reportsModelWeightProgress('load')).toBe(true);
  });
});

describe('App streamed completion telemetry', () => {
  it('uses observed stream events when final engine usage is incomplete', () => {
    expect(reconcileCompletionTokens(5, 64)).toBe(64);
  });

  it('keeps the engine count when one stream event contains multiple tokens', () => {
    expect(reconcileCompletionTokens(12, 8)).toBe(12);
  });

  it('fails closed to the observed count for invalid engine usage', () => {
    expect(reconcileCompletionTokens(Number.NaN, 7)).toBe(7);
  });

  it('keeps context usage consistent with a reconciled completion count', () => {
    expect(reconcileTotalTokens(5, 5, 64)).toBe(69);
    expect(reconcileTotalTokens(80, 5, 64)).toBe(80);
  });
});

describe('App backend diagnostics', () => {
  it('publishes the complete backend report for the browser gate without rendering it', () => {
    const report = {
      backends: ['WebGPU'],
      nGraphSplits: 1,
      opsOnCpu: 0,
      layersGpu: { offloaded: 65, total: 65 },
      flashAttention: false,
      cacheTypeK: 'f16',
      cacheTypeV: 'f16',
      webgpuKvBufferBytes: 1024,
      webgpuTrace: ['@@WEBGPU_TRACE@@completion_end id=1 steps=65 error=0'],
    };

    publishBackendReport(report);

    expect(globalThis.__bonsaiBackendReport).toEqual(report);
    delete globalThis.__bonsaiBackendReport;
  });
});