CI / ci (push) Canceled after 0s
The external guest's watch (PostsModelImports.startExternalWatch) reads disk raw while the session's text/lastFlushedText came through readDocument's normalizing external branch (BOM strip + CRLF->LF), so diskTextChanged raised conflictAlert on unchanged BOM/CRLF files — re-arming after every sibling directory event and after either alert answer, blocking departures and quit (brief-32, argus cycle-6). Three RED pins over the real watch closure (externalWatcher.onEvent): no conflict on an unchanged BOM/CRLF guest, no re-arm after yes-reload or keep-local, and the load-bearing negative — a real foreign write still raises. Targeted rail at base: 11 executed / 8 passed / 3 failed (all three new pins), 164 SwiftCompile lines.
280 lines
11 KiB
Swift
280 lines
11 KiB
Swift
//
|
|
// 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
|
|
}
|
|
|
|
/// brief-32: the guest-watch pins' own temp dir — `startExternalWatch`
|
|
/// observes the file's CONTAINING folder, so their guest must live where
|
|
/// no sibling test ever writes.
|
|
private func watchedDir() throws -> URL {
|
|
let dir = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent("museion-watch-\(UUID().uuidString)", isDirectory: true)
|
|
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
|
return dir
|
|
}
|
|
|
|
@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("projects/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("projects/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 = try #require("# 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("projects/posts/same-title/post.md"), encoding: .utf8)
|
|
#expect(bare.hasPrefix("---\ntype: post\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 `import overwrites external type key with post`() throws {
|
|
let home = try importHome()
|
|
let ext = try writeExternal("outline.md", """
|
|
---
|
|
type: outline
|
|
date: 2026-10-12
|
|
---
|
|
|
|
# Outside Outline
|
|
|
|
Body.
|
|
""")
|
|
defer { try? FileManager.default.removeItem(at: ext) }
|
|
|
|
let post = try PostStore.importFile(at: ext, origin: .writer, home: home)
|
|
|
|
#expect(post.id == "outside-outline")
|
|
#expect(post.type == .post)
|
|
let onDisk = try String(contentsOf: home.appendingPathComponent("projects/posts/outside-outline/post.md"), encoding: .utf8)
|
|
#expect(onDisk.contains("type: post"))
|
|
#expect(!onDisk.contains("type: outline"))
|
|
let scanned = try #require(Post.scan(home: home).first)
|
|
#expect(scanned.type == .post)
|
|
}
|
|
|
|
@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.")
|
|
}
|
|
|
|
// MARK: - The guest watch (brief-32: the #67 compare's disk read)
|
|
|
|
@Test func `external watch raises no 67 conflict on an unchanged BOM and CRLF file`() throws {
|
|
let model = PostsModel()
|
|
let dir = try watchedDir()
|
|
let ext = dir.appendingPathComponent("watched.md")
|
|
// Windows-authored file: BOM + CRLF. The open read normalizes
|
|
// (readDocument's external branch); the watch must read in the
|
|
// SAME representation or every directory event reads as #67.
|
|
try "\u{FEFF}# Title\r\n\r\nBody line.\r\n".write(to: ext, atomically: true, encoding: .utf8)
|
|
defer { try? FileManager.default.removeItem(at: dir) }
|
|
|
|
model.openExternal(ext)
|
|
let session = try #require(model.externalSession)
|
|
#expect(session.text == "# Title\n\nBody line.\n")
|
|
|
|
// Sibling directory events re-fire the compare (the watch observes
|
|
// the containing folder): representation alone must never raise.
|
|
model.externalWatcher.onEvent?()
|
|
#expect(!session.conflictAlert)
|
|
model.externalWatcher.onEvent?()
|
|
#expect(!session.conflictAlert)
|
|
}
|
|
|
|
@Test func `external watch still conflicts on a real write and never re-arms after yes-reload`() throws {
|
|
let model = PostsModel()
|
|
let dir = try watchedDir()
|
|
let ext = dir.appendingPathComponent("foreign.md")
|
|
try "\u{FEFF}# Title\r\n\r\nBody line.\r\n".write(to: ext, atomically: true, encoding: .utf8)
|
|
defer { try? FileManager.default.removeItem(at: dir) }
|
|
|
|
model.openExternal(ext)
|
|
let session = try #require(model.externalSession)
|
|
|
|
// A REAL external modification (different content, same BOM/CRLF
|
|
// representation): the departure gate must still see it.
|
|
try "\u{FEFF}# Title\r\n\r\nRewritten underneath.\r\n".write(to: ext, atomically: true, encoding: .utf8)
|
|
model.externalWatcher.onEvent?()
|
|
#expect(session.conflictAlert)
|
|
|
|
// Yes-reload takes the foreign content, normalized:
|
|
session.reloadFromDisk()
|
|
#expect(!session.conflictAlert)
|
|
#expect(session.text == "# Title\n\nRewritten underneath.\n")
|
|
|
|
// The next directory event reads the same unchanged bytes — the
|
|
// false conflict must not re-arm.
|
|
model.externalWatcher.onEvent?()
|
|
#expect(!session.conflictAlert)
|
|
}
|
|
|
|
@Test func `external watch never re-arms after keep-local on a BOM and CRLF file`() throws {
|
|
let model = PostsModel()
|
|
let dir = try watchedDir()
|
|
let ext = dir.appendingPathComponent("kept.md")
|
|
try "\u{FEFF}# Title\r\n\r\nBody line.\r\n".write(to: ext, atomically: true, encoding: .utf8)
|
|
defer { try? FileManager.default.removeItem(at: dir) }
|
|
|
|
model.openExternal(ext)
|
|
let session = try #require(model.externalSession)
|
|
|
|
try "\u{FEFF}# Title\r\n\r\nForeign body.\r\n".write(to: ext, atomically: true, encoding: .utf8)
|
|
model.externalWatcher.onEvent?()
|
|
#expect(session.conflictAlert)
|
|
|
|
// No: keep local — the disk state is seen, the local buffer stays
|
|
// and stays pending.
|
|
session.keepLocal()
|
|
#expect(!session.conflictAlert)
|
|
#expect(session.text == "# Title\n\nBody line.\n")
|
|
#expect(session.saveState == .changed)
|
|
|
|
model.externalWatcher.onEvent?()
|
|
#expect(!session.conflictAlert)
|
|
}
|
|
}
|