feat(editor): land editor core — open, edit, autosave (S1.2)
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
Posts open into the TextKit2 surface (ADR-0010 mode B) lifted from the proven spike: styled source, scaled headings, dimmed syntax, native undo/IME. One EditorSession per open post drives a ~1 s debounced autosave (flush on selection change, window close, app quit; ⌘S immediate), the Saved/Saving/Failed dot, and the per-post .museion-state.json cursor sidecar that dies with the folder. External edits raise an explicit Reload/Keep-Local alert — the debounce never answers it for the writer, and drafts keep the lazy-folder first-⌘S rule (#62). Reviewer fanout fixes folded in: session teardown at every lifecycle exit (rename flushes before the folder move; trash cancels the debounce), draft ⌘S continues in place, conflict holds the debounce, rescan's disk read off-main, ~35 lines of dead code out. Closes #19
This commit is contained in:
1 parent
530f852f75
commit
f2953ee07e
13 files changed
+893
-87
No files matched your search
@@ -22,6 +22,7 @@ Owned by the root AGENTS.md contract.
|
||||
- `ProfileProvisionerTests.swift` extends it: the fake creates the profile at `<binaryDir>/profiles/<name>` (self-locating, race-free under parallel tests); payload dirs are built at runtime; the bundled-payload test asserts manifest ↔ payload correspondence. #48 failure-path tests live in `ProfileProvisionerFailureTests.swift` (own file — Not-Now defer + re-fail, config.yaml byte-identity + fresh-profile no-config (mcp_servers negative invariant), state.json engine-gate-only, copy-phase catch branch, path-traversal rejection, orphaned-temp sweep). Shared helpers (`makeFakeHermes`, `makePayload`, `defaultSkills`, `provTempDir`) are internal in `ProfileProvisionerTests.swift`. `ProfileBackupTests.swift` (own helpers `backupTempDir`/`makeProfileFixture`) injects profile/backups roots, a fixed clock, and a `psScan` stub — a dev machine may run the real engine, so the ps scan is never trusted in tests.
|
||||
- `PostLibraryTests.swift` (#17) — throwaway Homes via `makeHome`/`writePost` in temp dirs: bare corpus posts (First Draft/writer defaults), lenient + quoted frontmatter (`pass_state`/`origin`), lazy folders / hidden dirs / symlinks / noise not surfacing, localizedStandardCompare title sort, `untitled` edges (blank, marker-only, unterminated frontmatter), leading-blank-line frontmatter, default Home = `~/Documents/Museion`.
|
||||
- `PostStoreTests.swift` (#18) — write side via `storeHome`/`readPost` (same temp-dir pattern): slug algorithm cases (diacritics, CJK, cap, punctuation-only → untitled), lazy first write + #59 stamp round-trip through `Post.scan`, untitled fallback, case-insensitive collision suffixes, rename (retitle + move, insert-title path, same-slug retitle-only, collision -2), trash removes from posts/, overwrite preservation + bare-post normalization, `readBody` stripping, blocked-posts failure path.
|
||||
- `EditorSessionTests.swift` (#19) — session mechanics via `makeSessionHome`/`seedPost`: debounce → flush + sidecar round-trip, clean-flush no-op + ⌘S immediate, #67 conflict (echo suppressed, Yes-reload, No-keep-local → overwrite), write failure → `.failed` + retry.
|
||||
|
||||
## Verification
|
||||
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
//
|
||||
// EditorSessionTests.swift
|
||||
// Museion
|
||||
//
|
||||
// #19 (S1.2): one runnable check per piece of session logic — debounce →
|
||||
// flush, save-state transitions, sidecar round-trip + Trash death, conflict
|
||||
// alert (echo suppression, Yes-reload, No-keep-local).
|
||||
//
|
||||
|
||||
import Foundation
|
||||
@testable import Museion
|
||||
import Testing
|
||||
|
||||
@MainActor
|
||||
private func makeSessionHome() -> URL {
|
||||
let dir = FileManager.default.temporaryDirectory
|
||||
.appendingPathComponent("museion-editor-\(UUID().uuidString)", isDirectory: true)
|
||||
try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
||||
return dir
|
||||
}
|
||||
|
||||
@MainActor
|
||||
private func seedPost(_ home: URL, id: String, body: String) {
|
||||
let dir = home.appendingPathComponent("posts/\(id)", isDirectory: true)
|
||||
try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
||||
try? PostStore.stamped(body: body, passState: .firstDraft, origin: .writer)
|
||||
.write(to: dir.appendingPathComponent("post.md"), atomically: true, encoding: .utf8)
|
||||
}
|
||||
|
||||
@Suite(.serialized)
|
||||
@MainActor
|
||||
struct EditorSessionTests {
|
||||
@Test func `debounce flushes after idle and stamps the sidecar`() async throws {
|
||||
let home = makeSessionHome()
|
||||
seedPost(home, id: "alpha", body: "# Alpha\n\nOne.")
|
||||
let session = EditorSession(postID: "alpha", home: home, debounce: .milliseconds(50))
|
||||
|
||||
#expect(session.saveState == .saved)
|
||||
session.edit("# Alpha\n\nOne. Two.")
|
||||
#expect(session.saveState == .saved) // still inside the debounce window
|
||||
|
||||
try await Task.sleep(for: .milliseconds(200))
|
||||
#expect(session.saveState == .saved) // saved → Saving… → Saved lands here
|
||||
#expect(PostStore.readBody(id: "alpha", home: home) == "# Alpha\n\nOne. Two.")
|
||||
|
||||
// Sidecar: written on flush, reads back.
|
||||
let cursor = PostStore.CursorState(selectionLocation: 3, selectionLength: 2, scrollOffset: 128)
|
||||
session.cursorUpdate(cursor)
|
||||
session.edit("# Alpha\n\nOne. Two. Three.")
|
||||
try await Task.sleep(for: .milliseconds(200))
|
||||
let restored = PostStore.readCursor(id: "alpha", home: home)
|
||||
#expect(restored == cursor)
|
||||
}
|
||||
|
||||
@Test func `flush is a no-op when clean and immediate when dirty via command-S path`() {
|
||||
let home = makeSessionHome()
|
||||
seedPost(home, id: "beta", body: "# Beta\n\nBody.")
|
||||
let session = EditorSession(postID: "beta", home: home)
|
||||
|
||||
// Clean: flush does nothing (no throw, no write churn).
|
||||
session.flush()
|
||||
#expect(session.saveState == .saved)
|
||||
|
||||
// Dirty + immediate flush (⌘S) — disk updated synchronously.
|
||||
session.edit("# Beta\n\nChanged.")
|
||||
session.flush()
|
||||
#expect(PostStore.readBody(id: "beta", home: home) == "# Beta\n\nChanged.")
|
||||
}
|
||||
|
||||
@Test func `external change alerts, echo suppressed; Yes reloads, No keeps local`() throws {
|
||||
let home = makeSessionHome()
|
||||
seedPost(home, id: "gamma", body: "# Gamma\n\nOriginal.")
|
||||
let session = EditorSession(postID: "gamma", home: home)
|
||||
|
||||
// Watcher echo of our own write: disk == lastFlushedText → no alert.
|
||||
session.edit("# Gamma\n\nMine now.")
|
||||
session.flush()
|
||||
session.diskDidChange(diskText: "# Gamma\n\nMine now.")
|
||||
#expect(!session.conflictAlert)
|
||||
|
||||
// Genuine external edit → alert.
|
||||
try PostStore.overwrite(id: "gamma", body: "# Gamma\n\nExternal.", home: home)
|
||||
session.diskDidChange(diskText: "# Gamma\n\nExternal.")
|
||||
#expect(session.conflictAlert)
|
||||
|
||||
// Yes-reload: text = disk, load generation moved (surface reloads).
|
||||
session.reloadFromDisk()
|
||||
#expect(!session.conflictAlert)
|
||||
#expect(session.text == "# Gamma\n\nExternal.")
|
||||
#expect(session.loadGeneration == 1)
|
||||
|
||||
// Second external edit → No: keep local; next flush overwrites.
|
||||
try PostStore.overwrite(id: "gamma", body: "# Gamma\n\nNewer external.", home: home)
|
||||
session.diskDidChange(diskText: "# Gamma\n\nNewer external.")
|
||||
#expect(session.conflictAlert)
|
||||
session.keepLocal()
|
||||
#expect(!session.conflictAlert)
|
||||
#expect(session.text == "# Gamma\n\nExternal.") // local kept
|
||||
session.edit("# Gamma\n\nLocal wins.")
|
||||
session.flush()
|
||||
#expect(PostStore.readBody(id: "gamma", home: home) == "# Gamma\n\nLocal wins.")
|
||||
}
|
||||
|
||||
@Test func `write failure flips saveState to failed and retries on next flush`() throws {
|
||||
let home = makeSessionHome()
|
||||
seedPost(home, id: "delta", body: "# Delta\n\nBody.")
|
||||
let session = EditorSession(postID: "delta", home: home)
|
||||
|
||||
// Vanish the post: overwrite throws (no resurrection), state = failed.
|
||||
try FileManager.default.removeItem(
|
||||
at: home.appendingPathComponent("posts/delta", isDirectory: true),
|
||||
)
|
||||
var reported: String?
|
||||
session.onError = { reported = $0 }
|
||||
session.edit("# Delta\n\nOrphan.")
|
||||
session.flush()
|
||||
#expect(session.saveState == .failed)
|
||||
#expect(reported != nil)
|
||||
|
||||
// Next flush retries (still failed until the post returns).
|
||||
session.flush()
|
||||
#expect(session.saveState == .failed)
|
||||
}
|
||||
|
||||
@Test func `debounce does not clobber while the conflict alert is unanswered`() async throws {
|
||||
let home = makeSessionHome()
|
||||
seedPost(home, id: "epsilon", body: "# Epsilon\n\nMine.")
|
||||
let session = EditorSession(postID: "epsilon", home: home, debounce: .milliseconds(50))
|
||||
|
||||
// External change lands mid-typing → alert up; debounce must hold fire.
|
||||
session.edit("# Epsilon\n\nMy newer local.")
|
||||
try PostStore.overwrite(id: "epsilon", body: "# Epsilon\n\nExternal.", home: home)
|
||||
session.diskDidChange(diskText: "# Epsilon\n\nExternal.")
|
||||
#expect(session.conflictAlert)
|
||||
|
||||
try await Task.sleep(for: .milliseconds(200)) // debounce window passes
|
||||
#expect(PostStore.readBody(id: "epsilon", home: home) == "# Epsilon\n\nExternal.") // external edit intact
|
||||
#expect(session.saveState == .saved)
|
||||
|
||||
// Keep Local → next explicit flush overwrites (the writer's choice).
|
||||
session.keepLocal()
|
||||
session.flush()
|
||||
#expect(PostStore.readBody(id: "epsilon", home: home) == "# Epsilon\n\nMy newer local.")
|
||||
}
|
||||
|
||||
@Test func `teardown flushFirst writes pending edits and cancels the debounce`() async throws {
|
||||
let home = makeSessionHome()
|
||||
seedPost(home, id: "zeta", body: "# Zeta\n\nBase.")
|
||||
let session = EditorSession(postID: "zeta", home: home, debounce: .milliseconds(50))
|
||||
|
||||
// flushFirst=true: pending text lands (rename reads disk next).
|
||||
session.edit("# Zeta\n\nReworked.")
|
||||
session.teardown(flushFirst: true)
|
||||
#expect(PostStore.readBody(id: "zeta", home: home) == "# Zeta\n\nReworked.")
|
||||
|
||||
// flushFirst=false (trash path): the armed debounce never fires.
|
||||
session.edit("# Zeta\n\nDoomed.") // session dropped by owner after teardown; debounce armed
|
||||
session.teardown(flushFirst: false)
|
||||
try await Task.sleep(for: .milliseconds(200))
|
||||
#expect(PostStore.readBody(id: "zeta", home: home) == "# Zeta\n\nReworked.") // unwritten
|
||||
#expect(session.saveState != .failed) // no spurious failure fired
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user