The tree was formatted with an older gofmt; go1.27's gofmt additionally wants: EOF exactly one newline (no trailing blank lines), imports sorted alphabetically within a block, mixed-precedence binary expressions re-spaced for grouping ((a+b)/c), single-field composite literals un-aligned, adjacent one-line method signatures aligned, and one-line bodies containing a compound statement expanded. Applied repo-wide (31 files under internal/); pure formatting, no semantic changes — build and the full test suite pass.
82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
package editor
|
|
|
|
import (
|
|
"log"
|
|
"testing"
|
|
"time"
|
|
|
|
"pad/internal/io/pool"
|
|
"pad/internal/io/pool/mock"
|
|
)
|
|
|
|
// TestWriteFailureTracking verifies that a write failure updates WriteFailed status
|
|
func TestWriteFailureTracking(t *testing.T) {
|
|
state := NewState()
|
|
filename := "test.txt"
|
|
state.Editor.Filename = filename
|
|
|
|
// Simulate a write failure
|
|
state.Editor.SetWriteFailed(filename, true)
|
|
|
|
if !state.Editor.WriteFailed() {
|
|
t.Errorf("Expected WriteFailed() to be true")
|
|
}
|
|
|
|
// Reset
|
|
state.Editor.SetWriteFailed(filename, false)
|
|
if state.Editor.WriteFailed() {
|
|
t.Errorf("Expected WriteFailed() to be false")
|
|
}
|
|
}
|
|
|
|
// TestAutoSave_RetryFails verifies that failed writes trigger retry mechanism.
|
|
// TestAutoSave_RetryFails verifies that failed writes trigger retry mechanism.
|
|
func TestAutoSave_RetryFails(t *testing.T) {
|
|
// Setup: New Logic, set mock FS to fail
|
|
l := NewLogic(nil, "/", func(string) {})
|
|
|
|
// Start a goroutine to drain frameChan to prevent deadlocks
|
|
go func() {
|
|
for range l.frameChan {
|
|
}
|
|
}()
|
|
|
|
filename := "test.txt"
|
|
l.state.Editor.Filename = filename
|
|
l.state.Editor.Buffer = "hello"
|
|
|
|
// Mock FS to fail on Write
|
|
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)
|
|
l.workerPool.Dispatch(task)
|
|
|
|
// 2. Manually process the result to trigger the failure state
|
|
res := <-l.workerPool.ResultChan()
|
|
l.handleWorkerResult(res)
|
|
|
|
// 3. Assert failure
|
|
if !l.state.Editor.WriteFailed() {
|
|
t.Errorf("Expected WriteFailed() to be true")
|
|
}
|
|
|
|
// 4. Assert retry scheduled
|
|
retryTriggered := false
|
|
select {
|
|
case filename := <-l.retryChan:
|
|
log.Printf("Test: Received retry for %s", filename)
|
|
retryTriggered = true
|
|
case <-time.After(5 * time.Second):
|
|
t.Log("Test: Timed out waiting for retry trigger in retryChan")
|
|
}
|
|
|
|
if !retryTriggered {
|
|
t.Errorf("Retry was not triggered")
|
|
}
|
|
}
|