Files
archie/cmd/archie-agent/main_test.go
T
sam 4c0a06df6d fix(container): write the task boot brief under .git
WriteTaskJSON wrote task.json to the worktree root -- the same worktree
the agent commits from. CommitAll stages with go-git's All option, and
go-git's Add does not honour .gitignore or .git/info/exclude, so the
brief was swept into the agent's commit and pushed onto every task
branch. That is how a task.json for issue #57 ended up committed in
the repo root.

This is the identical failure the prepared sentinel hit; worktree.go
already documents the fix. Nothing under .git can ever be tracked, so
put the brief there rather than papering over it with an exclude.
2026-07-31 01:13:08 +10:00

99 lines
2.6 KiB
Go

package main
import (
"encoding/json"
"log/slog"
"os"
"path/filepath"
"testing"
"github.com/samcharles93/archie-core/internal/container"
)
// gitDir returns a temp mount dir with the .git directory the daemon writes
// the boot brief into.
func gitDir(t *testing.T) string {
t.Helper()
dir := t.TempDir()
if err := os.MkdirAll(filepath.Join(dir, ".git"), 0o700); err != nil {
t.Fatal(err)
}
return dir
}
func TestBootTaskID(t *testing.T) {
log := slog.New(slog.DiscardHandler)
t.Run("no task.json -- shared pool mode", func(t *testing.T) {
id, ok := bootTaskID(t.TempDir(), log)
if ok || id != 0 {
t.Fatalf("bootTaskID() = (%d, %v), want (0, false)", id, ok)
}
})
t.Run("valid task.json -- dedicated per-task mode", func(t *testing.T) {
dir := gitDir(t)
data, err := json.Marshal(container.TaskPayload{ID: 42, Owner: "acme", Repo: "widget"})
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, ".git", "task.json"), data, 0o644); err != nil {
t.Fatal(err)
}
id, ok := bootTaskID(dir, log)
if !ok || id != 42 {
t.Fatalf("bootTaskID() = (%d, %v), want (42, true)", id, ok)
}
})
t.Run("malformed task.json falls back to shared pool mode", func(t *testing.T) {
dir := gitDir(t)
if err := os.WriteFile(filepath.Join(dir, ".git", "task.json"), []byte("not json"), 0o644); err != nil {
t.Fatal(err)
}
id, ok := bootTaskID(dir, log)
if ok || id != 0 {
t.Fatalf("bootTaskID() = (%d, %v), want (0, false)", id, ok)
}
})
t.Run("zero ID falls back to shared pool mode", func(t *testing.T) {
dir := gitDir(t)
data, _ := json.Marshal(container.TaskPayload{Owner: "acme", Repo: "widget"})
if err := os.WriteFile(filepath.Join(dir, ".git", "task.json"), data, 0o644); err != nil {
t.Fatal(err)
}
id, ok := bootTaskID(dir, log)
if ok || id != 0 {
t.Fatalf("bootTaskID() = (%d, %v), want (0, false)", id, ok)
}
})
}
func TestNATSConnectionSettings(t *testing.T) {
getenv := func(name string) string {
switch name {
case "NATS_URL":
return "nats://nats.example:4222"
case "NATS_TOKEN":
return "test-nats-token"
default:
return ""
}
}
t.Run("container environment", func(t *testing.T) {
url, token := natsConnectionSettings("", getenv)
if url != "nats://nats.example:4222" || token != "test-nats-token" {
t.Fatalf("natsConnectionSettings() = (%q, %q)", url, token)
}
})
t.Run("flag URL takes precedence", func(t *testing.T) {
url, token := natsConnectionSettings("nats://flag.example:4222", getenv)
if url != "nats://flag.example:4222" || token != "test-nats-token" {
t.Fatalf("natsConnectionSettings() = (%q, %q)", url, token)
}
})
}