package poweroff import ( "errors" "path/filepath" "testing" "time" ) // One accepted request per boot; the record is "current" only in the boot it was made in. func TestOnePerBootAndCurrentFollowsTheBoot(t *testing.T) { boot := "1000" s := New(filepath.Join(t.TempDir(), "poweroff.json"), func() string { return boot }) if s.Current() != nil { t.Fatal("no record yet, Current must be nil") } now := time.Date(2026, 10, 13, 18, 0, 0, 0, time.UTC) if _, err := s.Mark(now, 9201); err != nil { t.Fatalf("first Mark: %v", err) } if r := s.Current(); r == nil || !r.RequestedAt.Equal(now) || r.VMID != 9201 { t.Fatalf("Current after Mark = %+v", r) } if _, err := s.Mark(now.Add(time.Minute), 9201); !errors.Is(err, ErrAlreadyThisBoot) { t.Fatalf("second Mark in the same boot: err = %v, want ErrAlreadyThisBoot", err) } boot = "2000" // the machine was switched on again if s.Current() != nil { t.Fatal("a record from the previous boot must not be current") } if _, err := s.Mark(now.Add(time.Hour), 9201); err != nil { t.Fatalf("Mark in a new boot: %v", err) } } func TestUnknownBootFailsClosed(t *testing.T) { s := New(filepath.Join(t.TempDir(), "p.json"), func() string { return "" }) if _, err := s.Mark(time.Now(), 1); !errors.Is(err, ErrBootUnknown) { t.Fatalf("err = %v, want ErrBootUnknown", err) } }