app frame + storage
This commit is contained in:
+65
@@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
func TestNewModelWithoutTodos(t *testing.T) {
|
||||
project := t.TempDir()
|
||||
app, err := newModel(project)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if app.todosDir != filepath.Join(project, ".pi", "todos") || app.count != 0 {
|
||||
t.Fatalf("unexpected model: %+v", app)
|
||||
}
|
||||
if !strings.Contains(app.View(), "No todos yet") {
|
||||
t.Fatalf("missing empty state: %s", app.View())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewModelWithTodos(t *testing.T) {
|
||||
project := t.TempDir()
|
||||
dir := filepath.Join(project, ".pi", "todos")
|
||||
if err := os.MkdirAll(dir, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, name := range []string{"a.md", "b.md", "settings.json"} {
|
||||
if err := os.WriteFile(filepath.Join(dir, name), nil, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
app, err := newModel(project)
|
||||
if err != nil || app.count != 2 {
|
||||
t.Fatalf("unexpected result: %+v, %v", app, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidProjectAndTodoPath(t *testing.T) {
|
||||
project := t.TempDir()
|
||||
if _, err := newModel(filepath.Join(project, "missing")); err == nil || !strings.Contains(err.Error(), "project directory") {
|
||||
t.Fatalf("expected missing project error, got %v", err)
|
||||
}
|
||||
if err := os.Mkdir(filepath.Join(project, ".pi"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(project, ".pi", "todos"), nil, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := newModel(project); err == nil || !strings.Contains(err.Error(), "read todo directory") {
|
||||
t.Fatalf("expected todo directory error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQuitKey(t *testing.T) {
|
||||
app := model{}
|
||||
_, cmd := app.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'q'}})
|
||||
if cmd == nil || cmd() != tea.Quit() {
|
||||
t.Fatal("q should quit")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user