-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathHookRegistryActorTests.swift
More file actions
425 lines (349 loc) · 11.1 KB
/
HookRegistryActorTests.swift
File metadata and controls
425 lines (349 loc) · 11.1 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//
// HookRegistryActorTests.swift
// PersistentHistoryTrackingKitTests
//
// Created by Claude on 2025-01-06
//
import CoreData
import Testing
@testable import PersistentHistoryTrackingKit
@Suite("HookRegistryActor Tests")
struct HookRegistryActorTests {
@Test("Register and trigger hook")
func registerAndTriggerHook() async throws {
let harness = makeHarness(testName: "registerAndTriggerHook")
let recorder = HookContextRecorder()
await harness.registry.registerObserver(
entityName: "Person",
operation: .insert
) { contexts in
await recorder.record(contexts)
}
let objectIDURL = try await harness.createPersonAndProcess(name: "Alice")
let receivedContexts = await recorder.flattened()
#expect(receivedContexts.count == 1)
#expect(receivedContexts.first?.entityName == "Person")
#expect(receivedContexts.first?.operation == .insert)
#expect(receivedContexts.first?.author == "App1")
#expect(receivedContexts.first?.objectIDURL == objectIDURL)
}
@Test("Remove hook")
func removeHook() async throws {
let harness = makeHarness(testName: "removeHook")
let tracker = BoolTracker()
await harness.registry.registerObserver(
entityName: "Person",
operation: .insert
) { _ in
await tracker.mark()
}
await harness.registry.removeObserver(entityName: "Person", operation: .insert)
_ = try await harness.createPersonAndProcess(name: "Alice")
#expect(await tracker.value() == false)
}
@Test("Hooks for different entities do not interfere")
func differentEntityHooks() async throws {
let harness = makeHarness(testName: "differentEntityHooks")
let tracker = EntityTriggerTracker()
await harness.registry.registerObserver(
entityName: "Person",
operation: .insert
) { _ in
await tracker.markPerson()
}
await harness.registry.registerObserver(
entityName: "Item",
operation: .insert
) { _ in
await tracker.markItem()
}
_ = try await harness.createPersonAndProcess(name: "Alice")
let stateAfterPerson = await tracker.state()
#expect(stateAfterPerson.person == true)
#expect(stateAfterPerson.item == false)
let afterPersonInsert = try await makeIncrementalAfterDate()
_ = try await harness.createItemAndProcess(title: "Notebook", after: afterPersonInsert)
let finalState = await tracker.state()
#expect(finalState.person == true)
#expect(finalState.item == true)
}
@Test("Register hook returns UUID")
func registerHookReturnsUUID() async throws {
let harness = makeHarness(testName: "registerHookReturnsUUID")
let hookId = await harness.registry.registerObserver(
entityName: "Person",
operation: .insert
) { _ in }
#expect(hookId != UUID())
}
@Test("Remove hook by UUID")
func removeHookByUUID() async throws {
let harness = makeHarness(testName: "removeHookByUUID")
let tracker = BoolTracker()
let hookId = await harness.registry.registerObserver(
entityName: "Person",
operation: .insert
) { _ in
await tracker.mark()
}
let removed = await harness.registry.removeObserver(id: hookId)
#expect(removed == true)
_ = try await harness.createPersonAndProcess(name: "Alice")
#expect(await tracker.value() == false)
}
@Test("Remove nonexistent UUID returns false")
func removeNonexistentUUID() async throws {
let harness = makeHarness(testName: "removeNonexistentUUID")
let removed = await harness.registry.removeObserver(id: UUID())
#expect(removed == false)
}
@Test("Remove specific hook from multiple hooks")
func removeSpecificHookFromMultiple() async throws {
let harness = makeHarness(testName: "removeSpecificHookFromMultiple")
let counter = Counter()
let hookId1 = await harness.registry.registerObserver(
entityName: "Person",
operation: .insert
) { _ in
await counter.increment()
}
let hookId2 = await harness.registry.registerObserver(
entityName: "Person",
operation: .insert
) { _ in
await counter.increment()
}
let hookId3 = await harness.registry.registerObserver(
entityName: "Person",
operation: .insert
) { _ in
await counter.increment()
}
#expect(await harness.registry.removeObserver(id: hookId2) == true)
_ = try await harness.createPersonAndProcess(name: "Alice")
#expect(await counter.value() == 2)
#expect(await harness.registry.removeObserver(id: hookId1) == true)
#expect(await harness.registry.removeObserver(id: hookId3) == true)
#expect(await harness.registry.removeObserver(id: hookId2) == false)
}
@Test("Remove all hooks for entity and operation")
func removeAllHooksForEntityOperation() async throws {
let harness = makeHarness(testName: "removeAllHooksForEntityOperation")
let counter = Counter()
_ = await harness.registry.registerObserver(
entityName: "Person",
operation: .insert
) { _ in
await counter.increment()
}
_ = await harness.registry.registerObserver(
entityName: "Person",
operation: .insert
) { _ in
await counter.increment()
}
_ = await harness.registry.registerObserver(
entityName: "Person",
operation: .insert
) { _ in
await counter.increment()
}
_ = await harness.registry.registerObserver(
entityName: "Person",
operation: .update
) { _ in
await counter.increment()
}
await harness.registry.removeObserver(entityName: "Person", operation: .insert)
_ = try await harness.createPersonAndProcess(name: "Alice")
#expect(await counter.value() == 0)
let afterInsert = try await makeIncrementalAfterDate()
try await harness.updatePersonAndProcess(
matchName: "Alice",
newAge: 31,
after: afterInsert)
#expect(await counter.value() == 1)
}
@Test("Multiple hooks execute in registration order")
func multipleHooksExecutionOrder() async throws {
let harness = makeHarness(testName: "multipleHooksExecutionOrder")
let tracker = OrderTracker()
_ = await harness.registry.registerObserver(
entityName: "Person",
operation: .insert
) { _ in
await tracker.append(1)
}
_ = await harness.registry.registerObserver(
entityName: "Person",
operation: .insert
) { _ in
await tracker.append(2)
}
_ = await harness.registry.registerObserver(
entityName: "Person",
operation: .insert
) { _ in
await tracker.append(3)
}
_ = try await harness.createPersonAndProcess(name: "Alice")
#expect(await tracker.values() == [1, 2, 3])
}
@Test("Remove all observers clears all hooks and UUIDs")
func removeAllObserversClearsEverything() async throws {
let harness = makeHarness(testName: "removeAllObserversClearsEverything")
let counter = Counter()
let id1 = await harness.registry.registerObserver(
entityName: "Person",
operation: .insert
) { _ in
await counter.increment()
}
let id2 = await harness.registry.registerObserver(
entityName: "Person",
operation: .update
) { _ in
await counter.increment()
}
let id3 = await harness.registry.registerObserver(
entityName: "Item",
operation: .insert
) { _ in
await counter.increment()
}
await harness.registry.removeAllObservers()
_ = try await harness.createPersonAndProcess(name: "Alice")
let afterInsert = try await makeIncrementalAfterDate()
try await harness.updatePersonAndProcess(
matchName: "Alice",
newAge: 31,
after: afterInsert)
let afterUpdate = try await makeIncrementalAfterDate()
_ = try await harness.createItemAndProcess(title: "Notebook", after: afterUpdate)
#expect(await counter.value() == 0)
#expect(await harness.registry.removeObserver(id: id1) == false)
#expect(await harness.registry.removeObserver(id: id2) == false)
#expect(await harness.registry.removeObserver(id: id3) == false)
}
}
private extension HookRegistryActorTests {
func makeHarness(testName: String) -> HookTestHarness {
let container = TestModelBuilder.createContainer(
author: "App1",
testName: testName)
let registry = HookRegistryActor()
let timestampManager = TransactionTimestampManager(
userDefaults: TestModelBuilder.createTestUserDefaults(),
maximumDuration: 604_800)
let processor = TransactionProcessorActor(
container: container,
hookRegistry: registry,
cleanStrategy: .none,
timestampManager: timestampManager)
let writer = TestAppDataHandler(container: container, viewName: "Writer")
return HookTestHarness(
registry: registry,
processor: processor,
writer: writer)
}
}
private struct HookTestHarness {
let registry: HookRegistryActor
let processor: TransactionProcessorActor
let writer: TestAppDataHandler
@discardableResult
func createPersonAndProcess(
name: String,
age: Int32 = 30,
author: String = "App1",
after: Date? = nil
) async throws -> URL {
let objectIDURL = try await writer.createPerson(name: name, age: age, author: author)
_ = try await processor.processNewTransactions(
from: ["App1", "App2"],
after: after,
currentAuthor: "App2")
return objectIDURL
}
@discardableResult
func createItemAndProcess(
title: String,
author: String = "App1",
after: Date? = nil
) async throws -> URL {
let objectIDURL = try await writer.createItem(title: title, author: author)
_ = try await processor.processNewTransactions(
from: ["App1", "App2"],
after: after,
currentAuthor: "App2")
return objectIDURL
}
func updatePersonAndProcess(
matchName: String,
newAge: Int32,
author: String = "App1",
after: Date? = nil
) async throws {
try await writer.updatePeople(
[PersonUpdate(matchName: matchName, newAge: newAge)],
author: author)
_ = try await processor.processNewTransactions(
from: ["App1", "App2"],
after: after,
currentAuthor: "App2")
}
}
private actor HookContextRecorder {
private var contexts: [HookContext] = []
func record(_ contexts: [HookContext]) {
self.contexts.append(contentsOf: contexts)
}
func flattened() -> [HookContext] {
contexts
}
}
private actor BoolTracker {
private var marked = false
func mark() {
marked = true
}
func value() -> Bool {
marked
}
}
private actor Counter {
private var count = 0
func increment() {
count += 1
}
func value() -> Int {
count
}
}
private actor OrderTracker {
private var order: [Int] = []
func append(_ value: Int) {
order.append(value)
}
func values() -> [Int] {
order
}
}
private actor EntityTriggerTracker {
private var person = false
private var item = false
func markPerson() {
person = true
}
func markItem() {
item = true
}
func state() -> (person: Bool, item: Bool) {
(person, item)
}
}
private func makeIncrementalAfterDate() async throws -> Date {
let checkpoint = Date()
try await Task.sleep(nanoseconds: 100_000_000)
return checkpoint
}