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>
393 lines
12 KiB
Swift
393 lines
12 KiB
Swift
// ClipboardReplyFeedbackStoreTests.swift
|
|
// OSGKeyboardTests
|
|
|
|
@testable import OSGKeyboardShared
|
|
import XCTest
|
|
|
|
@MainActor
|
|
final class ClipboardReplyFeedbackStoreTests: XCTestCase {
|
|
private var suiteName: String!
|
|
private var defaults: UserDefaults!
|
|
private var store: ClipboardReplyFeedbackStore!
|
|
|
|
override func setUp() {
|
|
super.setUp()
|
|
suiteName = "group.com.osgkeyboard.reply-feedback.\(UUID().uuidString)"
|
|
defaults = UserDefaults(suiteName: suiteName)!
|
|
defaults.removePersistentDomain(forName: suiteName)
|
|
store = ClipboardReplyFeedbackStore(defaults: defaults)
|
|
}
|
|
|
|
override func tearDown() {
|
|
defaults.removePersistentDomain(forName: suiteName)
|
|
super.tearDown()
|
|
}
|
|
|
|
func testSelectionAndVerifiedFinalEditAreRecorded() throws {
|
|
let candidates = makeCandidates()
|
|
let recordID = try XCTUnwrap(
|
|
store.begin(
|
|
sourceText: "周六下午去看展吗?",
|
|
candidates: candidates,
|
|
styleID: "user.personal"
|
|
)
|
|
)
|
|
let answerID = UUID()
|
|
|
|
store.recordSelection(
|
|
recordID: recordID,
|
|
candidateID: candidates[2].id,
|
|
answerID: answerID
|
|
)
|
|
store.recordFinalEdit(
|
|
answerID: answerID,
|
|
text: "可以呀,几点出发?🙂",
|
|
revision: 1
|
|
)
|
|
|
|
let record = try XCTUnwrap(store.records().first)
|
|
XCTAssertEqual(record.outcome, .selected)
|
|
XCTAssertEqual(record.selectedCandidate?.kind, .playful)
|
|
XCTAssertEqual(record.answerID, answerID)
|
|
XCTAssertEqual(record.finalText, "可以呀,几点出发?🙂")
|
|
XCTAssertEqual(record.finalRevision, 1)
|
|
}
|
|
|
|
func testUnverifiedOrUnrelatedEditIsIgnored() throws {
|
|
let candidates = makeCandidates()
|
|
let recordID = try XCTUnwrap(
|
|
store.begin(
|
|
sourceText: "今晚要不要一起吃饭?",
|
|
candidates: candidates,
|
|
styleID: nil
|
|
)
|
|
)
|
|
let answerID = UUID()
|
|
store.recordSelection(
|
|
recordID: recordID,
|
|
candidateID: candidates[0].id,
|
|
answerID: answerID
|
|
)
|
|
|
|
store.recordFinalEdit(answerID: UUID(), text: "无关修改", revision: 1)
|
|
store.recordFinalEdit(answerID: answerID, text: "零版本", revision: 0)
|
|
|
|
let record = try XCTUnwrap(store.records().first)
|
|
XCTAssertNil(record.finalText)
|
|
XCTAssertNil(record.finalRevision)
|
|
}
|
|
|
|
func testSensitiveSourceOrCandidateIsRejected() {
|
|
let candidates = makeCandidates()
|
|
|
|
XCTAssertNil(
|
|
store.begin(
|
|
sourceText: "123456",
|
|
candidates: candidates,
|
|
styleID: nil
|
|
)
|
|
)
|
|
var unsafeCandidates = candidates
|
|
unsafeCandidates[0] = ClipboardReplyCandidateSnapshot(
|
|
kind: .ordinary,
|
|
text: "Bearer abcdefghijklmnopqrstuvwxyz",
|
|
emotion: "neutral"
|
|
)
|
|
XCTAssertNil(
|
|
store.begin(
|
|
sourceText: "普通消息",
|
|
candidates: unsafeCandidates,
|
|
styleID: nil
|
|
)
|
|
)
|
|
XCTAssertTrue(store.records().isEmpty)
|
|
}
|
|
|
|
func testDuplicateCandidateKindsAreRejected() {
|
|
let duplicate = [
|
|
ClipboardReplyCandidateSnapshot(
|
|
kind: .ordinary,
|
|
text: "第一条",
|
|
emotion: "neutral"
|
|
),
|
|
ClipboardReplyCandidateSnapshot(
|
|
kind: .ordinary,
|
|
text: "第二条",
|
|
emotion: "warm"
|
|
)
|
|
]
|
|
|
|
XCTAssertNil(
|
|
store.begin(
|
|
sourceText: "普通消息",
|
|
candidates: duplicate,
|
|
styleID: nil
|
|
)
|
|
)
|
|
}
|
|
|
|
func testRecordsAreCappedAndExpiredLocally() {
|
|
let now = Date()
|
|
for offset in 0..<(ClipboardReplyFeedbackStore.maximumRecords + 5) {
|
|
_ = store.begin(
|
|
sourceText: "消息\(offset)",
|
|
candidates: makeCandidates(suffix: "\(offset)"),
|
|
styleID: nil,
|
|
now: now.addingTimeInterval(TimeInterval(offset))
|
|
)
|
|
}
|
|
|
|
XCTAssertEqual(
|
|
store.records(
|
|
now: now.addingTimeInterval(
|
|
TimeInterval(ClipboardReplyFeedbackStore.maximumRecords + 5)
|
|
)
|
|
).count,
|
|
ClipboardReplyFeedbackStore.maximumRecords
|
|
)
|
|
|
|
XCTAssertTrue(
|
|
store.records(
|
|
now: now.addingTimeInterval(
|
|
ClipboardReplyFeedbackStore.retentionInterval + 1_000
|
|
)
|
|
).isEmpty
|
|
)
|
|
}
|
|
|
|
func testDiscardDoesNotCreatePositiveSelectionEvidence() throws {
|
|
let recordID = try XCTUnwrap(
|
|
store.begin(
|
|
sourceText: "你怎么看?",
|
|
candidates: makeCandidates(),
|
|
styleID: nil
|
|
)
|
|
)
|
|
|
|
store.recordDiscard(recordID: recordID)
|
|
|
|
let record = try XCTUnwrap(store.records().first)
|
|
XCTAssertEqual(record.outcome, .discarded)
|
|
XCTAssertNil(record.selectedCandidateID)
|
|
XCTAssertNil(record.answerID)
|
|
XCTAssertEqual(store.learningExamples().first?.selection, .discarded)
|
|
}
|
|
|
|
func testAIHistoryRevisionBackfillsVerifiedFinalText() throws {
|
|
let candidates = makeCandidates()
|
|
let recordID = try XCTUnwrap(
|
|
store.begin(
|
|
sourceText: "这版可以直接发吗?",
|
|
candidates: candidates,
|
|
styleID: "user.personal"
|
|
)
|
|
)
|
|
let answerID = UUID()
|
|
store.recordSelection(
|
|
recordID: recordID,
|
|
candidateID: candidates[0].id,
|
|
answerID: answerID
|
|
)
|
|
let history = SpeechHistoryStore(
|
|
defaults: defaults,
|
|
replyFeedbackStore: store
|
|
)
|
|
_ = history.applyHistoryMutation(
|
|
HistoryMutation(
|
|
action: .append,
|
|
entryID: answerID,
|
|
text: candidates[0].text,
|
|
source: .ai
|
|
)
|
|
)
|
|
|
|
_ = history.applyHistoryMutation(
|
|
HistoryMutation(
|
|
action: .update,
|
|
entryID: answerID,
|
|
expectedRevision: 0,
|
|
text: "我再看一下这版,确认后回复你。"
|
|
)
|
|
)
|
|
|
|
let record = try XCTUnwrap(store.records().first)
|
|
XCTAssertEqual(record.finalText, "我再看一下这版,确认后回复你。")
|
|
XCTAssertEqual(record.finalRevision, 1)
|
|
}
|
|
|
|
func testSingleOrdinaryAcceptanceRemainsWeakLearningEvidence() throws {
|
|
let candidate = ClipboardReplyCandidateSnapshot(
|
|
kind: .ordinary,
|
|
text: "我先看一下,再回复你。",
|
|
emotion: "neutral"
|
|
)
|
|
let recordID = try XCTUnwrap(
|
|
store.begin(
|
|
sourceText: "这个方案可以吗?",
|
|
candidates: [candidate],
|
|
styleID: nil
|
|
)
|
|
)
|
|
store.recordSelection(
|
|
recordID: recordID,
|
|
candidateID: candidate.id,
|
|
answerID: candidate.id
|
|
)
|
|
|
|
let example = try XCTUnwrap(store.learningExamples().first)
|
|
XCTAssertEqual(example.selection, .ordinary)
|
|
XCTAssertEqual(example.ordinaryCandidate, candidate.text)
|
|
XCTAssertNil(example.formalCandidate)
|
|
XCTAssertNil(example.playfulCandidate)
|
|
}
|
|
|
|
func testSceneDecisionTeachesStyleOnlyAfterUserFinalEdit() throws {
|
|
let candidate = ClipboardReplyCandidateSnapshot(
|
|
kind: .invitationDecline,
|
|
text: "谢谢邀请,不过这次先不去了。",
|
|
emotion: "grateful"
|
|
)
|
|
let recordID = try XCTUnwrap(
|
|
store.begin(
|
|
sourceText: "周六一起吃饭吗?",
|
|
candidates: [candidate],
|
|
styleID: "user.personal"
|
|
)
|
|
)
|
|
store.recordSelection(
|
|
recordID: recordID,
|
|
candidateID: candidate.id,
|
|
answerID: candidate.id
|
|
)
|
|
|
|
XCTAssertTrue(store.learningExamples().isEmpty)
|
|
|
|
store.recordFinalEdit(
|
|
answerID: candidate.id,
|
|
text: "这周六不行,下次约~",
|
|
revision: 1
|
|
)
|
|
let example = try XCTUnwrap(store.learningExamples().first)
|
|
XCTAssertEqual(example.selection, .contextual)
|
|
XCTAssertEqual(example.finalEdit, "这周六不行,下次约~")
|
|
}
|
|
|
|
func testRecentConversationTurnsReturnsSelectedExchangesOldestFirst() throws {
|
|
let now = Date()
|
|
let first = try selectedRecord(
|
|
source: "你有空吗?",
|
|
reply: "有的,怎么了",
|
|
at: now.addingTimeInterval(-120)
|
|
)
|
|
let second = try selectedRecord(
|
|
source: "周六一起吃饭?",
|
|
reply: "好啊,几点",
|
|
at: now.addingTimeInterval(-30)
|
|
)
|
|
_ = (first, second)
|
|
|
|
let turns = store.recentConversationTurns(now: now)
|
|
XCTAssertEqual(turns.count, 2)
|
|
XCTAssertEqual(turns[0].incoming, "你有空吗?")
|
|
XCTAssertEqual(turns[0].reply, "有的,怎么了")
|
|
XCTAssertEqual(turns[1].incoming, "周六一起吃饭?")
|
|
XCTAssertEqual(turns[1].reply, "好啊,几点")
|
|
}
|
|
|
|
func testRecentConversationTurnsExcludesOutsideWindowAndCurrentMessage() throws {
|
|
let now = Date()
|
|
_ = try selectedRecord(
|
|
source: "很久以前的消息",
|
|
reply: "旧回复",
|
|
at: now.addingTimeInterval(-10 * 60)
|
|
)
|
|
_ = try selectedRecord(
|
|
source: "刚刚的消息",
|
|
reply: "新回复",
|
|
at: now.addingTimeInterval(-60)
|
|
)
|
|
|
|
// Outside the 5-minute window is dropped; the message being replied to
|
|
// right now is excluded so it is not duplicated as context.
|
|
XCTAssertEqual(
|
|
store.recentConversationTurns(now: now).map(\.incoming),
|
|
["刚刚的消息"]
|
|
)
|
|
XCTAssertTrue(
|
|
store.recentConversationTurns(now: now, excludingIncoming: "刚刚的消息").isEmpty
|
|
)
|
|
}
|
|
|
|
func testRecentConversationTurnsIgnoresAwaitingAndDiscarded() throws {
|
|
let now = Date()
|
|
_ = try XCTUnwrap(
|
|
store.begin(
|
|
sourceText: "还没选的消息",
|
|
candidates: makeCandidates(suffix: "a"),
|
|
styleID: nil,
|
|
now: now.addingTimeInterval(-40)
|
|
)
|
|
)
|
|
let discardID = try XCTUnwrap(
|
|
store.begin(
|
|
sourceText: "被丢弃的消息",
|
|
candidates: makeCandidates(suffix: "b"),
|
|
styleID: nil,
|
|
now: now.addingTimeInterval(-30)
|
|
)
|
|
)
|
|
store.recordDiscard(recordID: discardID)
|
|
|
|
XCTAssertTrue(store.recentConversationTurns(now: now).isEmpty)
|
|
}
|
|
|
|
@discardableResult
|
|
private func selectedRecord(
|
|
source: String,
|
|
reply: String,
|
|
at date: Date
|
|
) throws -> UUID {
|
|
let candidate = ClipboardReplyCandidateSnapshot(
|
|
kind: .ordinary,
|
|
text: reply,
|
|
emotion: "neutral"
|
|
)
|
|
let recordID = try XCTUnwrap(
|
|
store.begin(
|
|
sourceText: source,
|
|
candidates: [candidate],
|
|
styleID: nil,
|
|
now: date
|
|
)
|
|
)
|
|
store.recordSelection(
|
|
recordID: recordID,
|
|
candidateID: candidate.id,
|
|
answerID: candidate.id
|
|
)
|
|
return recordID
|
|
}
|
|
|
|
private func makeCandidates(
|
|
suffix: String = ""
|
|
) -> [ClipboardReplyCandidateSnapshot] {
|
|
[
|
|
ClipboardReplyCandidateSnapshot(
|
|
kind: .ordinary,
|
|
text: "普通回复\(suffix)",
|
|
emotion: "neutral"
|
|
),
|
|
ClipboardReplyCandidateSnapshot(
|
|
kind: .formal,
|
|
text: "正式回复\(suffix)",
|
|
emotion: "professional"
|
|
),
|
|
ClipboardReplyCandidateSnapshot(
|
|
kind: .playful,
|
|
text: "趣味回复\(suffix) 🙂",
|
|
emotion: "playful"
|
|
)
|
|
]
|
|
}
|
|
}
|