turbo-editors/turbo-corepublic Fork 0
v1.0.2
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

📦 Turbo Core f3ade8d · on v1.0.2 · k33g · 8h ago
config_test.go · 290 lines · 7.7 KBGo Blame HistoryRaw
  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
package acp_test

import (
	"errors"
	"os"
	"path/filepath"
	"strings"
	"testing"

	"rickub.com/turbo-editors/turbo-core/acp"
	"rickub.com/turbo-editors/turbo-core/profile"
)

// testProfile is an editor whose user directory is a temporary one, so a test
// never reads the agents of whoever is running it.
func testProfile(t *testing.T) profile.Profile {
	t.Helper()

	p := profile.Profile{Name: "Turbo Test", Slug: "turbo-test", Language: "Test", ToolsMenu: "~T~est"}
	t.Setenv(p.DirEnvVar(), t.TempDir())
	return p
}

// writeProject puts an agents file into a project directory and returns it.
func writeProject(t *testing.T, p profile.Profile, contents string) string {
	t.Helper()

	dir := t.TempDir()
	path := acp.ProjectPath(p, dir)
	if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
		t.Fatalf("making %s: %v", filepath.Dir(path), err)
	}
	if err := os.WriteFile(path, []byte(contents), 0o644); err != nil {
		t.Fatalf("writing %s: %v", path, err)
	}
	return dir
}

// writeUser puts an agents file where the user's own would be.
func writeUser(t *testing.T, p profile.Profile, contents string) {
	t.Helper()

	path := acp.UserPath(p)
	if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
		t.Fatalf("making %s: %v", filepath.Dir(path), err)
	}
	if err := os.WriteFile(path, []byte(contents), 0o644); err != nil {
		t.Fatalf("writing %s: %v", path, err)
	}
}

func TestAnAgentIsReadWithEveryKey(t *testing.T) {
	p := testProfile(t)
	dir := writeProject(t, p, `
[[agent]]
name    = "Bob"
command = "docker"
args    = ["agent", "serve", "acp", "./agent.yaml"]
env     = { TELEMETRY_ENABLED = "false" }
cwd     = "backend"
`)

	list, err := acp.Load(p, dir)
	if err != nil {
		t.Fatalf("Load() error = %v", err)
	}
	if list.Len() != 1 {
		t.Fatalf("read %d agents, want 1", list.Len())
	}

	agent := list.Agents()[0]
	switch {
	case agent.Name != "Bob":
		t.Errorf("name is %q", agent.Name)
	case agent.Command != "docker":
		t.Errorf("command is %q", agent.Command)
	case strings.Join(agent.Args, " ") != "agent serve acp ./agent.yaml":
		t.Errorf("args are %v", agent.Args)
	case agent.Env["TELEMETRY_ENABLED"] != "false":
		t.Errorf("env is %v", agent.Env)
	case agent.Cwd != "backend":
		t.Errorf("cwd is %q", agent.Cwd)
	}
}

func TestEnvMayAlsoBeWrittenAsASubTable(t *testing.T) {
	// Both spellings are ordinary TOML and the documentation offers both, so a
	// reader who picks the one this test does not cover must not be wrong.
	p := testProfile(t)
	dir := writeProject(t, p, `
[[agent]]
name    = "Bob"
command = "docker"

[agent.env]
TOKEN = "secret"
`)

	list, err := acp.Load(p, dir)
	if err != nil {
		t.Fatalf("Load() error = %v", err)
	}
	if got := list.Agents()[0].Env["TOKEN"]; got != "secret" {
		t.Errorf("env is %v", list.Agents()[0].Env)
	}
}

func TestAProjectsAgentReplacesOneOfYours(t *testing.T) {
	// The user's file is your habits and the project's is this repository's, so
	// the project's is the more specific statement of the two. The rule is the
	// one snippets already follow.
	p := testProfile(t)
	writeUser(t, p, `
[[agent]]
name    = "Bob"
command = "yours"

[[agent]]
name    = "Everywhere"
command = "kept"
`)
	dir := writeProject(t, p, `
[[agent]]
name    = "Bob"
command = "theirs"

[[agent]]
name    = "Only here"
command = "added"
`)

	list, err := acp.Load(p, dir)
	if err != nil {
		t.Fatalf("Load() error = %v", err)
	}
	if list.Len() != 3 {
		t.Fatalf("read %d agents, want 3: %v", list.Len(), list.Agents())
	}

	bob, _ := list.ByName("Bob")
	if bob.Command != "theirs" {
		t.Errorf("Bob runs %q, want the project's %q", bob.Command, "theirs")
	}
	if _, ok := list.ByName("Everywhere"); !ok {
		t.Error("the user's other agent was lost")
	}
	if _, ok := list.ByName("Only here"); !ok {
		t.Error("the project's own agent was lost")
	}
}

