//go:build linux package tests import ( "context" "os" "path/filepath" "testing" "time" "hth/internal/app" "hth/internal/capture" hthv1 "hth/internal/gen/hth/api/v1" "hth/internal/grpcserver" "hth/internal/testutil" ) func TestCaptureReplayCapabilityHashRoundTrip(t *testing.T) { a, obs, _ := startMockApp(t) out := t.TempDir() conn, err := grpcserver.DialUnix(obs, 2*time.Second) if err != nil { t.Fatal(err) } defer conn.Close() cli := hthv1.NewObserveServiceClient(conn) ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() caps, err := cli.GetCapabilities(ctx, &hthv1.GetCapabilitiesRequest{DeviceId: "prod2u-0"}) if err != nil { t.Fatal(err) } wantHash := caps.Capabilities.GetCapabilityHash() if wantHash == "" { t.Fatal("empty capability hash") } man := capture.Manifest{ APIVersion: app.APIVersion, APISchemaVersion: app.APIVersion, ProfileHash: caps.Capabilities.GetProtocolContractHash(), ProfileContractHash: caps.Capabilities.GetProtocolContractHash(), ProfileID: caps.Capabilities.GetProfileId(), ProfileRevision: caps.Capabilities.GetProfileRevision(), CapabilityHash: wantHash, DeviceID: "prod2u-0", StartedAt: time.Now().UTC(), Status: capture.StatusIncomplete, } if err := capture.WriteManifest(out, man); err != nil { t.Fatal(err) } f, err := os.Create(capture.RecordsPath(out)) if err != nil { t.Fatal(err) } ev, err := cli.SubscribeEvents(ctx, &hthv1.SubscribeEventsRequest{DeviceId: "prod2u-0"}) if err != nil { t.Fatal(err) } for i := 0; i < 5; i++ { e, err := ev.Recv() if err != nil { break } rec, err := capture.EventRecord(e) if err != nil { t.Fatal(err) } if err := capture.AppendRecord(f, rec); err != nil { t.Fatal(err) } } f.Close() man.Status = capture.StatusComplete if err := capture.Seal(out, man); err != nil { t.Fatal(err) } _ = a dir := t.TempDir() robs := filepath.Join(dir, "o.sock") rctl := filepath.Join(dir, "c.sock") yaml := ` api_version: hth.api.v1 device_id: prod2u-0 backend: replay profile: ` + testutil.ProfilePath(t) + ` runtime_root: ` + dir + ` replay: path: ` + out + ` speed: 1000 step: true sockets: observe: ` + robs + ` control: ` + rctl + ` poll: temperature_ms: 50 motor_status_ms: 40 watchdog: heartbeat_period_ms: 5000 lease: min_ttl_ms: 50 max_ttl_ms: 2000 safety: fan: 10 ptc: 0 door_close_percent: 0 ring_buffer_size: 32 subscriber_buffer_size: 4 grpc: reflection: false ` cfgPath := filepath.Join(dir, "hthd.yaml") if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil { t.Fatal(err) } ra, err := app.New(cfgPath, "replay", nil) if err != nil { t.Fatal(err) } rctx, rcancel := context.WithCancel(context.Background()) errCh := make(chan error, 1) go func() { errCh <- ra.Run(rctx) }() t.Cleanup(func() { rcancel(); ra.Stop(); <-errCh }) waitUnix(t, robs, 3*time.Second) rc, err := grpcserver.DialUnix(robs, 2*time.Second) if err != nil { t.Fatal(err) } defer rc.Close() rcli := hthv1.NewObserveServiceClient(rc) got, err := rcli.GetCapabilities(context.Background(), &hthv1.GetCapabilitiesRequest{DeviceId: "prod2u-0"}) if err != nil { t.Fatal(err) } if got.Capabilities.GetCapabilityHash() != wantHash { t.Fatalf("capability hash mock %s replay %s", wantHash, got.Capabilities.GetCapabilityHash()) } if _, err := rcli.StepReplay(context.Background(), &hthv1.StepReplayRequest{DeviceId: "prod2u-0"}); err == nil { t.Fatal("observe StepReplay must be denied by default") } waitUnix(t, rctl, 3*time.Second) ctlConn, err := grpcserver.DialUnix(rctl, 2*time.Second) if err != nil { t.Fatal(err) } defer ctlConn.Close() ccli := hthv1.NewControlServiceClient(ctlConn) step, err := ccli.StepReplay(context.Background(), &hthv1.StepReplayRequest{DeviceId: "prod2u-0"}) if err != nil { t.Fatal(err) } if !step.Advanced && step.Message == "" { t.Fatal("step") } snap, err := rcli.GetSnapshot(context.Background(), &hthv1.GetSnapshotRequest{DeviceId: "prod2u-0"}) if err != nil { t.Fatal(err) } if snap.Snapshot == nil { t.Fatal("snapshot") } if ev := step.GetEvent(); ev != nil { if ev.GetEventId() == 0 { t.Fatal("replay event_id must be assigned by the current daemon") } if ev.GetSourceEventId() != 0 && ev.GetSourceEventId() == ev.GetEventId() && ev.GetSourceCaptureId() == "" { t.Fatal("source provenance must remain distinct from the live replay event_id") } } evCtx, evCancel := context.WithTimeout(context.Background(), 2*time.Second) defer evCancel() evStream, err := rcli.SubscribeEvents(evCtx, &hthv1.SubscribeEventsRequest{DeviceId: "prod2u-0"}) if err != nil { t.Fatal(err) } var lastID uint64 for i := 0; i < 3; i++ { ev, err := evStream.Recv() if err != nil { break } if ev.GetEventId() == 0 { t.Fatal("replay event_id must be assigned by the current daemon") } if lastID != 0 && ev.GetEventId() <= lastID { t.Fatalf("replay event ids must be strictly increasing: %d then %d", lastID, ev.GetEventId()) } lastID = ev.GetEventId() if ev.GetSourceEventId() != 0 && ev.GetSourceEventId() == ev.GetEventId() && ev.GetSourceCaptureId() == "" { t.Fatal("source provenance must remain distinct from the live replay event_id") } } } func TestWatchdogStopsWhenActorFrozen(t *testing.T) { a, _, _ := startMockApp(t) if !a.WatchdogShouldPet() { t.Fatal("healthy mock should pet") } a.Dev.TestingSetProgress(time.Time{}) if a.WatchdogShouldPet() { t.Fatal("frozen actor must stop watchdog pet") } } func TestWatchdogKeepsPettingDuringBoundedScan(t *testing.T) { a, _, _ := startMockApp(t) if !a.WatchdogShouldPet() { t.Fatal("healthy mock should pet") } a.Dev.TestingSetInflightSince(5 * time.Second) if !a.WatchdogShouldPet() { t.Fatal("5s temperature scan must not stop systemd watchdog pet") } } func TestWatchdogStopsWhenSerialWorkerExceeded(t *testing.T) { a, _, _ := startMockApp(t) a.Dev.TestingSetInflightSince(a.Dev.MaxSerialTxn() + time.Second) if a.WatchdogShouldPet() { t.Fatal("serial worker beyond MaxSerialTxn must stop watchdog pet") } }