-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner_test.go
More file actions
353 lines (326 loc) · 9.21 KB
/
Copy pathrunner_test.go
File metadata and controls
353 lines (326 loc) · 9.21 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package main
import (
"fmt"
"net"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
func TestMain(m *testing.M) {
if len(os.Args) > 1 && os.Args[1] == "_serve" {
if err := runRunner(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "_serve: %v\n", err)
os.Exit(1)
}
os.Exit(0)
}
os.Exit(m.Run())
}
func TestIsTestExecutable(t *testing.T) {
tests := []struct {
path string
want bool
}{
{"/tmp/gander.test", true},
{"/tmp/gander.test.exe", true},
{"/usr/local/bin/gander", false},
{"/tmp/gander", false},
}
for _, tc := range tests {
if got := isTestExecutable(tc.path); got != tc.want {
t.Errorf("isTestExecutable(%q) = %v, want %v", tc.path, got, tc.want)
}
}
}
func TestEnsureRunnerRefusesTestBinary(t *testing.T) {
home := t.TempDir()
_, err := ensureRunner(home)
if err == nil {
t.Fatal("expected refusal to spawn the test binary as runner")
}
if !strings.Contains(err.Error(), "test binary") {
t.Errorf("got %v, want test-binary refusal", err)
}
}
func TestWatchManagerLoadRefusesWideMode(t *testing.T) {
home := t.TempDir()
path := filepath.Join(home, watchesFileName)
body := `{"version":1,"daemon_token":"abc","port":7821,"watches":[]}`
if err := os.WriteFile(path, []byte(body), 0644); err != nil {
t.Fatal(err)
}
m := newWatchManager(home)
err := m.load()
if err == nil {
t.Fatalf("expected refusal to load watches.json with mode 0644")
}
if !strings.Contains(err.Error(), "refusing to load") {
t.Errorf("error %q did not mention refusal reason", err)
}
}
func TestWatchManagerLoadAcceptsStrictMode(t *testing.T) {
home := t.TempDir()
path := filepath.Join(home, writesFileName())
_ = path
body := `{"version":1,"daemon_token":"abc","port":7821,"watches":[]}`
if err := os.WriteFile(filepath.Join(home, watchesFileName), []byte(body), 0600); err != nil {
t.Fatal(err)
}
m := newWatchManager(home)
if err := m.load(); err != nil {
t.Fatalf("unexpected load error: %v", err)
}
if m.daemonToken != "abc" {
t.Errorf("daemon token not loaded; got %q", m.daemonToken)
}
if m.port != 7821 {
t.Errorf("port not loaded; got %d", m.port)
}
}
func TestWatchManagerPersistProduces0600File(t *testing.T) {
home := t.TempDir()
m := newWatchManager(home)
if err := m.ensureDaemonToken(); err != nil {
t.Fatal(err)
}
m.port = 7821
if err := m.persist(); err != nil {
t.Fatal(err)
}
st, err := os.Stat(filepath.Join(home, watchesFileName))
if err != nil {
t.Fatal(err)
}
if mode := st.Mode().Perm(); mode != 0600 {
t.Errorf("watches.json mode = %04o, want 0600", mode)
}
}
func TestWatchManagerRegisterWritesTokenAndURL(t *testing.T) {
home := t.TempDir()
m := newWatchManager(home)
if err := m.ensureDaemonToken(); err != nil {
t.Fatal(err)
}
m.port = 7821
doc := filepath.Join(t.TempDir(), "doc.md")
if err := os.WriteFile(doc, []byte("# hello\n"), 0644); err != nil {
t.Fatal(err)
}
info, err := m.register(doc, "local", shareRef{})
if err != nil {
t.Fatal(err)
}
if info.Token == "" {
t.Error("expected token on returned info")
}
if !strings.HasPrefix(info.URL, "http://127.0.0.1:7821/w/") {
t.Errorf("URL = %q, expected /w/ prefix", info.URL)
}
if !strings.Contains(info.URL, "?t="+info.Token) {
t.Errorf("URL %q does not carry token %q", info.URL, info.Token)
}
}
func TestWatchManagerReRegisterReturnsExisting(t *testing.T) {
home := t.TempDir()
m := newWatchManager(home)
if err := m.ensureDaemonToken(); err != nil {
t.Fatal(err)
}
m.port = 7821
doc := filepath.Join(t.TempDir(), "doc.md")
if err := os.WriteFile(doc, []byte("# hello\n"), 0644); err != nil {
t.Fatal(err)
}
first, err := m.register(doc, "local", shareRef{})
if err != nil {
t.Fatal(err)
}
second, err := m.register(doc, "local", shareRef{})
if err != nil {
t.Fatal(err)
}
if first.ID != second.ID {
t.Errorf("reregister returned a different id: %s vs %s", first.ID, second.ID)
}
if first.Token != second.Token {
t.Errorf("reregister returned a different token")
}
}
// writesFileName exists so the table reads naturally; it just returns the
// constant used by watchManager.
func writesFileName() string { return watchesFileName }
func TestRunnerHTTPRejectsRequestWithoutToken(t *testing.T) {
home := t.TempDir()
m := newWatchManager(home)
if err := m.ensureDaemonToken(); err != nil {
t.Fatal(err)
}
m.port = 7821
h, err := newRunnerHTTPOnPort(m, 0)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { h.shutdown() })
tests := []struct {
name string
path string
want int
expect string
}{
{"healthz-no-token", "/healthz", http.StatusForbidden, "forbidden"},
{"healthz-with-token", "/healthz?t=" + m.daemonToken, http.StatusOK, ""},
{"watch-bogus-id", "/w/nope?t=irrelevant", http.StatusNotFound, ""},
{"watch-bogus-id-no-token", "/w/nope", http.StatusNotFound, ""},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, tc.path, nil)
w := httptest.NewRecorder()
h.srv.Handler.ServeHTTP(w, r)
if w.Code != tc.want {
t.Errorf("got %d, want %d (body=%q)", w.Code, tc.want, w.Body.String())
}
if tc.expect != "" && !strings.Contains(w.Body.String(), tc.expect) {
t.Errorf("body %q does not contain %q", w.Body.String(), tc.expect)
}
})
}
}
func TestRunnerHTTPRejectsBadWatchToken(t *testing.T) {
home := t.TempDir()
m := newWatchManager(home)
if err := m.ensureDaemonToken(); err != nil {
t.Fatal(err)
}
m.port = 7821
// Register a real watch.
doc := filepath.Join(t.TempDir(), "doc.md")
if err := os.WriteFile(doc, []byte("# body\n"), 0644); err != nil {
t.Fatal(err)
}
info, err := m.register(doc, "local", shareRef{})
if err != nil {
t.Fatal(err)
}
m.mu.Lock()
e := m.entries[info.ID]
e.state = mustStateForTest(t, doc)
m.mu.Unlock()
h, err := newRunnerHTTPOnPort(m, 0)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { h.shutdown() })
r := httptest.NewRequest(http.MethodGet, "/w/"+info.ID+"?t=wrong-token", nil)
w := httptest.NewRecorder()
h.srv.Handler.ServeHTTP(w, r)
if w.Code != http.StatusForbidden {
t.Errorf("wrong-token request: got %d, want 403 (body=%q)", w.Code, w.Body.String())
}
r2 := httptest.NewRequest(http.MethodGet, "/w/"+info.ID+"?t="+info.Token, nil)
w2 := httptest.NewRecorder()
h.srv.Handler.ServeHTTP(w2, r2)
if w2.Code != http.StatusOK {
t.Errorf("valid-token request: got %d, want 200 (body=%q)", w2.Code, w2.Body.String())
}
}
func mustStateForTest(t *testing.T, path string) *watchState {
t.Helper()
body, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
contentHTML, headings := renderMarkdownWithIDs(string(body))
html := []byte(buildHTML(contentHTML, headings, true))
hash := hashBytes(body)
return newWatchState(path, html, contentHTML, headings, hash)
}
func TestWatchManagerLoadRestoresShareRef(t *testing.T) {
home := t.TempDir()
body := `{
"version": 1,
"daemon_token": "abc",
"port": 7821,
"watches": [{
"id": "0cfb59c4",
"path": "/tmp/doc.md",
"mode": "share",
"token": "tok",
"started_at": "2026-08-27T17:00:00Z",
"share": {"uuid": "u-1", "short_id": "TqcYKLEp", "url": "http://127.0.0.1:7331/s/TqcYKLEp"}
}]
}`
if err := os.WriteFile(filepath.Join(home, watchesFileName), []byte(body), 0600); err != nil {
t.Fatal(err)
}
m := newWatchManager(home)
if err := m.load(); err != nil {
t.Fatalf("load: %v", err)
}
e, ok := m.entries["0cfb59c4"]
if !ok {
t.Fatal("watch not loaded")
}
if e.info.ShortID != "TqcYKLEp" {
t.Errorf("ShortID = %q, want TqcYKLEp", e.info.ShortID)
}
if e.info.UUID != "u-1" {
t.Errorf("UUID = %q, want u-1", e.info.UUID)
}
if e.info.ShareURL != "http://127.0.0.1:7331/s/TqcYKLEp" {
t.Errorf("ShareURL = %q", e.info.ShareURL)
}
}
func TestStopByIDMatchesShortID(t *testing.T) {
home := t.TempDir()
m := newWatchManager(home)
if err := m.ensureDaemonToken(); err != nil {
t.Fatal(err)
}
doc := filepath.Join(t.TempDir(), "doc.md")
if err := os.WriteFile(doc, []byte("# hi\n"), 0644); err != nil {
t.Fatal(err)
}
info, err := m.register(doc, "share", shareRef{
UUID: "u-1", ShortID: "TqcYKLEp", URL: "http://127.0.0.1:7331/s/TqcYKLEp",
})
if err != nil {
t.Fatal(err)
}
if !m.stopByID("TqcYKLEp") {
t.Fatal("stopByID(shortID) returned false")
}
if _, ok := m.entries[info.ID]; ok {
t.Error("watch still registered after stop by short id")
}
}
func TestNewRunnerHTTPFallsBackWhenPortBusy(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer ln.Close()
busy := ln.Addr().(*net.TCPAddr).Port
m := newWatchManager(t.TempDir())
h, err := newRunnerHTTPOnPort(m, busy)
if err != nil {
t.Fatalf("expected fallback listen, got %v", err)
}
t.Cleanup(func() { h.shutdown() })
if m.port == busy {
t.Errorf("bound the busy port %d; want a fallback port", busy)
}
if m.port == 0 {
t.Error("mgr.port was not updated to the fallback port")
}
}
// TestIPCRoundTripDisallowUnknownFields is intentionally a no-op: the
// json.Decoder on the daemon side is configured with
// DisallowUnknownFields() and the policy is covered end-to-end by the
// smoke flow. This stub documents the contract and is a placeholder for
// a future in-process server test once runnerIPC exposes a Start-able
// listener.
func TestIPCRoundTripDisallowUnknownFields(t *testing.T) {}