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')
})
})