Files
hapi/ios/HapiTests/ContentSearchModelTests.swift
T
4387dc7f4a feat(ios): search message content and jump to the matched message
Port the H5 content-search scope to the native session list:

- The search field gains a Sessions/Content scope menu. Content mode
  debounces the hub's POST /sessions/content-search over the sessions
  visible under the current machine/status/unread filters, discards stale
  responses by generation, and surfaces loading, failure, and
  potentially-incomplete result states.
- Result rows keep the two-line contract: the matched snippet (plus a
  truncation mark) replaces the project line, while server relevance
  order is preserved.
- Tapping a hit passes its message id to the chat. A row already in the
  loaded transcript scrolls directly; otherwise the window controller
  loads the hub's bounded message-context page, enters history mode, and
  the chat scrolls/flashes the target. "Back to latest" still resets to
  the live tail because a context window is explicitly not the tail.

Adds content-search wire models, endpoint coverage, context-window
transition tests, and session-list model tests for scoping and rendering.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>
Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-12-01 07:22:36 +08:00

126 lines
4.4 KiB
Swift

import HapiClient
import XCTest
@testable import Hapi
@testable import HapiProtocol
@MainActor
final class ContentSearchModelTests: XCTestCase {
private actor Probe {
private(set) var queries: [String] = []
private(set) var idLists: [[String]] = []
func record(query: String, ids: [String]) {
queries.append(query)
idLists.append(ids)
}
}
private func waitUntil(
_ condition: @escaping @MainActor () -> Bool
) async throws {
for _ in 0..<60 {
if condition() { return }
try await Task.sleep(for: .milliseconds(50))
}
XCTFail("condition did not settle")
}
func testContentScopeScopesTheRequestAndRendersServerOrder() async throws {
let sessions = HomeFilterTestSessions([
HomeFilterTestData.summary("a", machine: "mac"),
HomeFilterTestData.summary("b", machine: "mac"),
HomeFilterTestData.summary("c", machine: "debian"),
])
let probe = Probe()
let matchA = SessionContentMatch(
messageId: "m-a",
role: "user",
seq: 10,
createdAt: 100,
snippet: "…needle in a…",
truncated: false
)
let matchB = SessionContentMatch(
messageId: "m-b",
role: "assistant",
seq: 20,
createdAt: 200,
snippet: "…needle in b…",
truncated: true
)
let response = SessionContentSearchResponse(
results: [
SessionContentSearchResult(
session: HomeFilterTestData.summary("b", machine: "mac"),
match: matchB
),
SessionContentSearchResult(
session: HomeFilterTestData.summary("a", machine: "mac"),
match: matchA
),
],
hasPotentiallyIncompleteResults: false
)
let model = SessionListModel(
sessionStore: sessions,
machineStore: HomeFilterTestMachines(),
lastSeenStore: LastSeenStore(),
hubUrl: "test",
contentSearch: { query, ids in
await probe.record(query: query, ids: ids)
return response
}
)
model.searchScope = .content
model.searchQuery = "needle"
try await waitUntil { !model.contentSearchLoading && model.contentSearchResponse != nil }
let recorded = await probe.queries
XCTAssertEqual(recorded, ["needle"])
let ids = await probe.idLists.first ?? []
XCTAssertEqual(Set(ids), ["a", "b", "c"])
XCTAssertEqual(model.rows.map(\.id), ["b", "a"], "Server relevance order wins")
XCTAssertEqual(model.rows.first?.contentSnippet, "…needle in b…")
XCTAssertEqual(model.rows.first?.contentSearchTruncated, true)
XCTAssertEqual(model.contentSearchMatch(for: "a")?.messageId, "m-a")
XCTAssertTrue(model.isSearching)
// Back to metadata scope: the hub search state is discarded.
model.searchScope = .metadata
XCTAssertNil(model.contentSearchResponse)
XCTAssertNil(model.contentSearchMatch(for: "a"))
model.searchQuery = ""
XCTAssertFalse(model.isSearching)
}
func testContentScopeScopesTheRequestToTheActiveMachineFilter() async throws {
let sessions = HomeFilterTestSessions([
HomeFilterTestData.summary("a", machine: "mac"),
HomeFilterTestData.summary("b", machine: "debian"),
])
let probe = Probe()
let model = SessionListModel(
sessionStore: sessions,
machineStore: HomeFilterTestMachines(),
lastSeenStore: LastSeenStore(),
hubUrl: "test",
contentSearch: { query, ids in
await probe.record(query: query, ids: ids)
return SessionContentSearchResponse(
results: [],
hasPotentiallyIncompleteResults: false
)
}
)
model.selectMachine("mac")
model.searchScope = .content
model.searchQuery = "needle"
try await waitUntil { !model.contentSearchLoading }
let ids = await probe.idLists.first ?? []
XCTAssertEqual(ids, ["a"])
XCTAssertTrue(model.rows.isEmpty)
}
}