package termview_test import ( "image" "image/color" "image/png" "os" "testing" "gioui.org/font/gofont" "gioui.org/gpu/headless" "gioui.org/layout" "gioui.org/op" "gioui.org/unit" "go.mitchellh.com/libghostty" "100gex/internal/sample" "100gex/internal/termview" ) // TestRenderSample renders the sample output off-screen and checks a few // pixels. Set TERMVIEW_PNG to a file path to also save the rendered image. func TestRenderSample(t *testing.T) { const width, height = 960, 600 win, err := headless.NewWindow(width, height) if err != nil { t.Skipf("no headless GPU context: %v", err) } defer win.Release() term, err := libghostty.NewTerminal(libghostty.WithSize(80, 24)) if err != nil { t.Fatal(err) } defer term.Close() scr := termview.NewScreen(t, term) defer scr.Close() term.VTWrite(sample.Output()) view, err := termview.New(termview.NewGlyphs(gofont.Collection())) if err != nil { t.Fatal(err) } defer view.Close() view.Focused = true // The view only asks for a size; a Rex server would apply it. view.OnResize = func(cols, rows uint16, cellW, cellH int) { if err := term.Resize(cols, rows, uint32(cellW), uint32(cellH)); err != nil { t.Fatal(err) } } var ops op.Ops gtx := layout.Context{ Ops: &ops, Metric: unit.Metric{PxPerDp: 1, PxPerSp: 1}, Constraints: layout.Exact(image.Pt(width, height)), } // The first frame sizes the terminal; the second draws it at that // size, as a frame after the server resizes it does. for range 2 { ops.Reset() if err := view.Update(gtx, scr); err != nil { t.Fatal(err) } if _, err := view.Layout(gtx); err != nil { t.Fatal(err) } } if err := win.Frame(&ops); err != nil { t.Fatal(err) } img := image.NewRGBA(image.Rect(0, 0, width, height)) if err := win.Screenshot(img); err != nil { t.Fatal(err) } if path := os.Getenv("TERMVIEW_PNG"); path != "" { f, err := os.Create(path) if err != nil { t.Fatal(err) } defer f.Close() if err := png.Encode(f, img); err != nil { t.Fatal(err) } } cols, rows := view.Size() if cols < 80 || rows < 20 { t.Fatalf("terminal sized to %dx%d, want at least 80x20 in %dx%d px", cols, rows, width, height) } cellW, cellH := width/int(cols), height/int(rows) // lower bound is exact enough for cell centres centre := func(col, row int) color.RGBA { return img.RGBAAt(col*cellW+cellW/2, row*cellH+cellH/2) } bg := img.RGBAAt(width-2, height-2) // Row 2 is "16 colours: " (13 cells) then " n " per colour; colour 1 // (red background) covers columns 16-18. Probe the leading space, since // the middle cell holds the digit. if red := centre(16, 2); !(int(red.R) > int(red.G)+60 && int(red.R) > int(red.B)+60) { t.Errorf("red background cell is %v, want a red", red) } if black := centre(13, 2); black == centre(16, 2) { t.Errorf("black and red background cells have the same colour %v", black) } // The cursor sits after "$ " on row 18 and is drawn as a solid block // that differs from the default background. if c := centre(2, 18); c == bg { t.Errorf("cursor cell is the background colour %v", c) } // An untouched cell far right on a blank row is the default background. if c := centre(int(cols)-1, 1); c != bg { t.Errorf("blank cell is %v, want background %v", c, bg) } }