90 lines
3.0 KiB
Go
90 lines
3.0 KiB
Go
package tui
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestUnchangedEditDoesNotOfferSubmission(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "definition.md")
|
|
original := "# stage first\n"
|
|
if err := os.WriteFile(path, []byte(original), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result := readEditedDraft("test", path, original, true, nil)
|
|
if result.err != nil || !result.unchanged {
|
|
t.Fatal("unchanged edit not detected", result)
|
|
}
|
|
m := New(context.Background(), "")
|
|
updated, cmd := m.Update(result)
|
|
m = updated.(Model)
|
|
if cmd != nil || m.confirmOpen || m.keys.confirm || m.pendingDraft != nil || m.submitID != "" {
|
|
t.Fatal("unchanged edit offered or sent submission")
|
|
}
|
|
// Returning to an existing changed draft must not discard that user work.
|
|
pending := &editedMsg{name: "test", source: "previous unsent changes", confirm: true}
|
|
m.pendingDraft = pending
|
|
updated, cmd = m.Update(result)
|
|
m = updated.(Model)
|
|
if cmd != nil || m.confirmOpen || m.pendingDraft != pending {
|
|
t.Fatal("no-op edit discarded or reopened existing draft")
|
|
}
|
|
}
|
|
|
|
func TestNeovimNoOpExit(t *testing.T) {
|
|
editor, err := exec.LookPath("nvim")
|
|
if err != nil {
|
|
t.Skip("Neovim not installed")
|
|
}
|
|
path := filepath.Join(t.TempDir(), "definition.md")
|
|
original := "# stage first\n\n```output\nResult.\n```\n"
|
|
if err := os.WriteFile(path, []byte(original), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
output, err := exec.CommandContext(ctx, editor, "--headless", "-u", "NONE", "-i", "NONE", path, "+q").CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("Neovim exit failed: %v %s", err, output)
|
|
}
|
|
result := readEditedDraft("test", path, original, true, err)
|
|
m := New(ctx, "")
|
|
updated, cmd := m.Update(result)
|
|
m = updated.(Model)
|
|
if !result.unchanged || m.confirmOpen || m.pendingDraft != nil || cmd != nil {
|
|
t.Fatal("actual Neovim no-op exit offered submission")
|
|
}
|
|
}
|
|
|
|
func TestChangedEditAndNewTemplateStillSubmit(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "definition.md")
|
|
original := "# stage first\n"
|
|
changed := original + "\n"
|
|
if err := os.WriteFile(path, []byte(changed), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result := readEditedDraft("test", path, original, true, nil)
|
|
if result.err != nil || result.unchanged {
|
|
t.Fatal("whitespace change must count as source change")
|
|
}
|
|
m := New(context.Background(), "")
|
|
updated, _ := m.Update(result)
|
|
m = updated.(Model)
|
|
if !m.confirmOpen || m.pendingDraft == nil || m.pendingDraft.source != changed {
|
|
t.Fatal("changed edit did not offer submission")
|
|
}
|
|
creation := readEditedDraft("new", path, changed, false, nil)
|
|
if creation.err != nil || creation.unchanged || creation.confirm {
|
|
t.Fatal("unchanged new template must retain creation behavior")
|
|
}
|
|
canceled := readEditedDraft("test", path, original, true, errors.New("editor aborted"))
|
|
if canceled.err == nil || canceled.unchanged {
|
|
t.Fatal("aborted editor treated as unchanged success")
|
|
}
|
|
}
|