Files
hapi/ios/Packages/HapiKit/Tests/HapiProtocolTests/SessionSortingTests.swift
T
weishu 628cb84bf5 feat(ios): web-parity session presence badges and list sort/filter
The home list showed presence only for connected sessions (a green dot,
pulsing while thinking) and left stopped sessions unmarked. Rows were
ordered by pins and the last message only, with no sort or status
choices.

- Presence badge follows web #2296: solid green pulsing while working,
  solid blue pulsing for background tasks only, a green ring when
  connected and waiting, and a power mark when stopped or archived.
  Activity moves off SessionRowStatus, which keeps only pending requests.
- Sort & Filter menu mirrors the web view menu: sort by last message or
  date created, Active first (on by default; manual pins still lead),
  show all / active only / archived only, plus the existing unread lens.
  Sort and Active first persist app-wide (SessionListOrderPrefs); the
  status filter stays per home like the other iOS filters and keeps the
  open session listed. Search keeps relevance with the base tie-break.
- Native parity docs now describe the actual base order.
2026-11-10 17:27:25 +08:00

80 lines
4.1 KiB
Swift

import Foundation
import HapiProtocol
import Testing
@Suite("User-activity session sorting")
struct SessionSortingTests {
private func summary(_ id: String, createdAt: Int = 0, at: Int? = nil) -> SessionSummary {
SessionSummary(id: id, active: false, thinking: false, activeAt: 0, updatedAt: 0,
createdAt: createdAt, lastUserMessageAt: at, metadataVersion: 0,
agentStateVersion: 0, todosUpdatedAt: 0, pendingRequestsCount: 0)
}
@Test func outputAndStatusDoNotReorderButNewInputDoes() {
var old = summary("old", createdAt: 1, at: 100)
let new = summary("new", createdAt: 2, at: 200)
old.updatedAt = 90_000
old.active = true
old.thinking = true
old.pendingRequestsCount = 4
#expect(sortSessionSummaries([old, new]).map(\.id) == ["new", "old"])
old.lastUserMessageAt = 300
#expect(sortSessionSummaries([old, new]).map(\.id) == ["old", "new"])
}
@Test func manualPinsStillComeFirst() {
var global = summary("global", at: 1)
global.globalPinned = true
var project = summary("project", at: 2)
project.pinned = true
#expect(sortSessionSummaries([summary("new", at: 99), project, global]).map(\.id) == ["global", "project", "new"])
}
@Test func creationFallbackAndTimestampTiesAreDeterministic() {
let rows = [summary("b", createdAt: 2), summary("a", createdAt: 2), summary("old", createdAt: 1)]
#expect(sortSessionSummaries(rows).map(\.id) == ["a", "b", "old"])
#expect(sortSessionSummaries(rows.reversed()).map(\.id) == ["a", "b", "old"])
}
@Test func activeFirstLeadsEachPinTierWithoutBeatingPins() {
var oldLive = summary("old-live", createdAt: 1, at: 100)
oldLive.active = true
let newStopped = summary("new-stopped", createdAt: 2, at: 200)
var pinnedStopped = summary("pinned-stopped", at: 1)
pinnedStopped.pinned = true
var pinnedLive = summary("pinned-live", at: 0)
pinnedLive.pinned = true
pinnedLive.active = true
var global = summary("global", at: 0)
global.globalPinned = true
let rows = [newStopped, oldLive, pinnedStopped, global, pinnedLive]
#expect(sortSessionSummaries(rows, order: .default).map(\.id)
== ["global", "pinned-live", "pinned-stopped", "old-live", "new-stopped"])
#expect(sortSessionSummaries(rows, order: .base).map(\.id)
== ["global", "pinned-stopped", "pinned-live", "new-stopped", "old-live"])
#expect(sortSessionSummaries(rows, order: .base) == sortSessionSummaries(rows))
}
@Test func createdSortIgnoresInputAndFallsBackToZero() {
let early = summary("early", createdAt: 1, at: 900)
let late = summary("late", createdAt: 5, at: 10)
let unknown = SessionSummary(id: "unknown", active: false, thinking: false, activeAt: 0, updatedAt: 0,
lastUserMessageAt: 999, metadataVersion: 0, agentStateVersion: 0,
todosUpdatedAt: 0, pendingRequestsCount: 0)
let created = SessionListOrder(sort: .created, activeFirst: false)
#expect(sortSessionSummaries([early, unknown, late], order: created).map(\.id) == ["late", "early", "unknown"])
#expect(sortSessionSummaries([early, unknown, late], order: .base).map(\.id) == ["unknown", "early", "late"])
#expect(SessionListSort.lastMessage.rawValue == "last-message", "Same stored value as web")
#expect(SessionListOrder.default == SessionListOrder(sort: .lastMessage, activeFirst: true))
}
@Test func patchClockIsMonotonicAndRenderRelevant() throws {
let row = summary("a", at: 100)
let patch = try JSONDecoder().decode(SessionPatch.self, from: Data("{\"lastUserMessageAt\":200}".utf8))
let next = SummaryPatching.applySessionSummaryPatch(row, patch)
#expect(next.lastUserMessageAt == 200)
#expect(!SummaryPatching.isRenderIrrelevantPatch(current: row, next: next))
#expect(SummaryPatching.applySessionSummaryPatch(next, SessionPatch(lastUserMessageAt: 50)).lastUserMessageAt == 200)
}
}