mirror of
https://github.com/wu736139669/hapi.git
synced 2026-12-03 06:47:03 +00:00
Complete the pending-request center's entry points: - Push payloads already carry the request id for permission and input pushes; the notification tap now passes it through AppModel into the home screen instead of only the session id. - Opening a session row whose summary still has pending requests lands on the first wait (async questions first, since they live in the collapsible bottom panel; approvals scroll to their tool row). - ChatModel queues the jump until the detail's request dictionary arrives, so a cold notification start does not drop the target and a blocking target waits for the transcript to settle before scrolling. - A focus token lets the split detail re-fire for the session that is already open. Routing and pending-target presentation tests cover the new request id. via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run> Co-Authored-By: Claude Code <noreply@anthropic.com>
97 lines
4.5 KiB
Swift
97 lines
4.5 KiB
Swift
import HapiClient
|
|
import XCTest
|
|
@testable import Hapi
|
|
@testable import HapiProtocol
|
|
|
|
/// Notification tap-through across paired hubs: the payload carries no hub,
|
|
/// so the owner is located (active hub first) before navigating. Fake HTTP
|
|
/// and in-memory credentials; no Keychain or network.
|
|
@MainActor
|
|
final class NotificationRoutingTests: XCTestCase {
|
|
private let hubA = "https://a.example.com"
|
|
private let hubB = "https://b.example.com"
|
|
|
|
/// Hub B owns `s-b`; hub A answers 404 for it. Hub C is offline.
|
|
private actor OwnerHTTP: HTTPPerforming {
|
|
private(set) var sessionLookups: [String] = []
|
|
|
|
func perform(_ request: URLRequest) async throws -> (Data, HTTPURLResponse) {
|
|
let url = try XCTUnwrap(request.url)
|
|
let host = url.host ?? ""
|
|
if url.path.hasPrefix("/api/sessions/") {
|
|
sessionLookups.append(host)
|
|
}
|
|
if host == "c.example.com" { throw URLError(.cannotConnectToHost) }
|
|
let owns = host == "b.example.com" && url.lastPathComponent == "s-b"
|
|
let status = owns ? 200 : 404
|
|
let body = owns
|
|
? #"{"session":{"id":"s-b","namespace":"default","seq":1,"createdAt":1,"updatedAt":1,"active":true,"activeAt":1,"metadata":null,"metadataVersion":1,"agentState":null,"agentStateVersion":1,"thinking":false,"thinkingAt":0}}"#
|
|
: #"{"error":"Session not found"}"#
|
|
let response = try XCTUnwrap(HTTPURLResponse(url: url, statusCode: status, httpVersion: "HTTP/1.1", headerFields: nil))
|
|
return (Data(body.utf8), response)
|
|
}
|
|
}
|
|
|
|
/// A JWT the auth manager accepts without refreshing (expires in 2100).
|
|
private static let jwt: String = {
|
|
func encode(_ json: String) -> String {
|
|
Data(json.utf8).base64EncodedString()
|
|
.replacingOccurrences(of: "+", with: "-")
|
|
.replacingOccurrences(of: "/", with: "_")
|
|
.replacingOccurrences(of: "=", with: "")
|
|
}
|
|
return encode(#"{"alg":"HS256","typ":"JWT"}"#) + "."
|
|
+ encode(#"{"uid":1,"ns":"default","iat":1700000000,"exp":4102444800}"#) + ".signature"
|
|
}()
|
|
|
|
private func credentials(_ hubs: [String]) throws -> InMemoryCredentialStore {
|
|
let store = InMemoryCredentialStore()
|
|
for hub in hubs {
|
|
try store.store(HubCredentials(hubUrl: hub, accessToken: "token", jwt: Self.jwt))
|
|
}
|
|
return store
|
|
}
|
|
|
|
func testLocateSkipsHubsThatDoNotKnowTheSession() async throws {
|
|
let http = OwnerHTTP()
|
|
let runner = PushActionRunner(
|
|
hubs: [hubA, "https://c.example.com", hubB],
|
|
credentialStore: try credentials([hubA, "https://c.example.com", hubB]),
|
|
performer: http
|
|
)
|
|
|
|
guard case .success(let owner) = await runner.locate(sessionId: "s-b") else {
|
|
return XCTFail("hub B owns the session")
|
|
}
|
|
XCTAssertEqual(owner, hubB)
|
|
let missing = await runner.locate(sessionId: "s-gone")
|
|
guard case .transient = missing else {
|
|
return XCTFail("an unreachable hub keeps the miss inconclusive, got \(missing)")
|
|
}
|
|
let lookups = await http.sessionLookups
|
|
XCTAssertEqual(Array(lookups.prefix(3)), ["a.example.com", "c.example.com", "b.example.com"])
|
|
}
|
|
|
|
func testTapSwitchesToTheOwningHubBeforeOpening() throws {
|
|
let defaults = try XCTUnwrap(UserDefaults(suiteName: "NotificationRoutingTests-\(UUID().uuidString)"))
|
|
let registry = HubRegistry(defaults: defaults)
|
|
registry.register(hubA)
|
|
registry.register(hubB)
|
|
registry.setActiveHub(hubA)
|
|
let model = AppModel(registry: registry, credentialStore: try credentials([hubA, hubB]), performer: OwnerHTTP())
|
|
XCTAssertEqual(model.state, .paired(activeHub: hubA))
|
|
|
|
model.openNotificationTarget(sessionId: "s-b", hubUrl: hubB, requestId: "req-9")
|
|
XCTAssertEqual(model.state, .paired(activeHub: hubB))
|
|
XCTAssertEqual(model.pendingOpenSessionId, "s-b")
|
|
XCTAssertEqual(model.pendingOpenRequestId, "req-9")
|
|
|
|
// No located owner (or an unpaired one): stay on the active hub.
|
|
model.openNotificationTarget(sessionId: "s-x", hubUrl: nil)
|
|
XCTAssertNil(model.pendingOpenRequestId, "a plain push clears an earlier request id")
|
|
model.openNotificationTarget(sessionId: "s-y", hubUrl: "https://unpaired.example.com")
|
|
XCTAssertEqual(model.state, .paired(activeHub: hubB))
|
|
XCTAssertEqual(model.pendingOpenSessionId, "s-y")
|
|
}
|
|
}
|