From 9d4f977026bbb4516f8cc97f715c0dc28bf2a13f Mon Sep 17 00:00:00 2001
From: Ariver <shanghai3168@gmail.com>
Date: Mon, 29 Jun 2026 20:16:29 +0800
Subject: [PATCH] Add performance diagnostics build

---
 privatevoice.src/app_live_caption_test.go |  149 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 149 insertions(+), 0 deletions(-)

diff --git a/privatevoice.src/app_live_caption_test.go b/privatevoice.src/app_live_caption_test.go
index 55b8316..c8380a7 100644
--- a/privatevoice.src/app_live_caption_test.go
+++ b/privatevoice.src/app_live_caption_test.go
@@ -3,6 +3,7 @@
 import (
 	"strings"
 	"testing"
+	"time"
 )
 
 func TestLiveCaptionDisplayTextFitsOverlayBuffer(t *testing.T) {
@@ -18,3 +19,151 @@
 		t.Fatalf("caption = %q, want leading truncation marker", got)
 	}
 }
+
+func TestReleaseTailCaptureDelayUsesEngineCapability(t *testing.T) {
+	a := &App{}
+	a.replaceEngine(tailCaptureTestEngine{delay: 300 * time.Millisecond})
+
+	if got := a.releaseTailCaptureDelay(); got != 300*time.Millisecond {
+		t.Fatalf("releaseTailCaptureDelay() = %v, want 300ms", got)
+	}
+}
+
+func TestReleaseTailCaptureDelayCanBeDisabledByEngineCapability(t *testing.T) {
+	a := &App{}
+	a.replaceEngine(tailCaptureTestEngine{delay: 0})
+
+	if got := a.releaseTailCaptureDelay(); got != 0 {
+		t.Fatalf("releaseTailCaptureDelay() = %v, want 0", got)
+	}
+}
+
+func TestReleaseTailCaptureDelayDefaultsToProtectionWindow(t *testing.T) {
+	a := &App{}
+	a.replaceEngine(plainTestEngine{})
+
+	if got := a.releaseTailCaptureDelay(); got != defaultReleaseTailDelay {
+		t.Fatalf("releaseTailCaptureDelay() = %v, want %v", got, defaultReleaseTailDelay)
+	}
+}
+
+func TestReleaseTailCaptureDelayDisabledWithoutEngine(t *testing.T) {
+	a := &App{}
+	a.replaceEngine(nil)
+
+	if got := a.releaseTailCaptureDelay(); got != 0 {
+		t.Fatalf("releaseTailCaptureDelay() = %v, want 0 without engine", got)
+	}
+}
+
+func TestHoldPreCaptureUsesEngineCapability(t *testing.T) {
+	a := &App{}
+	a.replaceEngine(holdPreCaptureTestEngine{enabled: true})
+
+	if !a.holdPreCaptureEnabled.Load() {
+		t.Fatal("hold pre-capture should be enabled by engine capability")
+	}
+}
+
+func TestHoldPreCaptureCanBeDisabledByEngineCapability(t *testing.T) {
+	a := &App{}
+	a.replaceEngine(holdPreCaptureTestEngine{enabled: false})
+
+	if a.holdPreCaptureEnabled.Load() {
+		t.Fatal("hold pre-capture should respect engine capability false")
+	}
+}
+
+func TestHoldPreCaptureDefaultsToTrueForReadyEngine(t *testing.T) {
+	a := &App{}
+	a.replaceEngine(plainTestEngine{})
+
+	if !a.holdPreCaptureEnabled.Load() {
+		t.Fatal("hold pre-capture should default to true for ready engines")
+	}
+}
+
+func TestHoldPreCaptureDisabledWithoutEngine(t *testing.T) {
+	a := &App{}
+	a.replaceEngine(nil)
+
+	if a.holdPreCaptureEnabled.Load() {
+		t.Fatal("hold pre-capture should be disabled without an engine")
+	}
+}
+
+func TestShouldStartHoldPreCaptureLocked(t *testing.T) {
+	a := &App{}
+	a.holdPreCaptureEnabled.Store(true)
+
+	if !a.shouldStartHoldPreCaptureLocked() {
+		t.Fatal("expected hold pre-capture to start when enabled and idle")
+	}
+
+	a.isRecording = true
+	if a.shouldStartHoldPreCaptureLocked() {
+		t.Fatal("should not pre-capture while already recording")
+	}
+	a.isRecording = false
+
+	a.isStoppingRecording = true
+	if a.shouldStartHoldPreCaptureLocked() {
+		t.Fatal("should not pre-capture while stopping")
+	}
+	a.isStoppingRecording = false
+
+	a.isFreetalking = true
+	if a.shouldStartHoldPreCaptureLocked() {
+		t.Fatal("should not pre-capture while tap recording is active")
+	}
+	a.isFreetalking = false
+
+	a.lastStopTime = time.Now()
+	if a.shouldStartHoldPreCaptureLocked() {
+		t.Fatal("should not pre-capture during post-stop debounce")
+	}
+}
+
+func TestStartRecordingIgnoredWhileStoppingRecording(t *testing.T) {
+	a := &App{isStoppingRecording: true}
+
+	a.startRecordingLocked()
+
+	if a.isRecording {
+		t.Fatal("startRecordingLocked should not start while stop tail capture is pending")
+	}
+}
+
+func TestStartTapRecordingIgnoredWhileStoppingRecording(t *testing.T) {
+	a := &App{isStoppingRecording: true}
+
+	a.startTapRecordingLocked()
+
+	if a.isRecording || a.isFreetalking {
+		t.Fatal("startTapRecordingLocked should not start while stop tail capture is pending")
+	}
+}
+
+type plainTestEngine struct{}
+
+func (plainTestEngine) Recognize([]float32) (string, error) { return "", nil }
+func (plainTestEngine) HardwareInfo() string                { return "test" }
+func (plainTestEngine) Close()                              {}
+
+type tailCaptureTestEngine struct {
+	plainTestEngine
+	delay time.Duration
+}
+
+func (e tailCaptureTestEngine) ReleaseTailCaptureDelay() time.Duration {
+	return e.delay
+}
+
+type holdPreCaptureTestEngine struct {
+	plainTestEngine
+	enabled bool
+}
+
+func (e holdPreCaptureTestEngine) HoldPreCaptureEnabled() bool {
+	return e.enabled
+}

--
Gitblit v1.9.3