Files
OSGKeyboard/OSGKeyboardTests/FlowPiPRecoveryPolicyTests.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

231 lines
6.8 KiB
Swift

// FlowPiPRecoveryPolicyTests.swift
// OSGKeyboardTests
@testable import OSGKeyboard
import XCTest
final class FlowPiPRecoveryPolicyTests: XCTestCase {
// MARK: - Reconciliation
func testNoSessionIntentNeverStartsRecovery() {
XCTAssertEqual(
FlowPiPRecoveryPolicy.reconciliationDecision(
wantsActiveSession: false,
isAppForeground: true,
isPictureInPictureActive: false
),
.noSessionIntent
)
}
func testActivePiPIsIdempotent() {
XCTAssertEqual(
FlowPiPRecoveryPolicy.reconciliationDecision(
wantsActiveSession: true,
isAppForeground: true,
isPictureInPictureActive: true
),
.alreadyActive
)
}
func testTakeoverWaitsWhileAppIsInBackground() {
XCTAssertEqual(
FlowPiPRecoveryPolicy.reconciliationDecision(
wantsActiveSession: true,
isAppForeground: false,
isPictureInPictureActive: false
),
.waitForForeground
)
}
func testReturningToForegroundStartsRecovery() {
XCTAssertEqual(
FlowPiPRecoveryPolicy.reconciliationDecision(
wantsActiveSession: true,
isAppForeground: true,
isPictureInPictureActive: false
),
.startRecovery
)
}
// MARK: - Bounded retry
func testRetryPolicyUsesThreeIncreasingBoundedAttempts() {
XCTAssertEqual(FlowPiPRecoveryPolicy.maxAttempts, 3)
XCTAssertEqual(
(1...FlowPiPRecoveryPolicy.maxAttempts).map {
FlowPiPRecoveryPolicy.retryDelay(beforeAttempt: $0)
},
[0, 0.25, 0.6]
)
XCTAssertEqual(FlowPiPRecoveryPolicy.totalBudget, 5)
}
func testRetryTimeoutsNeverExceedRemainingBudget() {
XCTAssertEqual(
FlowPiPRecoveryPolicy.hostTimeout(attempt: 1, remainingBudget: 0.4),
0.4
)
XCTAssertEqual(
FlowPiPRecoveryPolicy.hostTimeout(attempt: 2, remainingBudget: 4),
0.25
)
XCTAssertEqual(
FlowPiPRecoveryPolicy.activeTimeout(remainingBudget: 0.3),
0.3
)
XCTAssertEqual(
FlowPiPRecoveryPolicy.activeTimeout(remainingBudget: 4),
1.4
)
XCTAssertEqual(
FlowPiPRecoveryPolicy.activeTimeout(remainingBudget: 0.01),
0.01
)
}
// MARK: - Late completion safety
func testCurrentForegroundOperationCanContinue() {
XCTAssertTrue(
FlowPiPRecoveryPolicy.canContinue(
operation: 7,
currentOperation: 7,
wantsActiveSession: true,
isAppForeground: true,
taskIsCancelled: false
)
)
}
func testOldGenerationCannotOverwriteNewRecovery() {
XCTAssertFalse(
FlowPiPRecoveryPolicy.canContinue(
operation: 6,
currentOperation: 7,
wantsActiveSession: true,
isAppForeground: true,
taskIsCancelled: false
)
)
}
func testExplicitEndPreventsLateRecovery() {
XCTAssertFalse(
FlowPiPRecoveryPolicy.canContinue(
operation: 7,
currentOperation: 8,
wantsActiveSession: false,
isAppForeground: true,
taskIsCancelled: true
)
)
}
func testBackgroundAndCancellationBothStopRecovery() {
XCTAssertFalse(
FlowPiPRecoveryPolicy.canContinue(
operation: 7,
currentOperation: 7,
wantsActiveSession: true,
isAppForeground: false,
taskIsCancelled: false
)
)
XCTAssertFalse(
FlowPiPRecoveryPolicy.canContinue(
operation: 7,
currentOperation: 7,
wantsActiveSession: true,
isAppForeground: true,
taskIsCancelled: true
)
)
}
}
final class FlowPiPLossReportGateTests: XCTestCase {
/// The backgrounded heartbeat re-evaluates a lost PiP every second. One
/// incident must produce one report, not one per tick.
func testRepeatedLossChecksWithinOneEpisodeReportOnce() {
var gate = FlowPiPLossReportGate()
XCTAssertTrue(gate.shouldReport())
for _ in 0..<30 {
XCTAssertFalse(gate.shouldReport())
}
}
func testPiPBecomingActiveOpensANewEpisode() {
var gate = FlowPiPLossReportGate()
XCTAssertTrue(gate.shouldReport())
XCTAssertFalse(gate.shouldReport())
gate.pipBecameActive()
XCTAssertTrue(gate.shouldReport())
}
}
final class FlowSessionWarningReportGateTests: XCTestCase {
/// The whole point: a toast raised by a path nobody instrumented must still
/// produce a report.
func testUninstrumentedWarningIsReported() {
var gate = FlowSessionWarningReportGate()
XCTAssertTrue(
gate.shouldReport(
warning: "cannot start voice session",
lastExplicitReportAt: nil,
now: Date()
)
)
}
/// A path that already wrote a precise report must not be double-counted.
func testWarningIsSuppressedWhenAnExplicitReportJustLanded() {
var gate = FlowSessionWarningReportGate()
let now = Date()
XCTAssertFalse(
gate.shouldReport(
warning: "permissions missing",
lastExplicitReportAt: now.addingTimeInterval(-0.2),
now: now
)
)
}
func testOlderExplicitReportDoesNotSuppressANewWarning() {
var gate = FlowSessionWarningReportGate()
let now = Date()
XCTAssertTrue(
gate.shouldReport(
warning: "cannot start voice session",
lastExplicitReportAt: now.addingTimeInterval(
-(FlowSessionWarningReportGate.duplicateGrace + 1)
),
now: now
)
)
}
/// A toast that stays up must not re-report on every state republish.
func testSameWarningReportsOnlyOncePerEpisode() {
var gate = FlowSessionWarningReportGate()
let now = Date()
XCTAssertTrue(
gate.shouldReport(warning: "same", lastExplicitReportAt: nil, now: now)
)
XCTAssertFalse(
gate.shouldReport(warning: "same", lastExplicitReportAt: nil, now: now)
)
// Cleared and shown again is a genuinely new toast.
gate.warningCleared()
XCTAssertTrue(
gate.shouldReport(warning: "same", lastExplicitReportAt: nil, now: now)
)
}
}