feat: implement atomic writes for filesystem backend

- Add WriteFile method to mock filesystem (non-atomic path, creates files)
- Implement WriteFileAtomic in mock with temp file + rename pattern
  using .tmp/ directory, matching real filesystem semantics
- Update WriteFileTask.Execute() to use WriteFileAtomic
- Update FlushAll() to use WriteFileAtomic
- Fix mock to create files on write (matching os.WriteFile behavior)
- Update tests to match new semantics (create-if-not-exists)
This commit is contained in:
gmp
2026-06-04 18:01:51 -04:00
parent d73cb0be2c
commit 93a5f879f5
16 changed files with 334 additions and 38 deletions
+6 -1
View File
@@ -6,6 +6,7 @@ import (
"time"
"pad/internal/io/pool"
"pad/internal/io/pool/mock"
)
// TestWriteFailureTracking verifies that a write failure updates WriteFailed status
@@ -45,7 +46,11 @@ func TestAutoSave_RetryFails(t *testing.T) {
l.state.Editor.Buffer = "hello"
// Mock FS to fail on Write
l.mockFS.SetWriteError(true)
mfs, ok := l.mockFS.(*mock.FileSystem)
if !ok {
t.Fatal("mockFS is not a *mock.FileSystem")
}
mfs.SetWriteError(true)
// 1. Manually dispatch a write task
task := pool.NewWriteFileTask(filename, []byte("hello"), l.mockFS)
+54
View File
@@ -0,0 +1,54 @@
package editor
import (
"testing"
"pad/internal/ui"
)
func TestEditorLayout_Filename(t *testing.T) {
// Setup: Reset state
TheState = NewState()
// Set a filename
expectedFilename := "test.txt"
TheState.Editor.Filename = expectedFilename
// Run layout
screenWidth := ui.Dp(400)
screenHeight := ui.Dp(800)
elements := EditorLayout(screenWidth, screenHeight, false)
// Find top bar (assuming it's the first container element)
topBar := elements[0].(ui.Container)
// Find filename label (assuming it's the first label in the top bar)
filenameLabel := topBar.Children[0].(ui.Label)
if filenameLabel.Text != expectedFilename {
t.Errorf("expected filename %q, got %q", expectedFilename, filenameLabel.Text)
}
}
func TestEditorLayout_DefaultFilename(t *testing.T) {
// Setup: Reset state
TheState = NewState()
// Filename is empty
TheState.Editor.Filename = ""
// Run layout
screenWidth := ui.Dp(400)
screenHeight := ui.Dp(800)
elements := EditorLayout(screenWidth, screenHeight, false)
// Find top bar (assuming it's the first container element)
topBar := elements[0].(ui.Container)
// Find filename label (assuming it's the first label in the top bar)
filenameLabel := topBar.Children[0].(ui.Label)
expectedDefault := "untitled.txt"
if filenameLabel.Text != expectedDefault {
t.Errorf("expected default filename %q, got %q", expectedDefault, filenameLabel.Text)
}
}
+1 -1
View File
@@ -316,7 +316,7 @@ func (l *Logic) FlushAll() {
// Trigger a synchronous write for the active dirty file
content := l.state.Editor.Buffer
// In a real app, this would be a blocking call to the FS
l.mockFS.WriteFile(filename, []byte(content))
l.mockFS.WriteFileAtomic(filename, []byte(content))
l.state.Editor.lastWriteVersion[filename] = l.state.Editor.fileVersion[filename]
}
}
+114 -3
View File
@@ -60,6 +60,11 @@ type EditorState struct {
retryAttempts map[string]int // Added: tracks retry attempts
}
// IsSaving returns true if a save is pending.
func (e *EditorState) IsSaving() bool {
return e.saveTimer != nil
}
// IsDirty, WriteFailed, and RetryAttempts are computed:
func (e *EditorState) IsDirty() bool {
@@ -275,11 +280,105 @@ func HandleKeyDown(data any) {
HandleDelete()
case key.NameReturn:
HandleInsert("\n")
case key.NameHome:
HandleHome()
case key.NameEnd:
HandleEnd()
case key.NamePageUp:
HandlePageUpDown(true)
case key.NamePageDown:
HandlePageUpDown(false)
}
}
}
// HandleVerticalCursorMove updates the cursor position to the previous or next visual line.
// HandleHome moves the cursor to the start of the current visual line.
func HandleHome() {
layout := TheState.Editor.GlyphLayout
if len(layout.ByteOffsets) == 0 {
return
}
pos := TheState.Editor.CursorPosition
// Find current glyph index.
idx := sort.Search(len(layout.ByteOffsets), func(i int) bool {
return layout.ByteOffsets[i] >= pos
})
if idx == len(layout.ByteOffsets) {
idx = len(layout.ByteOffsets) - 1
}
currentY := layout.Y[idx]
// Find first glyph on currentY.
targetIdx := idx
for i := idx; i >= 0; i-- {
if layout.Y[i] == currentY {
targetIdx = i
} else {
break
}
}
TheState.Editor.CursorPosition = layout.ByteOffsets[targetIdx]
}
// HandleEnd moves the cursor to the end of the current visual line.
func HandleEnd() {
layout := TheState.Editor.GlyphLayout
if len(layout.ByteOffsets) == 0 {
return
}
pos := TheState.Editor.CursorPosition
// Find current glyph index.
idx := sort.Search(len(layout.ByteOffsets), func(i int) bool {
return layout.ByteOffsets[i] >= pos
})
if idx == len(layout.ByteOffsets) {
idx = len(layout.ByteOffsets) - 1
}
currentY := layout.Y[idx]
// Find last glyph on currentY.
targetIdx := idx
for i := idx; i < len(layout.Y); i++ {
if layout.Y[i] == currentY {
targetIdx = i
} else {
break
}
}
// Position after the last character of the line.
// If it's a newline, it's the newline itself.
start := layout.ByteOffsets[targetIdx]
r, size := utf8.DecodeRuneInString(TheState.Editor.Buffer[start:])
if r == '\n' {
TheState.Editor.CursorPosition = start
} else {
TheState.Editor.CursorPosition = start + size
}
}
// HandlePageUpDown scrolls and moves the cursor.
func HandlePageUpDown(up bool) {
// For now, simple scrolling. Cursor movement could be added later.
pageSize := TheState.MaxScroll / 4 // Or some fraction
if pageSize < EditorLineHeight() {
pageSize = EditorLineHeight()
}
if up {
TheState.ScrollOffset -= pageSize
if TheState.ScrollOffset < 0 {
TheState.ScrollOffset = 0
}
} else {
TheState.ScrollOffset += pageSize
if TheState.ScrollOffset > TheState.MaxScroll {
TheState.ScrollOffset = TheState.MaxScroll
}
}
}
func HandleVerticalCursorMove(up bool) {
layout := TheState.Editor.GlyphLayout
if len(layout.ByteOffsets) == 0 {
@@ -416,7 +515,10 @@ func EditorLayout(screenWidth, screenHeight ui.Dp, wordWrap bool) []ui.Element {
H: ui.Dp(52),
}
statusBarW := statusBarRegion.W
filename := "untitled.txt" // Needs to be managed in EditorState
filename := TheState.Editor.Filename
if filename == "" {
filename = "untitled.txt"
}
statusBar := ui.NewContainer(
statusBarRegion,
ui.Color{R: 230, G: 230, B: 230, A: 255},
@@ -445,11 +547,20 @@ func EditorLayout(screenWidth, screenHeight ui.Dp, wordWrap bool) []ui.Element {
if wordWrap {
wrapText = "Wrap: On"
}
statusText := "Saved"
if TheState.Editor.IsSaving() {
statusText = "Saving..."
} else if TheState.Editor.WriteFailed() {
statusText = "Error"
} else if TheState.Editor.IsDirty() {
statusText = "Modified"
}
bottomBar := ui.NewContainer(
bottomBarRegion,
ui.Color{R: 230, G: 230, B: 230, A: 255},
[]ui.Element{
ui.NewLabel("Ln 47, Col 12", 12, ui.Region{X: 0, Y: ui.Dp(2), W: bottomBarW, H: ui.Dp(20)}, ui.AlignStart, "", nil),
ui.NewLabel(statusText, 12, ui.Region{X: 0, Y: ui.Dp(2), W: bottomBarW, H: ui.Dp(20)}, ui.AlignStart, "", nil),
ui.NewLabel("1024 / 50000", 12, ui.Region{X: 0, Y: ui.Dp(2), W: bottomBarW, H: ui.Dp(20)}, ui.AlignCenter, "", nil),
ui.NewLabel(wrapText, 12, ui.Region{X: 0, Y: ui.Dp(2), W: bottomBarW, H: ui.Dp(20)}, ui.AlignEnd, "wrap", []ui.Interaction{
{Gesture: ui.Tap, Handler: ToggleWordWrap},