Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions apps/mobile/src/tokens.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/// <reference types="node" />
// Hermit's design tokens (docs/design/polish/decisions.md): every color is a docs/brand/tokens.json value, or a mix of
// two of them (mix): the surfaces between the canvas and white, the tonal fills between a surface and an accent, and
// the scrims. Light is Quiet craft; dark is Deep sea. The motion tokens live beside them (docs/design/polish/decisions.md,
// "Motion"): plain numbers, no animation library.
import { test } from "node:test";
import assert from "node:assert/strict";
import { mix } from "./theme.ts";
import { duration, easing, stagger, staggerDelay } from "./motion.ts";

test("mix(a, b, t) holds at a when t is 0 and at b when t is 1", () => {
assert.equal(mix("#F5EDE1", "#B4483A", 0), "#F5EDE1");
assert.equal(mix("#F5EDE1", "#B4483A", 1), "#B4483A");
});

test("mix rounds to a whole hex channel at the midpoint", () => {
// Half of #000000 → #808080: the blend rounds, it does not truncate.
assert.equal(mix("#000000", "#FFFFFF", 0.5), "#808080");
});

test("mix answers in uppercase #RRGGBB even when the inputs are lowercase", () => {
assert.match(mix("#ffffff", "#2b221b", 0.5), /^#[0-9A-F]{6}$/);
});

test("mix keeps every channel between the two inputs while t is between 0 and 1", () => {
const pairs: Array<[string, string]> = [
["#000000", "#FFFFFF"],
["#06121D", "#5EF0CF"],
["#2B221B", "#8A5912"],
];
for (const [a, b] of pairs) {
for (const t of [0.25, 0.5, 0.75]) {
const c = mix(a, b, t);
const blend = [0, 1, 2].map((i) => parseInt(c.slice(1 + i * 2, 3 + i * 2), 16));
const from = [0, 1, 2].map((i) => parseInt(a.slice(1 + i * 2, 3 + i * 2), 16));
const to = [0, 1, 2].map((i) => parseInt(b.slice(1 + i * 2, 3 + i * 2), 16));
for (let i = 0; i < 3; i++) {
assert.ok(
blend[i] >= Math.min(from[i], to[i]) && blend[i] <= Math.max(from[i], to[i]),
`channel ${i} of ${c} should stay between ${a} and ${b} at t=${t}`,
);
}
}
}
});

test("staggerDelay spaces items out by one step and stops at stagger.max", () => {
assert.equal(staggerDelay(0), 0);
assert.equal(staggerDelay(1), stagger.step);
assert.equal(staggerDelay(2), stagger.step * 2);
assert.equal(staggerDelay(stagger.max - 1), (stagger.max - 1) * stagger.step);
assert.equal(staggerDelay(stagger.max), stagger.max * stagger.step);
assert.equal(staggerDelay(stagger.max + 4), stagger.max * stagger.step);
});

test("duration tokens are positive whole milliseconds, a press is instant and sheet is the longest", () => {
const values = Object.values(duration);
for (const v of values) {
assert.ok(Number.isInteger(v), `duration ${v} should be a whole number of milliseconds`);
assert.ok(v > 0, `duration ${v} should be positive`);
}
assert.ok(duration.press < 100, `a press should read as instant, got ${duration.press}`);
assert.equal(duration.sheet, Math.max(...values), `sheet should be the longest duration, got ${duration.sheet}`);
});

test("easing curves are four numbers whose control-x values sit between 0 and 1", () => {
for (const curve of Object.values(easing)) {
assert.equal(curve.length, 4, `a cubic-bezier needs four numbers, got ${curve.length}`);
for (const x of [curve[0], curve[2]]) {
assert.ok(x >= 0 && x <= 1, `a cubic-bezier x value should sit between 0 and 1, got ${x}`);
}
}
});
Loading