forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionItemsFTSRepairTests.swift
More file actions
112 lines (96 loc) · 3.83 KB
/
Copy pathActionItemsFTSRepairTests.swift
File metadata and controls
112 lines (96 loc) · 3.83 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
import GRDB
import XCTest
@testable import Omi_Computer
final class ActionItemsFTSRepairTests: XCTestCase {
private var testUserId: String!
private var userDir: URL!
override func setUp() async throws {
try await super.setUp()
testUserId = "action-items-fts-repair-test-\(UUID().uuidString)"
await RewindDatabase.shared.close()
await ActionItemStorage.shared.invalidateCache()
RewindDatabase.currentUserId = testUserId
await RewindDatabase.shared.configure(userId: testUserId)
try await RewindDatabase.shared.initialize()
let appSupport = FileManager.default
.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
userDir =
appSupport
.appendingPathComponent("Omi", isDirectory: true)
.appendingPathComponent("users", isDirectory: true)
.appendingPathComponent(testUserId, isDirectory: true)
}
override func tearDown() async throws {
await RewindDatabase.shared.close()
await ActionItemStorage.shared.invalidateCache()
RewindDatabase.currentUserId = nil
if let userDir { try? FileManager.default.removeItem(at: userDir) }
try await super.tearDown()
}
func testLocalInsertRepairsMissingActionItemsFTSWithoutDroppingDurableRows() async throws {
let existing = try await ActionItemStorage.shared.insertLocalActionItem(
ActionItemRecord(description: "preserve existing durable row", source: "test"),
// This isolated repair fixture deliberately has no runtime owner.
authorization: .unrestricted)
XCTAssertNotNil(existing.id)
guard let dbQueue = await RewindDatabase.shared.getDatabaseQueue() else {
return XCTFail("database should be initialized")
}
try await dbQueue.write { db in
try db.execute(sql: "DROP TABLE action_items_fts")
}
let repaired = try await ActionItemStorage.shared.insertLocalActionItem(
ActionItemRecord(description: "insert after fts repair", source: "test"),
authorization: .unrestricted)
XCTAssertNotNil(repaired.id)
let durableDescriptions = try await dbQueue.read { db in
try String.fetchAll(
db,
sql: "SELECT description FROM action_items ORDER BY id")
}
XCTAssertEqual(
durableDescriptions,
[
"preserve existing durable row",
"insert after fts repair",
])
let ftsDescriptions = try await dbQueue.read { db in
try String.fetchAll(
db,
sql: """
SELECT action_items.description
FROM action_items_fts
JOIN action_items ON action_items_fts.rowid = action_items.id
WHERE action_items_fts MATCH 'repair OR durable'
ORDER BY action_items.id
""")
}
XCTAssertEqual(ftsDescriptions, durableDescriptions)
}
func testRepairRebuildsDroppedActionItemsFTSFromDurableRows() async throws {
let existing = try await ActionItemStorage.shared.insertLocalActionItem(
ActionItemRecord(description: "direct repair durable row", source: "test"),
// This isolated repair fixture deliberately has no runtime owner.
authorization: .unrestricted)
XCTAssertNotNil(existing.id)
guard let dbQueue = await RewindDatabase.shared.getDatabaseQueue() else {
return XCTFail("database should be initialized")
}
try await dbQueue.write { db in
try db.execute(sql: "DROP TABLE action_items_fts")
}
try await RewindDatabase.shared.repairActionItemsFTS(in: dbQueue, reason: "direct repair test")
let matches = try await dbQueue.read { db in
try String.fetchAll(
db,
sql: """
SELECT action_items.description
FROM action_items_fts
JOIN action_items ON action_items_fts.rowid = action_items.id
WHERE action_items_fts MATCH 'direct'
ORDER BY action_items.id
""")
}
XCTAssertEqual(matches, ["direct repair durable row"])
}
}