Skip to content

Instantly share code, notes, and snippets.

@hovsep
Last active March 19, 2026 19:43
Show Gist options
  • Select an option

  • Save hovsep/df068b69bc707e109be7e96cf5caeb56 to your computer and use it in GitHub Desktop.

Select an option

Save hovsep/df068b69bc707e109be7e96cf5caeb56 to your computer and use it in GitHub Desktop.
// What it checks: run simulation for 100ms and check the ECG, we must see a few R-peaks
func Test_Human(t *testing.T) {
tests := []struct {
name string
assertions func(t *testing.T, sim *step_sim.Simulation)
}{
{
name: "heart is beating",
assertions: func(t *testing.T, sim *step_sim.Simulation) {
var observedCardiacActivity []float64
var observedHeartRate []int
aggState := sim.FM.ComponentByName("aggregated_state")
require.NotNil(t, aggState)
sim.FM.SetupHooks(func(hooks *fmesh.Hooks) {
hooks.AfterRun(func(mesh *fmesh.FMesh) error {
sigAct := aggState.OutputByName("human-Leon::heart_cardiac_activation").Signals().First()
require.NotNil(t, sigAct)
observedCardiacActivity = append(observedCardiacActivity, helper.AsF64(sigAct))
sigRate := aggState.OutputByName("human-Leon::heart_rate").Signals().First()
require.NotNil(t, sigAct)
observedHeartRate = append(observedHeartRate, helper.AsInt(sigRate))
return nil
})
})
helper.WithRunningSimulation(sim, defaultSimulationDuration, func() {
assert.NotEmpty(t, observedCardiacActivity)
assert.NotEmpty(t, observedHeartRate)
rPeaksFound := 0
inPeak := false
// Check for R-peaks
for _, v := range observedCardiacActivity {
if v > 0.0 && !inPeak {
rPeaksFound++
inPeak = true
continue
}
if v == 0.0 {
inPeak = false
}
}
assert.Greater(t, rPeaksFound, 1)
assert.Less(t, rPeaksFound, 10)
})
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmdChan := make(chan step_sim.Command)
fm := getSimulationMesh()
sim := step_sim.NewSimulation(context.Background(), cmdChan, fm)
if tt.assertions != nil {
tt.assertions(t, sim)
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment