~bigbes/lethe

ref: 538e632d05ea9cd425e52f87d4877822bd721a75 lethe/web/src/lib/toolCalls.test.ts -rw-r--r-- 3.3 KiB
538e632d — Eugene Blikh collector: skip rejected rows after partial ingest 24 days ago
                                                                                
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
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { bootstrapToolCalls, setToolCalls, getToolCallsPreference } from './toolCalls'

// ── localStorage stub ────────────────────────────────────────────────────────

function makeLocalStorageStub(): Storage {
  const store: Record<string, string> = {}
  return {
    getItem: (key: string) => store[key] ?? null,
    setItem: (key: string, val: string) => { store[key] = val },
    removeItem: (key: string) => { delete store[key] },
    clear: () => { Object.keys(store).forEach(k => delete store[k]) },
    key: (index: number) => Object.keys(store)[index] ?? null,
    get length() { return Object.keys(store).length },
  } as Storage
}

// ── Setup / teardown ─────────────────────────────────────────────────────────

let lsStub: Storage

beforeEach(() => {
  lsStub = makeLocalStorageStub()
  vi.stubGlobal('localStorage', lsStub)

  delete document.documentElement.dataset['showToolcalls']
})

afterEach(() => {
  vi.restoreAllMocks()
  vi.unstubAllGlobals()
})

// ── Tests ────────────────────────────────────────────────────────────────────

describe('getToolCallsPreference', () => {
  it('no stored value → "yes"', () => {
    expect(getToolCallsPreference()).toBe('yes')
  })

  it('stored "no" → "no"', () => {
    lsStub.setItem('showToolCalls', 'no')
    expect(getToolCallsPreference()).toBe('no')
  })

  it('stored "yes" → "yes"', () => {
    lsStub.setItem('showToolCalls', 'yes')
    expect(getToolCallsPreference()).toBe('yes')
  })
})

describe('bootstrapToolCalls', () => {
  it('no stored value → data-show-toolcalls="true"', () => {
    bootstrapToolCalls()
    expect(document.documentElement.dataset['showToolcalls']).toBe('true')
  })

  it('stored "no" → data-show-toolcalls="false"', () => {
    lsStub.setItem('showToolCalls', 'no')
    bootstrapToolCalls()
    expect(document.documentElement.dataset['showToolcalls']).toBe('false')
  })

  it('does not register a matchMedia listener', () => {
    const spy = vi.spyOn(window, 'matchMedia')
    bootstrapToolCalls()
    expect(spy).not.toHaveBeenCalled()
  })
})

describe('setToolCalls', () => {
  it('setToolCalls("no") stores and sets data-show-toolcalls="false"', () => {
    setToolCalls('no')
    expect(lsStub.getItem('showToolCalls')).toBe('no')
    expect(document.documentElement.dataset['showToolcalls']).toBe('false')
  })

  it('setToolCalls("yes") stores and sets data-show-toolcalls="true"', () => {
    setToolCalls('yes')
    expect(lsStub.getItem('showToolCalls')).toBe('yes')
    expect(document.documentElement.dataset['showToolcalls']).toBe('true')
  })

  it('setToolCalls(null) removes key and applies default data-show-toolcalls="true"', () => {
    lsStub.setItem('showToolCalls', 'no')
    document.documentElement.dataset['showToolcalls'] = 'false'
    setToolCalls(null)
    expect(lsStub.getItem('showToolCalls')).toBeNull()
    expect(document.documentElement.dataset['showToolcalls']).toBe('true')
  })
})