Files
MujicaUI/finance/price_text_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

107 lines
3.3 KiB
Go

package finance
import (
"bytes"
"testing"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// tradingTester runs view in the default theme with trend t.
func tradingTester(t core.Trend, w, h int, view func(c *ui.Context)) *ui.Tester {
return ui.NewTester(func(c *ui.Context) {
core.Use(c, core.Settings{})
core.UseTrend(c, t)
ui.Column(c).Padding(10).AlignItems(ui.Start).Children(func() { view(c) })
}, w, h)
}
func TestPriceTextFormatAndTrend(t *testing.T) {
price := 12345.678
tt := tradingTester(core.GreenUp, 300, 100, func(c *ui.Context) {
PriceText(c, price, PriceTextOptions{Decimals: 2, Compare: true, Reference: 12000})
})
if !tt.HasText("12,345.68") {
t.Fatalf("texts %q", tt.Texts())
}
if _, ok := tt.Find("12,345.68, up"); !ok {
t.Fatal("trend not spoken")
}
price = 11000
tt.Frame()
if _, ok := tt.Find("11,000.00, down"); !ok {
t.Fatalf("down not spoken: %q", tt.Texts())
}
tt = tradingTester(core.GreenUp, 300, 100, func(c *ui.Context) {
PriceText(c, 1234.5, PriceTextOptions{Decimals: 3, NoGrouping: true})
})
if !tt.HasText("1234.500") {
t.Fatalf("ungrouped %q", tt.Texts())
}
}
func TestTrendColorsFollowUseTrend(t *testing.T) {
var up, down [2]ui.Color
for i, tr := range []core.Trend{core.GreenUp, core.RedUp} {
tradingTester(tr, 100, 100, func(c *ui.Context) {
up[i], down[i] = core.TrendColor(c, 1), core.TrendColor(c, -1)
})
}
k := theme.Light()
if up[0] != k.Success || down[0] != k.Danger || up[1] != k.Danger || down[1] != k.Success {
t.Fatalf("up %v down %v", up, down)
}
defer func() {
if recover() == nil {
t.Fatal("unknown trend accepted")
}
}()
tradingTester(core.Trend(7), 100, 100, func(*ui.Context) {})
}
// A change tints once; reduced motion never tints.
func TestPriceTextFlashesOnceAndNotUnderReducedMotion(t *testing.T) {
for _, still := range []bool{false, true} {
prefs := ui.Preferences{ReduceMotion: still, TextScale: 1}
price := 10.0
tt := tradingTester(core.GreenUp, 200, 60, func(c *ui.Context) { PriceText(c, price, PriceTextOptions{}) })
tt.SetPreferences(prefs)
price = 10.5
tt.Frame()
fresh := tradingTester(core.GreenUp, 200, 60, func(c *ui.Context) { PriceText(c, price, PriceTextOptions{}) })
fresh.SetPreferences(prefs)
fresh.Frame()
same := bytes.Equal(tt.Image().Pix, fresh.Image().Pix)
if same == !still {
t.Fatalf("reduced motion %v: flashed %v", still, !same)
}
tt.Advance(priceTextFlash + 20*time.Millisecond)
if !bytes.Equal(tt.Image().Pix, fresh.Image().Pix) {
t.Fatalf("reduced motion %v: flash did not end", still)
}
}
}
func TestPriceTextRejectsNegativeDecimals(t *testing.T) {
defer func() {
if recover() == nil {
t.Fatal("negative decimals accepted")
}
}()
tradingTester(core.GreenUp, 100, 100, func(c *ui.Context) { PriceText(c, 1, PriceTextOptions{Decimals: -1}) })
}
func TestPriceTextSnapshot(t *testing.T) {
snapshot(t, "price_text", 320, 150, func(c *ui.Context) {
ui.Column(c).Padding(20).Gap(12).AlignItems(ui.Start).Children(func() {
PriceText(c, 18234.5, PriceTextOptions{Size: theme.H2Size, Compare: true, Reference: 18000})
PriceText(c, 0.004521, PriceTextOptions{Decimals: 6, Compare: true, Reference: 0.0046})
PriceText(c, 100, PriceTextOptions{Compare: true, Reference: 100})
})
})
}