devbox/internal/daemon/pause_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

120 lines
3.7 KiB
Go

package daemon
import (
"context"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/shoemoney/devbox/internal/config"
"github.com/shoemoney/devbox/internal/hub"
"github.com/shoemoney/devbox/internal/hub/blobstore"
"github.com/shoemoney/devbox/internal/hub/meta"
"github.com/shoemoney/devbox/internal/transport"
)
// absent reports whether path stays absent for the whole window. It's the
// inverse of waitFor: it proves a change did NOT propagate.
func absent(path string, d time.Duration) bool {
deadline := time.Now().Add(d)
for time.Now().Before(deadline) {
if _, err := os.Stat(path); err == nil {
return false
}
time.Sleep(50 * time.Millisecond)
}
return true
}
// TestPauseForAutoResumes proves PauseFor pauses immediately and clears the
// pause on its own after the duration, and that a plain Pause cancels a pending
// auto-resume (an explicit indefinite pause must override a scheduled resume).
func TestPauseForAutoResumes(t *testing.T) {
d, err := New(t.TempDir(), config.Daemon{}, "h", t.Logf)
if err != nil {
t.Fatal(err)
}
d.PauseFor(40 * time.Millisecond)
if !d.StateSnapshot().Paused {
t.Fatal("PauseFor should pause immediately")
}
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) && d.StateSnapshot().Paused {
time.Sleep(10 * time.Millisecond)
}
if d.StateSnapshot().Paused {
t.Fatal("PauseFor did not auto-resume after its duration")
}
// A plain Pause after scheduling an auto-resume must stay paused indefinitely.
d.PauseFor(40 * time.Millisecond)
d.Pause() // cancels the pending 40ms auto-resume
time.Sleep(200 * time.Millisecond)
if !d.StateSnapshot().Paused {
t.Fatal("plain Pause did not cancel the pending auto-resume")
}
}
// TestPauseGatesSyncing is the load-bearing pause test: while paused, a local
// edit must NOT reach the peer; after resume it must. If Pause() were wired but
// runMount still synced through the trigger, the "absent while paused" assertion
// would fail — so this test fails loudly if pause doesn't gate syncing.
func TestPauseGatesSyncing(t *testing.T) {
db, err := meta.Open(":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
store, err := blobstore.NewDisk(t.TempDir())
if err != nil {
t.Fatal(err)
}
srv := httptest.NewServer(hub.NewServer(db, store).Handler())
defer srv.Close()
bearerA := join(t, db, srv.URL, "alice")
bearerB := join(t, db, srv.URL, "bob")
ca := transport.New(srv.URL)
ca.SetBearer(bearerA)
if err := ca.Publish("s"); err != nil {
t.Fatal(err)
}
rootA, rootB := t.TempDir(), t.TempDir()
dirA := t.TempDir()
dirB := t.TempDir()
cfgA := config.Daemon{Hub: srv.URL, Bearer: bearerA, Mounts: []config.Mount{{Share: "s", Local: rootA, Hub: srv.URL}}}
cfgB := config.Daemon{Hub: srv.URL, Bearer: bearerB, Mounts: []config.Mount{{Share: "s", Local: rootB, Hub: srv.URL}}}
dA, err := New(dirA, cfgA, "alice", t.Logf)
if err != nil {
t.Fatal(err)
}
dB, err := New(dirB, cfgB, "bob", t.Logf)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go dA.Run(ctx)
go dB.Run(ctx)
time.Sleep(300 * time.Millisecond) // let both subscribe + do initial sync
// Pause Alice, then make a local edit. It must NOT reach Bob while paused.
dA.Pause()
writeF(t, rootA, "while-paused.txt", "should not propagate yet\n")
peerPath := filepath.Join(rootB, "while-paused.txt")
if !absent(peerPath, 2*time.Second) {
t.Fatal("edit propagated while paused — Pause() did not gate syncing")
}
// Resume Alice: the buffered edit must now converge to Bob.
dA.Resume()
if !waitFor(peerPath, "should not propagate yet\n", 10*time.Second) {
t.Fatal("edit did not propagate after resume — Resume() did not catch up")
}
}