diff --git a/MuseionTests/ImportExternalTests.swift b/MuseionTests/ImportExternalTests.swift index 45bf022..4eccb48 100644 --- a/MuseionTests/ImportExternalTests.swift +++ b/MuseionTests/ImportExternalTests.swift @@ -25,6 +25,16 @@ private func writeExternal(_ name: String, _ body: String) throws -> URL { 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 { @@ -190,4 +200,80 @@ struct ImportExternalTests { #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) + } }