fix: history load notifies and never overwrites unreadable file; dictionary keeps ids across external edits, undo safety

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-01 16:04:01 +02:00
co-authored by Claude Sonnet 5.5
parent 5542f9cf70
commit 31c34a1d4a
8 changed files with 150 additions and 6 deletions
+29
View File
@@ -57,6 +57,35 @@ struct HistoryStoreTests {
#expect(store.entries.isEmpty)
}
@Test func loadNotifiesOnChange() {
let url = tempFile()
let writer = HistoryStore(fileURL: url)
writer.add(entry("a", at: 0)); writer.add(entry("b", at: 1))
writer.flush()
let store = HistoryStore(fileURL: url)
var calls = 0
store.onChange = { calls += 1 }
#expect(store.load() == .loaded)
#expect(calls == 1)
#expect(store.entries.count == 2)
}
@Test func unreadableFileIsReportedAndNeverOverwritten() throws {
let url = tempFile()
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true) // history.json is a directory
let store = HistoryStore(fileURL: url)
guard case .unreadable = store.load() else {
Issue.record("expected unreadable"); return
}
store.add(entry("a", at: 0))
store.flush()
var isDirectory: ObjCBool = false
#expect(FileManager.default.fileExists(atPath: url.path, isDirectory: &isDirectory))
#expect(isDirectory.boolValue)
#expect(store.lastError != nil)
}
@Test func onChangeFiresForEdits() {
let store = HistoryStore(fileURL: tempFile())
var calls = 0