Files
MujicaUI/chart/chord_diagram_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

115 lines
3.3 KiB
Go

package chart
import (
"fmt"
"math"
"testing"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
)
var chordNames = []string{"Nave", "Choir", "Crypt", "Cloister"}
var chordMatrix = [][]float64{{0, 5, 2, 3}, {4, 0, 1, 0}, {1, 2, 0, 6}, {2, 0, 3, 0}}
func TestChordPlace(t *testing.T) {
l := chordPlace(chordMatrix)
if l.total != 29 || len(l.pairs) != 5 {
t.Fatalf("total %v pairs %v", l.total, l.pairs)
}
span := 2*math.Pi - 4*chordGap
if got := l.arcs[0][1] - l.arcs[0][0]; math.Abs(got-span*10/29) > 1e-9 {
t.Fatalf("Nave arc %v", got)
}
if l.sub[0][1][1]-l.sub[0][1][0] <= 0 || l.sub[1][3][1] != l.sub[1][3][0] {
t.Fatal("sub-arcs not sized by the matrix")
}
}
func TestChordDiagramReadout(t *testing.T) {
h := &chartHarness{build: func(c *ui.Context) ChartResult {
return ChordDiagram(c, chordNames, chordMatrix, ChordDiagramOptions{Frame: ChartFrame{Label: "Paths"}})
}}
tt := ui.NewTester(h.view, 520, 420)
tabTo(t, tt, "Paths")
tt.Key(0, ui.KeyRight)
if got := chartReadout(tt); got != "Nave · out 10 · in 7" {
t.Fatalf("group %q", got)
}
tt.Key(0, ui.KeyEnd)
if got := chartReadout(tt); got != "Crypt → Cloister 6 · Cloister → Crypt 3" {
t.Fatalf("chord %q", got)
}
tt.Key(0, ui.KeyEscape)
b := h.box(tt)
cx, cy, rad := b.X+b.W/2, b.Y+120, float32(240)/2-30
tt.Move(cx+(rad+6)*0.5, cy-(rad+6)*0.866) // 30°: inside the first arc
if h.res.Point != 0 {
t.Fatalf("hover %d", h.res.Point)
}
}
func TestChordDiagramBad(t *testing.T) {
for name, m := range map[string][][]float64{"ragged": {{1, 2}, {1}}, "negative": {{0, -1}, {1, 0}}} {
func() {
defer func() {
if recover() == nil {
t.Errorf("%s: no panic", name)
}
}()
ui.NewTester(func(c *ui.Context) {
core.Use(c, core.Settings{})
ChordDiagram(c, []string{"a", "b"}, m, ChordDiagramOptions{Frame: ChartFrame{Label: "x"}})
}, 300, 300)
}()
}
}
func TestChordDiagramSnapshot(t *testing.T) {
snapshot(t, "chord_diagram", 520, 340, func(c *ui.Context) {
ui.Column(c).Padding(16).Children(func() {
ChordDiagram(c, chordNames, chordMatrix, ChordDiagramOptions{Frame: ChartFrame{Label: "Paths"}})
})
})
}
func TestChordPlaceSelfRelation(t *testing.T) {
l := chordPlace([][]float64{{3}})
if len(l.pairs) != 1 || l.pairs[0] != [2]int{0, 0} {
t.Fatalf("self relation %v", l.pairs)
}
}
func TestChordDiagramDenseDataBoundedAndDisabled(t *testing.T) {
const n = 450
names := make([]string, n)
matrix := make([][]float64, n)
for i := range matrix {
names[i] = fmt.Sprint(i)
matrix[i] = make([]float64, n)
for j := range matrix[i] {
matrix[i][j] = 1
}
}
h := &chartHarness{build: func(c *ui.Context) ChartResult {
return ChordDiagram(c, names, matrix, ChordDiagramOptions{Frame: ChartFrame{Label: "Dense chords"}})
}}
var shown *[]int
h.built = func(r ChartResult) { shown = ui.Local(r.Element, "mujica.chord.visible", func() []int { return nil }) }
tt := ui.NewTester(h.view, 520, 420)
if len(*shown) == 0 || len(*shown) > 360 {
t.Fatalf("built %d chords", len(*shown))
}
tabTo(t, tt, "Dense chords")
tt.Key(0, ui.KeyEnd)
if h.res.Point != n+n*(n+1)/2-1 || len(*shown) > 361 {
t.Fatalf("last relation %d, built %d", h.res.Point, len(*shown))
}
h.disabled = true
tt.Frame()
if h.res.Point != -1 {
t.Fatalf("disabled chord %d", h.res.Point)
}
}