Files
OSGKeyboard/OSGKeyboardTests/FlowAdaptiveDownsamplerTests.swift
T
rockyandClaude Opus 5 0023741455 feat(app): add crash visibility, memory relief, and clipboard reply work
Squashes the working tree that had accumulated on cloud-agent. Two
strands of work, kept in one commit at the maintainer's request.

Crash and stability (this session):

- Subscribe to MetricKit so crash, hang, CPU, and disk-write diagnostics
  are recorded on device. Keyboard-extension memory kills are the most
  likely way a user loses the keyboard mid-sentence and iOS does not
  classify them as crashes, so they never reach App Store Connect.
- Upload those reports to POST /v1/analytics/diagnostics from internal
  builds only (local Debug and TestFlight), reusing the analytics
  installation identifier and skipping entirely when analytics are off.
  App Store builds keep everything on device.
- Shed the Chinese engine, English lexicon, and in-flight pipeline work
  at the 40 MiB and 48 MiB budget thresholds instead of waiting for a
  system memory warning that jetsam often preempts.
- Install audio taps with the input node's live format through one
  shared adaptive resampler, so an audio-route change mid-recording no
  longer raises an uncatchable format-mismatch exception. Covers the
  dictation preview and the Mac recorder, which still used the pattern
  the Flow capture path had already moved away from.
- Harden the librime bridge: never pass a nil string across a nonnull
  boundary when a user dictionary holds malformed UTF-8, null-check the
  engine API vector, and distrust a candidate count without its array.
- Guard the expanded candidate grid against a list that shrinks while
  the collection view is still laying out against the previous count.
- Resolve localized text without the App Group so the screen shown when
  that container is missing renders its message instead of crashing on
  launch, which is the one situation it exists to handle.
- Gate CI on the App Group and Keychain wiring that the keyboard
  extension hard-depends on, with a self-test proving the check fails.

Clipboard, reply, and semantics (pre-existing working tree):

- Unify the reply center, refresh keyboard AI features, and add native
  event and reminder exporters.
- Expand clipboard semantic routing with authored corpora and a
  product-blind holdout, plus the scripts that build and validate them.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-18 17:31:29 -07:00

