Adopt a consistent testing standard that prevents flaky behavior, leaked goroutines, and false positives.

1) Always register cleanup for started resources

bf, err := Init(ctx, cfg)
require.NoError(t, err)
t.Cleanup(func() { bf.Shutdown() })

2) Make concurrency/time-sensitive tests deterministic

deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
  if body.closeCount.Load() > 0 { break }
  time.Sleep(2 * time.Millisecond)
}
stop(); cleanup()

3) Ensure assertions are non-vacuous and validate invariants

attachBilledUsageFromContext(ctx, bifrostErr)
// Mutate original after attach; snapshot must not change.
usage.PromptTokens = 999
require.Equal(t, 10, bifrostErr.ExtraFields.BilledUsage.PromptTokens)

4) Keep mocks faithful to production side effects/commit semantics

candidate := budgets[i]          // copy
candidate.IsCalendarAligned = calendarAligned
if err := candidate.SetOverrideAt(...); err != nil { return nil, err }
budgets[i] = candidate          // commit only on success

5) Fail fast during test setup

if ok := a.GateSend(traceID, chunk0, false, false, ch, ctx); !ok {
  t.Fatalf("setup chunk 0: GateSend returned false")
}