package installguide import ( "context" "slices" "sync/atomic" "testing" "time" ) func TestManagerLifecycleAndCloseZeroResidue(t *testing.T) { var changes atomic.Int32 m := NewManager(func() { changes.Add(1) }) started := make(chan Guide, 2) exited := make(chan string, 2) m.runner = func(ctx context.Context, g Guide) error { started <- g <-ctx.Done() exited <- g.Name return nil } if !m.Start(context.Background(), Guide{Name: "mac", PID: 101}) { t.Fatal("Start returned false on open manager") } g := <-started if !m.HasActive("mac", 101) || m.HasActive("mac", 999) { t.Fatal("HasActive mismatch") } if names := m.ActiveNames(); !slices.Equal(names, []string{"mac"}) { t.Fatalf("ActiveNames = %v, want [mac]", names) } // Wrong PID must not signal unfold. if m.Unfold("mac", 999) { t.Fatal("Unfold succeeded for mismatched PID") } select { case <-g.Unfold: t.Fatal("signaled unfold for mismatched PID") default: } if !m.Unfold("mac", 101) { t.Fatal("Unfold failed for matching PID") } select { case <-g.Unfold: default: t.Fatal("unfold was not signaled for matching PID") } if m.CancelStopped(map[int]bool{101: true}) { t.Fatal("running guide was canceled") } // Close cancels all active guides and rejects future Start calls. m.Close() select { case got := <-exited: if got != "mac" { t.Fatalf("exited = %q, want mac", got) } case <-time.After(2 * time.Second): t.Fatal("active guide did not exit after Close") } if names := m.ActiveNames(); len(names) != 0 { t.Fatalf("ActiveNames after Close = %v, want empty", names) } if changes.Load() == 0 { t.Fatal("expected onChange callback after Close") } if m.Start(context.Background(), Guide{Name: "mac2", PID: 202}) { t.Fatal("Start succeeded after Close") } }