update the UI

This commit is contained in:
2026-09-29 10:03:15 +02:00
parent 1d29d0952d
commit 2925127110
8 changed files with 219 additions and 62 deletions
@@ -19,6 +19,7 @@ import Testing
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")
@@ -141,9 +142,7 @@ import Testing
asked.append(path)
return path == "a.txt" ? .save : .discard
}
h.store.open("a.txt")
await h.store.settle()
h.store.edit("a2")
h.strand("a.txt", disk: "a", buffer: "a2")
h.store.open("b.txt")
await h.store.settle()
h.store.edit("b2")
@@ -158,9 +157,7 @@ import Testing
@Test func cancellingThePromptKeepsEveryEdit() async {
let h = EditorHarness(files: ["a.txt": "a", "b.txt": "b"])
h.store.confirmUnsavedClose = { $0 == "a.txt" ? .discard : .cancel }
h.store.open("a.txt")
await h.store.settle()
h.store.edit("a2")
h.strand("a.txt", disk: "a", buffer: "a2")
h.store.open("b.txt")
await h.store.settle()
h.store.edit("b2")
+116 -41
View File
@@ -24,32 +24,13 @@ import Testing
#expect(h.store.history == ["b.php", "a.php"])
}
@Test func switchingAwayKeepsADirtyBufferAndItIsStillDirtyOnReturn() async {
let h = EditorHarness(files: ["a.php": "a", "b.php": "b"])
var asked = false
h.store.confirmUnsavedClose = { _ in asked = true; return .cancel }
h.store.open("a.php")
await h.store.settle()
h.store.edit("my edit")
h.store.open("b.php")
await h.store.settle()
#expect(!asked)
#expect(h.store.dirtyPaths == ["a.php"])
h.store.open("a.php")
await h.store.settle()
#expect(h.store.activePath == "a.php")
#expect(h.store.text(for: "a.php") == "my edit")
#expect(h.store.isDirty("a.php"))
#expect(h.files.writes.isEmpty)
}
@Test func theOpenFileComesFirstAmongTheDirtyPaths() async {
let h = EditorHarness(files: ["a": "a", "b": "b", "c": "c"])
for path in ["c", "a", "b"] {
h.store.open(path)
await h.store.settle()
h.store.edit(path + "!")
}
h.strand("c", disk: "c", buffer: "c!")
h.strand("a", disk: "a", buffer: "a!")
h.store.open("b")
await h.store.settle()
h.store.edit("b!")
#expect(h.store.dirtyPaths == ["b", "a", "c"])
}
@@ -102,6 +83,116 @@ import Testing
}
}
@MainActor
@Suite struct EditorStoreSwitchTests {
private func dirty(_ files: [String: String] = ["a.txt": "a", "b.txt": "b"]) async -> EditorHarness {
let h = EditorHarness(files: files)
h.store.open("a.txt")
await h.store.settle()
h.store.edit("a edited")
return h
}
@Test func reopeningACleanFileReadsWhatChangedOnDisk() async {
let h = EditorHarness(files: ["a.txt": "one", "b.txt": "b"])
h.store.open("a.txt")
await h.store.settle()
h.store.open("b.txt")
await h.store.settle()
h.files.set("a.txt", "agent wrote this")
h.store.open("a.txt")
await h.store.settle()
#expect(h.store.text(for: "a.txt") == "agent wrote this")
}
@Test func switchingAwayFromADirtyFileAsks() async {
let h = await dirty()
var asked: [String] = []
h.store.confirmUnsavedClose = { asked.append($0); return .cancel }
h.store.open("b.txt")
#expect(asked == ["a.txt"])
}
@Test func cancelKeepsTheFileAndItsEdits() async {
let h = await dirty()
var accepted = false
h.store.confirmUnsavedClose = { _ in .cancel }
let opened = h.store.open("b.txt") { accepted = true }
await h.store.settle()
#expect(!opened && !accepted)
#expect(h.store.activePath == "a.txt")
#expect(h.store.text(for: "a.txt") == "a edited")
#expect(h.store.history == ["a.txt"])
}
@Test func discardDropsTheEditsAndOpensTheNewFile() async {
let h = await dirty()
h.store.confirmUnsavedClose = { _ in .discard }
#expect(h.store.open("b.txt"))
await h.store.settle()
#expect(h.store.activePath == "b.txt")
#expect(h.store.dirtyPaths.isEmpty)
#expect(h.store.text(for: "a.txt") == "a")
#expect(h.files.writes.isEmpty)
}
@Test func saveWritesThenOpens() async {
let h = await dirty()
var accepted = false
h.store.confirmUnsavedClose = { _ in .save }
h.store.open("b.txt") { accepted = true }
await h.store.settle()
#expect(h.files.current("a.txt") == "a edited")
#expect(h.store.activePath == "b.txt")
#expect(accepted)
}
@Test func aFailedSaveKeepsTheDirtyFileOpen() async {
let h = await dirty()
var accepted = false
var toasts: [String] = []
h.store.showToast = { title, _ in toasts.append(title) }
h.store.confirmUnsavedClose = { _ in .save }
h.files.failWrites = true
h.store.open("b.txt") { accepted = true }
await h.store.settle()
#expect(!accepted)
#expect(h.store.activePath == "a.txt")
#expect(h.store.isDirty("a.txt"))
#expect(toasts == ["Save failed"])
}
@Test func reopeningTheSameDirtyFileAsksThenReadsDisk() async {
let h = await dirty()
var asked: [String] = []
h.store.confirmUnsavedClose = { asked.append($0); return .discard }
h.files.set("a.txt", "agent wrote this")
h.store.open("a.txt")
await h.store.settle()
#expect(asked == ["a.txt"])
#expect(h.store.text(for: "a.txt") == "agent wrote this")
#expect(!h.store.isDirty("a.txt"))
}
@Test func withAutoSaveTheDirtyFileIsSavedWithoutAsking() async {
let h = EditorHarness(files: ["a.txt": "a", "b.txt": "b"])
h.store.editorConfig.autoSave = true
var asked = false
var toasts: [String] = []
h.store.confirmUnsavedClose = { _ in asked = true; return .cancel }
h.store.showToast = { title, _ in toasts.append(title) }
h.store.open("a.txt")
await h.store.settle()
h.store.edit("a edited")
h.store.open("b.txt")
await h.store.settle()
#expect(!asked)
#expect(toasts.isEmpty)
#expect(h.files.current("a.txt") == "a edited")
#expect(h.store.activePath == "b.txt")
}
}
@MainActor
@Suite struct EditorStoreDirtyTests {
@Test func anEditMakesTheFileDirtyAndTypingBackCleansIt() async {
@@ -218,25 +309,9 @@ import Testing
#expect(!asked)
}
@Test func closingAsksOnlyAboutTheOpenFile() async {
let h = EditorHarness(files: ["a.txt": "one", "b.txt": "b"])
h.store.open("a.txt")
await h.store.settle()
h.store.edit("two")
h.store.open("b.txt")
await h.store.settle()
var asked = false
h.store.confirmUnsavedClose = { _ in asked = true; return .cancel }
#expect(await h.store.requestCloseActive())
#expect(!asked)
#expect(h.store.isDirty("a.txt"))
}
@Test func resolvingUnsavedSeesABufferThatIsNoLongerOpen() async {
let h = EditorHarness(files: ["a.txt": "one", "b.txt": "b"])
h.store.open("a.txt")
await h.store.settle()
h.store.edit("two")
h.strand("a.txt", disk: "one", buffer: "two")
h.store.open("b.txt")
await h.store.settle()
var asked: [String] = []
@@ -406,6 +406,50 @@ import Testing
#expect(harness.model.editor?.activePath == nil)
}
@Test func cancellingTheUnsavedPromptLeavesTheExplorerAndFocusAlone() async {
let harness = WorkspaceHarness(contents: ["a.php": "one", "b.php": "b"])
await harness.start()
harness.model.open("a.php")
await harness.model.settle()
harness.model.editor?.edit("two")
harness.model.layout.setFocusZone(.terminal)
harness.system.unsavedCloseChoice = .cancel
harness.model.open("b.php")
await harness.model.settle()
#expect(harness.system.unsavedCloseQuestions == ["a.php"])
#expect(harness.model.editor?.activePath == "a.php")
#expect(harness.model.explorer?.selectedPath == "a.php")
#expect(harness.model.layout.focusZone == .terminal)
}
@Test func aFailedSaveLeavesTheExplorerOnTheDirtyFile() async {
let harness = WorkspaceHarness(contents: ["a.php": "one", "b.php": "b"])
await harness.start()
harness.model.open("a.php")
await harness.model.settle()
harness.model.editor?.edit("two")
harness.system.unsavedCloseChoice = .save
harness.files.failWrites = true
harness.model.open("b.php")
await harness.model.settle()
#expect(harness.model.editor?.activePath == "a.php")
#expect(harness.model.explorer?.selectedPath == "a.php")
}
@Test func savingOnTheUnsavedPromptThenRevealsTheNewFile() async {
let harness = WorkspaceHarness(contents: ["a.php": "one", "b.php": "b"])
await harness.start()
harness.model.open("a.php")
await harness.model.settle()
harness.model.editor?.edit("two")
harness.system.unsavedCloseChoice = .save
harness.model.open("b.php")
await harness.model.settle()
#expect(harness.files.current("a.php") == "two")
#expect(harness.model.editor?.activePath == "b.php")
#expect(harness.model.explorer?.selectedPath == "b.php")
}
@Test func commandWWithoutAnOpenFileClosesTheWindow() async {
let harness = WorkspaceHarness()
await harness.start()
@@ -76,3 +76,10 @@ struct EditorHarness {
store.dismissDiffOverlay = { overlays.diffDismissals += 1 }
}
}
extension EditorHarness {
/// Unsaved edits on a file that is not open; opening another file never leaves these behind.
func strand(_ path: String, disk: String, buffer: String) {
store.states[path] = FileState(buffer: buffer, disk: disk)
}
}