import { test } from 'node:test' import assert from 'node:assert/strict' import { quantize, countBeads, validateGrid, cropRect } from '../src/core/pattern.ts' let colors = [{ code: 'B', name: '黑', hex: '#000000', location: '', available: true }, { code: 'W', name: '白', hex: '#ffffff', location: '', available: true }] test('透明像素不是白色豆;统计仅包括实际豆子', () => { let cells = quantize(new Uint8ClampedArray([255,255,255,255, 0,0,0,255, 255,255,255,0]), colors) assert.deepEqual(cells, ['W','B',null]) assert.deepEqual(countBeads(cells), { W: 1, B: 1 }) }) test('色板量化使用最近的可用颜色', () => { assert.deepEqual(quantize(new Uint8ClampedArray([230,230,230,255]), colors), ['W']) assert.throws(() => quantize(new Uint8ClampedArray([0,0,0,255]), []), /色板/) }) test('非法尺寸、未知色号、空图不能保存', () => { assert.throws(() => validateGrid(2, 2, ['W'], colors), /网格/) assert.throws(() => validateGrid(1, 1, ['X'], colors), /色号/) assert.throws(() => validateGrid(1, 1, [null], colors), /空/) assert.doesNotThrow(() => validateGrid(1, 1, ['W'], colors)) }) test('中心裁剪保持目标比例', () => { assert.deepEqual(cropRect(400,200,1), { x:100,y:0,width:200,height:200 }) assert.deepEqual(cropRect(200,400,1), { x:0,y:100,width:200,height:200 }) })