package schedule import ( "context" "github.com/carlosframework/platform/internal/hostwrite" "github.com/carlosframework/platform/internal/registry" "path/filepath" "testing" ) type replacementWork struct{ valid *bool } func (w replacementWork) With(_ context.Context, _ hostwrite.Identity, fn func() error) error { if !*w.valid { return hostwrite.ErrUnavailable } return fn() } func TestSelectedHeldReplacementKeepsRegistrationAndRefusesStale(t *testing.T) { reg, err := registry.Open(filepath.Join(t.TempDir(), "registry.db")) if err != nil { t.Fatal(err) } defer reg.Close() valid := true selected := SelectedTimers{Store: reg, Work: replacementWork{&valid}, Context: context.Background(), Host: "shop.example"} rows := []registry.Timer{{Host: "shop.example", Name: "one", Kind: "once", Spec: "spec", Path: "/tick", NextAt: 123, DueAt: 120, Registration: "from-source"}} if err := selected.ReplaceTimers("shop.example", rows); err != nil { t.Fatal(err) } got, err := reg.GetTimer("shop.example", "one") if err != nil || got.Registration != "from-source" || got.DueAt != 120 { t.Fatal(got, err) } valid = false if err := selected.ReplaceTimers("shop.example", nil); err == nil { t.Fatal("stale landing cleared timers") } got, err = reg.GetTimer("shop.example", "one") if err != nil || got.Registration != "from-source" { t.Fatal(got, err) } }