font.test.js

2.5 kB · javascript · 59 lines

1import { expect, test } from 'bun:test';2import { existsSync } from 'node:fs';3import { join } from 'node:path';4import { animate, cycle, letters, merge, HOLD } from './font.js';5import FONT from './font.json' with { type: 'json' };67const WORDMARK = 'MRLYPROD';89test('the wordmark writes itself in stroke order, one cell a frame', () => {10  const write = animate(WORDMARK, 1);11  expect([write.rows, write.cols, write.fps]).toEqual([7, 49, 25]);12  expect(write.frames[0]).toEqual([]);13  expect(write.frames.length).toBe(104);14  for (let i = 1; i < write.frames.length; i++) expect(write.frames[i].length).toBe(write.frames[i - 1].length + 1);15  expect(write.frames[1]).toEqual([5 * 49 + 1]);16  const grid = letters(WORDMARK).grid;17  const lit = grid.flat().filter(Boolean).length;18  expect(write.frames[103].length).toBe(lit);19});2021test('the eight letters fold into an X in twenty-two frames', () => {22  const folded = merge(WORDMARK, 1);23  expect(folded.length).toBe(22);24  expect(folded[0]).toEqual(animate(WORDMARK, 1).frames[103]);25  const x = [];26  FONT.X.rows.forEach((row, r) => [...row].forEach((ch, c) => ch === '1' && x.push((1 + r) * 49 + 22 + c)));27  expect(folded[21]).toEqual(x);28});2930test('the cycle loops through both halves with a rest after each', () => {31  const anim = cycle(WORDMARK, 1);32  expect(anim.frames.length).toBe(2 * 104 + 2 * 22 + 4 * HOLD);33  expect(anim.frames.length).toBe(352);34  for (const frame of anim.frames) {35    expect(frame.every((v, i) => i === 0 || frame[i - 1] < v)).toBe(true);36    expect(frame.every((v) => v < anim.rows * anim.cols)).toBe(true);37  }38});3940test('any string writes itself and a lone glyph has nothing to merge', () => {41  for (const text of ['a', 'hi', 'mrly.net', '(1)']) {42    const write = animate(text, 2);43    const { rows, cols, grid } = letters(text);44    expect([write.rows, write.cols]).toEqual([rows + 4, cols + 4]);45    expect(write.frames.length).toBe(grid.flat().filter(Boolean).length + 1);46  }47  expect(merge('A', 1).length).toBe(1);48});4950test('the kit matches the crate frame for frame', async () => {51  const pkg = join(import.meta.dir, '..', 'pkg');52  if (!existsSync(join(pkg, 'mrlydemo.js'))) return;53  const wasm = await import(join(pkg, 'mrlydemo.js'));54  await wasm.default({ module_or_path: await Bun.file(join(pkg, 'mrlydemo_bg.wasm')).arrayBuffer() });55  for (const text of ['MRLYPROD', 'SIERPINSKI', 'mrly.net', '(1)', 'Hi 42', 'A']) {56    expect(animate(text, 1)).toEqual(JSON.parse(wasm.font_animate(text, 1)));57    expect(cycle(text, 1, 25)).toEqual(JSON.parse(wasm.font_cycle(text, 1, 25)));58  }59});