summaryrefslogtreecommitdiff
path: root/internal/tui/export/model_test.go
blob: 2d474359856ad796add87292cd9310788af44d53 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package export

import (
	"errors"
	"strings"
	"testing"

	tea "charm.land/bubbletea/v2"
)

func TestOpenAndClose(t *testing.T) {
	m := NewModel().Open()
	if !m.Visible() {
		t.Fatalf("expected modal to be visible after Open")
	}
	m = m.Close()
	if m.Visible() {
		t.Fatalf("expected modal to be hidden after Close")
	}
}

func TestEnterEmitsRequest(t *testing.T) {
	m := NewModel().Open()
	next, cmd := m.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
	if cmd == nil {
		t.Fatalf("expected request command on enter")
	}
	if !next.exporting {
		t.Fatalf("expected exporting state after enter")
	}
	req, ok := cmd().(RequestMsg)
	if !ok {
		t.Fatalf("expected RequestMsg from enter command")
	}
	if req.Option != OptionCSV {
		t.Fatalf("expected CSV as default export option, got %v", req.Option)
	}
}

func TestCancelOptionCloses(t *testing.T) {
	m := NewModel().Open()
	m.selected = len(optionValues) - 1
	next, cmd := m.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
	if cmd != nil {
		t.Fatalf("expected no command when selecting cancel")
	}
	if next.Visible() {
		t.Fatalf("expected modal to close on cancel option")
	}
}

func TestStatusMessages(t *testing.T) {
	m := NewModel().Open()
	m.exporting = true

	next, _ := m.Update(CompletedMsg{Path: "out.csv"})
	if next.exporting {
		t.Fatalf("expected exporting=false after completion")
	}
	if !strings.Contains(next.status, "out.csv") {
		t.Fatalf("expected completion path in status")
	}

	next.exporting = true
	next, _ = next.Update(FailedMsg{Err: errors.New("boom")})
	if next.exporting {
		t.Fatalf("expected exporting=false after failure")
	}
	if !strings.Contains(next.status, "boom") {
		t.Fatalf("expected failure reason in status")
	}
}