Files
MujicaUI/chart/sankey_chart_test.go
ZacharyZhang-NYandClaude Opus 5.5 bcce34cda1 Upgrade MyGo to v0.5.5
MyGo v0.5.5 returns ui.Element as a checked value handle instead of a
pointer. Migrated with `mygo migrate-ui`, then fixed what the codemod
leaves to review:

- Element nil checks become .Valid(); absent elements are ui.Element{}.
- Input handlers and paint callbacks run outside the build pass, where
  elements expire and Context methods are inert: read IsDisabled into a
  local during the build, keep a ui.Handle for Focus, use c.Services()
  for clipboard access, create ui.Local state during the build.
- Add core.AutoFocus: v0.5.5 records an overlay's opener after its
  content builds, so same-frame autofocus lost focus restore on close.
  It focuses one frame later; cascader rows defer their focus likewise.
- core.OverlayLayer identifies build passes by the root element, not
  c.Now(), which repeats across passes and under the Tester clock.
- Context menus survive the opening press counted as PressedOutside;
  split button tooltip no longer reorders its open menu; disabled
  menubar rows pass presses to the panel; Form defers blur-started
  async checks so the growing message no longer moves Submit away
  from the release; Combobox drops its disabled copy now that MyGo
  blocks typing under a disabled wrapper (canary flipped to a guard).
- The Tester clock is virtual: tests use Advance/Now instead of
  time.Sleep/time.Now and read element state inside the view. The web
  mirror advances the clock to wall time before each frame and input.
- AGENTS.md and docs describe the value Element API and the new pin.

Snapshot goldens are darwin-only and were not re-verified here.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 01:37:54 -04:00

84 lines
2.5 KiB
Go

package chart
import (
"fmt"
"testing"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
)
var sankeyNodes = []SankeyNode{{"tithe", "Tithes"}, {"alms", "Alms"}, {"fund", "Fund"}, {"roof", "Roof"}, {"bread", "Bread"}, {"bells", "Bells"}}
var sankeyLinks = []SankeyLink{
{"tithe", "fund", 60}, {"alms", "fund", 30}, {"alms", "bread", 10},
{"fund", "roof", 50}, {"fund", "bells", 40},
}
func TestSankeyPlaceStable(t *testing.T) {
l := sankeyPlace(sankeyNodes, sankeyLinks)
if fmt.Sprint(l.col) != "[0 0 1 2 1 2]" || l.cols != 3 || l.in[2] != 90 || l.out[1] != 40 {
t.Fatalf("layout %+v", l)
}
lit := sankeyLit(l, len(sankeyNodes), 0) // Tithes: its fund, roof and bells, not alms
if !lit[2] || !lit[3] || !lit[5] || lit[1] || lit[4] || !lit[6] || lit[7] {
t.Fatalf("lit from Tithes %v", lit)
}
lit = sankeyLit(l, len(sankeyNodes), 6+3) // the fund → roof link
if !lit[0] || !lit[1] || !lit[3] || lit[5] || lit[6+4] {
t.Fatalf("lit from fund → roof %v", lit)
}
}
func TestSankeyChartReadout(t *testing.T) {
h := &chartHarness{build: func(c *ui.Context) ChartResult {
return SankeyChart(c, sankeyNodes, sankeyLinks, SankeyChartOptions{Frame: ChartFrame{Label: "Money"}})
}}
tt := ui.NewTester(h.view, 560, 440)
tabTo(t, tt, "Money")
tt.Key(0, ui.KeyRight)
tt.Key(0, ui.KeyRight)
tt.Key(0, ui.KeyRight)
if got := chartReadout(tt); got != "Fund · in 90 · out 90" {
t.Fatalf("node %q", got)
}
tt.Key(0, ui.KeyEnd)
if got := chartReadout(tt); got != "Fund → Bells · 40" {
t.Fatalf("link %q", got)
}
tt.Key(0, ui.KeyEscape)
b := h.box(tt)
tt.Move(b.X+8+4, b.Y+10) // the first node, at the top left
if got := chartReadout(tt); got != "Tithes · in 0 · out 60" {
t.Fatalf("hover %q", got)
}
}
func TestSankeyChartBad(t *testing.T) {
for name, links := range map[string][]SankeyLink{
"cycle": {{"tithe", "fund", 1}, {"fund", "tithe", 1}},
"unknown": {{"tithe", "nowhere", 1}},
"zero": {{"tithe", "fund", 0}},
} {
func() {
defer func() {
if recover() == nil {
t.Errorf("%s: no panic", name)
}
}()
ui.NewTester(func(c *ui.Context) {
core.Use(c, core.Settings{})
SankeyChart(c, sankeyNodes, links, SankeyChartOptions{Frame: ChartFrame{Label: "x"}})
}, 300, 300)
}()
}
}
func TestSankeyChartSnapshot(t *testing.T) {
snapshot(t, "sankey_chart", 560, 320, func(c *ui.Context) {
ui.Column(c).Padding(16).Children(func() {
SankeyChart(c, sankeyNodes, sankeyLinks, SankeyChartOptions{Frame: ChartFrame{Label: "Money"}})
})
})
}