import { describe, expect, test } from 'bun:test'
import { readFileSync } from 'node:fs'
import { extractRules, matches, rewritePseudo } from '../tools/bench/css'
const css = `/* header */
.button,.button-green {
color:#000
}
.button:hover {
background-color:#e39827
}
.button-green:active {
color:red
}
.panel { border-image:url(assets/portal/data-03.png) 8/4px repeat }`
describe('bench css', () => {
test('selector matching respects class boundaries', () => {
expect(matches('.button:hover', '.button')).toBe(true)
expect(matches('.x,.button .icon', '.button')).toBe(true)
expect(matches('.button-green:active', '.button')).toBe(false)
expect(matches('.panel-inset.p0', '.panel-inset')).toBe(true)
expect(matches('.panel-inset-lighter.p0', '.panel-inset')).toBe(false)
})
test('pseudo rewrite', () => {
expect(rewritePseudo('.a:hover,.b:active .c:focus,.d:focus-within')).toBe('.a.is-hover,.b.is-active .c.is-focus,.d:focus-within')
})
test('extract keeps order, rewrites pseudo and urls', () => {
const out = extractRules(css, ['.button', '.panel'], (u) => `file:///X/${u}`)
expect(out).toContain('.button,.button-green {')
expect(out).toContain('.button.is-hover {')
expect(out).not.toContain('red')
expect(out).toContain('url(file:///X/assets/portal/data-03.png)')
expect(out.indexOf('.button,')).toBeLessThan(out.indexOf('.button.is-hover'))
expect(() => extractRules(css, ['.nothing'])).toThrow()
})
test('the real portal file yields the .panel-inset.p0 cascade', () => {
const real = readFileSync(new URL('../docs/reference/portal-css.txt', import.meta.url), 'utf8')
const out = extractRules(real, ['.panel-inset.p0'])
expect(out).toContain('background-color:#242324')
expect(out).toContain('inset 0px 0px 3px 0px #000000, 0px -2px 2px -1px #000000')
})
})
describe('bench refs', () => {
test('masks move into crop coordinates and clip', async () => {
const { cropRef } = await import('../tools/bench/refs')
const image = { width: 10, height: 10, data: new Uint8Array(400) }
const r = cropRef({ image: 'x', rect: [2, 2, 5, 5], masks: [[3, 3, 2, 2], [0, 0, 3, 3], [8, 8, 1, 1]] }, image)
expect(r.image.width).toBe(5)
expect(r.masks).toEqual([[1, 1, 2, 2], [0, 0, 1, 1]])
})
})