mirror of
https://github.com/wu736139669/hapi.git
synced 2026-12-03 06:47:03 +00:00
HTML preview: - HTML/HTM files get a Source/Preview toggle in the file viewer. Preview uses a WKWebView with JavaScript disabled and a non-persistent data store, matching the web's inline "preview sandbox · scripts disabled". - The mobile viewport meta is inserted only when the document lacks one (web prepareHtmlDocument), and tapped links leave for Safari instead of navigating the preview. Session sharing: - New share endpoints client (create/read/revoke) and wire models. The share link points at the hub's web /shared-session/<token> guest page; the six-digit access code is kept from the create response because the hub never returns it again. - A Share Session sheet creates/regenerates/revokes the link, copies the link or code, and offers the native share sheet. Reopening the sheet keeps a code created earlier in the same chat. Adds endpoint, URL-building, model, and HTML-document tests. via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run> Co-Authored-By: Claude Code <noreply@anthropic.com>
106 lines
3.3 KiB
Swift
106 lines
3.3 KiB
Swift
import HapiClient
|
|
import XCTest
|
|
@testable import Hapi
|
|
@testable import HapiProtocol
|
|
|
|
@MainActor
|
|
final class SessionShareModelTests: XCTestCase {
|
|
private func share(_ token: String = "tok") -> SessionShare {
|
|
SessionShare(
|
|
id: "sh-1",
|
|
sessionId: "s-1",
|
|
shareToken: token,
|
|
createdAt: 1,
|
|
updatedAt: 2
|
|
)
|
|
}
|
|
|
|
func testLoadShowsExistingLinkWithoutAnAccessCode() async {
|
|
let existing = share()
|
|
let model = SessionShareModel(
|
|
sessionId: "s-1",
|
|
baseURL: URL(string: "https://hub.example.com:8443")!,
|
|
service: SessionShareService(
|
|
load: { _ in existing },
|
|
create: { _ in throw TestFailure.unexpected },
|
|
revoke: { _ in throw TestFailure.unexpected }
|
|
)
|
|
)
|
|
await model.loadIfNeeded()
|
|
XCTAssertEqual(model.share?.shareToken, "tok")
|
|
XCTAssertNil(model.accessCode)
|
|
XCTAssertEqual(
|
|
model.shareURL?.absoluteString,
|
|
"https://hub.example.com:8443/shared-session/tok"
|
|
)
|
|
}
|
|
|
|
func testCreateStoresTheOneTimeCodeAndReopenKeepsIt() async {
|
|
let created = SessionShareCreateResponse(share: share("new"), accessCode: "654321")
|
|
let model = SessionShareModel(
|
|
sessionId: "s-1",
|
|
baseURL: URL(string: "https://hub.example.com")!,
|
|
service: SessionShareService(
|
|
load: { _ in nil },
|
|
create: { _ in created },
|
|
revoke: { _ in throw TestFailure.unexpected }
|
|
)
|
|
)
|
|
await model.create()
|
|
XCTAssertEqual(model.share?.shareToken, "new")
|
|
XCTAssertEqual(model.accessCode, "654321")
|
|
XCTAssertEqual(
|
|
model.shareURL?.absoluteString,
|
|
"https://hub.example.com/shared-session/new"
|
|
)
|
|
|
|
await model.loadIfNeeded()
|
|
XCTAssertEqual(model.accessCode, "654321", "reopening the sheet must not hide the code")
|
|
}
|
|
|
|
func testRevokeClearsTheShareAndCode() async {
|
|
let created = SessionShareCreateResponse(share: share(), accessCode: "111111")
|
|
let model = SessionShareModel(
|
|
sessionId: "s-1",
|
|
baseURL: URL(string: "https://hub.example.com")!,
|
|
service: SessionShareService(
|
|
load: { _ in nil },
|
|
create: { _ in created },
|
|
revoke: { _ in }
|
|
)
|
|
)
|
|
await model.create()
|
|
await model.revoke()
|
|
XCTAssertNil(model.share)
|
|
XCTAssertNil(model.accessCode)
|
|
XCTAssertNil(model.shareURL)
|
|
}
|
|
|
|
func testFailureSurfacesTheMessage() async {
|
|
let model = SessionShareModel(
|
|
sessionId: "s-1",
|
|
baseURL: URL(string: "https://hub.example.com")!,
|
|
service: SessionShareService(
|
|
load: { _ in throw TestFailure.failed },
|
|
create: { _ in throw TestFailure.failed },
|
|
revoke: { _ in }
|
|
)
|
|
)
|
|
await model.loadIfNeeded()
|
|
XCTAssertNotNil(model.errorMessage)
|
|
XCTAssertNil(model.share)
|
|
}
|
|
}
|
|
|
|
private enum TestFailure: LocalizedError {
|
|
case unexpected
|
|
case failed
|
|
|
|
var errorDescription: String? {
|
|
switch self {
|
|
case .unexpected: return "unexpected call"
|
|
case .failed: return "network failed"
|
|
}
|
|
}
|
|
}
|