Repository navigation
Expand file tree
/
Copy pathcell_list_test.go
More file actions
92 lines (80 loc) · 3.38 KB
/
Copy pathcell_list_test.go
File metadata and controls
92 lines (80 loc) · 3.38 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"bytes"
"context"
"errors"
"strings"
"testing"
"time"
"github.com/Agent-Field/codeaf/internal/chatlist"
"github.com/Agent-Field/codeaf/internal/wireauth"
)
// withListSource installs a Source for one test.
func withListSource(t *testing.T, src chatlist.Source, err error) {
t.Helper()
old := cellListSource
cellListSource = func() (chatlist.Source, error) { return src, err }
t.Cleanup(func() { cellListSource = old })
}
func TestCellListAll(t *testing.T) {
withListSource(t, chatlist.Static{
{Cell: "01AAA", Title: "Nightly index rebuild", Device: "host-a", Status: chatlist.Running, DurableAgo: 2 * time.Minute},
{Cell: "01BBB", Title: "Port the picker", Device: "host-a", Status: chatlist.Off, DurableAgo: 3 * time.Hour},
{Cell: "01CCC", Title: "Fix the flaky test", Device: "laptop", Status: chatlist.Branch, OrphanTurns: 2, DurableAgo: 5 * time.Hour},
{Cell: "01DDD", Title: "Notes", Device: "laptop", Status: chatlist.Idle, DurableAgo: 26 * time.Hour},
}, nil)
var out bytes.Buffer
if err := runCellIn([]string{"list", "--all"}, &out, t.TempDir()); err != nil {
t.Fatal(err)
}
want := []string{
"01AAA Nightly index rebuild host-a held on host-a 2m",
"01BBB Port the picker host-a host-a offline 3h",
"01CCC Fix the flaky test laptop 2 turns from laptop: discard 5h",
"01DDD Notes laptop - 1d 2h",
}
if got := strings.TrimSpace(out.String()); got != strings.Join(want, "\n") {
t.Fatalf("got:\n%s\nwant:\n%s", got, strings.Join(want, "\n"))
}
}
func TestCellListAllSyncOff(t *testing.T) {
old := cellListSource
t.Cleanup(func() { cellListSource = old })
t.Setenv("CODEAF_SYNC_URL", "off") // an empty value now means the built-in default
err := runCellIn([]string{"list", "--all"}, &bytes.Buffer{}, t.TempDir())
if err == nil || err.Error() != chatlist.SyncOff {
t.Fatalf("err = %v, want %q", err, chatlist.SyncOff)
}
}
type unreachableSource struct{}
func (unreachableSource) Rows(context.Context) ([]chatlist.Row, error) {
return nil, errors.New("no route")
}
func TestCellListAllUnreachable(t *testing.T) {
withListSource(t, unreachableSource{}, nil)
err := runCellIn([]string{"list", "--all"}, &bytes.Buffer{}, t.TempDir())
if err == nil || !strings.HasPrefix(err.Error(), chatlist.Unreachable) {
t.Fatalf("err = %v", err)
}
}
func TestCellListNeedsAll(t *testing.T) {
for _, args := range [][]string{{"list"}, {"list", "--some"}, {"list", "--all", "x"}} {
if err := runCellIn(args, &bytes.Buffer{}, t.TempDir()); err == nil || !strings.Contains(err.Error(), "usage") {
t.Errorf("%v: err = %v, want usage", args, err)
}
}
}
// errSource is a list that fails the way a relay that replaced the identity does.
type errSource struct{ err error }
func (e errSource) Rows(context.Context) ([]chatlist.Row, error) { return nil, e.err }
// A computer on an identity that was rotated is told what happened and what to
// do, not that other machines are unreachable.
func TestCellListOnAReplacedIdentity(t *testing.T) {
for cause, want := range map[error]string{wireauth.ErrGone: chatlist.ReplacedGone, wireauth.ErrRotated: chatlist.Replaced} {
withListSource(t, errSource{cause}, nil)
err := runCellIn([]string{"list", "--all"}, &bytes.Buffer{}, t.TempDir())
if err == nil || err.Error() != want {
t.Errorf("%v: got %v, want %q", cause, err, want)
}
}
}