-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
410 lines (375 loc) · 13.2 KB
/
Copy pathconfig_test.go
File metadata and controls
410 lines (375 loc) · 13.2 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package main
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func writeConfig(t *testing.T, cfg Config) string {
t.Helper()
dir := t.TempDir()
path := filepath.Join(dir, "config.json")
raw, err := json.Marshal(cfg)
if err != nil {
t.Fatalf("marshal: %v", err)
}
if err := os.WriteFile(path, raw, 0o600); err != nil {
t.Fatalf("write: %v", err)
}
return path
}
func TestResolveAccountDefaults(t *testing.T) {
cfg := &Config{Accounts: map[string]Account{
"default": {JID: "pi@chat.example.com", Password: "pw", Owner: "zach@chat.example.com"},
}}
got, err := resolveAccount(cfg, "")
if err != nil {
t.Fatalf("resolveAccount: %v", err)
}
if got.Service != "chat.example.com:5222" {
t.Errorf("Service = %q, want chat.example.com:5222", got.Service)
}
if got.Resource != "pi-msg" {
t.Errorf("Resource = %q, want pi-msg", got.Resource)
}
if got.Nick != "pi" {
t.Errorf("Nick = %q, want pi", got.Nick)
}
if got.RoomTrigger != "pi" {
t.Errorf("RoomTrigger = %q, want pi", got.RoomTrigger)
}
if got.RoomMode() {
t.Error("RoomMode() = true, want false (no room set)")
}
}
func TestResolveAccountRoomMode(t *testing.T) {
cfg := &Config{Accounts: map[string]Account{
"default": {
JID: "pi@chat.example.com", Password: "pw", Owner: "zach@chat.example.com",
Room: roomList{"team@muc.chat.example.com"}, Nick: "botpi",
},
}}
got, err := resolveAccount(cfg, "")
if err != nil {
t.Fatalf("resolveAccount: %v", err)
}
if !got.RoomMode() {
t.Error("RoomMode() = false, want true")
}
if len(got.Rooms) != 1 || got.Rooms[0] != "team@muc.chat.example.com" {
t.Errorf("Rooms = %v, want [team@muc.chat.example.com]", got.Rooms)
}
if got.Nick != "botpi" {
t.Errorf("Nick = %q, want botpi", got.Nick)
}
if got.RoomTrigger != "botpi" {
t.Errorf("RoomTrigger defaults to Nick: got %q, want botpi", got.RoomTrigger)
}
}
func TestResolveAccountPingInterval(t *testing.T) {
base := func(pi string) *Config {
return &Config{Accounts: map[string]Account{
"default": {JID: "a@x.com", Password: "p", Owner: "o@x.com", PingInterval: pi},
}}
}
// Unset → default cadence.
got, err := resolveAccount(base(""), "")
if err != nil {
t.Fatalf("resolveAccount: %v", err)
}
if got.PingInterval != defaultPingInterval {
t.Errorf("default PingInterval = %s, want %s", got.PingInterval, defaultPingInterval)
}
// Explicit duration string is parsed.
got, err = resolveAccount(base("2m"), "")
if err != nil {
t.Fatalf("resolveAccount: %v", err)
}
if got.PingInterval != 2*time.Minute {
t.Errorf("PingInterval = %s, want 2m", got.PingInterval)
}
// "0" disables keepalive.
got, err = resolveAccount(base("0"), "")
if err != nil {
t.Fatalf("resolveAccount: %v", err)
}
if got.PingInterval != 0 {
t.Errorf("PingInterval = %s, want 0", got.PingInterval)
}
// Garbage is a config error.
if _, err := resolveAccount(base("soon"), ""); err == nil {
t.Error("expected error for invalid pingInterval, got nil")
}
}
func TestResolveAccountSelection(t *testing.T) {
cfg := &Config{Accounts: map[string]Account{
"default": {JID: "a@x.com", Password: "p", Owner: "o@x.com"},
"work": {JID: "b@x.com", Password: "p", Owner: "o@x.com"},
}}
got, err := resolveAccount(cfg, "work")
if err != nil {
t.Fatalf("resolveAccount: %v", err)
}
if got.Name != "work" || got.JID != "b@x.com" {
t.Errorf("selected %q/%q, want work/b@x.com", got.Name, got.JID)
}
// Unknown requested falls back to default.
got, err = resolveAccount(cfg, "nope")
if err != nil {
t.Fatalf("resolveAccount fallback: %v", err)
}
if got.Name != "default" {
t.Errorf("fallback selected %q, want default", got.Name)
}
}
func TestResolveAccountMissingFields(t *testing.T) {
cfg := &Config{Accounts: map[string]Account{
"default": {JID: "a@x.com"},
}}
if _, err := resolveAccount(cfg, ""); err == nil {
t.Fatal("expected error for missing password/owner, got nil")
}
}
func TestLoadConfigMissing(t *testing.T) {
_, err := loadConfig(filepath.Join(t.TempDir(), "nope.json"))
if err == nil {
t.Fatal("expected errNoConfig, got nil")
}
}
func TestLoadConfigRoundTrip(t *testing.T) {
path := writeConfig(t, Config{Accounts: map[string]Account{
"default": {JID: "a@x.com", Password: "p", Owner: "o@x.com"},
}})
cfg, err := loadConfig(path)
if err != nil {
t.Fatalf("loadConfig: %v", err)
}
if _, ok := cfg.Accounts["default"]; !ok {
t.Error("default account not loaded")
}
}
func TestRoomConfigParsing(t *testing.T) {
// "room" accepts a single string...
var single Config
if err := json.Unmarshal([]byte(`{"accounts":{"default":{"room":"a@muc.x"}}}`), &single); err != nil {
t.Fatalf("string form: %v", err)
}
if got := []string(single.Accounts["default"].Room); len(got) != 1 || got[0] != "a@muc.x" {
t.Errorf("string form Room = %v, want [a@muc.x]", got)
}
// ...or an array of strings.
var multi Config
if err := json.Unmarshal([]byte(`{"accounts":{"default":{"room":["a@muc.x","b@muc.x"]}}}`), &multi); err != nil {
t.Fatalf("array form: %v", err)
}
if got := []string(multi.Accounts["default"].Room); len(got) != 2 || got[1] != "b@muc.x" {
t.Errorf("array form Room = %v, want [a@muc.x b@muc.x]", got)
}
// resolveAccount dedupes/cleans and drives RoomMode + multiple Rooms.
got, err := resolveAccount(&Config{Accounts: map[string]Account{
"default": {JID: "pi@x", Password: "p", Owner: "o@x",
Room: roomList{"a@muc.x", " a@muc.x ", "b@muc.x", ""}},
}}, "")
if err != nil {
t.Fatalf("resolveAccount: %v", err)
}
if len(got.Rooms) != 2 || got.Rooms[0] != "a@muc.x" || got.Rooms[1] != "b@muc.x" {
t.Errorf("resolved Rooms = %v, want [a@muc.x b@muc.x]", got.Rooms)
}
}
func TestResolveAccountErrorRoom(t *testing.T) {
got, err := resolveAccount(&Config{Accounts: map[string]Account{
"default": {JID: "pi@x", Password: "p", Owner: "o@x",
ErrorRoom: " errors@muc.x "},
}}, "")
if err != nil {
t.Fatalf("resolveAccount: %v", err)
}
if got.ErrorRoom != "errors@muc.x" {
t.Errorf("ErrorRoom = %q, want %q", got.ErrorRoom, "errors@muc.x")
}
}
func TestSessionStateRoundTrip(t *testing.T) {
dir := t.TempDir()
cfg := filepath.Join(dir, "config.json")
t.Setenv("PI_MSG_CONFIG", cfg)
if got := loadSessionState("slippy"); got != "" {
t.Fatalf("no state saved, got %q", got)
}
var logged []string
logf := func(level, msg string) { logged = append(logged, level+": "+msg) }
saveSessionState(logf, "slippy", "/some/path/session.jsonl")
if got := loadSessionState("slippy"); got != "/some/path/session.jsonl" {
t.Errorf("after save, load = %q", got)
}
if len(logged) != 0 {
t.Errorf("unexpected warnings: %v", logged)
}
// Per-account isolation.
if got := loadSessionState("beltino"); got != "" {
t.Errorf("different account read %q, want empty", got)
}
}
func TestReplayWindowMarkers(t *testing.T) {
dir := t.TempDir()
cfg := filepath.Join(dir, "config.json")
t.Setenv("PI_MSG_CONFIG", cfg)
ts := time.Date(2026, 8, 11, 12, 34, 56, 0, time.UTC)
var logged []string
logf := func(level, msg string) { logged = append(logged, level+": "+msg) }
// Nothing written → no window, and reads are empty.
if start, ok := replayWindowStart("slippy"); ok || !start.IsZero() {
t.Fatalf("no markers, got (%v,%v)", start, ok)
}
if got := readSwapStart("slippy"); got != "" {
t.Fatalf("no swapstart, got %q", got)
}
// swapstart is one-shot: read once, consumed.
markSwapStart(logf, "slippy", ts)
if got := readSwapStart("slippy"); got != ts.UTC().Format(time.RFC3339) {
t.Errorf("swapstart read = %q", got)
}
if got := readSwapStart("slippy"); got != "" {
t.Errorf("swapstart should be consumed on first read, got %q", got)
}
// lastout is persistent: read does not consume it.
markLastOut(logf, "slippy", ts)
if got := readLastOut("slippy"); got != ts.UTC().Format(time.RFC3339) {
t.Errorf("lastout read = %q", got)
}
if got := readLastOut("slippy"); got != ts.UTC().Format(time.RFC3339) {
t.Errorf("lastout should persist across reads, got %q", got)
}
// replayWindowStart prefers swapstart over lastout.
later := ts.Add(5 * time.Minute)
markSwapStart(logf, "slippy", later)
markLastOut(logf, "slippy", ts)
start, ok := replayWindowStart("slippy")
if !ok || !start.UTC().Equal(later) {
t.Errorf("replayWindowStart = (%v,%v), want swapstart %v", start, ok, later)
}
// Once the swapstart is consumed, lastout is the fallback.
markLastOut(logf, "slippy", later)
start, ok = replayWindowStart("slippy")
if !ok || !start.UTC().Equal(later) {
t.Errorf("replayWindowStart fallback = (%v,%v), want lastout %v", start, ok, later)
}
// Invalid swapstart is treated as absent and consumed.
if err := os.WriteFile(windowMarkerPath("slippy", "swapstart"), []byte("bogus\n"), 0o600); err != nil {
t.Fatal(err)
}
if got := readSwapStart("slippy"); got != "" {
t.Errorf("invalid swapstart should be ignored+consumed, got %q", got)
}
if len(logged) != 0 {
t.Errorf("unexpected warnings: %v", logged)
}
}
func TestStartDirectiveRoundTrip(t *testing.T) {
dir := t.TempDir()
cfg := filepath.Join(dir, "config.json")
t.Setenv("PI_MSG_CONFIG", cfg)
// Nothing written → no directive, and the call is a no-op.
if kind, payload := loadStartDirective("slippy"); kind != "" || payload != "" {
t.Fatalf("no directive written, got kind=%q payload=%q", kind, payload)
}
var logged []string
logf := func(level, msg string) { logged = append(logged, level+": "+msg) }
writeStartDirective(logf, "slippy", StartProactive)
if kind, payload := loadStartDirective("slippy"); kind != StartProactive || payload != "" {
t.Errorf("after write, load = (%q,%q), want (%q,\"\")", kind, payload, StartProactive)
}
// The directive is one-shot: consumed when read.
if kind, _ := loadStartDirective("slippy"); kind != "" {
t.Errorf("directive should be consumed on first read, got %q", kind)
}
// Idle round-trips too.
writeStartDirective(logf, "slippy", StartIdle)
if kind, _ := loadStartDirective("slippy"); kind != StartIdle {
t.Errorf("idle directive, load = %q", kind)
}
// Invalid contents are treated as absent and consumed (no error).
if err := os.WriteFile(startDirectivePath("slippy"), []byte("bogus\n"), 0o600); err != nil {
t.Fatal(err)
}
if kind, _ := loadStartDirective("slippy"); kind != "" {
t.Errorf("invalid directive should be ignored, got %q", kind)
}
if len(logged) != 0 {
t.Errorf("unexpected warnings: %v", logged)
}
}
// TestPromptDirective covers the invocation-time initial prompt payload shape
// (pi-msg#35): writePromptDirective → loadStartDirective must yield the prompt
// kind with the exact task text, survive a multi-line body, be one-shot, and
// treat blank payloads as absent (no directive, no crash).
func TestPromptDirective(t *testing.T) {
dir := t.TempDir()
cfg := filepath.Join(dir, "config.json")
t.Setenv("PI_MSG_CONFIG", cfg)
var logged []string
logf := func(level, msg string) { logged = append(logged, level+": "+msg) }
// Round-trip a single-line task.
task := "resolve zachpmanson/pi-msg#35 and open a PR"
writePromptDirective(logf, "slippy", task)
kind, payload := loadStartDirective("slippy")
if kind != StartPrompt {
t.Fatalf("kind = %q, want %q", kind, StartPrompt)
}
if payload != task {
t.Errorf("payload = %q, want %q", payload, task)
}
// One-shot: consumed on first read, like the enum directives.
if kind, _ := loadStartDirective("slippy"); kind != "" {
t.Errorf("prompt directive should be consumed on first read, got %q", kind)
}
// A multi-line task body survives intact.
multi := "resolve issue #1:\n - run the tests\n - push the branch"
writePromptDirective(logf, "slippy", multi)
kind, payload = loadStartDirective("slippy")
if kind != StartPrompt || payload != multi {
t.Errorf("multi-line round-trip = (%q,%q), want (%q,%q)", kind, payload, StartPrompt, multi)
}
// A file whose payload is all whitespace is treated as absent (consumed).
if err := os.WriteFile(startDirectivePath("slippy"), []byte(StartPrompt+"\n \n"), 0o600); err != nil {
t.Fatal(err)
}
if kind, payload := loadStartDirective("slippy"); kind != "" || payload != "" {
t.Errorf("blank prompt payload should be absent, got (%q,%q)", kind, payload)
}
// writePromptDirective refuses blank bodies with a warning, no file.
writePromptDirective(logf, "slippy", " ")
if kind, _ := loadStartDirective("slippy"); kind != "" {
t.Errorf("blank writePromptDirective should not write a directive, got %q", kind)
}
if len(logged) != 1 || !strings.Contains(logged[0], "empty prompt payload") {
t.Errorf("expected one empty-payload warning, got %v", logged)
}
}
func TestResolveAccountCreditWatch(t *testing.T) {
cfg := &Config{Accounts: map[string]Account{
"default": {JID: "pi@chat.example.com", Password: "pw", Owner: "zach@chat.example.com",
CreditWatch: &CreditWatch{MinBelowUsd: 2}},
}}
got, err := resolveAccount(cfg, "")
if err != nil {
t.Fatalf("resolve: %v", err)
}
if got.MinCreditUsd != 2 {
t.Fatalf("MinCreditUsd = %v, want 2", got.MinCreditUsd)
}
}
func TestResolveAccountCreditWatchDisabled(t *testing.T) {
cfg := &Config{Accounts: map[string]Account{
"default": {JID: "j@chat.example.com", Password: "pw", Owner: "zach@chat.example.com"},
}}
got, err := resolveAccount(cfg, "")
if err != nil {
t.Fatalf("resolve: %v", err)
}
if got.MinCreditUsd != 0 {
t.Fatalf("MinCreditUsd = %v, want 0", got.MinCreditUsd)
}
}