▍ humdrum codex / dots v1.0.0
1.8 KB raw
 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
package cli

import (
	"strings"
	"testing"

	"github.com/humdrum-tiv/dots-cli/internal/day"
)

func sp(s string) *string { return &s }

func TestFormatChanges(t *testing.T) {
	names := map[string]string{"t1": "archer", "t2": "foofaraw"}
	changes := []day.Change{
		{Index: 29, FromID: sp("t1"), ToID: sp("t2")},
		{Index: 30, FromID: sp("t1"), ToID: nil},
	}
	out := FormatChanges(changes, names, 30)
	if !strings.Contains(out, "14:30") || !strings.Contains(out, "archer → foofaraw") {
		t.Fatalf("out %q", out)
	}
	if !strings.Contains(out, "15:00") || !strings.Contains(out, "archer → (cleared)") {
		t.Fatalf("out %q", out)
	}
}

func TestConfirm(t *testing.T) {
	// --yes bypasses everything
	ok, err := Confirm("overwrite?", true, strings.NewReader(""), &strings.Builder{}, false)
	if err != nil || !ok {
		t.Fatalf("yes flag: ok=%v err=%v", ok, err)
	}
	// non-TTY without --yes = hard error
	if _, err := Confirm("overwrite?", false, strings.NewReader("y\n"), &strings.Builder{}, false); err == nil {
		t.Fatal("non-TTY without --yes must error")
	}
	// TTY: y accepts, anything else declines
	ok, _ = Confirm("overwrite?", false, strings.NewReader("y\n"), &strings.Builder{}, true)
	if !ok {
		t.Fatal("y should accept")
	}
	ok, _ = Confirm("overwrite?", false, strings.NewReader("n\n"), &strings.Builder{}, true)
	if ok {
		t.Fatal("n should decline")
	}
	ok, _ = Confirm("overwrite?", false, strings.NewReader("\n"), &strings.Builder{}, true)
	if ok {
		t.Fatal("empty should decline (default N)")
	}
	// y without newline (EOF) should accept
	ok, _ = Confirm("overwrite?", false, strings.NewReader("y"), &strings.Builder{}, true)
	if !ok {
		t.Fatal("y without newline should accept")
	}
	// immediate EOF (empty input) should decline
	ok, _ = Confirm("overwrite?", false, strings.NewReader(""), &strings.Builder{}, true)
	if ok {
		t.Fatal("immediate EOF should decline")
	}
}