CI / ci (push) Canceled after 0s
Two-step publish to Micro.blog over Micropub: Send as Draft (JSON h=entry create — name = the Title, markdown content verbatim, post-status draft), review via the preview link, then Publish (the action=update post-status flip). The piece remembers its draft URL as a frontmatter micropub_url stamp — in-place, body and foreign keys byte-identical — so a draft survives a reopen; Send Again re-drafts after edits. The app token lives in the Keychain (login keychain; entered once in the sheet, re-offered only on auth failure — ADR-0014's no-Keychain rule covers only provider keys hermes reads from .env). Frontmatter category: is permitted but never sent in v1 — pinned on the wire. The draft URL is trust-boundary data landing in frontmatter, so every URL passes a format gate (https + host, no line/control characters) at BOTH ends: the response decode (Location header → body url; the preview link is display-only, never the publish target) and Post.scan's read-back — imported frontmatter rides verbatim, and a newline--- rider would otherwise close the block and reparse as attacker markdown in the writer's piece. Twenty tests pin the wire contract, decode matrix, own-slot keychain round-trip, stamp/scan, and the model flow (flush-first, refusal guards, the category AC); all four audit guards are flip-verified. Full suite 529 passed / 0 failed. Live end-to-end on a real post (needs the owner's app token) remains open on the ticket.
145 lines
5.5 KiB
Swift
145 lines
5.5 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, home: home)
|
|
model.session?.edit("# Flush Me\n\nEdited on the way.") // inside the debounce window
|
|
|
|
let log = MicropubRequestLog()
|
|
let outcome = await model.micropubSendDraft(
|
|
home: home, 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(
|
|
home: home, 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(
|
|
home: home, 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, home: home)
|
|
|
|
let log = MicropubRequestLog()
|
|
let outcome = await model.micropubSendDraft(
|
|
home: home, 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, home: home)
|
|
// 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(
|
|
home: home, 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, home: home)
|
|
|
|
let log = MicropubRequestLog()
|
|
_ = await model.micropubSendDraft(
|
|
home: home, 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, home: home)
|
|
|
|
let log = MicropubRequestLog()
|
|
_ = await model.micropubSendDraft(
|
|
home: home, token: "tok",
|
|
transport: micropubCanned(201, body: "{}", headers: ["Location": "https://example.micro.blog/first"], log: log),
|
|
)
|
|
_ = await model.micropubSendDraft(
|
|
home: home, 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")
|
|
}
|
|
}
|