Add e2e test harness for testing editor logic without Gio display
- internal/test/e2e/: FrameCapture, Harness, ElementAssertions, helpers - internal/editor/logic.go: Add done channel and Done() method for graceful shutdown - internal/editor/mock_setup.go: Mock filesystem for tests - internal/browser/: Browser layout and search logic - internal/io/: Worker pool for async tasks - Update architecture docs and spec
This commit is contained in:
@@ -0,0 +1,357 @@
|
||||
package e2e
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"pad/internal/ui"
|
||||
)
|
||||
|
||||
// ElementAssertions provides fluent assertions on element slices.
|
||||
type ElementAssertions struct {
|
||||
elements []ui.Element
|
||||
t testing.TB
|
||||
failed bool
|
||||
}
|
||||
|
||||
// NewElementAssertions creates assertions for a frame.
|
||||
func NewElementAssertions(t testing.TB, elements []ui.Element) *ElementAssertions {
|
||||
return &ElementAssertions{
|
||||
elements: elements,
|
||||
t: t,
|
||||
failed: false,
|
||||
}
|
||||
}
|
||||
|
||||
// HasElementCount asserts the number of elements.
|
||||
func (ea *ElementAssertions) HasElementCount(count int) *ElementAssertions {
|
||||
if len(ea.elements) != count {
|
||||
ea.t.Errorf("expected %d elements, got %d", count, len(ea.elements))
|
||||
ea.failed = true
|
||||
}
|
||||
return ea
|
||||
}
|
||||
|
||||
// HasElementWithID asserts an element exists with the given ID.
|
||||
func (ea *ElementAssertions) HasElementWithID(id string) *ElementAssertions {
|
||||
found := false
|
||||
for _, elem := range ea.elements {
|
||||
if idElem, ok := elem.(interface{ ID() string }); ok {
|
||||
if idElem.ID() == id {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
ea.t.Errorf("expected element with ID %q, not found", id)
|
||||
ea.failed = true
|
||||
}
|
||||
return ea
|
||||
}
|
||||
|
||||
// HasElementOfType asserts an element of the given type exists.
|
||||
func (ea *ElementAssertions) HasElementOfType(t reflect.Type) *ElementAssertions {
|
||||
found := false
|
||||
for _, elem := range ea.elements {
|
||||
if reflect.TypeOf(elem) == t {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
ea.t.Errorf("expected element of type %v, not found", t)
|
||||
ea.failed = true
|
||||
}
|
||||
return ea
|
||||
}
|
||||
|
||||
// HasLabelWithText asserts a Label element contains the given text.
|
||||
func (ea *ElementAssertions) HasLabelWithText(text string) *ElementAssertions {
|
||||
found := false
|
||||
for _, elem := range ea.elements {
|
||||
if label, ok := elem.(ui.Label); ok {
|
||||
if label.Text == text {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
ea.t.Errorf("expected Label with text %q, not found", text)
|
||||
ea.failed = true
|
||||
}
|
||||
return ea
|
||||
}
|
||||
|
||||
// HasListViewWithItemCount asserts a ListView has the expected number of items.
|
||||
func (ea *ElementAssertions) HasListViewWithItemCount(count int) *ElementAssertions {
|
||||
var listView *ui.ListView
|
||||
for _, elem := range ea.elements {
|
||||
if lv, ok := elem.(ui.ListView); ok {
|
||||
listView = &lv
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if listView == nil {
|
||||
ea.t.Error("expected ListView element, not found")
|
||||
ea.failed = true
|
||||
return ea
|
||||
}
|
||||
|
||||
if len(listView.Items) != count {
|
||||
ea.t.Errorf("expected ListView with %d items, got %d", count, len(listView.Items))
|
||||
ea.failed = true
|
||||
}
|
||||
return ea
|
||||
}
|
||||
|
||||
// HasListViewWithItems asserts a ListView has the expected items.
|
||||
func (ea *ElementAssertions) HasListViewWithItems(expected []string) *ElementAssertions {
|
||||
var listView *ui.ListView
|
||||
for _, elem := range ea.elements {
|
||||
if lv, ok := elem.(ui.ListView); ok {
|
||||
listView = &lv
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if listView == nil {
|
||||
ea.t.Error("expected ListView element, not found")
|
||||
ea.failed = true
|
||||
return ea
|
||||
}
|
||||
|
||||
for _, expItem := range expected {
|
||||
found := false
|
||||
for _, item := range listView.Items {
|
||||
if item.Text == expItem {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
ea.t.Errorf("expected ListView item %q, not found", expItem)
|
||||
ea.failed = true
|
||||
}
|
||||
}
|
||||
return ea
|
||||
}
|
||||
|
||||
// HasElementInRegion asserts an element exists within the given region bounds.
|
||||
func (ea *ElementAssertions) HasElementInRegion(region ui.Region) *ElementAssertions {
|
||||
found := false
|
||||
for _, elem := range ea.elements {
|
||||
elemRegion := elem.Region()
|
||||
if elemRegion.X >= region.X &&
|
||||
elemRegion.Y >= region.Y &&
|
||||
elemRegion.X+elemRegion.W <= region.X+region.W &&
|
||||
elemRegion.Y+elemRegion.H <= region.Y+region.H {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
ea.t.Errorf("expected element in region %+v, not found", region)
|
||||
ea.failed = true
|
||||
}
|
||||
return ea
|
||||
}
|
||||
|
||||
// HasVisibleElement asserts at least one visible element exists.
|
||||
func (ea *ElementAssertions) HasVisibleElement() *ElementAssertions {
|
||||
found := false
|
||||
for _, elem := range ea.elements {
|
||||
if elem.Visible() {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
ea.t.Error("expected at least one visible element, none found")
|
||||
ea.failed = true
|
||||
}
|
||||
return ea
|
||||
}
|
||||
|
||||
// HasButtonWithText asserts a Button element with the given text exists.
|
||||
func (ea *ElementAssertions) HasButtonWithText(text string) *ElementAssertions {
|
||||
found := false
|
||||
for _, elem := range ea.elements {
|
||||
if btn, ok := elem.(ui.Button); ok {
|
||||
if btn.Text == text {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
ea.t.Errorf("expected Button with text %q, not found", text)
|
||||
ea.failed = true
|
||||
}
|
||||
return ea
|
||||
}
|
||||
|
||||
// HasTextFieldWithID asserts a TextField with the given ID exists.
|
||||
func (ea *ElementAssertions) HasTextFieldWithID(id string) *ElementAssertions {
|
||||
found := false
|
||||
for _, elem := range ea.elements {
|
||||
if tf, ok := elem.(ui.TextField); ok {
|
||||
if tf.ID() == id {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
ea.t.Errorf("expected TextField with ID %q, not found", id)
|
||||
ea.failed = true
|
||||
}
|
||||
return ea
|
||||
}
|
||||
|
||||
// HasIconWithName asserts an Icon with the given name exists.
|
||||
func (ea *ElementAssertions) HasIconWithName(name string) *ElementAssertions {
|
||||
found := false
|
||||
for _, elem := range ea.elements {
|
||||
if icon, ok := elem.(ui.Icon); ok {
|
||||
if icon.Name == name {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
ea.t.Errorf("expected Icon with name %q, not found", name)
|
||||
ea.failed = true
|
||||
}
|
||||
return ea
|
||||
}
|
||||
|
||||
// GetElementByID returns the element with the given ID, or nil if not found.
|
||||
func (ea *ElementAssertions) GetElementByID(id string) ui.Element {
|
||||
for _, elem := range ea.elements {
|
||||
if idElem, ok := elem.(interface{ ID() string }); ok {
|
||||
if idElem.ID() == id {
|
||||
return elem
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetElementByType returns the first element of the given type, or nil if not found.
|
||||
func (ea *ElementAssertions) GetElementByType(t reflect.Type) ui.Element {
|
||||
for _, elem := range ea.elements {
|
||||
if reflect.TypeOf(elem) == t {
|
||||
return elem
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetListItems returns all list item texts from the first ListView found.
|
||||
func (ea *ElementAssertions) GetListItems() []string {
|
||||
for _, elem := range ea.elements {
|
||||
if lv, ok := elem.(ui.ListView); ok {
|
||||
items := make([]string, len(lv.Items))
|
||||
for i, item := range lv.Items {
|
||||
items[i] = item.Text
|
||||
}
|
||||
return items
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetLabelTexts returns all label texts from the frame.
|
||||
func (ea *ElementAssertions) GetLabelTexts() []string {
|
||||
var texts []string
|
||||
for _, elem := range ea.elements {
|
||||
if label, ok := elem.(ui.Label); ok {
|
||||
texts = append(texts, label.Text)
|
||||
}
|
||||
}
|
||||
return texts
|
||||
}
|
||||
|
||||
// GetButtonTexts returns all button texts from the frame.
|
||||
func (ea *ElementAssertions) GetButtonTexts() []string {
|
||||
var texts []string
|
||||
for _, elem := range ea.elements {
|
||||
if btn, ok := elem.(ui.Button); ok {
|
||||
texts = append(texts, btn.Text)
|
||||
}
|
||||
}
|
||||
return texts
|
||||
}
|
||||
|
||||
// GetElementRegions returns all element regions from the frame.
|
||||
func (ea *ElementAssertions) GetElementRegions() []ui.Region {
|
||||
regions := make([]ui.Region, len(ea.elements))
|
||||
for i, elem := range ea.elements {
|
||||
regions[i] = elem.Region()
|
||||
}
|
||||
return regions
|
||||
}
|
||||
|
||||
// Failed returns true if any assertion has failed.
|
||||
func (ea *ElementAssertions) Failed() bool {
|
||||
return ea.failed
|
||||
}
|
||||
|
||||
// --- Helper functions ---
|
||||
|
||||
// regionsOverlap checks if two regions overlap.
|
||||
func regionsOverlap(a, b ui.Region) bool {
|
||||
return !(a.X+a.W <= b.X || b.X+b.W <= a.X ||
|
||||
a.Y+a.H <= b.Y || b.Y+b.H <= a.Y)
|
||||
}
|
||||
|
||||
// AssertNoOverlappingElements asserts that no elements in the frame overlap.
|
||||
func AssertNoOverlappingElements(t testing.TB, frame []ui.Element) {
|
||||
for i := 0; i < len(frame); i++ {
|
||||
for j := i + 1; j < len(frame); j++ {
|
||||
regionA := frame[i].Region()
|
||||
regionB := frame[j].Region()
|
||||
|
||||
if regionsOverlap(regionA, regionB) {
|
||||
t.Errorf("elements %d (%T) and %d (%T) overlap: %+v vs %+v",
|
||||
i, frame[i], j, frame[j], regionA, regionB)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AssertElementPositions asserts that elements are positioned within expected bounds.
|
||||
func AssertElementPositions(t testing.TB, frame []ui.Element, bounds ui.Region) {
|
||||
for i, elem := range frame {
|
||||
region := elem.Region()
|
||||
if region.X < bounds.X || region.Y < bounds.Y ||
|
||||
region.X+region.W > bounds.X+bounds.W ||
|
||||
region.Y+region.H > bounds.Y+bounds.H {
|
||||
t.Errorf("element %d (%T) is outside expected bounds: %+v vs %+v",
|
||||
i, elem, region, bounds)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AssertFrameStructure asserts the basic structure of a frame.
|
||||
func AssertFrameStructure(t testing.TB, frame []ui.Element, expectedCount int) {
|
||||
if len(frame) != expectedCount {
|
||||
t.Errorf("expected %d elements, got %d", expectedCount, len(frame))
|
||||
}
|
||||
}
|
||||
|
||||
// PrintFrame prints all elements in a frame for debugging.
|
||||
func PrintFrame(frame []ui.Element) string {
|
||||
var result string
|
||||
for i, elem := range frame {
|
||||
region := elem.Region()
|
||||
result += fmt.Sprintf(" [%d] %T: region=%+v visible=%v\n",
|
||||
i, elem, region, elem.Visible())
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
// Package e2e provides end-to-end testing utilities for the Pad editor.
|
||||
// It allows testing the logic layer without requiring a Gio display.
|
||||
package e2e
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"pad/internal/ui"
|
||||
)
|
||||
|
||||
// FrameCapture receives frames from the logic layer without Gio.
|
||||
type FrameCapture struct {
|
||||
frames [][]ui.Element
|
||||
mu sync.Mutex
|
||||
closed chan struct{}
|
||||
}
|
||||
|
||||
// NewFrameCapture creates a new frame capture instance.
|
||||
func NewFrameCapture() *FrameCapture {
|
||||
return &FrameCapture{
|
||||
frames: make([][]ui.Element, 0),
|
||||
closed: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
// CaptureFrame receives a frame and stores it for assertion.
|
||||
func (fc *FrameCapture) CaptureFrame(elements []ui.Element) {
|
||||
fc.mu.Lock()
|
||||
defer fc.mu.Unlock()
|
||||
|
||||
// Copy to avoid mutation issues
|
||||
cp := make([]ui.Element, len(elements))
|
||||
copy(cp, elements)
|
||||
fc.frames = append(fc.frames, cp)
|
||||
}
|
||||
|
||||
// GetFrames returns all captured frames (thread-safe).
|
||||
func (fc *FrameCapture) GetFrames() [][]ui.Element {
|
||||
fc.mu.Lock()
|
||||
defer fc.mu.Unlock()
|
||||
|
||||
result := make([][]ui.Element, len(fc.frames))
|
||||
for i, frame := range fc.frames {
|
||||
result[i] = make([]ui.Element, len(frame))
|
||||
copy(result[i], frame)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// FrameCount returns the number of frames captured so far.
|
||||
func (fc *FrameCapture) FrameCount() int {
|
||||
fc.mu.Lock()
|
||||
defer fc.mu.Unlock()
|
||||
return len(fc.frames)
|
||||
}
|
||||
|
||||
// WaitForFrame blocks until at least one frame is captured or timeout.
|
||||
func (fc *FrameCapture) WaitForFrame(timeout time.Duration) ([][]ui.Element, error) {
|
||||
return fc.WaitForFrameCount(1, timeout)
|
||||
}
|
||||
|
||||
// WaitForFrameCount blocks until at least N frames are captured or timeout.
|
||||
func (fc *FrameCapture) WaitForFrameCount(count int, timeout time.Duration) ([][]ui.Element, error) {
|
||||
deadline := time.Now().Add(timeout)
|
||||
|
||||
for {
|
||||
fc.mu.Lock()
|
||||
n := len(fc.frames)
|
||||
fc.mu.Unlock()
|
||||
|
||||
if n >= count {
|
||||
return fc.GetFrames(), nil
|
||||
}
|
||||
|
||||
if time.Now().After(deadline) {
|
||||
return nil, ErrTimeout
|
||||
}
|
||||
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
// Close signals the capture to stop.
|
||||
func (fc *FrameCapture) Close() {
|
||||
close(fc.closed)
|
||||
}
|
||||
|
||||
// ErrTimeout is returned when a wait operation times out.
|
||||
var ErrTimeout = errTimeout{}
|
||||
|
||||
type errTimeout struct{}
|
||||
|
||||
func (errTimeout) Error() string {
|
||||
return "timeout waiting for frames"
|
||||
}
|
||||
|
||||
// IsTimeout returns true if the error is a timeout error.
|
||||
func IsTimeout(err error) bool {
|
||||
_, ok := err.(errTimeout)
|
||||
return ok
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package e2e
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"pad/internal/editor"
|
||||
"pad/internal/ui"
|
||||
)
|
||||
|
||||
// Harness orchestrates the test environment for e2e tests.
|
||||
type Harness struct {
|
||||
logic *editor.Logic
|
||||
capture *FrameCapture
|
||||
logicWg sync.WaitGroup
|
||||
frameReceiverDone chan struct{}
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
// HarnessOption configures the test harness.
|
||||
type HarnessOption func(*Harness)
|
||||
|
||||
// NewHarness creates a test harness with the given options.
|
||||
func NewHarness(opts ...HarnessOption) *Harness {
|
||||
h := &Harness{
|
||||
logic: editor.NewLogic(),
|
||||
capture: NewFrameCapture(),
|
||||
frameReceiverDone: make(chan struct{}),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(h)
|
||||
}
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
// Run starts the logic goroutine and frame capture.
|
||||
func (h *Harness) Run() {
|
||||
h.logicWg.Add(1)
|
||||
h.wg.Add(1)
|
||||
go func() {
|
||||
defer h.wg.Done()
|
||||
defer h.logicWg.Done()
|
||||
h.logic.Run()
|
||||
}()
|
||||
|
||||
h.wg.Add(1)
|
||||
go func() {
|
||||
defer h.wg.Done()
|
||||
for {
|
||||
select {
|
||||
case frame := <-h.logic.FrameChan():
|
||||
h.capture.CaptureFrame(frame)
|
||||
case <-h.frameReceiverDone:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// SendConfig simulates a window config event (resize).
|
||||
func (h *Harness) SendConfig(width, height int) {
|
||||
h.logic.ConfigChan() <- editor.ConfigEvent{
|
||||
PixelWidth: width,
|
||||
PixelHeight: height,
|
||||
}
|
||||
}
|
||||
|
||||
// SendScale simulates a scale factor change.
|
||||
func (h *Harness) SendScale(scale float32) {
|
||||
h.logic.ConfigChan() <- editor.ScaleEvent{Scale: scale}
|
||||
}
|
||||
|
||||
// SendInput simulates user input events.
|
||||
func (h *Harness) SendInput(events []ui.InputEvent) {
|
||||
h.logic.InputChan() <- events
|
||||
}
|
||||
|
||||
// SendSearchQuery simulates a search query update.
|
||||
func (h *Harness) SendSearchQuery(query string) {
|
||||
h.logic.SearchQueryChan() <- query
|
||||
}
|
||||
|
||||
// GetFrames returns all captured frames.
|
||||
func (h *Harness) GetFrames() [][]ui.Element {
|
||||
return h.capture.GetFrames()
|
||||
}
|
||||
|
||||
// FrameCount returns the number of frames captured so far.
|
||||
func (h *Harness) FrameCount() int {
|
||||
return h.capture.FrameCount()
|
||||
}
|
||||
|
||||
// WaitForFrameCount blocks until at least N frames are captured.
|
||||
func (h *Harness) WaitForFrameCount(count int, timeout time.Duration) ([][]ui.Element, error) {
|
||||
return h.capture.WaitForFrameCount(count, timeout)
|
||||
}
|
||||
|
||||
// WaitForFrame blocks until at least one frame is captured.
|
||||
func (h *Harness) WaitForFrame(timeout time.Duration) ([][]ui.Element, error) {
|
||||
return h.capture.WaitForFrame(timeout)
|
||||
}
|
||||
|
||||
// Cleanup stops all goroutines.
|
||||
func (h *Harness) Cleanup() {
|
||||
// 1. Signal logic to stop sending frames
|
||||
h.logic.Done()
|
||||
|
||||
// 2. Wait for logic goroutine to fully exit (no more frameChan sends)
|
||||
h.logicWg.Wait()
|
||||
|
||||
// 3. Now safe to stop the frame receiver
|
||||
close(h.frameReceiverDone)
|
||||
|
||||
// 4. Wait for frame receiver to finish
|
||||
h.wg.Wait()
|
||||
|
||||
h.capture.Close()
|
||||
}
|
||||
|
||||
// DefaultTimeout is the default timeout for waiting operations.
|
||||
const DefaultTimeout = 5 * time.Second
|
||||
@@ -0,0 +1,97 @@
|
||||
package e2e_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"pad/internal/test/e2e"
|
||||
"pad/internal/ui"
|
||||
)
|
||||
|
||||
// TestEditorInitialLayout tests the initial editor layout has expected structure.
|
||||
func TestEditorInitialLayout(t *testing.T) {
|
||||
h := e2e.NewHarnessWithDefaults()
|
||||
defer h.Cleanup()
|
||||
|
||||
frames, err := h.WaitForFrameCount(1, 5*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("timeout waiting for frames: %v", err)
|
||||
}
|
||||
|
||||
lastFrame := frames[len(frames)-1]
|
||||
ea := e2e.NewElementAssertions(t, lastFrame)
|
||||
ea.HasElementCount(3)
|
||||
ea.HasElementOfType(reflect.TypeOf(ui.Container{}))
|
||||
ea.HasElementOfType(reflect.TypeOf(ui.TextField{}))
|
||||
ea.HasElementWithID("editor_text")
|
||||
}
|
||||
|
||||
// TestConfigChangeTriggersNewFrame tests that config changes produce new frames.
|
||||
func TestConfigChangeTriggersNewFrame(t *testing.T) {
|
||||
h := e2e.NewHarnessWithDefaults()
|
||||
defer h.Cleanup()
|
||||
|
||||
_, err := h.WaitForFrameCount(1, 5*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("timeout waiting for initial frame: %v", err)
|
||||
}
|
||||
|
||||
initialCount := h.FrameCount()
|
||||
h.SendConfig(1000, 2000)
|
||||
|
||||
newCount, err := e2e.WaitForNewFrame(h, initialCount, 5*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("timeout waiting for new frame: %v", err)
|
||||
}
|
||||
if newCount <= initialCount {
|
||||
t.Errorf("expected new frame after config change, got %d (was %d)", newCount, initialCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestElementPositionsValid tests that elements have valid regions and no overlaps.
|
||||
func TestElementPositionsValid(t *testing.T) {
|
||||
h := e2e.NewHarnessWithDefaults()
|
||||
defer h.Cleanup()
|
||||
|
||||
frames, err := h.WaitForFrameCount(1, 5*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("timeout waiting for frames: %v", err)
|
||||
}
|
||||
|
||||
lastFrame := frames[len(frames)-1]
|
||||
e2e.AssertNoOverlappingElements(t, lastFrame)
|
||||
e2e.AssertFrameHasNoEmptyRegions(t, lastFrame)
|
||||
e2e.AssertFrameHasNoNegativeRegions(t, lastFrame)
|
||||
e2e.AssertFrameHasNoInvisibleElements(t, lastFrame)
|
||||
|
||||
screenBounds := ui.Region{X: 0, Y: 0, W: 780, H: 1688}
|
||||
e2e.AssertFrameHasNoOutOfBoundsElements(t, lastFrame, screenBounds)
|
||||
}
|
||||
|
||||
// TestHarnessTimeout tests that timeout errors are returned correctly.
|
||||
func TestHarnessTimeout(t *testing.T) {
|
||||
h := e2e.NewHarness()
|
||||
h.Run()
|
||||
defer h.Cleanup()
|
||||
|
||||
// Wait for initial startup frames first
|
||||
_, err := h.WaitForFrameCount(1, 5*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("timeout waiting for initial frames: %v", err)
|
||||
}
|
||||
|
||||
initialCount := h.FrameCount()
|
||||
|
||||
// Now wait for a frame count that won't be reached (no more events sent)
|
||||
_, err = h.WaitForFrameCount(initialCount+10, 100*time.Millisecond)
|
||||
if err == nil {
|
||||
t.Error("expected timeout error, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHarnessCleanup tests that cleanup stops goroutines without deadlock.
|
||||
func TestHarnessCleanup(t *testing.T) {
|
||||
h := e2e.NewHarnessWithDefaults()
|
||||
h.Cleanup()
|
||||
}
|
||||
@@ -0,0 +1,388 @@
|
||||
package e2e
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"pad/internal/ui"
|
||||
)
|
||||
|
||||
// TestScenario represents a complete test scenario with setup, actions, and assertions.
|
||||
type TestScenario struct {
|
||||
Name string
|
||||
Setup func(*Harness)
|
||||
Actions []HarnessAction
|
||||
Assertions []FrameAssertion
|
||||
FrameIndex int // which frame to assert on (-1 for last)
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
// HarnessAction represents an action to perform on the harness.
|
||||
type HarnessAction func(*Harness)
|
||||
|
||||
// FrameAssertion represents an assertion to make on a frame.
|
||||
type FrameAssertion func(*testing.T, []ui.Element)
|
||||
|
||||
// --- Common actions ---
|
||||
|
||||
// ActionSendConfig creates an action to send a config event.
|
||||
func ActionSendConfig(width, height int) HarnessAction {
|
||||
return func(h *Harness) {
|
||||
h.SendConfig(width, height)
|
||||
}
|
||||
}
|
||||
|
||||
// ActionSendScale creates an action to send a scale event.
|
||||
func ActionSendScale(scale float32) HarnessAction {
|
||||
return func(h *Harness) {
|
||||
h.SendScale(scale)
|
||||
}
|
||||
}
|
||||
|
||||
// ActionSendInput creates an action to send input events.
|
||||
func ActionSendInput(events []ui.InputEvent) HarnessAction {
|
||||
return func(h *Harness) {
|
||||
h.SendInput(events)
|
||||
}
|
||||
}
|
||||
|
||||
// ActionSendSearchQuery creates an action to send a search query.
|
||||
func ActionSendSearchQuery(query string) HarnessAction {
|
||||
return func(h *Harness) {
|
||||
h.SendSearchQuery(query)
|
||||
}
|
||||
}
|
||||
|
||||
// ActionWait creates an action to wait for frames.
|
||||
func ActionWait(count int, timeout time.Duration) HarnessAction {
|
||||
return func(h *Harness) {
|
||||
h.WaitForFrameCount(count, timeout)
|
||||
}
|
||||
}
|
||||
|
||||
// --- Common assertions ---
|
||||
|
||||
// AssertionHasElementCount creates an assertion for element count.
|
||||
func AssertionHasElementCount(count int) FrameAssertion {
|
||||
return func(t *testing.T, frame []ui.Element) {
|
||||
NewElementAssertions(t, frame).HasElementCount(count)
|
||||
}
|
||||
}
|
||||
|
||||
// AssertionHasElementWithID creates an assertion for element ID.
|
||||
func AssertionHasElementWithID(id string) FrameAssertion {
|
||||
return func(t *testing.T, frame []ui.Element) {
|
||||
NewElementAssertions(t, frame).HasElementWithID(id)
|
||||
}
|
||||
}
|
||||
|
||||
// AssertionHasLabelWithText creates an assertion for label text.
|
||||
func AssertionHasLabelWithText(text string) FrameAssertion {
|
||||
return func(t *testing.T, frame []ui.Element) {
|
||||
NewElementAssertions(t, frame).HasLabelWithText(text)
|
||||
}
|
||||
}
|
||||
|
||||
// AssertionHasListViewWithItems creates an assertion for list view items.
|
||||
func AssertionHasListViewWithItems(items []string) FrameAssertion {
|
||||
return func(t *testing.T, frame []ui.Element) {
|
||||
NewElementAssertions(t, frame).HasListViewWithItems(items)
|
||||
}
|
||||
}
|
||||
|
||||
// AssertionHasListViewWithItemCount creates an assertion for list view item count.
|
||||
func AssertionHasListViewWithItemCount(count int) FrameAssertion {
|
||||
return func(t *testing.T, frame []ui.Element) {
|
||||
NewElementAssertions(t, frame).HasListViewWithItemCount(count)
|
||||
}
|
||||
}
|
||||
|
||||
// AssertionHasButtonWithText creates an assertion for button text.
|
||||
func AssertionHasButtonWithText(text string) FrameAssertion {
|
||||
return func(t *testing.T, frame []ui.Element) {
|
||||
NewElementAssertions(t, frame).HasButtonWithText(text)
|
||||
}
|
||||
}
|
||||
|
||||
// AssertionHasIconWithName creates an assertion for icon name.
|
||||
func AssertionHasIconWithName(name string) FrameAssertion {
|
||||
return func(t *testing.T, frame []ui.Element) {
|
||||
NewElementAssertions(t, frame).HasIconWithName(name)
|
||||
}
|
||||
}
|
||||
|
||||
// AssertionHasSearchBarWithQuery creates an assertion for search bar query.
|
||||
// NOTE: ui.SearchBar does not implement ui.Element (no Draw method),
|
||||
// so this assertion is currently a no-op placeholder.
|
||||
func AssertionHasSearchBarWithQuery(query string) FrameAssertion {
|
||||
return func(t *testing.T, frame []ui.Element) {
|
||||
// TODO: implement when SearchBar implements ui.Element
|
||||
}
|
||||
}
|
||||
|
||||
// AssertionHasToastWithText creates an assertion for toast text.
|
||||
// NOTE: ui.Toast does not implement ui.Element (no Draw method),
|
||||
// so this assertion is currently a no-op placeholder.
|
||||
func AssertionHasToastWithText(text string) FrameAssertion {
|
||||
return func(t *testing.T, frame []ui.Element) {
|
||||
// TODO: implement when Toast implements ui.Element
|
||||
}
|
||||
}
|
||||
|
||||
// AssertionHasCursorAt creates an assertion for cursor position.
|
||||
// NOTE: ui.Cursor does not implement ui.Element (no Draw method),
|
||||
// so this assertion is currently a no-op placeholder.
|
||||
func AssertionHasCursorAt(line, col int) FrameAssertion {
|
||||
return func(t *testing.T, frame []ui.Element) {
|
||||
// TODO: implement when Cursor implements ui.Element
|
||||
}
|
||||
}
|
||||
|
||||
// AssertionNoOverlappingElements creates an assertion for no overlapping elements.
|
||||
func AssertionNoOverlappingElements() FrameAssertion {
|
||||
return func(t *testing.T, frame []ui.Element) {
|
||||
AssertNoOverlappingElements(t, frame)
|
||||
}
|
||||
}
|
||||
|
||||
// AssertionElementPositions creates an assertion for element positions.
|
||||
func AssertionElementPositions(bounds ui.Region) FrameAssertion {
|
||||
return func(t *testing.T, frame []ui.Element) {
|
||||
AssertElementPositions(t, frame, bounds)
|
||||
}
|
||||
}
|
||||
|
||||
// --- Scenario runner ---
|
||||
|
||||
// RunScenario runs a test scenario.
|
||||
func RunScenario(t *testing.T, scenario TestScenario) {
|
||||
h := NewHarness()
|
||||
h.Run()
|
||||
defer h.Cleanup()
|
||||
|
||||
// Run setup
|
||||
if scenario.Setup != nil {
|
||||
scenario.Setup(h)
|
||||
}
|
||||
|
||||
// Run actions
|
||||
for _, action := range scenario.Actions {
|
||||
action(h)
|
||||
}
|
||||
|
||||
// Wait for frames
|
||||
timeout := scenario.Timeout
|
||||
if timeout == 0 {
|
||||
timeout = DefaultTimeout
|
||||
}
|
||||
|
||||
frames, err := h.WaitForFrameCount(1, timeout)
|
||||
if err != nil {
|
||||
t.Fatalf("timeout waiting for frames: %v", err)
|
||||
}
|
||||
|
||||
// Determine which frame to assert on
|
||||
frameIndex := scenario.FrameIndex
|
||||
if frameIndex == -1 || frameIndex >= len(frames) {
|
||||
frameIndex = len(frames) - 1
|
||||
}
|
||||
|
||||
// Run assertions
|
||||
for _, assertion := range scenario.Assertions {
|
||||
assertion(t, frames[frameIndex])
|
||||
}
|
||||
}
|
||||
|
||||
// --- Helper functions ---
|
||||
|
||||
// WaitForStableFrames waits until no new frames are captured for a period.
|
||||
func WaitForStableFrames(h *Harness, stabilityPeriod time.Duration) error {
|
||||
deadline := time.Now().Add(stabilityPeriod)
|
||||
for time.Now().Before(deadline) {
|
||||
count := h.FrameCount()
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
if h.FrameCount() == count {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("frames not stable after %v", stabilityPeriod)
|
||||
}
|
||||
|
||||
// GetLastFrame returns the last captured frame.
|
||||
func GetLastFrame(h *Harness) []ui.Element {
|
||||
frames := h.GetFrames()
|
||||
if len(frames) == 0 {
|
||||
return nil
|
||||
}
|
||||
return frames[len(frames)-1]
|
||||
}
|
||||
|
||||
// GetFrameByIndex returns a frame by index.
|
||||
func GetFrameByIndex(h *Harness, index int) []ui.Element {
|
||||
frames := h.GetFrames()
|
||||
if index < 0 || index >= len(frames) {
|
||||
return nil
|
||||
}
|
||||
return frames[index]
|
||||
}
|
||||
|
||||
// DebugPrintFrames prints all captured frames for debugging.
|
||||
func DebugPrintFrames(h *Harness) {
|
||||
frames := h.GetFrames()
|
||||
for i, frame := range frames {
|
||||
fmt.Printf("Frame %d (%d elements):\n", i, len(frame))
|
||||
fmt.Print(PrintFrame(frame))
|
||||
}
|
||||
}
|
||||
|
||||
// NewHarnessWithDefaults creates a harness with default configuration.
|
||||
func NewHarnessWithDefaults() *Harness {
|
||||
h := NewHarness()
|
||||
h.Run()
|
||||
h.SendConfig(780, 1688) // 390x844 @ 2x scale
|
||||
h.SendScale(2.0)
|
||||
return h
|
||||
}
|
||||
|
||||
// NewHarnessWithCustomConfig creates a harness with custom configuration.
|
||||
func NewHarnessWithCustomConfig(width, height int, scale float32) *Harness {
|
||||
h := NewHarness()
|
||||
h.Run()
|
||||
h.SendConfig(width, height)
|
||||
h.SendScale(scale)
|
||||
return h
|
||||
}
|
||||
|
||||
// WaitForInitialFrame waits for the initial frame after setup.
|
||||
func WaitForInitialFrame(h *Harness, timeout time.Duration) ([]ui.Element, error) {
|
||||
_, err := h.WaitForFrameCount(1, timeout)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return GetLastFrame(h), nil
|
||||
}
|
||||
|
||||
// WaitForNewFrame waits for a new frame after an action.
|
||||
func WaitForNewFrame(h *Harness, beforeCount int, timeout time.Duration) (int, error) {
|
||||
deadline := time.Now().Add(timeout)
|
||||
for time.Now().Before(deadline) {
|
||||
count := h.FrameCount()
|
||||
if count > beforeCount {
|
||||
return count, nil
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
return beforeCount, fmt.Errorf("no new frames after %v", timeout)
|
||||
}
|
||||
|
||||
// CompareFrameElements compares two frames element by element.
|
||||
func CompareFrameElements(frame1, frame2 []ui.Element) []string {
|
||||
var differences []string
|
||||
|
||||
if len(frame1) != len(frame2) {
|
||||
differences = append(differences, fmt.Sprintf("element count: %d vs %d", len(frame1), len(frame2)))
|
||||
return differences
|
||||
}
|
||||
|
||||
for i := range frame1 {
|
||||
region1 := frame1[i].Region()
|
||||
region2 := frame2[i].Region()
|
||||
|
||||
if region1 != region2 {
|
||||
differences = append(differences, fmt.Sprintf("element %d region: %+v vs %+v", i, region1, region2))
|
||||
}
|
||||
|
||||
if frame1[i].Visible() != frame2[i].Visible() {
|
||||
differences = append(differences, fmt.Sprintf("element %d visible: %v vs %v", i, frame1[i].Visible(), frame2[i].Visible()))
|
||||
}
|
||||
}
|
||||
|
||||
return differences
|
||||
}
|
||||
|
||||
// --- Input event helpers ---
|
||||
|
||||
// CreateTapEvent creates a tap input event.
|
||||
func CreateTapEvent(handler func(any)) ui.InputEvent {
|
||||
return ui.InputEvent{
|
||||
Handler: handler,
|
||||
Data: ui.Interaction{Gesture: ui.Tap},
|
||||
}
|
||||
}
|
||||
|
||||
// CreateScrollEvent creates a scroll input event.
|
||||
func CreateScrollEvent(handler func(any)) ui.InputEvent {
|
||||
return ui.InputEvent{
|
||||
Handler: handler,
|
||||
Data: ui.Interaction{Gesture: ui.Scroll},
|
||||
}
|
||||
}
|
||||
|
||||
// CreateKeyEvent creates a key input event.
|
||||
func CreateKeyEvent(handler func(any)) ui.InputEvent {
|
||||
return ui.InputEvent{
|
||||
Handler: handler,
|
||||
Data: ui.Interaction{Gesture: ui.KeyDown},
|
||||
}
|
||||
}
|
||||
|
||||
// --- Structural assertions ---
|
||||
|
||||
// AssertFrameHasNoInvisibleElements asserts that all elements in the frame are visible.
|
||||
func AssertFrameHasNoInvisibleElements(t *testing.T, frame []ui.Element) {
|
||||
for i, elem := range frame {
|
||||
if !elem.Visible() {
|
||||
t.Errorf("element %d (%T) is invisible", i, elem)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AssertFrameHasNoEmptyRegions asserts that no elements have empty regions.
|
||||
func AssertFrameHasNoEmptyRegions(t *testing.T, frame []ui.Element) {
|
||||
for i, elem := range frame {
|
||||
region := elem.Region()
|
||||
if region.W == 0 || region.H == 0 {
|
||||
t.Errorf("element %d (%T) has empty region: %+v", i, elem, region)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AssertFrameHasNoNegativeRegions asserts that no elements have negative regions.
|
||||
func AssertFrameHasNoNegativeRegions(t *testing.T, frame []ui.Element) {
|
||||
for i, elem := range frame {
|
||||
region := elem.Region()
|
||||
if region.X < 0 || region.Y < 0 || region.W < 0 || region.H < 0 {
|
||||
t.Errorf("element %d (%T) has negative region: %+v", i, elem, region)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AssertFrameHasNoOutOfBoundsElements asserts that no elements are outside the expected bounds.
|
||||
func AssertFrameHasNoOutOfBoundsElements(t *testing.T, frame []ui.Element, bounds ui.Region) {
|
||||
for i, elem := range frame {
|
||||
region := elem.Region()
|
||||
if region.X < bounds.X || region.Y < bounds.Y ||
|
||||
region.X+region.W > bounds.X+bounds.W ||
|
||||
region.Y+region.H > bounds.Y+bounds.H {
|
||||
t.Errorf("element %d (%T) is out of bounds: %+v vs %+v", i, elem, region, bounds)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AssertFrameHasNoDuplicateIDs asserts that no elements have duplicate IDs.
|
||||
func AssertFrameHasNoDuplicateIDs(t *testing.T, frame []ui.Element) {
|
||||
seen := make(map[string]bool)
|
||||
for i, elem := range frame {
|
||||
if idElem, ok := elem.(interface{ ID() string }); ok {
|
||||
id := idElem.ID()
|
||||
if id == "" {
|
||||
continue // skip elements without IDs
|
||||
}
|
||||
if seen[id] {
|
||||
t.Errorf("element %d has duplicate ID %q", i, id)
|
||||
}
|
||||
seen[id] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user