Files
app/frontend/src/lib/utils.check.ts
T
stroblme ffaf66c14e Write a run's elapsed time once, in the duration formatter's units
The runs table drew when a run started and how long it took in two
notations ('56m ago | 55.5 min'), and for a run still going those are the
same reading twice. `dur` now steps past the minute into hours and days,
the ago reading is derived from it, and a running run writes only the
elapsed one.
2026-08-28 11:02:59 +02:00

55 lines
1.7 KiB
TypeScript

/**
* The cases `si` and `dur` get wrong when their rounding is written the
* obvious way.
*
* Run: `bun src/lib/utils.check.ts` (there is no unit runner; the suite in
* `tests/` drives a running stack).
*/
import assert from "node:assert/strict"
import { dur, si } from "./utils"
// Nothing is prefixed in the plain window: these are written with a unit after
// them, and "400m ms" is not an improvement on "0.4 ms".
assert.equal(si(0), "0")
assert.equal(si(0.4), "0.4")
assert.equal(si(203.63), "204")
// Rounding must not leave a value in the step it just rounded out of.
assert.equal(si(999.7), "1k")
assert.equal(si(15000), "15k")
assert.equal(si(-2.5e6), "-2.5M")
// A small reading has to survive as itself; a series of these is not zeroes.
assert.equal(si(0.0031, 4), "3.1m")
assert.equal(si(0.000031), "31µ")
assert.equal(si(1e-9), "1.0e-9")
// Two neighbouring readings stay apart at the sparkline readout's four digits.
assert.notEqual(si(1234, 4), si(1235, 4))
assert.equal(si(Number.NaN), "--")
console.log("si: ok")
// The unit follows the size, so no reading is left as a shortened number in
// front of a unit the call site guessed.
assert.equal(dur(0), "0 ms")
assert.equal(dur(203.63), "204 ms")
assert.equal(dur(1240), "1.24 s")
assert.equal(dur(90_000), "1.5 min")
assert.equal(dur(12_600_000), "3.5 h")
assert.equal(dur(259_200_000), "3 d")
// The plain window down to 0.01 ms, so a fast run keeps one unit across polls.
assert.equal(dur(0.4), "0.4 ms")
assert.equal(dur(0.0031, 4), "3.1 µs")
// Rounding must not leave a value in the step it just rounded out of.
assert.equal(dur(999.7), "1 s")
assert.equal(dur(59_999), "1 min")
assert.equal(dur(Number.NaN), "--")
console.log("dur: ok")