-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathwatcher_test.go
More file actions
97 lines (83 loc) · 3 KB
/
Copy pathwatcher_test.go
File metadata and controls
97 lines (83 loc) · 3 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
package agentcontext_test
import (
"context"
"os"
"path/filepath"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/agent/agentcontext"
"github.com/coder/coder/v2/testutil"
"github.com/coder/quartz"
)
func TestWatcher_FiresOnAgentsMdEdit(t *testing.T) {
t.Parallel()
dir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(dir, "AGENTS.md"), []byte("v1"), 0o600))
var fires atomic.Int32
w, err := agentcontext.NewWatcher(agentcontext.WatcherOptions{
Logger: testutil.Logger(t).Named("watcher"),
Clock: quartz.NewReal(),
Debounce: 10 * time.Millisecond,
OnChange: func() { fires.Add(1) },
})
require.NoError(t, err)
t.Cleanup(func() { _ = w.Close() })
ctx := testutil.Context(t, testutil.WaitShort)
w.Sync(ctx, []agentcontext.ScanRoot{{Path: dir}})
// Rewrite the file inside Eventually so the test does not race
// fsnotify's watch-setup window. As soon as the watch is live,
// the next write fires the debounce timer.
require.Eventually(t, func() bool {
_ = os.WriteFile(filepath.Join(dir, "AGENTS.md"), []byte("v2"), 0o600)
return fires.Load() >= 1
}, testutil.WaitShort, testutil.IntervalFast, "expected at least one fire after AGENTS.md edit")
}
func TestWatcher_FiresOnNewSkillFile(t *testing.T) {
t.Parallel()
dir := t.TempDir()
skillsRoot := filepath.Join(dir, ".agents", "skills")
require.NoError(t, os.MkdirAll(skillsRoot, 0o755))
var fires atomic.Int32
w, err := agentcontext.NewWatcher(agentcontext.WatcherOptions{
Logger: testutil.Logger(t).Named("watcher"),
Debounce: 10 * time.Millisecond,
OnChange: func() { fires.Add(1) },
})
require.NoError(t, err)
t.Cleanup(func() { _ = w.Close() })
ctx := testutil.Context(t, testutil.WaitShort)
w.Sync(ctx, []agentcontext.ScanRoot{{Path: dir}})
// Create SKILL.md inside Eventually so the test does not race
// fsnotify's watch-setup window. The Manager pre-creates the
// skill dir, then rewrites SKILL.md each tick until the watcher
// fires at least once.
skillDir := filepath.Join(skillsRoot, "foo")
require.NoError(t, os.MkdirAll(skillDir, 0o755))
require.Eventually(t, func() bool {
_ = os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte("---\nname: foo\ndescription: bar\n---\nbody"), 0o600)
return fires.Load() >= 1
}, testutil.WaitShort, testutil.IntervalFast, "expected fire after SKILL.md create")
}
func TestWatcher_CloseIsIdempotent(t *testing.T) {
t.Parallel()
w, err := agentcontext.NewWatcher(agentcontext.WatcherOptions{
Logger: testutil.Logger(t).Named("watcher"),
OnChange: func() {},
})
require.NoError(t, err)
require.NoError(t, w.Close())
require.NoError(t, w.Close())
}
func TestWatcher_SyncAfterCloseNoop(t *testing.T) {
t.Parallel()
w, err := agentcontext.NewWatcher(agentcontext.WatcherOptions{
Logger: testutil.Logger(t).Named("watcher"),
OnChange: func() {},
})
require.NoError(t, err)
require.NoError(t, w.Close())
// Must not panic.
w.Sync(context.Background(), []agentcontext.ScanRoot{{Path: t.TempDir()}})
}