-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathcontext_test.go
More file actions
194 lines (165 loc) · 5.56 KB
/
Copy pathcontext_test.go
File metadata and controls
194 lines (165 loc) · 5.56 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
package agentsocket_test
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"cdr.dev/slog/v3"
"github.com/coder/coder/v2/agent/agentcontext"
"github.com/coder/coder/v2/agent/agentsocket"
"github.com/coder/coder/v2/testutil"
)
// fakeContextManager is an in-memory agentsocket.ContextManager for tests.
type fakeContextManager struct {
sources []agentcontext.Source
snapshot agentcontext.Snapshot
resyncErr error
resynced bool
}
func (f *fakeContextManager) Sources() []agentcontext.Source { return f.sources }
func (f *fakeContextManager) HasSource(path string) (string, bool) {
for _, s := range f.sources {
if s.Path == path {
return s.Path, true
}
}
return "", false
}
func (f *fakeContextManager) AddSource(s agentcontext.Source) (agentcontext.Source, error) {
for _, existing := range f.sources {
if existing.Path == s.Path {
return existing, nil
}
}
f.sources = append(f.sources, s)
return s, nil
}
func (f *fakeContextManager) RemoveSource(path string) error {
for i, s := range f.sources {
if s.Path == path {
f.sources = append(f.sources[:i], f.sources[i+1:]...)
return nil
}
}
return agentcontext.ErrSourceNotFound
}
func (f *fakeContextManager) Snapshot() agentcontext.Snapshot { return f.snapshot }
func (f *fakeContextManager) Resync(_ context.Context) (agentcontext.Snapshot, error) {
if f.resyncErr != nil {
return agentcontext.Snapshot{}, f.resyncErr
}
f.resynced = true
return f.snapshot, nil
}
func TestDRPCAgentSocketService_Context(t *testing.T) {
t.Parallel()
t.Run("SourceCRUDAndSnapshot", func(t *testing.T) {
t.Parallel()
const sourcePath = "/home/coder/project"
cm := &fakeContextManager{
snapshot: agentcontext.Snapshot{
Version: 7,
Resources: []agentcontext.Resource{{
ID: "instruction_file:" + sourcePath + "/AGENTS.md",
Kind: agentcontext.KindInstructionFile,
Source: sourcePath + "/AGENTS.md",
SourcePath: sourcePath,
SizeBytes: 42,
Status: agentcontext.StatusOK,
Description: "be concise",
}, {
// A built-in resource (no source path) the show filter must skip.
ID: "instruction_file:/home/coder/.coder/AGENTS.md",
Kind: agentcontext.KindInstructionFile,
Source: "/home/coder/.coder/AGENTS.md",
Status: agentcontext.StatusOK,
}},
},
}
socketPath := testutil.AgentSocketPath(t)
ctx := testutil.Context(t, testutil.WaitShort)
server, err := agentsocket.NewServer(
slog.Make().Leveled(slog.LevelDebug),
agentsocket.WithPath(socketPath),
agentsocket.WithContextManager(cm),
)
require.NoError(t, err)
defer server.Close()
client := newSocketClient(ctx, t, socketPath)
// Add a source.
src, err := client.AddContextSource(ctx, sourcePath)
require.NoError(t, err)
require.Equal(t, sourcePath, src.Path)
// It shows up in the list.
sources, err := client.ContextSources(ctx)
require.NoError(t, err)
require.Len(t, sources, 1)
require.Equal(t, sourcePath, sources[0].Path)
// Get the registered source.
got, err := client.GetContextSource(ctx, sourcePath)
require.NoError(t, err)
require.Equal(t, sourcePath, got.Path)
// Getting an unregistered source errors.
_, err = client.GetContextSource(ctx, "/nope")
require.Error(t, err)
// Snapshot carries resources with their source path stamped.
snap, err := client.GetContextSnapshot(ctx)
require.NoError(t, err)
require.EqualValues(t, 7, snap.Version)
require.Len(t, snap.Resources, 2)
require.Equal(t, agentcontext.KindInstructionFile.String(), snap.Resources[0].Kind)
require.Equal(t, sourcePath, snap.Resources[0].SourcePath)
require.EqualValues(t, 42, snap.Resources[0].SizeBytes)
// Remove the source; removing again reports not found.
require.NoError(t, client.RemoveContextSource(ctx, sourcePath))
err = client.RemoveContextSource(ctx, sourcePath)
require.Error(t, err)
require.Contains(t, err.Error(), "not found")
})
t.Run("Resync", func(t *testing.T) {
t.Parallel()
cm := &fakeContextManager{snapshot: agentcontext.Snapshot{Version: 3}}
socketPath := testutil.AgentSocketPath(t)
ctx := testutil.Context(t, testutil.WaitShort)
server, err := agentsocket.NewServer(
slog.Make().Leveled(slog.LevelDebug),
agentsocket.WithPath(socketPath),
agentsocket.WithContextManager(cm),
)
require.NoError(t, err)
defer server.Close()
client := newSocketClient(ctx, t, socketPath)
snap, err := client.ResyncContext(ctx)
require.NoError(t, err)
require.EqualValues(t, 3, snap.Version)
require.True(t, cm.resynced)
})
t.Run("NoManagerErrors", func(t *testing.T) {
t.Parallel()
socketPath := testutil.AgentSocketPath(t)
ctx := testutil.Context(t, testutil.WaitShort)
// No WithContextManager: the context RPCs must fail cleanly.
server, err := agentsocket.NewServer(
slog.Make().Leveled(slog.LevelDebug),
agentsocket.WithPath(socketPath),
)
require.NoError(t, err)
defer server.Close()
client := newSocketClient(ctx, t, socketPath)
// Every context RPC independently guards a nil context manager;
// exercise all of them so dropping a guard surfaces as a test
// failure rather than an agent-killing nil dereference in a DRPC
// handler.
_, err = client.ContextSources(ctx)
require.Error(t, err)
_, err = client.GetContextSource(ctx, "/tmp/x")
require.Error(t, err)
_, err = client.AddContextSource(ctx, "/tmp/x")
require.Error(t, err)
err = client.RemoveContextSource(ctx, "/tmp/x")
require.Error(t, err)
_, err = client.GetContextSnapshot(ctx)
require.Error(t, err)
_, err = client.ResyncContext(ctx)
require.Error(t, err)
})
}