c2f07bd8d2
Introduce a standalone macOS menu-bar app (OSGKeyboardMac) that reuses the platform-agnostic OSGKeyboardShared core: record -> cloud/local ASR -> polish -> insert. Local mode uses Qwen3-ASR via mlx-swift-asr (macOS 15+, Apple Silicon); iOS targets stay zero-SPM. Harden iCloud sync for multi-device correctness: - Per-field settings merge (appSettings.v2) so concurrent edits no longer clobber each other's unrelated fields. - Per-device usage statistics (G-Counter) that sum instead of max(). - Tombstoned dictionary/history merge so deletes propagate and entries can't resurrect. - API keys replicate via iCloud Keychain, never iCloud KVS JSON; pulling a legacy blob without key fields no longer wipes local Keychain entries. - Add a low-risk "Sync Now" action in Settings. Fix Flow keyboard mic state: stay orange until the host publishes a real ready contract, share a single MicVoiceAvailability gate, and self-heal stale cross-process heartbeat jitter instead of getting stuck. Extract shared storage (SpeechHistoryStore/UsageStatisticsStore, ConfigurationStore) into OSGKeyboardShared and add tests for the new sync/merge logic.
135 lines
4.8 KiB
Swift
135 lines
4.8 KiB
Swift
// UsageStatisticsCloudSyncTests.swift
|
|
// OSGKeyboardTests
|
|
//
|
|
// Hermetic tests for cumulative usage statistics iCloud merge.
|
|
|
|
import XCTest
|
|
@testable import OSGKeyboardShared
|
|
|
|
@MainActor
|
|
final class UsageStatisticsCloudSyncTests: XCTestCase {
|
|
|
|
private var suiteName: String!
|
|
private var defaults: UserDefaults!
|
|
private var store: AppGroupStore!
|
|
private var kvs: FakeUbiquitousKeyValueStore!
|
|
private var sync: UsageStatisticsCloudSync!
|
|
private let deviceA = "device-a"
|
|
private let deviceB = "device-b"
|
|
|
|
override func setUp() {
|
|
super.setUp()
|
|
suiteName = "group.com.osgkeyboard.shared.tests.usage.\(UUID().uuidString)"
|
|
defaults = UserDefaults(suiteName: suiteName)!
|
|
defaults.removePersistentDomain(forName: suiteName)
|
|
defaults.set(deviceA, forKey: "sync.deviceID.v1")
|
|
store = AppGroupStore(defaults: defaults)
|
|
store.setSettingsICloudSyncEnabled(true)
|
|
kvs = FakeUbiquitousKeyValueStore()
|
|
sync = UsageStatisticsCloudSync(kvs: kvs) { [unowned self] in store }
|
|
}
|
|
|
|
override func tearDown() {
|
|
defaults.removePersistentDomain(forName: suiteName)
|
|
super.tearDown()
|
|
}
|
|
|
|
func testGCounterMergeSumsAcrossDevices() {
|
|
let local = SyncedUsageStatisticsV2(devices: [
|
|
deviceA: UsageStatisticsDeviceSlice(
|
|
updatedAt: Date(timeIntervalSince1970: 100),
|
|
dictationDurationSeconds: 30,
|
|
dictationCharacterCount: 120,
|
|
translationCharacterCount: 10
|
|
),
|
|
])
|
|
let remote = SyncedUsageStatisticsV2(devices: [
|
|
deviceB: UsageStatisticsDeviceSlice(
|
|
updatedAt: Date(timeIntervalSince1970: 200),
|
|
dictationDurationSeconds: 45,
|
|
dictationCharacterCount: 80,
|
|
translationCharacterCount: 25
|
|
),
|
|
])
|
|
|
|
let merged = SyncedUsageStatisticsV2.merge(local: local, remote: remote).aggregated
|
|
|
|
XCTAssertEqual(merged.dictationDurationSeconds, 75)
|
|
XCTAssertEqual(merged.dictationCharacterCount, 200)
|
|
XCTAssertEqual(merged.translationCharacterCount, 35)
|
|
}
|
|
|
|
func testGCounterMergeTakesMaxForSameDevice() {
|
|
let local = SyncedUsageStatisticsV2(devices: [
|
|
deviceA: UsageStatisticsDeviceSlice(
|
|
updatedAt: Date(timeIntervalSince1970: 100),
|
|
dictationDurationSeconds: 30,
|
|
dictationCharacterCount: 120,
|
|
translationCharacterCount: 10
|
|
),
|
|
])
|
|
let remote = SyncedUsageStatisticsV2(devices: [
|
|
deviceA: UsageStatisticsDeviceSlice(
|
|
updatedAt: Date(timeIntervalSince1970: 200),
|
|
dictationDurationSeconds: 45,
|
|
dictationCharacterCount: 80,
|
|
translationCharacterCount: 25
|
|
),
|
|
])
|
|
|
|
let merged = SyncedUsageStatisticsV2.merge(local: local, remote: remote).aggregated
|
|
|
|
XCTAssertEqual(merged.dictationDurationSeconds, 45)
|
|
XCTAssertEqual(merged.dictationCharacterCount, 120)
|
|
XCTAssertEqual(merged.translationCharacterCount, 25)
|
|
}
|
|
|
|
func testPullAndMergeAppliesRemoteTotals() async throws {
|
|
let remote = SyncedUsageStatisticsV2(devices: [
|
|
deviceB: UsageStatisticsDeviceSlice(
|
|
updatedAt: Date(),
|
|
dictationDurationSeconds: 90,
|
|
dictationCharacterCount: 500,
|
|
translationCharacterCount: 40
|
|
),
|
|
])
|
|
try sync.push(remote)
|
|
|
|
await sync.pullAndMerge(store: store)
|
|
|
|
let loaded = SyncedUsageStatisticsStorage.load(from: defaults).aggregated
|
|
XCTAssertEqual(loaded.dictationDurationSeconds, 90)
|
|
XCTAssertEqual(loaded.dictationCharacterCount, 500)
|
|
XCTAssertEqual(loaded.translationCharacterCount, 40)
|
|
}
|
|
|
|
func testPullUnionsIndependentDeviceTotals() async throws {
|
|
SyncedUsageStatisticsStorage.upsertCurrentDeviceSlice(
|
|
UsageStatisticsDeviceSlice(
|
|
updatedAt: Date(),
|
|
dictationDurationSeconds: 10,
|
|
dictationCharacterCount: 300,
|
|
translationCharacterCount: 0
|
|
),
|
|
defaults: defaults,
|
|
deviceID: deviceA
|
|
)
|
|
let remote = SyncedUsageStatisticsV2(devices: [
|
|
deviceB: UsageStatisticsDeviceSlice(
|
|
updatedAt: Date(),
|
|
dictationDurationSeconds: 20,
|
|
dictationCharacterCount: 150,
|
|
translationCharacterCount: 5
|
|
),
|
|
])
|
|
try sync.push(remote)
|
|
|
|
await sync.pullAndMerge(store: store)
|
|
|
|
let loaded = SyncedUsageStatisticsStorage.load(from: defaults).aggregated
|
|
XCTAssertEqual(loaded.dictationDurationSeconds, 30)
|
|
XCTAssertEqual(loaded.dictationCharacterCount, 450)
|
|
XCTAssertEqual(loaded.translationCharacterCount, 5)
|
|
}
|
|
}
|