92 lines
1.7 KiB
Go
92 lines
1.7 KiB
Go
package editor
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"pad/internal/io/pool/mock"
|
|
)
|
|
|
|
func TestAutoSaveE2E(t *testing.T) {
|
|
// 1. Setup
|
|
mockFS := mock.NewFileSystem()
|
|
filename := "/test.txt"
|
|
initialContent := "Hello"
|
|
mockFS.AddFile(filename, []byte(initialContent), time.Now())
|
|
|
|
l := NewLogic(mockFS)
|
|
go l.Run()
|
|
defer l.Done()
|
|
|
|
// Drain frameChan to prevent deadlocks
|
|
go func() {
|
|
for range l.FrameChan() {
|
|
}
|
|
}()
|
|
|
|
l.state.Editor.Filename = filename
|
|
TheState = l.state
|
|
|
|
// 2. Open File
|
|
OpenFile(filename)
|
|
l.state.Editor.Buffer = initialContent
|
|
l.state.Editor.CursorPosition = len(initialContent)
|
|
|
|
// 3. Edit
|
|
HandleInsert(" World")
|
|
|
|
// 4. Trigger auto-save
|
|
l.markDirty()
|
|
|
|
// 5. Wait for the write to complete
|
|
success := false
|
|
for i := 0; i < 20; i++ {
|
|
content, _ := mockFS.ReadFile(filename)
|
|
if string(content) == "Hello World" {
|
|
success = true
|
|
break
|
|
}
|
|
time.Sleep(200 * time.Millisecond)
|
|
}
|
|
|
|
// 6. Assert
|
|
if !success {
|
|
t.Errorf("Timed out waiting for file to save")
|
|
}
|
|
}
|
|
|
|
func TestFlushOnExitE2E(t *testing.T) {
|
|
// 1. Setup
|
|
mockFS := mock.NewFileSystem()
|
|
filename := "/test.txt"
|
|
initialContent := "Hello"
|
|
mockFS.AddFile(filename, []byte(initialContent), time.Now())
|
|
|
|
l := NewLogic(mockFS)
|
|
go l.Run()
|
|
defer l.Done()
|
|
|
|
// Drain frameChan to prevent deadlocks
|
|
go func() {
|
|
for range l.FrameChan() {
|
|
}
|
|
}()
|
|
|
|
l.state.Editor.Filename = filename
|
|
l.state.Editor.Buffer = initialContent
|
|
TheState = l.state
|
|
|
|
// 2. Edit
|
|
l.state.Editor.CursorPosition = len(initialContent)
|
|
HandleInsert(" World")
|
|
|
|
// 3. Trigger Flush
|
|
l.FlushAll()
|
|
|
|
// 4. Assert
|
|
content, _ := mockFS.ReadFile(filename)
|
|
if string(content) != "Hello World" {
|
|
t.Errorf("Expected 'Hello World', got %q", string(content))
|
|
}
|
|
}
|