-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathantigravity_settings_test.go
More file actions
146 lines (122 loc) · 4.26 KB
/
Copy pathantigravity_settings_test.go
File metadata and controls
146 lines (122 loc) · 4.26 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
package agent
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEnsureAntigravityReviewPermissionsCreatesFile(t *testing.T) {
path := filepath.Join(t.TempDir(), "settings.json")
require.NoError(t, ensureAntigravityReviewPermissions(path))
assertSettingsAllow(t, path, antigravityReviewAllowPermissions...)
}
func TestEnsureAntigravityReviewPermissionsMergesAllow(t *testing.T) {
path := filepath.Join(t.TempDir(), "nested", "settings.json")
writeSettings(t, path, map[string]any{
"model": "gemini-3.1-pro-preview",
"permissions": map[string]any{
"allow": []any{"command(git)", "read_file(*)"},
"deny": []any{"command(rm -rf)"},
"ask": []any{"command(*)"},
},
"enableTerminalSandbox": true,
})
require.NoError(t, ensureAntigravityReviewPermissions(path))
doc := readSettings(t, path)
assert.Equal(t, "gemini-3.1-pro-preview", doc["model"])
assert.Equal(t, true, doc["enableTerminalSandbox"])
permissions := doc["permissions"].(map[string]any)
assert.Equal(t, []any{"command(rm -rf)"}, permissions["deny"])
assert.Equal(t, []any{"command(*)"}, permissions["ask"])
allow := asStrings(t, permissions["allow"])
assert.Equal(t, "command(git)", allow[0], "existing allow entries stay first")
assert.Contains(t, allow, "read_file(*)")
assert.Equal(t, 1, countStrings(allow, "read_file(*)"), "do not duplicate existing allows")
for _, rule := range antigravityReviewAllowPermissions {
assert.Contains(t, allow, rule)
}
}
func TestEnsureAntigravityReviewPermissionsIdempotent(t *testing.T) {
path := filepath.Join(t.TempDir(), "settings.json")
require.NoError(t, ensureAntigravityReviewPermissions(path))
first := readRaw(t, path)
require.NoError(t, ensureAntigravityReviewPermissions(path))
second := readRaw(t, path)
assert.Equal(t, first, second)
}
func TestEnsureAntigravityReviewPermissionsDoesNotClobberInvalidJSON(t *testing.T) {
path := filepath.Join(t.TempDir(), "settings.json")
require.NoError(t, os.WriteFile(path, []byte("{not-json"), 0o644))
err := ensureAntigravityReviewPermissions(path)
require.Error(t, err)
assert.Equal(t, "{not-json", string(readRaw(t, path)))
}
func TestEnsureAntigravityReviewPermissionsRejectsNonObjectPermissions(t *testing.T) {
path := filepath.Join(t.TempDir(), "settings.json")
writeSettings(t, path, map[string]any{"permissions": "always-proceed"})
err := ensureAntigravityReviewPermissions(path)
require.Error(t, err)
doc := readSettings(t, path)
assert.Equal(t, "always-proceed", doc["permissions"])
}
func TestEnsureAntigravityReviewPermissionsEmptyPathIsNoop(t *testing.T) {
require.NoError(t, ensureAntigravityReviewPermissions(""))
}
func TestAntigravityReviewAllowPermissionsCoverProdFailures(t *testing.T) {
assert.Contains(t, antigravityReviewAllowPermissions, "read_file(*)")
for _, cmd := range []string{"pwd", "wc", "ls", "cat", "head", "tail", "stat", "file"} {
assert.Contains(t, antigravityReviewAllowPermissions, "command("+cmd+")")
}
}
func writeSettings(t *testing.T, path string, doc map[string]any) {
t.Helper()
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755))
data, err := json.MarshalIndent(doc, "", " ")
require.NoError(t, err)
require.NoError(t, os.WriteFile(path, append(data, '\n'), 0o644))
}
func readSettings(t *testing.T, path string) map[string]any {
t.Helper()
var doc map[string]any
require.NoError(t, json.Unmarshal(readRaw(t, path), &doc))
return doc
}
func readRaw(t *testing.T, path string) []byte {
t.Helper()
raw, err := os.ReadFile(path)
require.NoError(t, err)
return raw
}
func assertSettingsAllow(t *testing.T, path string, want ...string) {
t.Helper()
doc := readSettings(t, path)
permissions, ok := doc["permissions"].(map[string]any)
require.True(t, ok)
allow := asStrings(t, permissions["allow"])
for _, rule := range want {
assert.Contains(t, allow, rule)
}
}
func asStrings(t *testing.T, raw any) []string {
t.Helper()
items, ok := raw.([]any)
require.True(t, ok)
out := make([]string, 0, len(items))
for _, item := range items {
s, ok := item.(string)
require.True(t, ok)
out = append(out, s)
}
return out
}
func countStrings(items []string, want string) int {
n := 0
for _, item := range items {
if item == want {
n++
}
}
return n
}