Repository navigation
Expand file tree
/
Copy pathcli_test.go
More file actions
177 lines (167 loc) · 5.77 KB
/
Copy pathcli_test.go
File metadata and controls
177 lines (167 loc) · 5.77 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"errors"
"path/filepath"
"reflect"
"strings"
"testing"
)
func TestRoute(t *testing.T) {
cases := []struct {
args []string
cmd string
rest []string
}{
{nil, "help", nil},
{[]string{"-h"}, "help", nil},
{[]string{"--version"}, "version", nil},
{[]string{"-root", "/x", "-listen", ":80"}, "serve", []string{"-root", "/x", "-listen", ":80"}},
{[]string{"install", "--lan"}, "install", []string{"--lan"}},
{[]string{"demo"}, "demo", []string{}},
}
for _, c := range cases {
cmd, rest := route(c.args)
if cmd != c.cmd || len(rest) != len(c.rest) || (len(rest) > 0 && !reflect.DeepEqual(rest, c.rest)) {
t.Errorf("route(%q) = %s %q, want %s %q", c.args, cmd, rest, c.cmd, c.rest)
}
}
}
func TestServiceURL(t *testing.T) {
cases := []struct{ listen, mdns, want string }{
{"127.0.0.1:4777", "", "http://localhost:4777"},
{":80", "agentyard", "http://agentyard.local"},
{":8080", "yard", "http://yard.local:8080"},
{"[::]:80", "", "http://localhost"},
{"0.0.0.0:4777", "", "http://localhost:4777"},
{"192.168.1.5:4777", "", "http://192.168.1.5:4777"},
}
for _, c := range cases {
if got := serviceURL(c.listen, c.mdns); got != c.want {
t.Errorf("serviceURL(%q, %q) = %s, want %s", c.listen, c.mdns, got, c.want)
}
}
if got := probeAddr(":80"); got != "127.0.0.1:80" {
t.Errorf("probeAddr(:80) = %s", got)
}
}
func testConfig() installConfig {
return installConfig{
Exe: "/opt/homebrew/bin/agentyard",
Root: "/Users/ada/R&D <repos>",
Listen: ":80",
Name: "agentyard",
ClaudeDir: "/Users/ada/.claude",
CodexDir: "/Users/ada/.codex",
Path: "/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin",
Log: "/Users/ada/Library/Logs/agentyard.log",
}
}
func TestRenderPlist(t *testing.T) {
c := testConfig()
p := renderPlist(c)
for _, want := range []string{
"<string>" + agentLabel + "</string>",
"<string>/opt/homebrew/bin/agentyard</string>\n <string>serve</string>",
"<string>/Users/ada/R&D <repos></string>",
"<string>-listen</string>\n <string>:80</string>",
"<string>-mdns</string>\n <string>agentyard</string>",
"<string>-sessions-lan=false</string>",
"<key>GOMEMLIMIT</key>\n <string>32MiB</string>",
"<key>KeepAlive</key>\n <true/>",
"<key>RunAtLoad</key>\n <true/>",
"<key>ProcessType</key>\n <string>Background</string>",
"<key>Nice</key>\n <integer>10</integer>",
"<string>/Users/ada/Library/Logs/agentyard.log</string>",
} {
if !strings.Contains(p, want) {
t.Errorf("plist lacks %q\n%s", want, p)
}
}
if strings.Contains(p, "R&D") {
t.Error("plist has an unescaped &")
}
}
func TestPlistRoundTrip(t *testing.T) {
for _, c := range []installConfig{testConfig(), {Exe: "/x/agentyard", Root: "/r", Listen: "127.0.0.1:4777", SessionsLAN: true}} {
args, err := plistArgs([]byte(renderPlist(c)))
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(args, c.args()) {
t.Fatalf("args round trip:\n got %q\nwant %q", args, c.args())
}
back, ok := configFromArgs(args)
c.Path, c.Log = "", ""
if !ok || back != c {
t.Fatalf("config round trip: %+v, want %+v", back, c)
}
if back.URL() != c.URL() {
t.Fatalf("URL changed: %s", back.URL())
}
}
if _, ok := configFromArgs([]string{"/x/worktreesd", "-root", "/r"}); ok {
t.Error("a pre-agentyard plist was taken for an agentyard install")
}
}
func TestInstallConfigLabels(t *testing.T) {
c := testConfig()
if !c.LAN() || c.URL() != "http://agentyard.local" {
t.Errorf("LAN config: lan=%v url=%s", c.LAN(), c.URL())
}
c.Listen, c.Name = "127.0.0.1:4777", ""
if c.LAN() || c.URL() != "http://localhost:4777" || c.sessionsLabel() != "this Mac only" {
t.Errorf("local config: lan=%v url=%s sessions=%s", c.LAN(), c.URL(), c.sessionsLabel())
}
}
func TestAppVersion(t *testing.T) {
defer func(v string) { version = v }(version)
version = "1.2.3"
if appVersion() != "1.2.3" {
t.Errorf("ldflags version ignored: %s", appVersion())
}
version = ""
if v := appVersion(); v == "" {
t.Error("empty version")
}
}
// agentyard remove is the copyable command: it re-checks, so a worktree that
// came into use after the dashboard printed the command is refused.
func TestCLIRemove(t *testing.T) {
base, repo := gitRepo(t)
busy, free := filepath.Join(base, "wt", "busy"), filepath.Join(base, "wt", "free")
gitT(t, repo, "worktree", "add", "-q", "-b", "busy", busy)
gitT(t, repo, "worktree", "add", "-q", "-b", "free", free)
s := &scanner{sizes: &sizeCache{m: map[string]sizeEntry{}}, repos: []string{repo}, cycle: 1}
s.scan() // saved to the cache, which remove reads when no agent answers
sleep := startSleep(t, busy)
err := runRemove([]string{"-listen", "127.0.0.1:1", busy, free})
if err != nil {
t.Fatal(err)
}
if !exists(busy) || exists(free) {
t.Fatalf("busy kept=%v, free removed=%v", exists(busy), !exists(free))
}
sleep.Process.Kill()
sleep.Wait()
if err := runRemove([]string{"-listen", "127.0.0.1:1", "-dry-run", busy}); err != nil || !exists(busy) {
t.Fatalf("dry run: %v", err)
}
if err := runRemove([]string{"-listen", "127.0.0.1:1"}); !errors.Is(err, errUsage) {
t.Fatalf("no paths: %v", err)
}
}
// Paths are taken as typed: relative to here, never cleaned.
func TestCLIRemovePaths(t *testing.T) {
base, repo := gitRepo(t)
wt := filepath.Join(base, "wt", "rel")
gitT(t, repo, "worktree", "add", "-q", "-b", "rel", wt)
s := &scanner{sizes: &sizeCache{m: map[string]sizeEntry{}}, repos: []string{repo}, cycle: 1}
s.scan()
t.Chdir(filepath.Join(base, "wt"))
if err := runRemove([]string{"-listen", "127.0.0.1:1", "-dry-run", "x/../rel"}); err == nil {
t.Fatal("a path with .. was accepted")
}
if err := runRemove([]string{"-listen", "127.0.0.1:1", "rel"}); err != nil || exists(wt) {
t.Fatalf("relative path: %v, still there=%v", err, exists(wt))
}
}