Public: anyone can read this; the crew can change it.
A test pins /system's sizes, spans and sparkline, and a reading outside 0–100 or not a number still draws one block #6889
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/6876-a-test-pins-system-s-sizes-spans-and-spa → main 2 commits fork 1 thread · 1 unresolved
2 files
changed +36
−2
src/system/format.ts +2 −2
| @@ -11,9 +11,9 @@ export const bytes = (n: number): string => { | ||
| 11 | 11 | }; |
| 12 | 12 | |
| 13 | 13 | const BLOCKS = "▁▂▃▄▅▆▇█"; |
| 14 | /** A history as block characters, one per reading, each 0–100. */ | |
| 14 | /** A history as block characters, one per reading, each 0–100; one that is not a finite number draws the lowest. */ | |
| 15 | 15 | export const spark = (history: readonly number[]): string => |
| 16 | history.map((n) => BLOCKS[Math.min(7, Math.max(0, Math.floor((n / 100) * 8)))]).join(""); | |
| 16 | history.map((n) => BLOCKS[Number.isFinite(n) ? Math.min(7, Math.max(0, Math.floor((n / 100) * 8))) : 0]).join(""); | |
| 17 | 17 | |
| 18 | 18 | export const span = (s: number): string => |
| 19 | 19 | s < 90 ? `${s}s` : s < 5400 ? `${Math.round(s / 60)}m` : s < 172_800 ? `${Math.round(s / 3600)}h` : `${Math.round(s / 86_400)}d`; |
test/system-format.test.ts added +34
| @@ -0,0 +1,34 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { plural } from "../src/kernel/plural.ts"; | |
| 3 | import { bytes, span, spark } from "../src/system/format.ts"; | |
| 4 | ||
| 5 | test("bytes at its edges", () => { | |
| 6 | expect(bytes(0)).toBe("0 B"); | |
| 7 | expect(bytes(1023)).toBe("1023 B"); | |
| 8 | expect(bytes(1024)).toBe("1.0 KiB"); | |
| 9 | expect(bytes(10 * 1024)).toBe("10 KiB"); | |
| 10 | expect(bytes(1.5 * 1024 ** 3)).toBe("1.5 GiB"); | |
| 11 | }); | |
| 12 | ||
| 13 | test("span at its edges", () => { | |
| 14 | expect(span(89)).toBe("89s"); | |
| 15 | expect(span(120)).toBe("2m"); | |
| 16 | expect(span(5399)).toBe("90m"); | |
| 17 | expect(span(172_800)).toBe("2d"); | |
| 18 | }); | |
| 19 | ||
| 20 | test("spark draws one block per reading, clamped, and never drops a bad one", () => { | |
| 21 | expect(spark([0, 50, 100])).toBe("▁▅█"); | |
| 22 | expect(spark([-5, 150])).toBe("▁█"); | |
| 23 | const s = spark([20, NaN, Infinity, -Infinity, 100]); | |
| 24 | expect([...s].length).toBe(5); | |
| 25 | expect(s).toBe("▂▁▁▁█"); | |
| 26 | expect(spark([])).toBe(""); | |
| 27 | }); | |
| 28 | ||
| 29 | test("plural", () => { | |
| 30 | expect(plural(1, "comment")).toBe("1 comment"); | |
| 31 | expect(plural(0, "comment")).toBe("0 comments"); | |
| 32 | expect(plural(2, "person", "people")).toBe("2 people"); | |
| 33 | expect(plural(1, "person", "people")).toBe("1 person"); | |
| 34 | }); |
new above
offline
Non-blocking:
pluralissrc/kernel/plural.ts, not a /system formatter, andtest/tui-widgets.test.tsalready exercises it. It reads better in its owntest/plural.test.ts, or dropped from here.