import { describe, expect, test } from 'bun:test'
import { inheritStyles, resolve, rgba, type StyleSource } from '../src/core/gameStyle'
const dirt = { position: [20, 0], corner_size: 8, tint: [15, 7, 3, 100], draw_type: 'outer' }
const fixture: StyleSource = {
constants: { default_sprite_scale: 0.5, default_tileset: '__core__/graphics/gui-new.png' },
fonts: { semibold: { from: 'default-semibold', size: 14, filename: 'font.ttf', weight: 600 } },
raw: {
button: {
type: 'button_style', font: 'semibold', padding: 2, left_padding: 8,
minimal_width: 108, minimal_height: 28, default_font_color: {},
clicked_vertical_offset: 1,
default_graphical_set: { base: { position: [0, 0], corner_size: 8 }, shadow: dirt },
hovered_graphical_set: { base: { position: [34, 17], corner_size: 8 }, shadow: dirt, glow: { ...dirt, tint: [225, 177, 106, 255] } },
disabled_font_color: [179, 179, 179],
selected_graphical_set: { base: { position: [51, 17], corner_size: 8 } },
},
child: { type: 'button_style', padding: 0, width: 40, height: 20 },
grandchild: { type: 'button_style', parent: 'child', natural_size: [30, 15], right_padding: 3 },
clear: { type: 'button_style', default_graphical_set: {} },
replacement: { type: 'button_style', default_graphical_set: { base: { position: [4, 4], size: 10 } } },
checkbox: {
type: 'checkbox_style', font_color: [1, 1, 1], disabled_font_color: [1, 1, 1, 0.5],
default_graphical_set: { base: { position: [0, 0], size: 28 } },
selected_graphical_set: { base: { position: [56, 0], size: 28 } },
disabled_graphical_set: { base: { position: [28, 0], size: 28 } },
checkmark: { position: [112, 0], size: 28 }, disabled_checkmark: { position: [456, 188], size: 28 },
intermediate_mark: { position: [456, 160], size: 28 },
},
table: { type: 'table_style', cell_padding: 1 },
rows: {
type: 'table_style', apply_row_graphical_set_per_column: true, top_cell_padding: 4,
default_row_graphical_set: { position: [208, 17], corner_size: 8 },
hovered_graphical_set: { position: [34, 17], corner_size: 8 },
selected_graphical_set: { position: [51, 17], corner_size: 8 },
},
},
}
describe('style inheritance and metrics', () => {
test('implicit roots, explicit parents, shorthand overrides and natural sizes', () => {
const s = resolve(fixture, 'grandchild')
expect(s.metrics).toEqual({ minimal_width: 40, maximal_width: 40, natural_width: 30, minimal_height: 20, maximal_height: 20, natural_height: 15 })
expect(s.padding).toEqual({ top: 0, right: 3, bottom: 0, left: 0 })
expect(s.fontDefinition).toMatchObject({ filename: 'font.ttf', size: 14, weight: 600 })
expect(resolve(fixture, 'button', 'clicked').contentOffset).toEqual([0, 1])
expect(resolve(fixture, 'button').contentOffset).toEqual([0, 0])
})
test('nested graphical sets replace, including explicit empty tables; raw is unchanged', () => {
const before = JSON.stringify(fixture)
expect(resolve(fixture, 'clear').layers).toEqual([])
expect(resolve(fixture, 'replacement').layers.map(l => l.name)).toEqual(['base'])
expect(JSON.stringify(fixture)).toBe(before)
})
test('unknown styles, missing parents and cycles fail explicitly', () => {
expect(() => resolve(fixture, 'absent')).toThrow('Unknown style')
expect(() => inheritStyles({ a: { parent: 'b' } })).toThrow('Unknown style: b')
expect(() => inheritStyles({ a: { parent: 'b' }, b: { parent: 'a' } })).toThrow('inheritance cycle')
})
})
describe('state, colour and layer resolution', () => {
test('colours accept normalized, byte, named and empty forms', () => {
expect(rgba([1, 0.5, 0, 0.5])).toEqual([255, 127.5, 0, 127.5])
expect(rgba({ r: 15, g: 7, b: 3, a: 100 })).toEqual([15, 7, 3, 100])
expect(rgba({})).toEqual([0, 0, 0, 255])
expect(rgba([179, 179, 179])).toEqual([179, 179, 179, 255])
expect(resolve(fixture, 'button', 'disabled').fontColor).toEqual([179, 179, 179, 255])
})
test('hover returns shadow/base/glow with atlas pieces and screen extents', () => {
const s = resolve(fixture, 'button', 'hovered')
expect(s.layers.map(l => l.name)).toEqual(['shadow', 'base', 'glow'])
expect(s.layers[0]!.outerExtent).toEqual({ top: 4, right: 4, bottom: 4, left: 4 })
expect(s.layers[1]!.outerExtent).toEqual({ top: 0, right: 0, bottom: 0, left: 0 })
expect(s.layers[1]!.pieces.right_bottom).toMatchObject({ position: [43, 26], size: [8, 8], scale: 0.5 })
expect(s.layers[2]!.tint).toEqual([225, 177, 106, 255])
expect(s.fontColor).toEqual([0, 0, 0, 255])
})
test('checkbox checked selects the orange face and adds the proper mark last', () => {
const checked = resolve(fixture, 'checkbox', 'checked')
expect(checked.layers.map(l => l.position)).toEqual([[56, 0], [112, 0]])
expect(checked.layers.every(l => !l.stretchMonolith)).toBe(true)
expect(resolve(fixture, 'checkbox', 'unchecked').layers).toHaveLength(1)
expect(resolve(fixture, 'checkbox', 'disabled-checked').layers.map(l => l.position)).toEqual([[28, 0], [456, 188]])
expect(resolve(fixture, 'checkbox', 'disabled-checked').fontColor).toEqual([255, 255, 255, 127.5])
expect(resolve(fixture, 'checkbox', 'intermediate').layers.at(-1)!.position).toEqual([456, 160])
})
test('table row sets are shorthand layers and retain per-cell semantics', () => {
expect(resolve(fixture, 'rows').layers[0]!.position).toEqual([208, 17])
expect(resolve(fixture, 'rows', 'hovered').layers[0]!.position).toEqual([34, 17])
expect(resolve(fixture, 'rows', 'selected').layers[0]!.position).toEqual([51, 17])
expect(resolve(fixture, 'rows').applyRowGraphicalSetPerColumn).toBe(true)
expect(resolve(fixture, 'rows').cellPadding).toEqual({ top: 4, right: 1, bottom: 1, left: 1 })
})
test('explicit pieces override compact pieces and preserve tint, opacity and blend mode', () => {
const s = resolve({ raw: { box: { graphical_set: {
base: { position: [1, 2], corner_size: [4, 6], scale: 0.25, draw_type: 'outer',
top_outer_border_shift: 1, right_outer_border_shift: -1, tint: [128, 255, 255], opacity: 0.5,
center: { filename: 'other.png', position: [2, 3], size: [2, 4], scale: 1, blend_mode: 'additive' } },
} } } }, 'box').layers[0]!
expect(s.pieces.center).toMatchObject({ filename: 'other.png', position: [2, 3], size: [2, 4], scale: 1, opacity: 0.5, blend_mode: 'additive' })
expect(s.outerExtent).toEqual({ top: 0.5, right: 0, bottom: 1.5, left: 1 })
expect(s.pieces.left_top!.tint).toEqual([128, 255, 255, 255])
})
test('empty and none layers vanish; explicit composition borders are logical px', () => {
const s = resolve({ raw: { box: { graphical_set: { shadow: {}, glow: { type: 'none' }, base: {
top: { position: [8, 0], size: [1, 8] }, left: { position: [0, 8], size: [8, 1] },
right_border: 3, top_tiling: true,
} } } } }, 'box').layers
expect(s).toHaveLength(1)
expect(s[0]!.borders).toEqual({ top: 4, right: 3, bottom: 0, left: 4 })
expect(s[0]!.tiling).toEqual({ top_tiling: true })
})
})