import Foundation import HelderCore @testable import HelderUI import Testing @MainActor @Suite struct EditorStoreSafetyTests { private func autoSaving(_ files: [String: String]) -> EditorHarness { let h = EditorHarness(files: files) var config = HelderConfig.Editor() config.autoSave = true h.store.editorConfig = config return h } @Test func autoSaveKeepsAPendingSaveWhenTheTabChanges() async { let h = autoSaving(["a.txt": "a", "b.txt": "b"]) h.store.open("a.txt") await h.store.settle() h.store.edit("a edited") h.store.open("b.txt") await h.store.settle() h.store.edit("b edited") await h.store.settle() #expect(h.files.current("a.txt") == "a edited") #expect(h.files.current("b.txt") == "b edited") } @Test func pendingAutoSavesAreWrittenBeforeTheUnsavedPrompt() async { let h = autoSaving(["a.txt": "a"]) var asked: [String] = [] h.store.confirmUnsavedClose = { asked.append($0); return .cancel } h.store.open("a.txt") await h.store.settle() h.store.edit("typed") #expect(await h.store.resolveUnsaved()) #expect(h.files.current("a.txt") == "typed") #expect(asked.isEmpty) } @Test func anOlderReadThatLandsLastIsDropped() async { let h = EditorHarness(files: ["a.txt": "old"]) h.store.open("a.txt") await h.store.settle() let gate = h.files.holdNextRead("a.txt") let stale = Task { await h.store.filesChanged(nil) } #expect(await eventually { gate.isWaiting }) h.files.set("a.txt", "new") await h.store.reloadFromDisk("a.txt") gate.open() await stale.value #expect(h.store.text(for: "a.txt") == "new") } @Test func forgettingAFileDropsEveryPieceOfItsState() async { let h = EditorHarness(files: ["a.php": "x"]) h.files.setImage("logo.png", Data([1])) h.git.change("a.php", makeDiff(original: "", updated: "x")) h.store.open("a.php", OpenOptions(side: .staged)) h.store.open("logo.png") await h.store.settle() h.store.forget("a.php", isDirectory: false) h.store.forget("logo.png", isDirectory: false) #expect(h.store.states.isEmpty) #expect(h.store.modes.isEmpty && h.store.sides.isEmpty && h.store.images.isEmpty) } @Test func renamingCarriesImagesAlong() async { let h = EditorHarness() h.files.setImage("img/logo.png", Data([1, 2])) h.store.open("img/logo.png") await h.store.settle() h.store.remap(from: "img", to: "assets", isDirectory: true) #expect(h.store.image(for: "assets/logo.png") == Data([1, 2])) #expect(h.store.image(for: "img/logo.png") == nil) } @Test func aReopenedFileNeverReusesAnOldRevision() async { let h = EditorHarness(files: ["a.txt": "one"]) h.store.open("a.txt") await h.store.settle() let first = h.store.revision(for: "a.txt") h.store.forget("a.txt", isDirectory: false) h.files.set("a.txt", "two") h.store.open("a.txt") await h.store.settle() #expect(h.store.revision(for: "a.txt") != first) #expect(h.store.text(for: "a.txt") == "two") } @Test func aFileDeletedOnDiskKeepsItsTextAsUnsavedEdits() async { let h = EditorHarness(files: ["a.txt": "precious"]) h.store.open("a.txt") await h.store.settle() h.files.remove("a.txt") await h.store.filesChanged(nil) #expect(h.store.isDeleted("a.txt")) #expect(h.store.isDirty("a.txt")) #expect(h.store.text(for: "a.txt") == "precious") #expect(h.files.writes.isEmpty) } @Test func autoSaveNeverResurrectsADeletedFile() async { let h = autoSaving(["a.txt": "precious"]) h.store.open("a.txt") await h.store.settle() h.files.remove("a.txt") await h.store.filesChanged(nil) h.store.edit("precious, edited") await h.store.settle() #expect(h.files.writes.isEmpty) } @Test func savingADeletedFileWritesItBackOnPurpose() async throws { let h = EditorHarness(files: ["a.txt": "precious"]) h.store.open("a.txt") await h.store.settle() h.files.remove("a.txt") await h.store.filesChanged(nil) try await h.store.save() #expect(h.files.current("a.txt") == "precious") #expect(!h.store.isDeleted("a.txt")) #expect(!h.store.isDirty("a.txt")) } @Test func aFileThatComesBackIsCleanAgain() async { let h = EditorHarness(files: ["a.txt": "same"]) h.store.open("a.txt") await h.store.settle() h.files.remove("a.txt") await h.store.filesChanged(nil) h.files.set("a.txt", "same") await h.store.filesChanged(nil) #expect(!h.store.isDeleted("a.txt")) #expect(!h.store.isDirty("a.txt")) } @Test func theUnsavedPromptAsksForEveryDirtyFileTheOpenOneFirst() async { let h = EditorHarness(files: ["a.txt": "a", "b.txt": "b"]) var asked: [String] = [] h.store.confirmUnsavedClose = { path in asked.append(path) return path == "a.txt" ? .save : .discard } h.strand("a.txt", disk: "a", buffer: "a2") h.store.open("b.txt") await h.store.settle() h.store.edit("b2") #expect(await h.store.resolveUnsaved()) #expect(h.store.activePath == "b.txt") #expect(asked == ["b.txt", "a.txt"]) #expect(h.files.current("a.txt") == "a2") #expect(h.files.current("b.txt") == "b") #expect(h.store.dirtyPaths.isEmpty) } @Test func cancellingThePromptKeepsEveryEdit() async { let h = EditorHarness(files: ["a.txt": "a", "b.txt": "b"]) h.store.confirmUnsavedClose = { $0 == "a.txt" ? .discard : .cancel } h.strand("a.txt", disk: "a", buffer: "a2") h.store.open("b.txt") await h.store.settle() h.store.edit("b2") #expect(await h.store.resolveUnsaved() == false) #expect(h.store.text(for: "a.txt") == "a2") #expect(h.store.text(for: "b.txt") == "b2") } }