164 lines
7.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// FlowAdaptiveDownsamplerTests.swift
// OSGKeyboard · Tests
//
// `AVAudioEngine.installTap(format:)` raises an UNCATCHABLE NSException when
// the format handed to it no longer matches the input node's live format, and
// that kills the whole app. The fix everywhere is the same: install with
// `format: nil` and let this resampler absorb whatever rate actually arrives.
//
// These tests pin the behaviour that makes `format: nil` safe — converting
// across a mid-stream route change — plus the numeric clamp that keeps a
// degenerate route from trapping on the realtime audio thread.
import AVFoundation
@testable import OSGKeyboardHostSupport
import XCTest
final class FlowAdaptiveDownsamplerTests: XCTestCase {
private let targetFormat = AVAudioFormat(
commonFormat: .pcmFormatFloat32,
sampleRate: 16_000,
channels: 1,
interleaved: false
)
private func makeBuffer(sampleRate: Double, frames: AVAudioFrameCount) -> AVAudioPCMBuffer? {
guard let format = AVAudioFormat(
commonFormat: .pcmFormatFloat32,
sampleRate: sampleRate,
channels: 1,
interleaved: false
), let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: frames) else {
return nil
}
buffer.frameLength = frames
// A quiet ramp: silence would also convert, but non-zero samples prove
// the converter actually ran rather than handing back an empty scratch.
if let channel = buffer.floatChannelData?[0] {
for index in 0..<Int(frames) {
channel[index] = Float(index % 64) / 64.0 * 0.1
}
}
return buffer
}
func testConvertsHardwareRateToTargetRate() throws {
let target = try XCTUnwrap(targetFormat)
let downsampler = FlowAdaptiveDownsampler(targetFormat: target)
let buffer = try XCTUnwrap(makeBuffer(sampleRate: 48_000, frames: 4_096))
guard case .converted(let output) = downsampler.convertReusingScratch(buffer) else {
return XCTFail("expected a converted buffer for a 48 kHz tap")
}
XCTAssertEqual(output.format.sampleRate, 16_000)
XCTAssertGreaterThan(output.frameLength, 0)
}
func testRebuildsConverterWhenTheRouteChangesMidStream() throws {
let target = try XCTUnwrap(targetFormat)
let downsampler = FlowAdaptiveDownsampler(targetFormat: target)
// Built-in mic …
let wideband = try XCTUnwrap(makeBuffer(sampleRate: 48_000, frames: 4_096))
guard case .converted = downsampler.convertReusingScratch(wideband) else {
return XCTFail("expected the first buffer to convert")
}
// … then the user connects a headset and the node switches rate. This
// is the exact transition that crashed a tap installed with an
// explicit format.
let narrowband = try XCTUnwrap(makeBuffer(sampleRate: 24_000, frames: 4_096))
guard case .converted(let output) = downsampler.convertReusingScratch(narrowband) else {
return XCTFail("expected the post-route-change buffer to convert")
}
XCTAssertEqual(output.format.sampleRate, 16_000)
XCTAssertGreaterThan(output.frameLength, 0)
}
func testUpsamplesSubTargetTelephonyRates() throws {
let target = try XCTUnwrap(targetFormat)
let downsampler = FlowAdaptiveDownsampler(targetFormat: target)
let buffer = try XCTUnwrap(makeBuffer(sampleRate: 8_000, frames: 4_096))
// 4096 × (16k / 8k) = 8192 frames — exactly the scratch capacity, so
// this is the boundary the headroom constant was sized for.
guard case .converted(let output) = downsampler.convertReusingScratch(buffer) else {
return XCTFail("expected an 8 kHz telephony route to convert")
}
XCTAssertGreaterThan(output.frameLength, 0)
}
func testGrowsTheScratchForAnOversizedTapSliceInsteadOfDroppingAudio() throws {
let target = try XCTUnwrap(targetFormat)
let downsampler = FlowAdaptiveDownsampler(targetFormat: target)
// 4096 × (16k / 4k) = 16,384 output frames against the 8,192-frame
// starting scratch. `bufferSize:` is only a hint — macOS hands pro
// interfaces larger slices — so this must grow, not drop the audio.
let buffer = try XCTUnwrap(makeBuffer(sampleRate: 4_000, frames: 4_096))
guard case .converted(let output) = downsampler.convertReusingScratch(buffer) else {
return XCTFail("expected an oversized slice to grow the scratch and convert")
}
XCTAssertGreaterThan(output.frameLength, 8_192)
// The grown scratch is reused: a following ordinary buffer must still
// convert, and must not report the previous buffer's frame count.
let followUp = try XCTUnwrap(makeBuffer(sampleRate: 48_000, frames: 4_096))
guard case .converted(let second) = downsampler.convertReusingScratch(followUp) else {
return XCTFail("expected the next buffer to convert against the grown scratch")
}
XCTAssertLessThan(second.frameLength, output.frameLength)
}
func testRefusesAConversionPastTheGrowthCeiling() throws {
let target = try XCTUnwrap(targetFormat)
let downsampler = FlowAdaptiveDownsampler(targetFormat: target)
// 32,768 × (16k / 8k) = 65,536 output frames — past the 32,768 ceiling.
// Growth has a limit so a nonsense ratio cannot allocate without bound
// on the realtime thread; past it, dropping the frame is correct.
let buffer = try XCTUnwrap(makeBuffer(sampleRate: 8_000, frames: 32_768))
guard case .failed(let failure, _, let inputFrames, let wantedFrames) =
downsampler.convertReusingScratch(buffer) else {
return XCTFail("expected a conversion past the ceiling to be reported")
}
XCTAssertEqual(failure, .scratchOverflow)
XCTAssertEqual(inputFrames, 32_768)
// The report still carries a finite, usable number for diagnostics.
XCTAssertGreaterThan(wantedFrames, 0)
}
func testUnsupportedRateFailsCleanlyRatherThanTrapping() throws {
let target = try XCTUnwrap(targetFormat)
let downsampler = FlowAdaptiveDownsampler(targetFormat: target)
// A 1 Hz "route" is not something `AVAudioConverter` will build for.
// The contract that matters is the same either way: report the drop,
// never trap.
let buffer = try XCTUnwrap(makeBuffer(sampleRate: 1, frames: 4_096))
guard case .failed(let failure, _, _, _) =
downsampler.convertReusingScratch(buffer) else {
return XCTFail("expected a degenerate rate to be reported, not converted")
}
XCTAssertTrue(
failure == .converterCreateFailed || failure == .scratchOverflow,
"unexpected failure \(failure.label)"
)
}
func testRecoversAfterADroppedFrame() throws {
let target = try XCTUnwrap(targetFormat)
let downsampler = FlowAdaptiveDownsampler(targetFormat: target)
let degenerate = try XCTUnwrap(makeBuffer(sampleRate: 1, frames: 4_096))
_ = downsampler.convertReusingScratch(degenerate)
// One bad frame must not wedge the resampler for the rest of the
// session — the route can recover.
let healthy = try XCTUnwrap(makeBuffer(sampleRate: 48_000, frames: 4_096))
guard case .converted(let output) = downsampler.convertReusingScratch(healthy) else {
return XCTFail("expected the resampler to recover after a dropped frame")
}
XCTAssertGreaterThan(output.frameLength, 0)
}
}