| 1 | import XCTest |
| 2 | @testable import LChatCore |
| 3 | |
| 4 | // 🤖 Round-trips for the local message store: upsert → page, live-echo merge |
| 5 | // (bare echo must not clobber reactions), delete, and FTS search. |
| 6 | final class MessageStoreTests: XCTestCase { |
| 7 | func testRoundTripMergeAndSearch() async { |
| 8 | let store = MessageStore.shared |
| 9 | let instance = "test-\(UUID().uuidString)" |
| 10 | var p = MessagePayload() |
| 11 | p.text = "the quick encrypted fox" |
| 12 | p.name = "Ada" |
| 13 | await store.upsert(instance: instance, thread: "t1", [ |
| 14 | .init(id: 1, at: 100, edited: false, |
| 15 | reactions: [Reaction(emoji: "👍", count: 2, mine: true)], payload: p) |
| 16 | ]) |
| 17 | // A live echo (no reactions) must not clobber the stored tally. |
| 18 | await store.upsert(instance: instance, thread: "t1", [ |
| 19 | .init(id: 1, at: 100, edited: false, reactions: [], payload: p) |
| 20 | ]) |
| 21 | var page = await store.page(instance: instance, thread: "t1") |
| 22 | XCTAssertEqual(page.count, 1) |
| 23 | XCTAssertEqual(page[0].payload.text, "the quick encrypted fox") |
| 24 | XCTAssertEqual(page[0].reactions.first?.count, 2) |
| 25 | |
| 26 | let hits = await store.search(instance: instance, query: "encrypted") |
| 27 | XCTAssertEqual(hits.first?.message.id, 1) |
| 28 | |
| 29 | await store.delete(instance: instance, thread: "t1", id: 1) |
| 30 | page = await store.page(instance: instance, thread: "t1") |
| 31 | XCTAssertTrue(page.isEmpty) |
| 32 | await store.clear(instance: instance) |
| 33 | } |
| 34 | |
| 35 | // suspend() (the 0xdead10cc lock-hygiene close) must be transparent: |
| 36 | // everything written before it reads back after, via the lazy reopen. |
| 37 | func testSuspendReopensLazily() async { |
| 38 | let store = MessageStore.shared |
| 39 | let instance = "test-\(UUID().uuidString)" |
| 40 | var p = MessagePayload() |
| 41 | p.text = "written before the suspend" |
| 42 | await store.upsert(instance: instance, thread: "t1", [ |
| 43 | .init(id: 1, at: 100, edited: false, reactions: [], payload: p) |
| 44 | ]) |
| 45 | await store.suspend() |
| 46 | let page = await store.page(instance: instance, thread: "t1") |
| 47 | XCTAssertEqual(page.first?.payload.text, "written before the suspend") |
| 48 | // And a fresh write after the close still lands. |
| 49 | p.text = "written after" |
| 50 | await store.upsert(instance: instance, thread: "t1", [ |
| 51 | .init(id: 2, at: 200, edited: false, reactions: [], payload: p) |
| 52 | ]) |
| 53 | let again = await store.page(instance: instance, thread: "t1") |
| 54 | XCTAssertEqual(again.count, 2) |
| 55 | await store.clear(instance: instance) |
| 56 | } |
| 57 | } |
| 58 | |