-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathPlatformBalanceSyncServiceClearTests.swift
More file actions
77 lines (69 loc) · 3.05 KB
/
Copy pathPlatformBalanceSyncServiceClearTests.swift
File metadata and controls
77 lines (69 loc) · 3.05 KB
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
import SwiftData
import XCTest
@testable import SwiftDashSDK
@testable import SwiftExampleApp
@MainActor
final class PlatformBalanceSyncServiceClearTests: XCTestCase {
/// The Platform Sync "Clear" button must delete BOTH platform-address
/// SwiftData stores — the cached per-address balances
/// (`PersistentPlatformAddress`) and the network-scoped sync-state
/// watermark (`PersistentPlatformAddressesSyncState`) — so the UI
/// reads zero and the next sync is a full rescan rather than a ~2s
/// incremental resume (the reported "Clear didn't work" symptom).
///
/// The Rust-side watermark reset is skipped here because no wallet
/// manager is configured (the `if let walletManager` guard short-
/// circuits); that path is covered by the platform-wallet Rust unit
/// test (`reset_sync_state_clears_watermark_and_seed`) and manual
/// simulator verification.
func testClearLocalStateWipesPlatformAddressRows() async throws {
let container = try DashModelContainer.createInMemory()
let context = ModelContext(container)
let walletId = Data(repeating: 0x44, count: 32)
context.insert(
PersistentPlatformAddress(
address: "yTestPlatformAddr",
addressType: 0,
addressHash: Data(repeating: 0x01, count: 20),
accountIndex: 0,
addressIndex: 0,
derivationPath: "m/9'/1'/17'/0'/0'/0",
balance: 294_627_247_940,
walletId: walletId
)
)
context.insert(
PersistentPlatformAddressesSyncState(
walletId: Self.syncStateScopeId(for: .testnet),
network: .testnet,
syncHeight: 10,
syncTimestamp: 20,
lastKnownRecentBlock: 30
)
)
try context.save()
// Sanity: both rows present before the clear.
XCTAssertEqual(try fetch(PersistentPlatformAddress.self, in: container).count, 1)
XCTAssertEqual(try fetch(PersistentPlatformAddressesSyncState.self, in: container).count, 1)
let service = PlatformBalanceSyncService()
await service.clearLocalState(modelContext: context)
XCTAssertTrue(
try fetch(PersistentPlatformAddress.self, in: container).isEmpty,
"cached per-address balances must be deleted"
)
XCTAssertTrue(
try fetch(PersistentPlatformAddressesSyncState.self, in: container).isEmpty,
"the sync-state watermark must be deleted so the next sync is a full rescan"
)
}
private static func syncStateScopeId(for network: Network) -> Data {
var data = Data("platform-sync:\(network.networkName)".utf8.prefix(32))
if data.count < 32 {
data.append(Data(repeating: 0, count: 32 - data.count))
}
return data
}
private func fetch<T: PersistentModel>(_ type: T.Type, in container: ModelContainer) throws -> [T] {
try ModelContext(container).fetch(FetchDescriptor<T>())
}
}