-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool_manifest_test.go
More file actions
173 lines (156 loc) · 4.21 KB
/
Copy pathtool_manifest_test.go
File metadata and controls
173 lines (156 loc) · 4.21 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
package main
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/spf13/cobra"
)
func TestLoadToolManifestTOML(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "tools.toml")
content := `
tool_root = "."
tools = ["bash", "tasks_write", "fs"]
state_id = "550e8400-e29b-41d4-a716-446655440000"
state_ttl_sec = 3600
[bash]
allow = ["git ", "rg "]
deny = ["rm "]
allow_all = false
timeout = "15s"
max_output_bytes = 64000
[tasks_write]
output = "tasks.json"
print = true
[fs]
ignore_dirs = ["node_modules"]
search_timeout = "3s"
[[custom]]
name = "custom.echo"
description = "Echo args"
command = ["cat"]
schema = { type = "object", properties = { message = { type = "string" } }, required = ["message"] }
`
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatalf("write manifest: %v", err)
}
manifest, err := loadToolManifest(path)
if err != nil {
t.Fatalf("load manifest: %v", err)
}
if manifest.ToolRoot != "." {
t.Fatalf("expected tool_root '.', got %q", manifest.ToolRoot)
}
if len(manifest.Tools) != 3 {
t.Fatalf("expected 3 tools, got %d", len(manifest.Tools))
}
if manifest.Bash == nil || manifest.Bash.Timeout != "15s" {
t.Fatalf("expected bash timeout")
}
if manifest.TasksWrite == nil || manifest.TasksWrite.Output != "tasks.json" {
t.Fatalf("expected tasks_write output")
}
if manifest.FS == nil || len(manifest.FS.IgnoreDirs) != 1 {
t.Fatalf("expected fs config")
}
if len(manifest.Custom) != 1 {
t.Fatalf("expected custom tool")
}
}
func TestLoadToolManifestJSON(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "tools.json")
content := `{
"tool_root": ".",
"tools": ["bash"],
"bash": {
"allow": ["git "],
"timeout": "5s"
}
}`
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatalf("write manifest: %v", err)
}
manifest, err := loadToolManifest(path)
if err != nil {
t.Fatalf("load manifest: %v", err)
}
if manifest.Bash == nil || manifest.Bash.Timeout != "5s" {
t.Fatalf("expected bash timeout")
}
}
func TestApplyToolManifest(t *testing.T) {
flags := &agentLoopFlags{}
cmd := &cobra.Command{}
bindAgentLoopFlags(cmd, flags)
if err := cmd.Flags().Set("tool", "bash"); err != nil {
t.Fatalf("set flag: %v", err)
}
allowAll := true
timeout := "20s"
maxBytes := uint64(128000)
ttl := int64(7200)
manifest := toolManifest{
ToolRoot: "/tmp",
Tools: []string{"bash", "tasks_write"},
StateTTLSeconds: &ttl,
Bash: &toolManifestBash{
Allow: []string{"git "},
AllowAll: &allowAll,
Timeout: timeout,
MaxOutputBytes: &maxBytes,
},
TasksWrite: &toolManifestTasks{
Output: "tasks.json",
},
}
if err := applyToolManifest(flags, manifest, cmd.Flags()); err != nil {
t.Fatalf("apply manifest: %v", err)
}
if len(flags.tools) != 1 || flags.tools[0] != "bash" {
t.Fatalf("expected tools from flag to win, got %#v", flags.tools)
}
if flags.toolRoot != "/tmp" {
t.Fatalf("expected tool root to come from manifest")
}
if flags.stateTTLSeconds != ttl {
t.Fatalf("expected state ttl from manifest")
}
if flags.bashTimeout != 20*time.Second {
t.Fatalf("expected bash timeout to be parsed")
}
if flags.bashMaxOutBytes != maxBytes {
t.Fatalf("expected bash max output bytes")
}
if flags.tasksOutputPath != "tasks.json" {
t.Fatalf("expected tasks output path")
}
}
func TestApplyToolManifestInference(t *testing.T) {
flags := &agentLoopFlags{}
cmd := &cobra.Command{}
bindAgentLoopFlags(cmd, flags)
manifest := toolManifest{
Bash: &toolManifestBash{Allow: []string{"git "}},
TasksWrite: &toolManifestTasks{Output: "tasks.json"},
FS: &toolManifestFS{IgnoreDirs: []string{"node_modules"}},
}
if err := applyToolManifest(flags, manifest, cmd.Flags()); err != nil {
t.Fatalf("apply manifest: %v", err)
}
if len(flags.tools) != 3 {
t.Fatalf("expected inferred tools, got %v", flags.tools)
}
}
func TestLoadToolManifestUnsupportedExt(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "tools.yaml")
if err := os.WriteFile(path, []byte("{}"), 0o600); err != nil {
t.Fatalf("write manifest: %v", err)
}
_, err := loadToolManifest(path)
if err == nil {
t.Fatal("expected error for unsupported extension")
}
}