Repository navigation
Expand file tree
/
Copy pathproject_test.go
More file actions
276 lines (256 loc) · 7.98 KB
/
Copy pathproject_test.go
File metadata and controls
276 lines (256 loc) · 7.98 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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestDiscoverProjectFindsNearest(t *testing.T) {
root := t.TempDir()
nested := filepath.Join(root, "a", "b")
if err := os.MkdirAll(nested, 0o755); err != nil {
t.Fatal(err)
}
outer := filepath.Join(root, ".clau.toml")
if err := os.WriteFile(outer, []byte(""), 0o644); err != nil {
t.Fatal(err)
}
if got := discoverProject(nested); got != outer {
t.Errorf("discoverProject = %q, want %q", got, outer)
}
inner := filepath.Join(root, "a", ".clau.toml")
if err := os.WriteFile(inner, []byte(""), 0o644); err != nil {
t.Fatal(err)
}
if got := discoverProject(nested); got != inner {
t.Errorf("nearest must win: got %q, want %q", got, inner)
}
}
func TestDiscoverProjectNone(t *testing.T) {
if got := discoverProject(t.TempDir()); got != "" {
t.Errorf("discoverProject = %q, want empty", got)
}
}
func TestDiscoverProjectSkipsDirectory(t *testing.T) {
root := t.TempDir()
nested := filepath.Join(root, "sub")
if err := os.MkdirAll(filepath.Join(nested, ".clau.toml"), 0o755); err != nil {
t.Fatal(err)
}
real := filepath.Join(root, ".clau.toml")
if err := os.WriteFile(real, []byte(""), 0o644); err != nil {
t.Fatal(err)
}
if got := discoverProject(nested); got != real {
t.Errorf("a directory named .clau.toml must be skipped: got %q, want %q", got, real)
}
}
func TestTrustPathXDG(t *testing.T) {
t.Setenv("XDG_STATE_HOME", filepath.Join("/tmp", "state"))
want := filepath.Join("/tmp", "state", "clau", "trust.toml")
if got := trustPath(); got != want {
t.Errorf("trustPath = %q, want %q", got, want)
}
}
func TestTrustRoundTrip(t *testing.T) {
p := filepath.Join(t.TempDir(), "deep", "trust.toml")
store := map[string]string{"/proj/.clau.toml": "abc123"}
if err := saveTrust(p, store); err != nil {
t.Fatal(err)
}
got, corrupt := loadTrust(p)
if corrupt {
t.Fatal("round-tripped store reported corrupt")
}
if got["/proj/.clau.toml"] != "abc123" {
t.Errorf("store = %v", got)
}
}
func TestLoadTrustMissingIsEmpty(t *testing.T) {
store, corrupt := loadTrust(filepath.Join(t.TempDir(), "none.toml"))
if corrupt || len(store) != 0 {
t.Errorf("missing store: got %v corrupt=%v, want empty/false", store, corrupt)
}
}
func TestLoadTrustCorrupt(t *testing.T) {
p := filepath.Join(t.TempDir(), "trust.toml")
if err := os.WriteFile(p, []byte("[trusted\n"), 0o644); err != nil {
t.Fatal(err)
}
store, corrupt := loadTrust(p)
if !corrupt || len(store) != 0 {
t.Errorf("corrupt store: got %v corrupt=%v, want empty/true", store, corrupt)
}
}
func TestHashFile(t *testing.T) {
p := filepath.Join(t.TempDir(), "f")
if err := os.WriteFile(p, []byte("hello\n"), 0o644); err != nil {
t.Fatal(err)
}
want := "5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03"
got, err := hashFile(p)
if err != nil {
t.Fatal(err)
}
if got != want {
t.Errorf("hashFile = %q, want %q", got, want)
}
}
// projectFixture writes a global config, a project dir with .clau.toml,
// and points CLAU_CONFIG and XDG_STATE_HOME at per-test locations.
// It returns the project dir and the project file path.
func projectFixture(t *testing.T, globalTOML, projectTOML string) (string, string) {
t.Helper()
t.Setenv("CLAU_CONFIG", writeConfig(t, globalTOML))
t.Setenv("XDG_STATE_HOME", t.TempDir())
dir := t.TempDir()
proj := filepath.Join(dir, ".clau.toml")
if err := os.WriteFile(proj, []byte(projectTOML), 0o644); err != nil {
t.Fatal(err)
}
return dir, proj
}
func trustNow(t *testing.T, proj string) {
t.Helper()
hash, err := hashFile(proj)
if err != nil {
t.Fatal(err)
}
store, _ := loadTrust(trustPath())
store[proj] = hash
if err := saveTrust(trustPath(), store); err != nil {
t.Fatal(err)
}
}
func TestLoadEffectiveConfigTrustedApplies(t *testing.T) {
dir, proj := projectFixture(t, "[profiles.rev]\nmodel = \"opus\"\n", "[profiles.rev]\nmodel = \"haiku\"\n")
trustNow(t, proj)
cfg, st, err := loadEffectiveConfig(dir, true)
if err != nil {
t.Fatal(err)
}
if !st.Trusted || !st.Applied || st.Path != proj {
t.Errorf("status = %+v", st)
}
if cfg.Profiles["rev"].Model != "haiku" {
t.Errorf("project layer not applied: %+v", cfg.Profiles["rev"])
}
}
func TestLoadEffectiveConfigUntrustedEnforceErrors(t *testing.T) {
dir, proj := projectFixture(t, "", "[profiles.rev]\nmodel = \"haiku\"\n")
_, _, err := loadEffectiveConfig(dir, true)
if err == nil {
t.Fatal("expected error for untrusted project config")
}
if !strings.Contains(err.Error(), proj) || !strings.Contains(err.Error(), "not trusted") {
t.Errorf("err = %v", err)
}
}
func TestLoadEffectiveConfigUntrustedNoEnforce(t *testing.T) {
dir, proj := projectFixture(t, "[profiles.rev]\nmodel = \"opus\"\n", "[profiles.rev]\nmodel = \"haiku\"\n")
cfg, st, err := loadEffectiveConfig(dir, false)
if err != nil {
t.Fatal(err)
}
if st.Path != proj || st.Trusted || st.Applied || st.Changed {
t.Errorf("status = %+v", st)
}
if cfg.Profiles["rev"].Model != "opus" {
t.Errorf("untrusted layer leaked into config: %+v", cfg.Profiles["rev"])
}
}
func TestLoadEffectiveConfigChangedHash(t *testing.T) {
dir, proj := projectFixture(t, "", "[profiles.rev]\nmodel = \"haiku\"\n")
trustNow(t, proj)
if err := os.WriteFile(proj, []byte("[profiles.rev]\nmodel = \"opus\"\n"), 0o644); err != nil {
t.Fatal(err)
}
_, st, err := loadEffectiveConfig(dir, true)
if err == nil || !strings.Contains(err.Error(), "changed since it was trusted") {
t.Errorf("err = %v", err)
}
if !st.Changed {
t.Errorf("status = %+v", st)
}
}
func TestLoadEffectiveConfigEnvSkips(t *testing.T) {
dir, _ := projectFixture(t, "", "[profiles.rev]\nmodel = \"haiku\"\n")
t.Setenv("CLAU_NO_PROJECT", "1")
cfg, st, err := loadEffectiveConfig(dir, true)
if err != nil {
t.Fatal(err)
}
if st.Path != "" {
t.Errorf("status = %+v, want no project", st)
}
if _, ok := cfg.Profiles["rev"]; ok {
t.Error("project profile applied despite CLAU_NO_PROJECT")
}
}
func TestLoadEffectiveConfigGlobalIsProjectFile(t *testing.T) {
dir := t.TempDir()
proj := filepath.Join(dir, ".clau.toml")
if err := os.WriteFile(proj, []byte("[profiles.rev]\nmodel = \"haiku\"\n"), 0o644); err != nil {
t.Fatal(err)
}
t.Setenv("CLAU_CONFIG", proj)
t.Setenv("XDG_STATE_HOME", t.TempDir())
cfg, st, err := loadEffectiveConfig(dir, true)
if err != nil {
t.Fatal(err)
}
if st.Path != "" {
t.Errorf("global file must not double-apply as project layer: %+v", st)
}
if cfg.Profiles["rev"].Model != "haiku" {
t.Errorf("global config lost: %+v", cfg.Profiles)
}
}
func TestLoadEffectiveConfigTrustedParseError(t *testing.T) {
dir, proj := projectFixture(t, "", "[models\n")
trustNow(t, proj)
_, _, err := loadEffectiveConfig(dir, true)
if err == nil || !strings.Contains(err.Error(), proj) {
t.Errorf("err = %v, want parse error naming the project file", err)
}
}
func TestProjectDeclarations(t *testing.T) {
p := filepath.Join(t.TempDir(), ".clau.toml")
body := "[models]\ng = \"glm-5.2\"\n\n[profiles.deploy]\nflags = [\"-p\"]\n"
if err := os.WriteFile(p, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
models, profiles, efforts, err := projectDeclarations(p)
if err != nil {
t.Fatal(err)
}
if !models["g"] || len(models) != 1 {
t.Errorf("models = %v", models)
}
if !profiles["deploy"] || len(profiles) != 1 {
t.Errorf("profiles = %v", profiles)
}
if efforts {
t.Error("no [efforts] table, but efforts reported as declared")
}
}
func TestProjectDeclarationsEfforts(t *testing.T) {
p := filepath.Join(t.TempDir(), ".clau.toml")
if err := os.WriteFile(p, []byte("[efforts]\n6 = \"max\"\n"), 0o644); err != nil {
t.Fatal(err)
}
_, _, efforts, err := projectDeclarations(p)
if err != nil {
t.Fatal(err)
}
if !efforts {
t.Error("[efforts] table not reported as declared")
}
// An empty table is inert (applyConfigFile keeps the global ladder).
if err := os.WriteFile(p, []byte("[efforts]\n"), 0o644); err != nil {
t.Fatal(err)
}
if _, _, efforts, err = projectDeclarations(p); err != nil || efforts {
t.Errorf("empty [efforts]: efforts=%v err=%v, want false nil", efforts, err)
}
}