clone

a browser grid puzzle game where you push words around to rewrite the rules of each level

main
miau-is-true / tests / title-fitting.test.js
title-fitting.test.js 2.1K
1import test from 'node:test';2import assert from 'node:assert/strict';3import { fitSceneTitle } from '../src/render/title-fitting.js';45function context() {6 return {7 font: '',8 measureText(value) {9 const size = Number(this.font.match(/(\d+(?:\.\d+)?)px/)?.[1] || 16);10 return { width: String(value).length * size * 0.5 };11 },12 };13}1415test('title fitting keeps a short title at the 22px floor ceiling', () => {16 const fit = fitSceneTitle(context(), 'Break the rule', { maxWidth: 240 });17 assert.equal(fit.size, 22);18 assert.deepEqual(fit.lines, ['Break the rule']);19 assert.equal(fit.overflow, false);20});2122test('title fitting shrinks complete copy before wrapping', () => {23 const fit = fitSceneTitle(context(), 'A medium authored title', { maxWidth: 220 });24 assert.ok(fit.size < 22);25 assert.deepEqual(fit.lines, ['A medium authored title']);26 assert.equal(fit.lines.join(' '), fit.text);27});2829test('title fitting wraps long authored copy at words with no ellipsis', () => {30 const fit = fitSceneTitle(context(), 'A deliberately long authored proof title for mobile review', { maxWidth: 150 });31 assert.equal(fit.size, 16);32 assert.equal(fit.lines.length, 2);33 assert.equal(fit.lines.join(' '), fit.text);34 assert.equal(fit.lines.some(line => line.includes('…')), false);35});3637test('title fitting reports an unbreakable overflow without dropping copy', () => {38 const fit = fitSceneTitle(context(), 'Supercalifragilisticexpialidocious proof', { maxWidth: 80 });39 assert.equal(fit.lines.join(' '), fit.text);40 assert.equal(fit.overflow, true);41});4243test('mobile fallback fits a 51-character title in three 16px lines', () => {44 const text = 'A Very Long Authored Proof Title for Narrow Screens';45 const twoLine = fitSceneTitle(context(), text, { maxWidth: 180, maxSize: 22, minSize: 16, maxLines: 2 });46 assert.equal(twoLine.overflow, true);47 const fallback = fitSceneTitle(context(), text, { maxWidth: 180, maxSize: 16, minSize: 16, maxLines: 3 });48 assert.equal(fallback.overflow, false);49 assert.equal(fallback.size, 16);50 assert.equal(fallback.lines.join(' '), text);51 assert.equal(fallback.lines.length, 3);52});