-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathcomment_memory_config_test.go
More file actions
171 lines (158 loc) · 4.11 KB
/
Copy pathcomment_memory_config_test.go
File metadata and controls
171 lines (158 loc) · 4.11 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
//go:build !integration
package workflow
import (
"os"
"path/filepath"
"testing"
"github.com/github/gh-aw/pkg/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCommentMemoryToolConfig(t *testing.T) {
tests := []struct {
name string
commentMemoryConfigYAML string
expectedCommentMemory *CommentMemoryConfig
}{
{
name: "defaults disabled when key absent",
commentMemoryConfigYAML: "",
},
{
name: "explicit false disables comment-memory",
commentMemoryConfigYAML: " comment-memory: false\n",
},
{
name: "explicit null disables comment-memory",
commentMemoryConfigYAML: " comment-memory: null\n",
},
{
name: "explicit true enables with defaults",
commentMemoryConfigYAML: " comment-memory: true\n",
expectedCommentMemory: &CommentMemoryConfig{
BaseSafeOutputConfig: BaseSafeOutputConfig{
Max: strPtr("1"),
},
MemoryID: "default",
},
},
{
name: "map config sets fields",
commentMemoryConfigYAML: ` comment-memory:
max: 3
memory-id: triage
target: "42"
target-repo: github/docs
allowed-repos:
- github/docs
- github/gh-aw
footer: false
`,
expectedCommentMemory: &CommentMemoryConfig{
BaseSafeOutputConfig: BaseSafeOutputConfig{
Max: strPtr("3"),
Footer: strPtr("false"),
},
MemoryID: "triage",
Target: "42",
TargetRepoSlug: "github/docs",
AllowedRepos: []string{"github/docs", "github/gh-aw"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := testutil.TempDir(t, "comment-memory-config-test")
toolsSection := ""
if tt.commentMemoryConfigYAML != "" {
toolsSection = "tools:\n" + tt.commentMemoryConfigYAML
}
workflow := `---
on: issues
engine: copilot
permissions:
contents: read
` + toolsSection + `safe-outputs:
add-comment:
max: 1
---
# Test
`
testFile := filepath.Join(tmpDir, "workflow.md")
err := os.WriteFile(testFile, []byte(workflow), 0644)
require.NoError(t, err, "Failed to write test workflow")
compiler := NewCompiler(WithVersion("1.0.0"))
workflowData, err := compiler.ParseWorkflowFile(testFile)
require.NoError(t, err, "Failed to parse workflow")
if tt.expectedCommentMemory != nil {
require.NotNil(t, workflowData.CommentMemoryConfig, "CommentMemory should be enabled")
assert.Equal(t, tt.expectedCommentMemory, workflowData.CommentMemoryConfig)
} else {
assert.Nil(t, workflowData.CommentMemoryConfig, "CommentMemory should be disabled")
}
})
}
}
func TestExtractCommentMemoryConfigInvalidFields(t *testing.T) {
compiler := NewCompiler(WithVersion("1.0.0"))
tests := []struct {
name string
rawConfig any
}{
{
name: "invalid max disables comment-memory",
rawConfig: map[string]any{
"max": "invalid",
},
},
{
name: "invalid footer disables comment-memory",
rawConfig: map[string]any{
"footer": "invalid",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := compiler.extractCommentMemoryConfig(&ToolsConfig{
CommentMemory: &CommentMemoryToolConfig{
Raw: tt.rawConfig,
},
})
assert.Nil(t, config, "CommentMemory should be disabled")
})
}
}
func TestParseCommentMemoryConfigValueBoolAndNil(t *testing.T) {
compiler := NewCompiler(WithVersion("1.0.0"))
tests := []struct {
name string
rawConfig any
expectedCommentMemory *CommentMemoryConfig
}{
{
name: "nil disables comment-memory",
rawConfig: nil,
},
{
name: "false disables comment-memory",
rawConfig: false,
},
{
name: "true enables defaults",
rawConfig: true,
expectedCommentMemory: &CommentMemoryConfig{
BaseSafeOutputConfig: BaseSafeOutputConfig{
Max: strPtr("1"),
},
MemoryID: "default",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := compiler.parseCommentMemoryConfigValue(tt.rawConfig)
assert.Equal(t, tt.expectedCommentMemory, config)
})
}
}