import { describe, it, expect, beforeEach } from 'vitest'
import { cache, normalizeCachePolicy, applyCachePolicy } from '../src/core/cache.js'
* Snapshot helpers to assert identity changes.
*/
function snapshotRefs() {
return {
image: cache.image,
background: cache.background,
resource: cache.resource,
defaultStyle: cache.defaultStyle,
baseStyle: cache.baseStyle,
computedStyle: cache.computedStyle,
measureHints: cache.measureHints,
}
}
function seedSomeData() {
cache.image.set('k', 1)
cache.background.set('b', 2)
cache.resource.set('r', 3)
cache.defaultStyle.set('d', 4)
cache.baseStyle.set('bs', 5)
cache.computedStyle.set({}, { c: 6 })
cache.measureHints.set({}, { cssLen: 1, w0: 2, csh: 3, csw: 4 })
}
describe('normalizeCachePolicy', () => {
it('maps booleans and known strings, defaults to "soft"', () => {
expect(normalizeCachePolicy(true)).toBe('soft')
expect(normalizeCachePolicy(false)).toBe('disabled')
expect(normalizeCachePolicy('auto')).toBe('soft')
expect(normalizeCachePolicy('full')).toBe('soft')
expect(normalizeCachePolicy('soft')).toBe('soft')
expect(normalizeCachePolicy('disabled')).toBe('disabled')
expect(normalizeCachePolicy('weird')).toBe('soft')
expect(normalizeCachePolicy(undefined)).toBe('soft')
expect(normalizeCachePolicy(123)).toBe('soft')
})
})
describe('applyCachePolicy', () => {
beforeEach(() => {
cache.image = new Map()
cache.background = new Map()
cache.resource = new Map()
cache.defaultStyle = new Map()
cache.baseStyle = new Map()
cache.computedStyle = new WeakMap()
cache.measureHints = new WeakMap()
})
it('any non-disabled policy: persistent caches kept', () => {
seedSomeData()
const before = snapshotRefs()
applyCachePolicy('auto')
const after = snapshotRefs()
expect(after.image).toBe(before.image)
expect(after.background).toBe(before.background)
expect(after.resource).toBe(before.resource)
expect(after.defaultStyle).toBe(before.defaultStyle)
expect(after.baseStyle).toBe(before.baseStyle)
expect(after.computedStyle).toBe(before.computedStyle)
})
it('soft: leaves the global caches intact (the session is no longer global)', () => {
seedSomeData()
const before = snapshotRefs()
applyCachePolicy('soft')
const after = snapshotRefs()
expect(after.image).toBe(before.image)
expect(after.background).toBe(before.background)
expect(after.resource).toBe(before.resource)
expect(after.defaultStyle).toBe(before.defaultStyle)
expect(after.baseStyle).toBe(before.baseStyle)
expect(after.computedStyle).toBe(before.computedStyle)
})
it("legacy 'full': persistent caches kept, session bucket still fresh per capture", () => {
seedSomeData()
const before = snapshotRefs()
applyCachePolicy('full')
const after = snapshotRefs()
expect(after.image).toBe(before.image)
expect(after.background).toBe(before.background)
expect(after.resource).toBe(before.resource)
expect(after.defaultStyle).toBe(before.defaultStyle)
expect(after.baseStyle).toBe(before.baseStyle)
expect(after.computedStyle).toBe(before.computedStyle)
})
it('disabled: re-instantiates EVERYTHING (global + session) and leaves it empty', () => {
seedSomeData()
const before = snapshotRefs()
applyCachePolicy('disabled')
const after = snapshotRefs()
expect(after.image).not.toBe(before.image)
expect(after.background).not.toBe(before.background)
expect(after.resource).not.toBe(before.resource)
expect(after.defaultStyle).not.toBe(before.defaultStyle)
expect(after.baseStyle).not.toBe(before.baseStyle)
expect(after.computedStyle).not.toBe(before.computedStyle)
expect(after.measureHints).not.toBe(before.measureHints)
expect(cache.image.size).toBe(0)
expect(cache.background.size).toBe(0)
expect(cache.resource.size).toBe(0)
expect(cache.defaultStyle.size).toBe(0)
expect(cache.baseStyle.size).toBe(0)
})
it('default (input desconocido): cae en soft', () => {
seedSomeData()
const before = snapshotRefs()
applyCachePolicy('unknown-policy')
const after = snapshotRefs()
expect(after.image).toBe(before.image)
expect(after.baseStyle).toBe(before.baseStyle)
expect(after.defaultStyle).toBe(before.defaultStyle)
expect(after.computedStyle).toBe(before.computedStyle)
})
})