summaryrefslogtreecommitdiff
path: root/internal/tui/flamegraph/animation_test.go
blob: 94272e26a67a4a4e5dd418297ac5e67b9ccfa747 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package flamegraph

import "testing"

func TestAnimationStateConvergesToTarget(t *testing.T) {
	state := NewAnimationState(30, 6.0, 1.0)
	state.SetTargets([]tuiFrame{{Path: "root", Col: 0, Width: 10}})
	state.SetTargets([]tuiFrame{{Path: "root", Col: 100, Width: 50}})

	active := true
	for i := 0; i < 180 && active; i++ {
		active = state.Tick(0)
	}
	if active {
		t.Fatalf("expected springs to settle within 180 ticks")
	}

	frames := state.CurrentFrames()
	if len(frames) != 1 {
		t.Fatalf("expected one interpolated frame, got %d", len(frames))
	}
	if frames[0].Col != 100 || frames[0].Width != 50 {
		t.Fatalf("expected settled frame at col=100 width=50, got col=%d width=%d", frames[0].Col, frames[0].Width)
	}
	if state.Tick(0) {
		t.Fatalf("expected settled animation to remain inactive")
	}
}

func TestAnimationStateHandlesAddedAndRemovedFrames(t *testing.T) {
	state := NewAnimationState(30, 6.0, 1.0)
	state.SetTargets([]tuiFrame{
		{Path: "root", Col: 0, Width: 20},
		{Path: "root\x1fchild", Col: 20, Width: 20},
	})
	if got := len(state.CurrentFrames()); got != 2 {
		t.Fatalf("expected 2 frames after initial targets, got %d", got)
	}

	state.SetTargets([]tuiFrame{
		{Path: "root\x1fchild", Col: 40, Width: 30},
	})
	frames := state.CurrentFrames()
	if len(frames) != 1 {
		t.Fatalf("expected removed frame to be dropped, got %d frames", len(frames))
	}
	if frames[0].Path != "root\x1fchild" {
		t.Fatalf("expected remaining frame path root\\x1fchild, got %q", frames[0].Path)
	}
}