Skip to content
28 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import { ROW_HEIGHT, rowAt, rowStarts, windowOf } from "./log-window.ts";
5
6test("rows start where the rows above them end, measured or not", () => {
7 const starts = rowStarts(4, (index) => (index === 1 ? 60 : undefined));
8 assert.deepEqual([...starts], [0, ROW_HEIGHT, ROW_HEIGHT + 60, 2 * ROW_HEIGHT + 60, 3 * ROW_HEIGHT + 60]);
9 assert.equal(rowAt(starts, 0), 0);
10 assert.equal(rowAt(starts, ROW_HEIGHT), 1);
11 assert.equal(rowAt(starts, ROW_HEIGHT + 59), 1);
12 assert.equal(rowAt(starts, ROW_HEIGHT + 60), 2);
13 assert.equal(rowAt(starts, 10_000), 3, "past the end is the last row");
14});
15
16test("only the rows on screen and a little around them are drawn", () => {
17 const starts = rowStarts(50_000, () => undefined);
18 // The screen shows rows 1000 to 1040.
19 const [from, to] = windowOf(starts, 1000 * ROW_HEIGHT, 1040 * ROW_HEIGHT, 200);
20 assert.deepEqual([from, to], [990, 1051]);
21 // At the very end, following the newest line.
22 const end = starts[50_000]!;
23 assert.deepEqual(windowOf(starts, end - 800, end, 200), [49_950, 50_000]);
24 // The log above or below the screen draws a few rows at the near end.
25 assert.deepEqual(windowOf(starts, -5000, -4000, 200), [0, 20]);
26 assert.deepEqual(windowOf(starts, end + 4000, end + 5000, 200), [49_980, 50_000]);
27 assert.deepEqual(windowOf(rowStarts(0, () => undefined), 0, 100), [0, 0]);
28});