▍ humdrum codex / ticktock v0.0.2
license AGPL-3.0
2.2 KB raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package grid

import (
	"testing"

	"github.com/charmbracelet/lipgloss"
	"github.com/mattn/go-runewidth"
)

// A calendar label carries a double-width emoji (🧠 = 2 cells). trunc and
// padCell must reason in display cells, not runes, or the cell overflows its
// column and the terminal wraps it — the alignment bug this guards against.

func TestTruncMeasuresDisplayWidthNotRunes(t *testing.T) {
	// "🧠 Focus time" is 12 runes but 13 display cells (emoji counts as 2).
	s := "🧠 Focus time"
	if got := runewidth.StringWidth(s); got != 13 {
		t.Fatalf("precondition: want display width 13, got %d", got)
	}
	// Truncating to 10 cells must yield a result that fits in 10 cells.
	out := trunc(s, 10)
	if w := runewidth.StringWidth(out); w > 10 {
		t.Fatalf("trunc(%q,10) display width = %d, want <= 10 (got %q)", s, w, out)
	}
	// A string already within width is returned untouched.
	if out := trunc(s, 20); out != s {
		t.Fatalf("trunc of in-width string changed it: %q", out)
	}
}

func TestPadCellPadsToDisplayWidth(t *testing.T) {
	// Emoji cell: 3 runes / 4 display cells; padding to a width must land on
	// exactly that many *cells*, not runes (which would over-pad and push the
	// divider right).
	const w = 22
	out := padCell("🧠 x", w)
	if got := runewidth.StringWidth(out); got != w {
		t.Fatalf("padCell display width = %d, want %d (got %q)", got, w, out)
	}
}

func TestRenderActCellCalendarKeepsColumnWidth(t *testing.T) {
	// The rendered lane cell must occupy exactly the requested display width
	// regardless of emoji or over-long titles.
	const aw = 22
	for _, tc := range []struct {
		name string
		cell Cell
	}{
		{"calendar+emoji", Cell{Kind: CellLabel, Act: ActCalendar, Text: "▪ 🧠 Focus time"}},
		{"plain active", Cell{Kind: CellLabel, Act: ActActive, Text: "organize"}},
		{"idle", Cell{Kind: CellLabel, Act: ActIdle, Text: "(idle)"}},
		{"continuation", Cell{Kind: CellCont, Act: ActCalendar}},
		{"long title truncates", Cell{Kind: CellLabel, Act: ActActive, Text: "Investigate story structure and the whole backlog pile"}},
	} {
		out := renderActCell(tc.cell, aw)
		if w := lipgloss.Width(out); w != aw {
			t.Errorf("%s: rendered width = %d, want %d (got %q)", tc.name, w, aw, out)
		}
	}
}