mirror of
https://github.com/wu736139669/hapi.git
synced 2026-12-05 07:07:04 +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>
230 lines
8.9 KiB
Swift
230 lines
8.9 KiB
Swift
import Foundation
|
|
import HapiClient
|
|
import HapiProtocol
|
|
import Testing
|
|
|
|
/// Transcription of the Android `FileViewerViewModelTest` suite against the
|
|
/// real `FileViewerModel` (only the gateway is faked). One adaptation: on
|
|
/// iOS the unified-diff *parser* lives in HapiUI, so `DiffState.ready`
|
|
/// carries the raw stdout judged through the injected `hasRenderableDiff`
|
|
/// probe (parse-level assertions are covered by `HapiUITests/DiffModelTests`);
|
|
/// these tests use the default non-empty probe.
|
|
@Suite("FileViewerModel")
|
|
@MainActor
|
|
struct FileViewerModelTests {
|
|
|
|
private func makeModel(
|
|
_ gateway: FakeFilesGateway,
|
|
path: String = "src/app.ts",
|
|
staged: Bool? = nil,
|
|
mode: FileViewerModel.Mode? = nil,
|
|
line: Int? = nil
|
|
) -> FileViewerModel {
|
|
FileViewerModel(
|
|
sessionId: "s1",
|
|
path: path,
|
|
initialStaged: staged,
|
|
initialMode: mode,
|
|
focusLine: line,
|
|
requester: gateway
|
|
)
|
|
}
|
|
|
|
private func b64(_ text: String) -> String {
|
|
Data(text.utf8).base64EncodedString()
|
|
}
|
|
|
|
private let sampleDiff = """
|
|
diff --git a/src/app.ts b/src/app.ts
|
|
--- a/src/app.ts
|
|
+++ b/src/app.ts
|
|
@@ -1,2 +1,2 @@
|
|
keep
|
|
-old
|
|
+new
|
|
"""
|
|
|
|
private func settled(_ model: FileViewerModel) async -> Bool {
|
|
await filesEventually { model.diff != .loading && model.content != .loading }
|
|
}
|
|
|
|
@Test func parsesTheDiffAndStaysInDiffMode() async {
|
|
let gateway = FakeFilesGateway()
|
|
await gateway.setDiffFile(GitCommandResponse(success: true, stdout: sampleDiff))
|
|
await gateway.setReadFile(FileReadResponse(success: true, content: b64("keep\nnew\n")))
|
|
let model = makeModel(gateway)
|
|
model.start()
|
|
|
|
#expect(await settled(model))
|
|
#expect(model.mode == .diff)
|
|
#expect(model.diff == .ready(unifiedDiff: sampleDiff))
|
|
#expect(await gateway.diffFileCalls == [DiffFileCall(path: "src/app.ts", staged: false)])
|
|
}
|
|
|
|
@Test func emptyDiffAutoFallsBackToFileMode() async {
|
|
let gateway = FakeFilesGateway()
|
|
await gateway.setDiffFile(GitCommandResponse(success: true, stdout: ""))
|
|
await gateway.setReadFile(FileReadResponse(success: true, content: b64("hello")))
|
|
let model = makeModel(gateway)
|
|
model.start()
|
|
|
|
#expect(await settled(model))
|
|
#expect(model.mode == .file)
|
|
#expect(model.diff == .empty)
|
|
#expect(model.content == .text("hello", language: "ts", isMarkdown: false))
|
|
}
|
|
|
|
@Test func failedDiffAutoFallsBackToFileModeWithMessageKept() async {
|
|
let gateway = FakeFilesGateway()
|
|
await gateway.setDiffFile(GitCommandResponse(success: false, error: "not a repo"))
|
|
await gateway.setReadFile(FileReadResponse(success: true, content: b64("x")))
|
|
let model = makeModel(gateway)
|
|
model.start()
|
|
|
|
#expect(await settled(model))
|
|
#expect(model.mode == .file)
|
|
#expect(model.diff == .failed("not a repo"))
|
|
}
|
|
|
|
@Test func explicitFileModeFromTheRouteDisablesAutoBehavior() async {
|
|
let gateway = FakeFilesGateway()
|
|
await gateway.setDiffFile(GitCommandResponse(success: true, stdout: sampleDiff))
|
|
await gateway.setReadFile(FileReadResponse(success: true, content: b64("body")))
|
|
let model = makeModel(gateway, mode: .file, line: 12)
|
|
model.start()
|
|
|
|
#expect(await settled(model))
|
|
#expect(model.mode == .file)
|
|
#expect(model.focusLine == 12)
|
|
// The diff still loaded, so the user can flip to it.
|
|
#expect(model.diff == .ready(unifiedDiff: sampleDiff))
|
|
}
|
|
|
|
@Test func stagedToggleReloadsTheDiffForTheOtherSide() async {
|
|
let gateway = FakeFilesGateway()
|
|
await gateway.setDiffFile(GitCommandResponse(success: true, stdout: sampleDiff))
|
|
await gateway.setReadFile(FileReadResponse(success: true, content: b64("x")))
|
|
let model = makeModel(gateway, staged: false)
|
|
model.start()
|
|
#expect(await settled(model))
|
|
|
|
model.setStaged(true)
|
|
#expect(await filesEventually { await gateway.diffFileCalls.count == 2 })
|
|
|
|
#expect(await gateway.diffFileCalls == [
|
|
DiffFileCall(path: "src/app.ts", staged: false),
|
|
DiffFileCall(path: "src/app.ts", staged: true),
|
|
])
|
|
#expect(model.staged)
|
|
|
|
// Same side again is a no-op.
|
|
model.setStaged(true)
|
|
try? await Task.sleep(for: .milliseconds(50))
|
|
#expect(await gateway.diffFileCalls.count == 2)
|
|
}
|
|
|
|
@Test func markdownFilesFlagMarkdownAndDefaultToPreview() async {
|
|
let gateway = FakeFilesGateway()
|
|
await gateway.setDiffFile(GitCommandResponse(success: true, stdout: ""))
|
|
await gateway.setReadFile(FileReadResponse(success: true, content: b64("# Title"), size: 7, modified: 123))
|
|
let model = makeModel(gateway, path: "README.md")
|
|
model.start()
|
|
|
|
#expect(await settled(model))
|
|
#expect(model.content == .text("# Title", language: "md", isMarkdown: true))
|
|
#expect(model.markdownPreview)
|
|
#expect(model.sizeBytes == 7)
|
|
#expect(model.modifiedAt == 123)
|
|
|
|
model.setMarkdownPreview(false)
|
|
#expect(model.markdownPreview == false)
|
|
}
|
|
|
|
@Test func imageExtensionsDecodeToImageContent() async {
|
|
let bytes = Data([1, 2, 3, 4])
|
|
let gateway = FakeFilesGateway()
|
|
await gateway.setDiffFile(GitCommandResponse(success: true, stdout: ""))
|
|
await gateway.setReadFile(FileReadResponse(success: true, content: bytes.base64EncodedString()))
|
|
let model = makeModel(gateway, path: "assets/logo.PNG")
|
|
model.start()
|
|
|
|
#expect(await settled(model))
|
|
#expect(model.content == .image(data: bytes, mimeType: "image/png"))
|
|
#expect(model.mode == .file)
|
|
}
|
|
|
|
@Test func nulBytesClassifyAsBinary() async {
|
|
let gateway = FakeFilesGateway()
|
|
await gateway.setDiffFile(GitCommandResponse(success: true, stdout: ""))
|
|
await gateway.setReadFile(FileReadResponse(
|
|
success: true,
|
|
content: Data([104, 105, 0, 106]).base64EncodedString()
|
|
))
|
|
let model = makeModel(gateway, path: "blob.bin")
|
|
model.start()
|
|
|
|
#expect(await settled(model))
|
|
#expect(model.content == .binary)
|
|
}
|
|
|
|
@Test func readFailureSurfacesItsMessage() async {
|
|
let gateway = FakeFilesGateway()
|
|
await gateway.setDiffFile(GitCommandResponse(success: true, stdout: ""))
|
|
await gateway.setReadFile(FileReadResponse(success: false, error: "File not found"))
|
|
let model = makeModel(gateway)
|
|
model.start()
|
|
|
|
#expect(await settled(model))
|
|
#expect(model.content == .failed("File not found"))
|
|
}
|
|
|
|
@Test func invalidBase64ClassifiesAsBinary() async {
|
|
let gateway = FakeFilesGateway()
|
|
await gateway.setDiffFile(GitCommandResponse(success: true, stdout: ""))
|
|
await gateway.setReadFile(FileReadResponse(success: true, content: "%%%not-base64%%%"))
|
|
let model = makeModel(gateway)
|
|
model.start()
|
|
|
|
#expect(await settled(model))
|
|
#expect(model.content == .binary)
|
|
}
|
|
|
|
@Test func htmlFilesExposeTheirSourceAndDefaultToPreview() async {
|
|
let gateway = FakeFilesGateway()
|
|
await gateway.setDiffFile(GitCommandResponse(success: true, stdout: ""))
|
|
await gateway.setReadFile(FileReadResponse(success: true, content: b64("<h1>Hi</h1>")))
|
|
let model = makeModel(gateway, path: "site/index.html")
|
|
model.start()
|
|
|
|
#expect(await settled(model))
|
|
#expect(model.isHTMLTextContent)
|
|
#expect(model.htmlText == "<h1>Hi</h1>")
|
|
#expect(model.htmlPreview)
|
|
model.setHTMLPreview(false)
|
|
#expect(!model.htmlPreview)
|
|
}
|
|
|
|
@Test func prepareHtmlDocumentAddsAViewportOnlyWhenMissing() {
|
|
#expect(FileContentHeuristics.isHTMLFile("a/b.htm"))
|
|
#expect(!FileContentHeuristics.isHTMLFile("a/b.html.txt"))
|
|
|
|
let withViewport = #"<head><meta name="viewport" content="width=1024"></head>"#
|
|
#expect(FileContentHeuristics.prepareHTMLDocument(withViewport) == withViewport)
|
|
|
|
let withHead = "<html><head><title>x</title></head></html>"
|
|
let preparedHead = FileContentHeuristics.prepareHTMLDocument(withHead)
|
|
#expect(preparedHead.contains("<head>\n<meta name=\"viewport\""))
|
|
#expect(preparedHead.contains("<title>x</title>"))
|
|
|
|
let withHtmlOnly = "<html><body>x</body></html>"
|
|
let preparedHtml = FileContentHeuristics.prepareHTMLDocument(withHtmlOnly)
|
|
#expect(preparedHtml.contains("<html>\n<head><meta name=\"viewport\""))
|
|
#expect(preparedHtml.contains("<body>x</body>"))
|
|
|
|
let fragment = "<p>x</p>"
|
|
let preparedFragment = FileContentHeuristics.prepareHTMLDocument(fragment)
|
|
#expect(preparedFragment.hasPrefix("<!doctype html><html><head>"))
|
|
#expect(preparedFragment.contains("<body><p>x</p></body>"))
|
|
}
|
|
}
|