feat(posts): import & external markdown — S1.1 (#22)
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
External .md/.markdown opens and edits as-is in a single guest slot (File > Open... Cmd+O, drag-onto-window): SessionTarget extends EditorSession to write in place verbatim, no cursor sidecar, the #67 conflict alert reused for foreign files. Import... (Shift+Cmd+O) copies multi-select files in as Posts behind ONE Origin ask (writer default, immutable); external frontmatter keys ride verbatim with pass_state/origin set; slug/collision machinery shared with writeNew. Audit fixes folded in: BOM stripped + CRLF normalized at the trust boundary (a CRLF pair is one Swift grapheme — \n splits saw one line); unreadable files refuse to open (loadFailed — a failed read never becomes a write); dropped web links and severed symlinks guarded; ~24 lines of reviewer-flagged dead weight removed. 7 new tests; full unit + UI suite green; swiftlint clean. Map #22 stays open pending the live calibration pass (#88 posture).
This commit is contained in:
1 parent
683d697367
commit
0f1c4ef4fe
14 files changed
+691
-49
No files matched your search
@@ -26,6 +26,7 @@ Owned by the root AGENTS.md contract.
|
||||
- `ReadingPresenterTests.swift` (#76, ADR-0011) — apex HTML pipeline coverage with inline fixture strings: chunker sentinel stamping, stamper attribute rewrite (multi-digit), `blocks(for:)` line↔range map (monotonic, 1-based lines, exact chunk offsets — the click→edit map), pipeline tests running the REAL bundled apex binary (headings/inline marks, `1. 1. 1.` renumber via native `<ol>`, mixed-lists stay split, table/quote/fence/link/HR constructs with exact `data-source-line` stamps, base64 image embed against `spike/prose`, native footnotes), fallback = escaped source, stylesheet carries the slot CSS vars.
|
||||
- `ProsePresenterTests.swift` (#77) — editing-surface parity pinned: heading ladder sizes (30/22/19/17-bold), half-height blanks (9pt), quote secondary-ink + indent, fence interior mono-14, inline traits (bold/italic/code/link underline + dimmed marks), CSS-1.8 leading calibration tolerance, CommonMark intraword-underscore rule, reattribute with fence-state retrofit.
|
||||
- `FocusModeTests.swift` (#21, map #21) — soft-centering band math pinned: in-band no-op, below-band recenter down, above-band recenter up, document-bottom clamp (parks, then no-ops while parked — no jump), short-document floor at 0.
|
||||
- `ImportExternalTests.swift` (#22, map #22) — import + external buffer via `importHome`/`writeExternal` (temp-dir convention): #95 frontmatter merge (external keys verbatim, our two keys win, original untouched), CRLF file normalized at the boundary (block detected, keys stamped, LF body), bare file → fresh block + #60 collision suffixes, empty file → `untitled`, unreadable file (UTF-16) → `loadFailed`, nothing written, original untouched, external session saves in place with NO sidecar (#93), #67 conflict raised on a foreign write + Yes-reload.
|
||||
|
||||
## Verification
|
||||
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
//
|
||||
// ImportExternalTests.swift
|
||||
// MuseionTests
|
||||
//
|
||||
// #22 (S1.1): import copies + external buffer — frontmatter merge (#95),
|
||||
// batch semantics (#94), external save-in-place with no sidecar (#93).
|
||||
//
|
||||
|
||||
import Foundation
|
||||
@testable import Museion
|
||||
import Testing
|
||||
|
||||
/// Throwaway Home in a temp dir (PostStoreTests convention).
|
||||
private func importHome() throws -> URL {
|
||||
let home = FileManager.default.temporaryDirectory
|
||||
.appendingPathComponent("museion-import-\(UUID().uuidString)", isDirectory: true)
|
||||
try FileManager.default.createDirectory(at: home, withIntermediateDirectories: true)
|
||||
return home
|
||||
}
|
||||
|
||||
private func writeExternal(_ name: String, _ body: String) throws -> URL {
|
||||
let url = FileManager.default.temporaryDirectory
|
||||
.appendingPathComponent("museion-ext-\(UUID().uuidString)-\(name)")
|
||||
try body.write(to: url, atomically: true, encoding: .utf8)
|
||||
return url
|
||||
}
|
||||
|
||||
@Suite(.serialized)
|
||||
@MainActor
|
||||
struct ImportExternalTests {
|
||||
@Test func `import keeps external keys and stamps ours`() throws {
|
||||
let home = try importHome()
|
||||
let ext = try writeExternal("note.md", """
|
||||
---
|
||||
title: My Outside Note
|
||||
date: 2026-10-11
|
||||
tags: [one, two]
|
||||
origin: ai
|
||||
pass_state: Pass-2 Done
|
||||
---
|
||||
|
||||
# My Outside Note
|
||||
|
||||
Body byte-for-byte.
|
||||
""")
|
||||
defer { try? FileManager.default.removeItem(at: ext) }
|
||||
|
||||
let post = try PostStore.importFile(at: ext, origin: .ai, home: home)
|
||||
|
||||
#expect(post.id == "my-outside-note")
|
||||
#expect(post.passState == .firstDraft)
|
||||
#expect(post.origin == .ai)
|
||||
let onDisk = try String(contentsOf: home.appendingPathComponent("posts/my-outside-note/post.md"), encoding: .utf8)
|
||||
// Our two keys win:
|
||||
#expect(onDisk.contains("pass_state: First Draft"))
|
||||
#expect(onDisk.contains("origin: ai"))
|
||||
// External keys ride along:
|
||||
#expect(onDisk.contains("date: 2026-10-11"))
|
||||
#expect(onDisk.contains("tags: [one, two]"))
|
||||
// Body byte-for-byte below the block:
|
||||
#expect(onDisk.contains("# My Outside Note\n\nBody byte-for-byte."))
|
||||
// The original file is untouched (copy, never move):
|
||||
let original = try String(contentsOf: ext, encoding: .utf8)
|
||||
#expect(original.contains("pass_state: Pass-2 Done"))
|
||||
#expect(!original.contains("First Draft"))
|
||||
}
|
||||
|
||||
@Test func `import survives CRLF frontmatter`() throws {
|
||||
let home = try importHome()
|
||||
// Windows-authored file: CRLF endings + frontmatter WITHOUT our keys.
|
||||
// CRLF is normalized to LF at the import boundary (a CRLF pair is one
|
||||
// grapheme in Swift — the \n-split machinery would see one line), so
|
||||
// the block is detected, our keys inserted, and the body lands LF.
|
||||
let ext = try writeExternal("crlf.md", "---\r\ndate: 2026-10-11\r\n---\r\n\r\n# CRLF Note\r\n\r\nBody.\r\n")
|
||||
defer { try? FileManager.default.removeItem(at: ext) }
|
||||
|
||||
let post = try PostStore.importFile(at: ext, origin: .ai, home: home)
|
||||
|
||||
#expect(post.id == "crlf-note")
|
||||
let onDisk = try String(contentsOf: home.appendingPathComponent("posts/crlf-note/post.md"), encoding: .utf8)
|
||||
#expect(onDisk.contains("pass_state: First Draft"))
|
||||
#expect(onDisk.contains("origin: ai"))
|
||||
#expect(onDisk.contains("date: 2026-10-11"))
|
||||
#expect(!onDisk.contains("\r")) // normalized at the boundary
|
||||
}
|
||||
|
||||
@Test func `unreadable external file never opens as a writable empty session`() async throws {
|
||||
// A UTF-16 encoded file: String(contentsOf: .utf8) fails → nil.
|
||||
let ext = FileManager.default.temporaryDirectory
|
||||
.appendingPathComponent("museion-ext-\(UUID().uuidString)-utf16.md")
|
||||
let data = "# Title\n\nBody.".data(using: .utf16)!
|
||||
try data.write(to: ext)
|
||||
defer { try? FileManager.default.removeItem(at: ext) }
|
||||
|
||||
let session = EditorSession(target: .externalFile(ext), debounce: .milliseconds(50))
|
||||
#expect(session.loadFailed)
|
||||
|
||||
// Type into the empty editor, let the debounce fire: nothing is written.
|
||||
session.edit("X")
|
||||
try await Task.sleep(for: .milliseconds(200))
|
||||
let disk = try String(contentsOf: ext, encoding: .utf16)
|
||||
#expect(disk == "# Title\n\nBody.") // original untouched
|
||||
}
|
||||
|
||||
@Test func `import bare file gets fresh block and untitled collision suffixes`() throws {
|
||||
let home = try importHome()
|
||||
let firstFile = try writeExternal("a.md", "# Same Title\n\nA.")
|
||||
let secondFile = try writeExternal("b.md", "# Same Title\n\nB.")
|
||||
defer { try? FileManager.default.removeItem(at: firstFile); try? FileManager.default.removeItem(at: secondFile) }
|
||||
|
||||
let first = try PostStore.importFile(at: firstFile, origin: .writer, home: home)
|
||||
let second = try PostStore.importFile(at: secondFile, origin: .writer, home: home)
|
||||
|
||||
#expect(first.id == "same-title")
|
||||
#expect(second.id == "same-title-2") // #60 machinery
|
||||
let bare = try String(contentsOf: home.appendingPathComponent("posts/same-title/post.md"), encoding: .utf8)
|
||||
#expect(bare.hasPrefix("---\npass_state: First Draft\norigin: writer\n---\n"))
|
||||
}
|
||||
|
||||
@Test func `import empty file is untitled`() throws {
|
||||
let home = try importHome()
|
||||
let ext = try writeExternal("empty.md", "")
|
||||
defer { try? FileManager.default.removeItem(at: ext) }
|
||||
let post = try PostStore.importFile(at: ext, origin: .writer, home: home)
|
||||
#expect(post.id == "untitled")
|
||||
#expect(post.title == "untitled")
|
||||
}
|
||||
|
||||
@Test func `external session saves in place with no sidecar`() async throws {
|
||||
let home = try importHome()
|
||||
let ext = try writeExternal("journal.md", "# Journal\n\nOne.")
|
||||
defer { try? FileManager.default.removeItem(at: ext) }
|
||||
|
||||
let session = EditorSession(target: .externalFile(ext), debounce: .milliseconds(50))
|
||||
#expect(session.postID == nil)
|
||||
#expect(session.fileURL == ext)
|
||||
#expect(session.text == "# Journal\n\nOne.")
|
||||
|
||||
session.edit("# Journal\n\nOne. Two.")
|
||||
try await Task.sleep(for: .milliseconds(200))
|
||||
#expect(session.saveState == .saved)
|
||||
|
||||
// Whole file verbatim — no frontmatter stamped:
|
||||
let disk = try String(contentsOf: ext, encoding: .utf8)
|
||||
#expect(disk == "# Journal\n\nOne. Two.")
|
||||
// No sidecar next to the foreign file (#93):
|
||||
let sidecar = ext.deletingLastPathComponent()
|
||||
.appendingPathComponent(".museion-state.json")
|
||||
#expect(!FileManager.default.fileExists(atPath: sidecar.path))
|
||||
}
|
||||
|
||||
@Test func `external session raises the 67 conflict on foreign write`() throws {
|
||||
_ = try importHome()
|
||||
let ext = try writeExternal("conflict.md", "# C\n\nBase.")
|
||||
defer { try? FileManager.default.removeItem(at: ext) }
|
||||
|
||||
let session = EditorSession(target: .externalFile(ext), debounce: .seconds(60))
|
||||
// Someone edits the file underneath us:
|
||||
try "# C\n\nForeign edit.".write(to: ext, atomically: true, encoding: .utf8)
|
||||
session.diskDidChange(diskText: "# C\n\nForeign edit.")
|
||||
#expect(session.conflictAlert)
|
||||
|
||||
// Yes-reload takes the foreign content:
|
||||
session.reloadFromDisk()
|
||||
#expect(!session.conflictAlert)
|
||||
#expect(session.text == "# C\n\nForeign edit.")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user