| 1 | import XCTest |
| 2 | @testable import LChatCore |
| 3 | |
| 4 | // 🤖 The backfill walk's decision logic (#584) — mirrors the web's |
| 5 | // backfillCursor tests (test/store.test.mjs), so the two walkers can't drift: |
| 6 | // resume from the persisted watermark, seed when nothing is stored, and never |
| 7 | // re-walk a completed thread. Plus the store's mark round-trip. |
| 8 | final class HistoryBackfillTests: XCTestCase { |
| 9 | func testCursorResumesFromWatermarkNotFromScratch() { |
| 10 | XCTAssertEqual(HistoryBackfill.cursor(markOldest: 42, markComplete: false, oldestStored: 900), 42, |
| 11 | "interrupted walk continues at its mark") |
| 12 | XCTAssertEqual(HistoryBackfill.cursor(markOldest: 0, markComplete: false, oldestStored: 900), 900, |
| 13 | "first walk starts at the oldest stored row") |
| 14 | XCTAssertEqual(HistoryBackfill.cursor(markOldest: 0, markComplete: false, oldestStored: 0), 0, |
| 15 | "empty store → seed a recent page first") |
| 16 | } |
| 17 | |
| 18 | func testCursorNeverRewalksACompletedThread() { |
| 19 | XCTAssertNil(HistoryBackfill.cursor(markOldest: 1, markComplete: true, oldestStored: 900)) |
| 20 | XCTAssertNil(HistoryBackfill.cursor(markOldest: 0, markComplete: true, oldestStored: 0), |
| 21 | "empty-thread completion holds too") |
| 22 | } |
| 23 | |
| 24 | func testMarkRoundTripAndOldestStored() async { |
| 25 | let store = MessageStore.shared |
| 26 | let instance = "test-\(UUID().uuidString)" |
| 27 | |
| 28 | var mark = await store.backfillMark(instance: instance, thread: "t1") |
| 29 | XCTAssertEqual(mark.oldest, 0) |
| 30 | XCTAssertFalse(mark.complete, "never-started thread reads (0, false)") |
| 31 | |
| 32 | await store.setBackfillMark(instance: instance, thread: "t1", oldest: 156, complete: false) |
| 33 | mark = await store.backfillMark(instance: instance, thread: "t1") |
| 34 | XCTAssertEqual(mark.oldest, 156) |
| 35 | XCTAssertFalse(mark.complete) |
| 36 | |
| 37 | await store.setBackfillMark(instance: instance, thread: "t1", oldest: 6, complete: true) |
| 38 | mark = await store.backfillMark(instance: instance, thread: "t1") |
| 39 | XCTAssertEqual(mark.oldest, 6) |
| 40 | XCTAssertTrue(mark.complete, "upsert replaces, never duplicates") |
| 41 | |
| 42 | var oldest = await store.oldestStoredId(instance: instance, thread: "t1") |
| 43 | XCTAssertEqual(oldest, 0, "no rows stored yet") |
| 44 | var p = MessagePayload() |
| 45 | p.text = "hello" |
| 46 | await store.upsert(instance: instance, thread: "t1", [ |
| 47 | .init(id: 40, at: 100, edited: false, reactions: [], payload: p), |
| 48 | .init(id: 12, at: 90, edited: false, reactions: [], payload: p), |
| 49 | ]) |
| 50 | oldest = await store.oldestStoredId(instance: instance, thread: "t1") |
| 51 | XCTAssertEqual(oldest, 12) |
| 52 | } |
| 53 | |
| 54 | func testProgressLabelNamesTheMonth() { |
| 55 | // 2025-03-15 (UTC) — the label localises but always carries the year. |
| 56 | let label = HistoryBackfill.progressLabel(1742000000) |
| 57 | XCTAssertTrue(label.hasPrefix("Searching back to"), label) |
| 58 | XCTAssertTrue(label.contains("2025"), label) |
| 59 | XCTAssertEqual(HistoryBackfill.progressLabel(0), "Searching…") |
| 60 | } |
| 61 | } |
| 62 | |