func TestTheOrderIsTheFilesOrder(t *testing.T) {
	// Somebody reordering the file expects the menu to reorder.
	p := testProfile(t)
	dir := writeProject(t, p, `
[[agent]]
name = "Third"
command = "c"

[[agent]]
name = "First"
command = "a"
`)

	list, err := acp.Load(p, dir)
	if err != nil {
		t.Fatalf("Load() error = %v", err)
	}
	if got := list.Agents()[0].Name; got != "Third" {
		t.Errorf("the first agent is %q, want the file's own first", got)
	}
}

func TestAMissingFileIsNotAnError(t *testing.T) {
	p := testProfile(t)

	list, err := acp.Load(p, t.TempDir())
	if err != nil {
		t.Fatalf("Load() error = %v", err)
	}
	if list.Len() != 0 {
		t.Errorf("read %d agents from nothing", list.Len())
	}
}

func TestABrokenFileIsRefusedWholesale(t *testing.T) {
	// A half-loaded menu offering three of your five agents is worse than an
	// error saying which line is wrong.
	p := testProfile(t)

	for _, bad := range []struct {
		name     string
		contents string
		says     string
	}{
		{"no name", "[[agent]]\ncommand = \"docker\"\n", "has no name"},
		{"no command", "[[agent]]\nname = \"Bob\"\n", "has no command"},
		{"two of a name", "[[agent]]\nname=\"Bob\"\ncommand=\"a\"\n\n[[agent]]\nname=\"Bob\"\ncommand=\"b\"\n", "two agents are called"},
		{"a key that is not one", "[[agent]]\nname=\"Bob\"\ncommand=\"a\"\ncomand=\"typo\"\n", "not a key"},
		{"not TOML at all", "[[agent}}\n", "reading"},
	} {
		t.Run(bad.name, func(t *testing.T) {
			dir := writeProject(t, p, bad.contents)

			list, err := acp.Load(p, dir)
			if err == nil {
				t.Fatalf("Load() accepted it and read %v", list.Agents())
			}
			if !strings.Contains(err.Error(), bad.says) {
				t.Errorf("Load() error = %q, want %q in it", err, bad.says)
			}
			if list.Len() != 0 {
				t.Errorf("Load() returned %d agents alongside the error", list.Len())
			}
		})
	}
}

func TestAMisspeltKeyIsRefusedRatherThanIgnored(t *testing.T) {
	// This is the one that matters most in practice: `comand` silently ignored
	// leaves an agent that cannot start and a file that looks right.
	p := testProfile(t)
	dir := writeProject(t, p, "[[agent]]\nname=\"Bob\"\ncommand=\"a\"\nenviron={X=\"1\"}\n")

	_, err := acp.Load(p, dir)
	if err == nil {
		t.Fatal("Load() accepted a key the format does not define")
	}
	if !strings.Contains(err.Error(), "environ") {
		t.Errorf("Load() error = %q, want the offending key named", err)
	}
}

func TestExistsSeesOnlyARegularFile(t *testing.T) {
	p := testProfile(t)

	empty := t.TempDir()
	if acp.Exists(p, empty) {
		t.Error("Exists() is true with no file")
	}

	dir := writeProject(t, p, "")
	if !acp.Exists(p, dir) {
		t.Error("Exists() is false with a file")
	}

	// A directory where the file should be is not something Load could read.
	inTheWay := t.TempDir()
	if err := os.MkdirAll(acp.ProjectPath(p, inTheWay), 0o755); err != nil {
		t.Fatalf("making the directory: %v", err)
	}
	if acp.Exists(p, inTheWay) {
		t.Error("Exists() counted a directory as the file")
	}
}

func TestCreateRefusesToOverwrite(t *testing.T) {
	p := testProfile(t)
	p.Templates.Agents = "# %[1]s and %[2]s\n"
	dir := t.TempDir()

	path, err := acp.Create(p, dir)
	if err != nil {
		t.Fatalf("Create() error = %v", err)
	}
	if _, err := acp.Create(p, dir); !errors.Is(err, acp.ErrExists) {
		t.Errorf("Create() error = %v, want ErrExists", err)
	}

	data, err := os.ReadFile(path)
	if err != nil {
		t.Fatalf("reading %s: %v", path, err)
	}
	if !strings.Contains(string(data), p.ProjectDir()) {
		t.Errorf("the created file is %q, want the project directory in it", data)
	}
	if !strings.Contains(string(data), acp.UserPath(p)) {
		t.Errorf("the created file is %q, want the user's path in it", data)
	}
}

func TestACommandLineReadsAsOne(t *testing.T) {
	agent := acp.Agent{Command: "docker", Args: []string{"agent", "serve", "acp", "a.yaml"}}
	if got, want := agent.CommandLine(), "docker agent serve acp a.yaml"; got != want {
		t.Errorf("CommandLine() = %q, want %q", got, want)
	}
	if got, want := (acp.Agent{Command: "solo"}).CommandLine(), "solo"; got != want {
		t.Errorf("CommandLine() = %q, want %q", got, want)
	}
}