Ariver
2026-07-12 8d5843394f6ac48c7f23c9fbf73d0bed3d99b069
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package main
 
import (
    "strings"
    "sync/atomic"
    "testing"
    "time"
 
    "voicesnap/internal/engine"
)
 
func TestLiveCaptionDisplayTextFitsOverlayBuffer(t *testing.T) {
    input := strings.Repeat("这是一句实时字幕", 12)
    got := liveCaptionDisplayText(input)
    if got == "" {
        t.Fatal("caption should not be empty")
    }
    if len([]byte(got)) > liveCaptionMaxBytes {
        t.Fatalf("caption bytes = %d, want <= %d", len([]byte(got)), liveCaptionMaxBytes)
    }
    if !strings.HasPrefix(got, "...") {
        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 TestReleaseTailCaptureDelayDefaultsToNoProtectionWindow(t *testing.T) {
    a := &App{}
    a.replaceEngine(plainTestEngine{})
 
    if got := a.releaseTailCaptureDelay(); got != 0 {
        t.Fatalf("releaseTailCaptureDelay() = %v, want 0", got)
    }
}
 
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 TestSlowRecognitionReason(t *testing.T) {
    tests := []struct {
        name        string
        recognizeMS int64
        totalMS     int64
        want        string
    }{
        {name: "fast", recognizeMS: 2000, totalMS: 2500, want: ""},
        {name: "recognize", recognizeMS: 2001, totalMS: 2400, want: "recognize"},
        {name: "total", recognizeMS: 1000, totalMS: 2501, want: "total"},
        {name: "both", recognizeMS: 2001, totalMS: 2501, want: "recognize,total"},
    }
 
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if got := slowRecognitionReason(tt.recognizeMS, tt.totalMS); got != tt.want {
                t.Fatalf("slowRecognitionReason(%d, %d) = %q, want %q", tt.recognizeMS, tt.totalMS, got, tt.want)
            }
        })
    }
}
 
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 TestHasEngineReflectsCurrentEngine(t *testing.T) {
    a := &App{}
    if a.hasEngine() {
        t.Fatal("new app should not report an engine")
    }
 
    a.replaceEngine(plainTestEngine{})
    if !a.hasEngine() {
        t.Fatal("app should report an engine after replaceEngine")
    }
 
    a.replaceEngine(nil)
    if a.hasEngine() {
        t.Fatal("app should not report an engine after clearing")
    }
}
 
func TestInitEngineSerializesAndDiscardsStaleEngine(t *testing.T) {
    first := &closeTrackingTestEngine{name: "first"}
    second := &closeTrackingTestEngine{name: "second"}
    firstStarted := make(chan struct{})
    allowFirst := make(chan struct{})
    calls := atomic.Int32{}
 
    a := &App{
        engineFactory: func(initID uint64) (engine.Engine, engineMetadata, bool, error) {
            call := calls.Add(1)
            switch call {
            case 1:
                close(firstStarted)
                <-allowFirst
                return first, engineMetadata{InitID: initID, ResolvedModelID: "first", HardwareInfo: "first"}, true, nil
            case 2:
                return second, engineMetadata{InitID: initID, ResolvedModelID: "second", HardwareInfo: "second"}, true, nil
            default:
                t.Fatalf("unexpected engine factory call %d", call)
                return nil, engineMetadata{}, false, nil
            }
        },
    }
 
    doneFirst := make(chan struct{})
    go func() {
        a.initEngine()
        close(doneFirst)
    }()
 
    select {
    case <-firstStarted:
    case <-time.After(time.Second):
        t.Fatal("first engine init did not start")
    }
 
    doneSecond := make(chan struct{})
    go func() {
        a.initEngine()
        close(doneSecond)
    }()
 
    waitForCondition(t, time.Second, func() bool {
        return a.engineInitSeq.Load() == 2
    })
    close(allowFirst)
 
    select {
    case <-doneFirst:
    case <-time.After(time.Second):
        t.Fatal("first engine init did not finish")
    }
    select {
    case <-doneSecond:
    case <-time.After(time.Second):
        t.Fatal("second engine init did not finish")
    }
 
    a.engineMu.Lock()
    gotEngine := a.eng
    gotMeta := a.engineMeta
    a.engineMu.Unlock()
 
    if gotEngine != second {
        t.Fatalf("final engine = %#v, want second", gotEngine)
    }
    if !first.closed.Load() {
        t.Fatal("stale first engine should be closed")
    }
    if second.closed.Load() {
        t.Fatal("current second engine should remain open")
    }
    if gotMeta.ResolvedModelID != "second" || gotMeta.InitID != 2 {
        t.Fatalf("engine metadata = %+v, want second init metadata", gotMeta)
    }
}
 
func waitForCondition(t *testing.T, timeout time.Duration, fn func() bool) {
    t.Helper()
    deadline := time.Now().Add(timeout)
    for time.Now().Before(deadline) {
        if fn() {
            return
        }
        time.Sleep(10 * time.Millisecond)
    }
    t.Fatal("condition was not met before timeout")
}
 
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 closeTrackingTestEngine struct {
    plainTestEngine
    name   string
    closed atomic.Bool
}
 
func (e *closeTrackingTestEngine) HardwareInfo() string { return e.name }
func (e *closeTrackingTestEngine) Close()               { e.closed.Store(true) }
 
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
}