Files
pipelines/internal/tui/editor_publication_test.go
T

90 lines
3.4 KiB
Go

package tui
import (
"context"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/server"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"time"
)
func TestEditorReturnFindsPublishedVersionAndReloadsLatest(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "publication.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
handler := server.Handler(ctx, store)
srv := httptest.NewServer(handler)
defer func() { cancel(); srv.Close(); server.WaitRuns(handler) }()
source := pipeline.Template([]string{"planner"}, true)
if _, err := store.Submit(ctx, "test", source); err != nil {
t.Fatal(err)
}
items, _ := store.List(ctx)
original, _ := store.Get(ctx, items[0].ID, 1)
edited := strings.Replace(source, "Describe the task.", "Published in OpenCode.", 1)
if edited == source {
t.Fatal("fixture did not change source")
}
if _, err := store.Submit(ctx, "test", edited); err != nil {
t.Fatal(err)
}
latest := strings.Replace(edited, "Published in OpenCode.", "Another newer update.", 1)
if _, err := store.Submit(ctx, "test", latest); err != nil {
t.Fatal(err)
}
m := New(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect")
connected := m.connect()().(connectedMsg)
defer connected.conn.CloseNow()
updated, _ := m.Update(connected)
m = updated.(Model)
m.detail = original
m.keys.detail = true
updated, check := m.Update(editedMsg{name: "test", path: "retained.md", source: edited, baseVersion: 1})
m = updated.(Model)
if check == nil || m.confirmOpen {
t.Fatal("must check before asking to submit")
}
updated, _ = m.Update(check())
m = updated.(Model)
if m.confirmOpen || m.pendingDraft != nil || m.detail.Version != 3 || m.detail.Source != latest || m.pipelines[0].Version != 3 || !strings.Contains(m.submitNotice, "Already published") {
t.Fatal("did not find earlier publication and reload latest", m.submitNotice, m.detail)
}
items, _ = store.List(ctx)
if items[0].Version != 3 {
t.Fatal("publication check created a duplicate version")
}
// A local unsent draft must still need approval, even if the server changed.
unsent := strings.Replace(edited, "Published in OpenCode.", "Local unsent edits.", 1)
updated, check = m.Update(editedMsg{name: "test", path: "retained.md", source: unsent, baseVersion: 1})
m = updated.(Model)
updated, _ = m.Update(check())
m = updated.(Model)
if !m.confirmOpen || m.pendingDraft.source != unsent || m.pendingDraft.baseVersion != 1 {
t.Fatal("unsent changes or original conflict guard lost")
}
}
func TestEditorPublicationFailureAndStaleResultDoNotDiscardDraft(t *testing.T) {
m := New(context.Background(), "")
m.editorCheckGeneration = 2
edit := editedMsg{name: "test", source: "unsent", baseVersion: 1}
updated, _ := m.finishEditorPublication(editorPublicationMsg{generation: 1, edit: edit, published: true})
m = updated.(Model)
if m.confirmOpen {
t.Fatal("stale result changed editor state")
}
result := checkEditorPublication(context.Background(), "invalid-url", edit, 2)().(editorPublicationMsg)
updated, _ = m.finishEditorPublication(result)
m = updated.(Model)
if !m.confirmOpen || m.pendingDraft.source != "unsent" || !strings.Contains(m.submitNotice, "Could not check") {
t.Fatal("failed check lost local edits", m.submitNotice)
}
}