OpenTalon / app · opentalon.ai / native / CompanionCore / Tests / CompanionCoreTests / TaskPagingTests.swift
| 1 | import Foundation |
| 2 | import Testing |
| 3 | @testable import CompanionCore |
| 4 | |
| 5 | @Test func serviceLinesHandlePartialPacketsAndLargeWorkspaceResponses() { |
| 6 | var buffer = ServiceLineBuffer() |
| 7 | #expect(buffer.append(Data("first\nsec".utf8)) == [Data("first".utf8)]) |
| 8 | #expect(buffer.append(Data()).isEmpty) |
| 9 | #expect(buffer.append(Data("ond\n\nthird\nlast".utf8)) == |
| 10 | [Data("second".utf8), Data(), Data("third".utf8)]) |
| 11 | #expect(buffer.append(Data("\n".utf8)) == [Data("last".utf8)]) |
| 12 | let chunk = Data(repeating: 0x61, count: 16_384) |
| 13 | for _ in 0..<2_048 { #expect(buffer.append(chunk).isEmpty) } |
| 14 | let lines = buffer.append(Data([0x0a])) |
| 15 | #expect(lines.count == 1) |
| 16 | #expect(lines.first?.count == 32 * 1_024 * 1_024) |
| 17 | #expect(lines.first?.allSatisfy { $0 == 0x61 } == true) |
| 18 | #expect(buffer.append(Data("next\n".utf8)) == [Data("next".utf8)]) |
| 19 | } |
| 20 | |
| 21 | @Test func taskWindowsStartAtSixAndReachEveryItemWithoutTruncation() { |
| 22 | for total in [0, 1, 6, 7, 101, 1_001] { |
| 23 | let tasks = Array(0..<total) |
| 24 | var page = TaskPageWindow() |
| 25 | #expect(page.limit == 6) |
| 26 | #expect(Array(tasks.prefix(page.limit)) == Array(tasks.prefix(6))) |
| 27 | while page.limit < total { |
| 28 | let previous = page.limit |
| 29 | page.revealNext(total: total) |
| 30 | #expect(page.limit > previous && page.limit - previous <= 6) |
| 31 | } |
| 32 | #expect(Array(tasks.prefix(page.limit)) == tasks) |
| 33 | let final = page.limit |
| 34 | page.revealNext(total: total) |
| 35 | #expect(page.limit == final) |
| 36 | page.reset() |
| 37 | #expect(page.limit == 6) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | @Test func dragScrollingRevealsItsTargetWithoutLoadingTheEntireColumn() { |
| 42 | var page = TaskPageWindow() |
| 43 | page.revealThrough(index: 6, total: 101) |
| 44 | #expect(page.limit == 12) |
| 45 | page.revealThrough(index: 18, total: 101) |
| 46 | #expect(page.limit == 24) |
| 47 | page.revealThrough(index: 100, total: 101) |
| 48 | #expect(page.limit == 101) |
| 49 | page.reset() |
| 50 | page.revealThrough(index: -1, total: 101) |
| 51 | page.revealThrough(index: 101, total: 101) |
| 52 | #expect(page.limit == 6) |
| 53 | } |
| 54 | |
| 55 | @Test func fastTaskDatesPreserveStoredTimestampSemantics() { |
| 56 | let fractional = ISO8601DateFormatter() |
| 57 | fractional.formatOptions = [.withInternetDateTime, .withFractionalSeconds] |
| 58 | let whole = ISO8601DateFormatter() |
| 59 | whole.formatOptions = [.withInternetDateTime] |
| 60 | for stamp in ["2026-11-21T12:34:56.123Z", "2026-11-21T12:34:56Z", |
| 61 | "2026-11-21T07:34:56.123-05:00", "2026-11-21T07:34:56-05:00", "invalid", ""] { |
| 62 | let parsed = TaskCreationOrder.date(from: stamp) |
| 63 | let previous = fractional.date(from: stamp) ?? whole.date(from: stamp) |
| 64 | if let parsed, let previous { #expect(abs(parsed.timeIntervalSince(previous)) < 0.000_001) } |
| 65 | else { #expect(parsed == previous) } |
| 66 | } |
| 67 | let dates = (0..<101).map { Date(timeIntervalSince1970: 1_795_000_000 + Double($0 * 37)) } |
| 68 | #expect(dates.map { TaskCreationOrder.date(from: fractional.string(from: $0)) } == dates.map(Optional.some)) |
| 69 | let oldDay = DateFormatter() |
| 70 | oldDay.locale = Locale(identifier: "en_US_POSIX") |
| 71 | oldDay.calendar = Calendar(identifier: .gregorian) |
| 72 | oldDay.dateFormat = "yyyy-MM-dd" |
| 73 | for date in dates + [Date(timeIntervalSince1970: 0), Date(timeIntervalSince1970: -1)] { |
| 74 | #expect(CalendarDay.key(date) == oldDay.string(from: date)) |
| 75 | } |
| 76 | } |