2026-04-04 12:44:15 -03:00
|
|
|
package state
|
|
|
|
|
|
|
|
|
|
import (
|
2026-04-04 22:24:56 -03:00
|
|
|
"os"
|
2026-04-04 12:44:15 -03:00
|
|
|
"path/filepath"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestWriteReadIntentRoundTrip(t *testing.T) {
|
|
|
|
|
p := filepath.Join(t.TempDir(), "intent.json")
|
|
|
|
|
if err := MustWriteIntent(p, IntentShuttingDown, "ups-threshold", "daemon"); err != nil {
|
|
|
|
|
t.Fatalf("write intent: %v", err)
|
|
|
|
|
}
|
|
|
|
|
in, err := ReadIntent(p)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("read intent: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if in.State != IntentShuttingDown {
|
|
|
|
|
t.Fatalf("expected state %q, got %q", IntentShuttingDown, in.State)
|
|
|
|
|
}
|
|
|
|
|
if in.Source != "daemon" {
|
|
|
|
|
t.Fatalf("expected source daemon, got %q", in.Source)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMustWriteIntentRejectsUnknownState(t *testing.T) {
|
|
|
|
|
p := filepath.Join(t.TempDir(), "intent.json")
|
|
|
|
|
if err := MustWriteIntent(p, "weird", "x", "y"); err == nil {
|
|
|
|
|
t.Fatalf("expected invalid state error")
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-04 22:24:56 -03:00
|
|
|
|
|
|
|
|
func TestReadIntentAutoHealsCorruptJSON(t *testing.T) {
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
p := filepath.Join(dir, "intent.json")
|
|
|
|
|
if err := os.WriteFile(p, []byte("{broken"), 0o640); err != nil {
|
|
|
|
|
t.Fatalf("write corrupt intent: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
in, err := ReadIntent(p)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("read intent with auto-heal: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if in.State != "" {
|
|
|
|
|
t.Fatalf("expected empty state after heal, got %q", in.State)
|
|
|
|
|
}
|
|
|
|
|
raw, err := os.ReadFile(p)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("read healed intent file: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if string(raw) != "{}\n" {
|
|
|
|
|
t.Fatalf("expected healed intent payload '{}', got %q", string(raw))
|
|
|
|
|
}
|
|
|
|
|
matches, err := filepath.Glob(filepath.Join(dir, "intent.json.corrupt-*"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("glob backup files: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if len(matches) != 1 {
|
|
|
|
|
t.Fatalf("expected 1 backup file, got %d (%v)", len(matches), matches)
|
|
|
|
|
}
|
|
|
|
|
}
|