devbox/cmd/devbox/main_test.go
shoemoney 8c5d576a72 feat(device): --json output, streaming chunker, pause --for, per-mount exclude, rescan knob ✨
Five device-side wins from the feature review:

- 🤖 `--json` on status/log/conflicts: machine-readable output for fleet
  scripting/monitoring. status prefers live daemon state (paused +
  base-snapshot), falls back to disk; not-joined → {"joined":false}.
- 🌊 streaming chunker: chunk.SplitReader feeds FastCDC from an io.Reader so
  the manifest build no longer slurps whole files into RAM — a multi-GB file
  can't OOM the daemon. Identical boundaries/hashes (proven vs fragmented reads).
- ⏱️  `devbox pause --for 2h`: control socket carries a duration; the daemon
  schedules an auto-resume timer. Re-pause replaces it; plain pause/resume
  cancel it (no surprise resume).
- 🙈 per-mount `devbox mount --exclude <pat>`: device-local ignore layered on
  the shared .devignore (LoadIgnoreWith) — skip a local build dir without
  touching the shared file. Honored by daemon sync, restore, and deploy.
- 🎛️  rescan cadence is now settings.sync.rescan_seconds (was a hardcoded 60s
  const) — tune the watcher fallback per machine without a rebuild.

#6 (doctor non-zero exit) was already shipped — doctor returns an error on ❌.
Tests: SplitReader-vs-buffered, PauseFor auto-resume + cancel, wire duration,
LoadIgnoreWith layering, status/conflicts --json. go test ./... -race clean.
2026-06-30 12:29:02 -05:00

94 lines
2.7 KiB
Go

package main
import (
"bytes"
"encoding/json"
"os"
"strconv"
"strings"
"testing"
)
// TestStatusJSON proves `status --json` emits valid JSON and reports not-joined
// on a fresh config dir (the scriptable path for fleet monitoring).
func TestStatusJSON(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir()) // empty → unjoined device
var buf bytes.Buffer
cmd := statusCmd()
cmd.SetOut(&buf)
cmd.SetArgs([]string{"--json"})
if err := cmd.Execute(); err != nil {
t.Fatalf("status --json: %v", err)
}
var got statusJSON
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
t.Fatalf("status --json emitted invalid JSON: %v\n%s", err, buf.String())
}
if got.Joined {
t.Fatalf("fresh config dir should report joined=false, got %+v", got)
}
}
// TestConflictsJSON proves `conflicts --json` emits a JSON array (never null).
func TestConflictsJSON(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
var buf bytes.Buffer
cmd := conflictsCmd()
cmd.SetOut(&buf)
cmd.SetArgs([]string{"--json"})
if err := cmd.Execute(); err != nil {
t.Fatalf("conflicts --json: %v", err)
}
var got []struct {
Share string `json:"share"`
Path string `json:"path"`
}
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
t.Fatalf("conflicts --json emitted invalid JSON: %v\n%s", err, buf.String())
}
if len(got) != 0 {
t.Fatalf("no mounts should yield empty list, got %v", got)
}
}
// TestWritePidArbitration covers the O_EXCL claim, the live-daemon refusal, and
// stale-pidfile recovery.
func TestWritePidArbitration(t *testing.T) {
dir := t.TempDir()
// First claim succeeds and records our (live) pid.
if err := writePid(dir); err != nil {
t.Fatalf("first writePid: %v", err)
}
if pid, ok := runningPid(dir); !ok || pid != os.Getpid() {
t.Fatalf("runningPid = (%d,%v), want (%d,true)", pid, ok, os.Getpid())
}
// A second claim must refuse: the pid in the file (us) is alive.
if err := writePid(dir); err == nil || !strings.Contains(err.Error(), "already running") {
t.Fatalf("second writePid = %v, want 'already running'", err)
}
// A stale pidfile (a pid that isn't running) is reclaimed, not refused.
dead := 0x7fffffff // implausibly-high pid; Signal(0) returns ESRCH
if err := os.WriteFile(pidPath(dir), []byte(strconv.Itoa(dead)), 0o600); err != nil {
t.Fatal(err)
}
if _, ok := runningPid(dir); ok {
t.Fatal("runningPid reported a dead pid as alive")
}
if err := writePid(dir); err != nil {
t.Fatalf("reclaiming a stale pidfile: %v", err)
}
if pid, _ := runningPid(dir); pid != os.Getpid() {
t.Fatalf("after reclaim pid = %d, want %d", pid, os.Getpid())
}
// removePid clears it.
removePid(dir)
if _, ok := runningPid(dir); ok {
t.Fatal("pidfile still live after removePid")
}
}