-
Notifications
You must be signed in to change notification settings - Fork 408
Expand file tree
/
Copy pathengine_agent_import_test.go
More file actions
414 lines (343 loc) · 12.8 KB
/
engine_agent_import_test.go
File metadata and controls
414 lines (343 loc) · 12.8 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
411
412
413
414
//go:build !integration
package workflow
import (
"os"
"path/filepath"
"strings"
"testing"
)
// TestCopilotEngineWithAgentFromEngineConfig tests that copilot engine includes --agent flag when specified in engine.agent
func TestCopilotEngineWithAgentFromEngineConfig(t *testing.T) {
engine := NewCopilotEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
EngineConfig: &EngineConfig{
ID: "copilot",
Agent: "my-custom-agent",
},
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 2 {
t.Fatalf("Expected 2 execution steps (preflight + execution), got %d", len(steps))
}
stepContent := strings.Join([]string(steps[1]), "\n")
// Copilot CLI expects agent identifier
if !strings.Contains(stepContent, `--agent my-custom-agent`) {
t.Errorf("Expected '--agent my-custom-agent' in copilot command, got:\n%s", stepContent)
}
}
// TestCopilotEngineWithAgentFromImports tests that agent imports do NOT set --agent flag
// Agent imports only import markdown content, not agent configuration
func TestCopilotEngineWithAgentFromImports(t *testing.T) {
engine := NewCopilotEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
EngineConfig: &EngineConfig{
ID: "copilot",
},
AgentFile: ".github/agents/test-agent.md",
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 2 {
t.Fatalf("Expected 2 execution steps (preflight + execution), got %d", len(steps))
}
stepContent := strings.Join([]string(steps[1]), "\n")
// Agent imports should NOT set --agent flag (only engine.agent does)
if strings.Contains(stepContent, `--agent`) {
t.Errorf("Did not expect '--agent' flag when only AgentFile is set (without engine.agent), got:\n%s", stepContent)
}
}
// TestCopilotEngineAgentOnlyFromEngineConfig tests that --agent flag is only set by engine.agent
func TestCopilotEngineAgentOnlyFromEngineConfig(t *testing.T) {
engine := NewCopilotEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
EngineConfig: &EngineConfig{
ID: "copilot",
Agent: "explicit-agent",
},
AgentFile: ".github/agents/import-agent.md",
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 2 {
t.Fatalf("Expected 2 execution steps (preflight + execution), got %d", len(steps))
}
stepContent := strings.Join([]string(steps[1]), "\n")
// Should only use explicit agent from engine.agent
if !strings.Contains(stepContent, `--agent explicit-agent`) {
t.Errorf("Expected '--agent explicit-agent' in copilot command, got:\n%s", stepContent)
}
// Should not use agent from imports
if strings.Contains(stepContent, `--agent import-agent`) {
t.Errorf("Did not expect '--agent import-agent' when engine.agent is set, got:\n%s", stepContent)
}
}
// TestCopilotEngineWithoutAgentFlag tests that copilot engine works without agent file
func TestCopilotEngineWithoutAgentFlag(t *testing.T) {
engine := NewCopilotEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
EngineConfig: &EngineConfig{
ID: "copilot",
},
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 2 {
t.Fatalf("Expected 2 execution steps (preflight + execution), got %d", len(steps))
}
stepContent := strings.Join([]string(steps[1]), "\n")
if strings.Contains(stepContent, "--agent") {
t.Errorf("Did not expect '--agent' flag when agent file is not specified, got:\n%s", stepContent)
}
}
// TestClaudeEngineWithAgentFromImports tests that claude engine prepends agent file content to prompt
func TestClaudeEngineWithAgentFromImports(t *testing.T) {
engine := NewClaudeEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
EngineConfig: &EngineConfig{
ID: "claude",
},
AgentFile: ".github/agents/test-agent.md",
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 1 {
t.Fatalf("Expected 1 execution step, got %d", len(steps))
}
stepContent := strings.Join([]string(steps[0]), "\n")
// Check that custom agent content extraction is present
if !strings.Contains(stepContent, `AGENT_CONTENT="$(awk`) {
t.Errorf("Expected agent content extraction in claude command, got:\n%s", stepContent)
}
// Check that agent file path is referenced with quoted GITHUB_WORKSPACE prefix
if !strings.Contains(stepContent, `"${GITHUB_WORKSPACE}/.github/agents/test-agent.md"`) {
t.Errorf("Expected agent file path with quoted GITHUB_WORKSPACE prefix in claude command, got:\n%s", stepContent)
}
// Check that agent content is prepended to prompt
if !strings.Contains(stepContent, "$AGENT_CONTENT") {
t.Errorf("Expected $AGENT_CONTENT variable in claude command, got:\n%s", stepContent)
}
}
// TestClaudeEngineWithoutAgentFile tests that claude engine works without agent file
func TestClaudeEngineWithoutAgentFile(t *testing.T) {
engine := NewClaudeEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
EngineConfig: &EngineConfig{
ID: "claude",
},
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 1 {
t.Fatalf("Expected 1 execution step, got %d", len(steps))
}
stepContent := strings.Join([]string(steps[0]), "\n")
// Should not have agent content extraction
if strings.Contains(stepContent, "AGENT_CONTENT") {
t.Errorf("Did not expect AGENT_CONTENT when agent file is not specified, got:\n%s", stepContent)
}
// Should still have the standard prompt
if !strings.Contains(stepContent, `"$(cat /tmp/gh-aw/aw-prompts/prompt.txt)"`) {
t.Errorf("Expected standard prompt reading in claude command, got:\n%s", stepContent)
}
}
// TestCodexEngineWithAgentFromImports tests that codex engine prepends agent file content to prompt
func TestCodexEngineWithAgentFromImports(t *testing.T) {
engine := NewCodexEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
EngineConfig: &EngineConfig{
ID: "codex",
},
AgentFile: ".github/agents/test-agent.md",
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 1 {
t.Fatalf("Expected 1 execution step, got %d", len(steps))
}
stepContent := strings.Join([]string(steps[0]), "\n")
// Check that agent content extraction is present
if !strings.Contains(stepContent, `AGENT_CONTENT="$(awk`) {
t.Errorf("Expected agent content extraction in codex command, got:\n%s", stepContent)
}
// Check that agent file path is referenced with quoted GITHUB_WORKSPACE prefix
if !strings.Contains(stepContent, `"${GITHUB_WORKSPACE}/.github/agents/test-agent.md"`) {
t.Errorf("Expected agent file path with quoted GITHUB_WORKSPACE prefix in codex command, got:\n%s", stepContent)
}
// Check that agent content is prepended to prompt using printf
if !strings.Contains(stepContent, `INSTRUCTION="$(printf`) {
t.Errorf("Expected printf with INSTRUCTION in codex command, got:\n%s", stepContent)
}
if !strings.Contains(stepContent, "$AGENT_CONTENT") {
t.Errorf("Expected $AGENT_CONTENT variable in codex command, got:\n%s", stepContent)
}
}
// TestCodexEngineWithoutAgentFile tests that codex engine works without agent file
func TestCodexEngineWithoutAgentFile(t *testing.T) {
engine := NewCodexEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
EngineConfig: &EngineConfig{
ID: "codex",
},
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 1 {
t.Fatalf("Expected 1 execution step, got %d", len(steps))
}
stepContent := strings.Join([]string(steps[0]), "\n")
// Should not have agent content extraction
if strings.Contains(stepContent, "AGENT_CONTENT") {
t.Errorf("Did not expect AGENT_CONTENT when agent file is not specified, got:\n%s", stepContent)
}
// Should have the standard instruction reading
if !strings.Contains(stepContent, `INSTRUCTION="$(cat "$GH_AW_PROMPT")"`) {
t.Errorf("Expected standard INSTRUCTION reading in codex command, got:\n%s", stepContent)
}
}
// TestAgentFileValidation tests compile-time validation of agent file existence
func TestAgentFileValidation(t *testing.T) {
// Create a temporary directory structure that mimics a repository
tmpDir, err := os.MkdirTemp("", "agent-validation-test")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tmpDir)
// Create the directory structure: .github/agents/ and .github/workflows/
agentsDir := filepath.Join(tmpDir, ".github", "agents")
workflowsDir := filepath.Join(tmpDir, ".github", "workflows")
if err := os.MkdirAll(agentsDir, 0755); err != nil {
t.Fatalf("Failed to create agents directory: %v", err)
}
if err := os.MkdirAll(workflowsDir, 0755); err != nil {
t.Fatalf("Failed to create workflows directory: %v", err)
}
// Create a valid agent file
agentContent := `---
on: push
title: Test Agent
---
# Test Agent Instructions
This is a test agent file.
`
validAgentFilePath := filepath.Join(agentsDir, "valid-agent.md")
if err := os.WriteFile(validAgentFilePath, []byte(agentContent), 0644); err != nil {
t.Fatalf("Failed to create valid agent file: %v", err)
}
// Test 1: Valid agent file
t.Run("valid_agent_file", func(t *testing.T) {
compiler := NewCompiler()
workflowData := &WorkflowData{
EngineConfig: &EngineConfig{
ID: "copilot",
},
AgentFile: validAgentFilePath,
}
workflowPath := filepath.Join(workflowsDir, "test.md")
err := compiler.validateAgentFile(workflowData, workflowPath)
if err != nil {
t.Errorf("Expected no error for valid agent file, got: %v", err)
}
})
// Test 2: Non-existent agent file
t.Run("nonexistent_agent_file", func(t *testing.T) {
compiler := NewCompiler()
workflowData := &WorkflowData{
EngineConfig: &EngineConfig{
ID: "copilot",
},
AgentFile: filepath.Join(agentsDir, "nonexistent.md"),
}
workflowPath := filepath.Join(workflowsDir, "test.md")
err := compiler.validateAgentFile(workflowData, workflowPath)
if err == nil {
t.Error("Expected error for non-existent agent file, got nil")
} else if !strings.Contains(err.Error(), "does not exist") {
t.Errorf("Expected 'does not exist' error, got: %v", err)
}
})
// Test 3: No agent file specified
t.Run("no_agent_file", func(t *testing.T) {
compiler := NewCompiler()
workflowData := &WorkflowData{
EngineConfig: &EngineConfig{
ID: "copilot",
},
}
workflowPath := filepath.Join(workflowsDir, "test.md")
err := compiler.validateAgentFile(workflowData, workflowPath)
if err != nil {
t.Errorf("Expected no error when agent file not specified, got: %v", err)
}
})
// Test 4: Nil engine config
t.Run("nil_engine_config", func(t *testing.T) {
compiler := NewCompiler()
workflowData := &WorkflowData{}
workflowPath := filepath.Join(workflowsDir, "test.md")
err := compiler.validateAgentFile(workflowData, workflowPath)
if err != nil {
t.Errorf("Expected no error when engine config is nil, got: %v", err)
}
})
}
// TestCheckoutWithAgentFromImports tests that checkout step is added when agent file is imported
func TestCheckoutWithAgentFromImports(t *testing.T) {
t.Run("checkout_added_with_agent", func(t *testing.T) {
compiler := NewCompiler()
workflowData := &WorkflowData{
EngineConfig: &EngineConfig{
ID: "copilot",
},
AgentFile: "/path/to/agent.md",
Permissions: "permissions:\n contents: read\n",
}
shouldCheckout := compiler.shouldAddCheckoutStep(workflowData)
if !shouldCheckout {
t.Error("Expected checkout to be added when agent file is specified")
}
})
t.Run("checkout_added_with_agent_no_contents_permission", func(t *testing.T) {
compiler := NewCompiler()
workflowData := &WorkflowData{
EngineConfig: &EngineConfig{
ID: "copilot",
},
AgentFile: "/path/to/agent.md",
Permissions: "permissions:\n issues: read\n",
}
shouldCheckout := compiler.shouldAddCheckoutStep(workflowData)
if !shouldCheckout {
t.Error("Expected checkout to be added when agent file is specified, even without contents permission")
}
})
t.Run("checkout_added_without_agent", func(t *testing.T) {
compiler := NewCompiler()
// Set action mode to release
compiler.SetActionMode(ActionModeRelease)
workflowData := &WorkflowData{
EngineConfig: &EngineConfig{
ID: "copilot",
},
Permissions: "permissions:\n issues: read\n",
}
shouldCheckout := compiler.shouldAddCheckoutStep(workflowData)
if !shouldCheckout {
t.Error("Expected checkout to be added (always needed unless already in custom steps)")
}
})
t.Run("checkout_with_custom_steps_containing_checkout", func(t *testing.T) {
compiler := NewCompiler()
workflowData := &WorkflowData{
EngineConfig: &EngineConfig{
ID: "copilot",
},
AgentFile: "/path/to/agent.md",
CustomSteps: "steps:\n - uses: actions/checkout@v4\n",
}
shouldCheckout := compiler.shouldAddCheckoutStep(workflowData)
if shouldCheckout {
t.Error("Expected checkout NOT to be added when custom steps already contain checkout, even with agent file")
}
})
}