feat(publish): illustrations travel inline (#34)
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
A piece's assets/ ink-sketch images publish with it: Send as Draft first uploads every referenced image to Micro.blog's media endpoint (q=config discovery, one multipart file POST per FILE — duplicate refs share an upload), then each  ref REWRITES IN PLACE with its uploaded URL in the wire content. The image renders exactly where the writer placed it and the alt rides the markdown; the piece on disk never changes. The first shape attached uploads as a Micropub photo property with mp-photo-alt — live dogfood (2026-11-01) showed the theme renders properties at the bottom of the post, losing position. No property ships at all now; inline refs are the only transport, which also makes the parsed body ref the only way an image ships — the alt floor at the illustration rail is therefore the only gate a photo needs (G12: a whitespace-empty alt refuses naming the file before anything touches the wire; the mentor is never consulted). Pre-flight gates every payload: the endpoint's exact formats (PNG/JPEG/GIF/WebP/SVG), path containment resolved through symlinks (a planted link never uploads outside-file bytes under the writer's token), all-or-nothing uploads. A URL carrying markdown-active characters rides the angle form  — a plain-paren destination would close at the first ) and smuggle a live link into the published post; the frontmatter trust gate does not cover markdown's grammar. Finish-audit (three reviewers) drove the angle form, the symlink containment, the per-file upload dedup, and four pins; all four behavior guards flip-verified in one batch cycle. Suite 546 passed / 0 failed; swiftformat + swiftlint clean. Ingest UX (drop image onto the editor) is #141, blocked by this issue.
This commit is contained in:
1 parent
414f1392ef
commit
695ec27e11
10 files changed
+840
-19
No files matched your search
@@ -141,4 +141,150 @@ struct MicropubModelTests {
|
||||
|
||||
#expect(Post.scan(home: home).first?.micropubURL == "https://example.micro.blog/second")
|
||||
}
|
||||
|
||||
// MARK: #34 (S6.2) — illustrations travel with it
|
||||
|
||||
/// Plants a PNG beside the piece; returns the piece's post.md URL.
|
||||
@MainActor private func plantIllustratedPiece(_ model: PostsModel, _ home: URL, body: String) throws -> URL {
|
||||
let post = try PostStore.writeNew(body: body, home: home)
|
||||
model.select(post.id, home: home)
|
||||
let dir = try #require(PostStore.folder(of: post.id, home: home))
|
||||
try FileManager.default.createDirectory(at: dir.appendingPathComponent("assets", isDirectory: true), withIntermediateDirectories: true)
|
||||
try micropubPNG().write(to: dir.appendingPathComponent("assets/path.png"))
|
||||
return dir.appendingPathComponent("post.md")
|
||||
}
|
||||
|
||||
@Test @MainActor func `illustrations upload once and travel inline where placed`() async throws {
|
||||
let (model, home) = try makeModelHome()
|
||||
let fileURL = try plantIllustratedPiece(
|
||||
model, home,
|
||||
body: "# Inked\n\nProse before.\n\n\n\nProse after.",
|
||||
)
|
||||
|
||||
let log = MicropubRequestLog()
|
||||
let outcome = await model.micropubSendDraft(home: home, token: "tok", transport: micropubMediaScripted(log: log))
|
||||
|
||||
#expect(outcome == .drafted(url: "https://example.micro.blog/inked", preview: nil))
|
||||
// The three hops, in order: q=config → upload → create.
|
||||
#expect(log.requests.count == 3)
|
||||
let config = URLComponents(url: log.requests[0].url ?? Micropub.endpoint, resolvingAgainstBaseURL: false)
|
||||
#expect(config?.queryItems?.contains(URLQueryItem(name: "q", value: "config")) == true)
|
||||
#expect(log.requests[1].url?.absoluteString == "https://micro.blog/micropub/media")
|
||||
#expect(log.requests[1].httpBody?.firstRange(of: micropubPNG()) != nil) // the PNG rode the multipart
|
||||
// The create: the ref REWRITTEN in place with its uploaded URL
|
||||
// — exactly where the writer placed it (2026-11-01: the photo
|
||||
// PROPERTY rendered at the bottom, position lost), alt in the
|
||||
// markdown, NO photo property keys.
|
||||
let props = try #require(micropubJSON(log.requests[2])["properties"] as? [String: Any])
|
||||
let expected = "# Inked\n\nProse before.\n\n\n\nProse after."
|
||||
#expect(props["content"] as? [String] == [expected])
|
||||
#expect(props["photo"] == nil)
|
||||
#expect(props["mp-photo-alt"] == nil)
|
||||
|
||||
// The piece on disk is untouched — only the wire rewrote the ref.
|
||||
let disk = try String(contentsOf: fileURL, encoding: .utf8)
|
||||
#expect(disk.contains(""))
|
||||
#expect(Post.scan(home: home).first?.micropubURL == "https://example.micro.blog/inked")
|
||||
}
|
||||
|
||||
@Test @MainActor func `a photo without alt text never ships`() async throws {
|
||||
let (model, home) = try makeModelHome()
|
||||
// Whitespace-only alt, not just empty — the floor mirrors the
|
||||
// TRIMMED emptiness (#6: an isEmpty guard would pass this).
|
||||
let fileURL = try plantIllustratedPiece(
|
||||
model, home,
|
||||
body: "# Inked\n\n\n\nProse.",
|
||||
)
|
||||
|
||||
let log = MicropubRequestLog()
|
||||
let outcome = await model.micropubSendDraft(home: home, token: "tok", transport: micropubMediaScripted(log: log))
|
||||
|
||||
guard case let .refused(reason) = outcome else {
|
||||
Issue.record("expected refused, got \(outcome)")
|
||||
return
|
||||
}
|
||||
#expect(reason.contains("assets/path.png")) // names the illustration
|
||||
#expect(log.requests.isEmpty) // the floor holds BEFORE anything touches the wire
|
||||
#expect(Post.scan(home: home).first?.micropubURL == nil)
|
||||
// The piece is untouched — the ref still rides it on disk.
|
||||
let disk = try String(contentsOf: fileURL, encoding: .utf8)
|
||||
#expect(disk.contains(""))
|
||||
}
|
||||
|
||||
@Test @MainActor func `a symlinked illustration refuses before anything goes out`() async throws {
|
||||
let (model, home) = try makeModelHome()
|
||||
let fileURL = try plantIllustratedPiece(
|
||||
model, home,
|
||||
body: "# Inked\n\n\n\nProse.",
|
||||
)
|
||||
// link.png is a symlink OUT of the piece — containment must
|
||||
// refuse it (the cards/learnings posture: never read through
|
||||
// a planted link), never upload the target's bytes.
|
||||
let outside = home.appendingPathComponent("secret.png")
|
||||
try micropubPNG().write(to: outside)
|
||||
try FileManager.default.createSymbolicLink(
|
||||
at: fileURL.deletingLastPathComponent().appendingPathComponent("assets/link.png"),
|
||||
withDestinationURL: outside,
|
||||
)
|
||||
|
||||
let log = MicropubRequestLog()
|
||||
let outcome = await model.micropubSendDraft(home: home, token: "tok", transport: micropubMediaScripted(log: log))
|
||||
|
||||
guard case let .refused(reason) = outcome else {
|
||||
Issue.record("expected refused, got \(outcome)")
|
||||
return
|
||||
}
|
||||
#expect(reason.contains("assets/link.png"))
|
||||
#expect(log.requests.isEmpty)
|
||||
}
|
||||
|
||||
@Test @MainActor func `duplicate refs to one asset upload once and share the url`() async throws {
|
||||
let (model, home) = try makeModelHome()
|
||||
_ = try plantIllustratedPiece(
|
||||
model, home,
|
||||
body: "# Inked\n\n\n\nTail.\n\n",
|
||||
)
|
||||
|
||||
let log = MicropubRequestLog()
|
||||
let outcome = await model.micropubSendDraft(home: home, token: "tok", transport: micropubMediaScripted(log: log))
|
||||
|
||||
#expect(outcome == .drafted(url: "https://example.micro.blog/inked", preview: nil))
|
||||
// config + ONE upload + create — the FILE uploads once, not once per ref.
|
||||
#expect(log.requests.count == 3)
|
||||
#expect(log.requests.filter { $0.url?.absoluteString.contains("/micropub/media") == true }.count == 1)
|
||||
let props = try #require(micropubJSON(log.requests[2])["properties"] as? [String: Any])
|
||||
let content = try #require((props["content"] as? [String])?.first)
|
||||
// Both refs share the one uploaded URL.
|
||||
#expect(content.components(separatedBy: "https://micro.blog/uploads/2026/ink.png").count == 3)
|
||||
}
|
||||
|
||||
@Test @MainActor func `a missing illustration refuses before anything goes out`() async throws {
|
||||
let (model, home) = try makeModelHome()
|
||||
_ = try plantIllustratedPiece(
|
||||
model, home,
|
||||
body: "# Inked\n\n\n\nProse.",
|
||||
) // path.png planted, ghost.png never
|
||||
|
||||
let log = MicropubRequestLog()
|
||||
let outcome = await model.micropubSendDraft(home: home, token: "tok", transport: micropubMediaScripted(log: log))
|
||||
|
||||
guard case .refused = outcome else {
|
||||
Issue.record("expected refused, got \(outcome)")
|
||||
return
|
||||
}
|
||||
#expect(log.requests.isEmpty)
|
||||
}
|
||||
|
||||
@Test @MainActor func `a piece without illustrations sends exactly one request`() async throws {
|
||||
let (model, home) = try makeModelHome()
|
||||
let post = try PostStore.writeNew(body: "# Plain\n\nBody.", home: home)
|
||||
model.select(post.id, home: home)
|
||||
|
||||
let log = MicropubRequestLog()
|
||||
_ = await model.micropubSendDraft(home: home, token: "tok", transport: micropubMediaScripted(log: log))
|
||||
|
||||
// The #33 path unchanged: no config round-trip, straight to the create.
|
||||
#expect(log.requests.count == 1)
|
||||
#expect(log.requests[0].url == Micropub.endpoint)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user