CI / ci (push) Canceled after 0s
One internal seam (PostsModelRail.swift) now pairs every library verb store write with exactly one library rescan and owns the error strip: saveDraft, newPostFromSession, rename, trash, createProject, moveToProject, archivePost, restorePost, unarchiveProject, importFiles, convertToPost, and runPass pass-state advance (12 sites on this base; the pass snapshot/stats writes, the publish stamp, and the editor flush stay outside by design). The model owns the Home: boundHome is set by bind() alone (app construction + Settings repoint); every public verb lost its home: parameter (create post/outline/session-born, rename, move, archive, restore, trash, convert, project creation, import, select, cards, passes, blind read, publish). The three rail entries are railWrite/railWriteReturningReason/ railWriteKeepingStrip — deliberately NOT overloads: a shared base name let unlabeled trailing-closure calls resolve into the labeled non-generic entry (Swift binds trailing closures to labeled first parameters), silently bypassing the strip and the pairing. Views + 11 test files swept to the homeless signatures (160 sites). RED suite green: 3/3. Full unit suite: 654/654. Fixes #179
291 lines
12 KiB
Swift
291 lines
12 KiB
Swift
//
|
|
// MicropubModelTests.swift
|
|
// MuseionTests
|
|
//
|
|
// #33 (S6.1) finish-audit split: the model-flow pins (guards +
|
|
// flush-first + the category AC) out of MicropubTests — that suite
|
|
// crossed its type-body ceiling. Scenario header + shared canned
|
|
// helpers live in MicropubTests.swift.
|
|
//
|
|
|
|
import Foundation
|
|
@testable import Museion
|
|
import Testing
|
|
|
|
@Suite("MicropubModelTests")
|
|
struct MicropubModelTests {
|
|
// MARK: The model flow (guards + flush-first + disk body + stamp)
|
|
|
|
@Test @MainActor func `sendDraft flushes the open piece and sends the disk body with the title`() async throws {
|
|
let (model, home) = try makeModelHome()
|
|
let post = try PostStore.writeNew(body: "# Flush Me\n\nOriginal.", home: home)
|
|
model.select(post.id)
|
|
model.session?.edit("# Flush Me\n\nEdited on the way.") // inside the debounce window
|
|
|
|
let log = MicropubRequestLog()
|
|
let outcome = await model.micropubSendDraft(
|
|
token: "tok",
|
|
transport: micropubCanned(201, body: "{}", headers: ["Location": "https://example.micro.blog/flush-me"], log: log),
|
|
)
|
|
|
|
#expect(outcome == .drafted(url: "https://example.micro.blog/flush-me", preview: nil))
|
|
let request = try #require(log.requests.first)
|
|
let body = String(bytes: request.httpBody ?? Data(), encoding: .utf8) ?? ""
|
|
#expect(body.contains("Edited on the way.")) // the flush landed before the read
|
|
let props = try #require(micropubJSON(request)["properties"] as? [String: Any])
|
|
#expect(props["name"] as? [String] == ["Flush Me"]) // Title publishes as the name
|
|
|
|
// The piece remembers its draft URL.
|
|
#expect(Post.scan(home: home).first?.micropubURL == "https://example.micro.blog/flush-me")
|
|
}
|
|
|
|
@Test @MainActor func `sendDraft refuses without an open saved piece`() async throws {
|
|
let (model, home) = try makeModelHome()
|
|
let log = MicropubRequestLog()
|
|
let outcome = await model.micropubSendDraft(
|
|
token: "tok",
|
|
transport: micropubCanned(201, body: "{}", log: log),
|
|
)
|
|
guard case .refused = outcome else {
|
|
Issue.record("expected refused, got \(outcome)")
|
|
return
|
|
}
|
|
#expect(log.requests.isEmpty) // nothing went out
|
|
}
|
|
|
|
@Test @MainActor func `sendDraft refuses an unsaved draft`() async throws {
|
|
let (model, home) = try makeModelHome()
|
|
model.newPost() // staged draft — no folder, no session
|
|
let log = MicropubRequestLog()
|
|
let outcome = await model.micropubSendDraft(
|
|
token: "tok",
|
|
transport: micropubCanned(201, body: "{}", log: log),
|
|
)
|
|
guard case .refused = outcome else {
|
|
Issue.record("expected refused, got \(outcome)")
|
|
return
|
|
}
|
|
#expect(log.requests.isEmpty)
|
|
}
|
|
|
|
@Test @MainActor func `sendDraft refuses a whitespace-only body and sends nothing`() async throws {
|
|
let (model, home) = try makeModelHome()
|
|
// All-whitespace body — no Title, no content (a heading-only
|
|
// piece DOES send: the Title line is content on Micro.blog).
|
|
let post = try PostStore.writeNew(body: " \n", home: home)
|
|
model.select(post.id)
|
|
|
|
let log = MicropubRequestLog()
|
|
let outcome = await model.micropubSendDraft(
|
|
token: "tok",
|
|
transport: micropubCanned(201, body: "{}", log: log),
|
|
)
|
|
guard case .refused = outcome else {
|
|
Issue.record("expected refused, got \(outcome)")
|
|
return
|
|
}
|
|
#expect(log.requests.isEmpty) // the trimmed-emptiness guard, #6's class
|
|
#expect(Post.scan(home: home).first?.micropubURL == nil)
|
|
}
|
|
|
|
@Test @MainActor func `frontmatter category never travels`() async throws {
|
|
let (model, home) = try makeModelHome()
|
|
let post = try PostStore.writeNew(body: "# Categorized\n\nBody.", home: home)
|
|
model.select(post.id)
|
|
// The writer's own category frontmatter — permitted, inert.
|
|
let fileURL = try #require(PostStore.folder(of: post.id, home: home)?.appendingPathComponent("post.md"))
|
|
let raw = try String(contentsOf: fileURL, encoding: .utf8)
|
|
try raw.replacingOccurrences(of: "origin: writer\n", with: "origin: writer\ncategory: Essays\n")
|
|
.write(to: fileURL, atomically: true, encoding: .utf8)
|
|
|
|
let log = MicropubRequestLog()
|
|
_ = await model.micropubSendDraft(
|
|
token: "tok",
|
|
transport: micropubCanned(201, body: "{}", headers: ["Location": "https://example.micro.blog/categorized"], log: log),
|
|
)
|
|
|
|
let request = try #require(log.requests.first)
|
|
let wire = String(bytes: request.httpBody ?? Data(), encoding: .utf8) ?? ""
|
|
#expect(!wire.lowercased().contains("category"))
|
|
#expect(!wire.contains("Essays"))
|
|
}
|
|
|
|
@Test @MainActor func `a failed draft stamps nothing`() async throws {
|
|
let (model, home) = try makeModelHome()
|
|
let post = try PostStore.writeNew(body: "# Fails\n\nBody.", home: home)
|
|
model.select(post.id)
|
|
|
|
let log = MicropubRequestLog()
|
|
_ = await model.micropubSendDraft(
|
|
token: "tok",
|
|
transport: micropubCanned(500, body: "{}", log: log),
|
|
)
|
|
|
|
#expect(Post.scan(home: home).first?.micropubURL == nil)
|
|
}
|
|
|
|
@Test @MainActor func `re-drafting overwrites the remembered url`() async throws {
|
|
let (model, home) = try makeModelHome()
|
|
let post = try PostStore.writeNew(body: "# Again\n\nBody.", home: home)
|
|
model.select(post.id)
|
|
|
|
let log = MicropubRequestLog()
|
|
_ = await model.micropubSendDraft(
|
|
token: "tok",
|
|
transport: micropubCanned(201, body: "{}", headers: ["Location": "https://example.micro.blog/first"], log: log),
|
|
)
|
|
_ = await model.micropubSendDraft(
|
|
token: "tok",
|
|
transport: micropubCanned(201, body: "{}", headers: ["Location": "https://example.micro.blog/second"], log: log),
|
|
)
|
|
|
|
#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)
|
|
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(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(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(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(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(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)
|
|
|
|
let log = MicropubRequestLog()
|
|
_ = await model.micropubSendDraft(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)
|
|
}
|
|
}
|