Allow portable drafts to publish to the connected server
This commit is contained in:
@@ -56,12 +56,7 @@ func (m Model) selectedDraft() (draft.Draft, string, error) {
|
||||
if err != nil {
|
||||
return d, "", err
|
||||
}
|
||||
if d.Server != "" && d.Server != m.url {
|
||||
return d, "", fmt.Errorf("draft belongs to a different server: %s", d.Server)
|
||||
}
|
||||
if d.Server == "" {
|
||||
d.Server = m.url
|
||||
}
|
||||
d = d.ForServer(m.url)
|
||||
source, err := draft.Read(d.Path)
|
||||
return d, source, err
|
||||
}
|
||||
|
||||
@@ -169,7 +169,7 @@ func TestDraftNamePromptCancelAndInvalidName(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDraftBrowserRejectsWrongServerAndKeepsUnnamedDraft(t *testing.T) {
|
||||
func TestDraftBrowserAcceptsOtherServerAndKeepsUnnamedDraft(t *testing.T) {
|
||||
t.Setenv("PIPELINES_DRAFT_DIR", t.TempDir())
|
||||
d, err := draft.Create("wrong", "source", "ws://another-server", true, 0)
|
||||
if err != nil {
|
||||
@@ -180,8 +180,8 @@ func TestDraftBrowserRejectsWrongServerAndKeepsUnnamedDraft(t *testing.T) {
|
||||
m = updated.(Model)
|
||||
updated, cmd := m.updateDrafts(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}})
|
||||
m = updated.(Model)
|
||||
if cmd != nil || m.confirmOpen || !strings.Contains(m.draftError, "different server") {
|
||||
t.Fatal("server affinity bypassed", d, m.draftError)
|
||||
if cmd != nil || !m.confirmOpen || m.draftError != "" || !m.pendingDraft.createOnly || m.pendingDraft.baseVersion != 0 {
|
||||
t.Fatal("portable draft was blocked or kept origin version scope", d, m.draftError)
|
||||
}
|
||||
m.pendingDraft = &editedMsg{path: d.Path, name: "", source: "source", createOnly: true}
|
||||
m.confirmOpen = true
|
||||
|
||||
@@ -359,6 +359,7 @@ func (m Model) confirmView(height int) string {
|
||||
lines[0] = "Create pipeline"
|
||||
lines[3] = "Creates a NEW pipeline only when you submit."
|
||||
}
|
||||
lines = append(lines, "Destination: "+m.url)
|
||||
if strings.Contains(m.pendingDraft.source, "```schedule") {
|
||||
lines = append(lines, "WARNING: submitting activates the included schedule.")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/draft"
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/server"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
func TestTUIEditDraftPublishesToConnectedServerAsNewPipeline(t *testing.T) {
|
||||
t.Setenv("PIPELINES_DRAFT_DIR", t.TempDir())
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "destination.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
srv := httptest.NewServer(server.Handler(ctx, store))
|
||||
defer srv.Close()
|
||||
url := "ws" + strings.TrimPrefix(srv.URL, "http") + "/connect"
|
||||
source := pipeline.Template([]string{"worker"}, true)
|
||||
d, err := draft.Create("portable", source, "ws://127.0.0.1:8080/connect", false, 7)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
metadata, err := os.ReadFile(d.Path + ".json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := New(ctx, url)
|
||||
connected := m.connect()().(connectedMsg)
|
||||
defer connected.conn.CloseNow()
|
||||
updated, read := m.Update(connected)
|
||||
m = updated.(Model)
|
||||
updated, _ = m.Update(read())
|
||||
m = updated.(Model)
|
||||
updated, _ = m.openDrafts()
|
||||
m = updated.(Model)
|
||||
updated, validate := m.updateDrafts(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'v'}})
|
||||
m = updated.(Model)
|
||||
updated, _ = m.Update(validate())
|
||||
m = updated.(Model)
|
||||
if !strings.Contains(m.draftError, "Validation passed") {
|
||||
t.Fatal("cross-server validation blocked", m.draftError)
|
||||
}
|
||||
updated, send := m.updateDrafts(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}})
|
||||
m = updated.(Model)
|
||||
items, _ := store.List(ctx)
|
||||
if send != nil || !m.confirmOpen || !m.pendingDraft.createOnly || m.pendingDraft.baseVersion != 0 || len(items) != 0 || !strings.Contains(m.confirmView(20), url) {
|
||||
t.Fatal("portable draft bypassed review or retained origin scope", m.pendingDraft, m.draftError)
|
||||
}
|
||||
updated, send = m.updateConfirm(tea.KeyMsg{Type: tea.KeyEnter})
|
||||
m = updated.(Model)
|
||||
if send == nil {
|
||||
t.Fatal("publication blocked")
|
||||
}
|
||||
if _, ok := send().(sentMsg); !ok {
|
||||
t.Fatal("publication send failed")
|
||||
}
|
||||
updated, _ = m.Update(m.read()())
|
||||
m = updated.(Model)
|
||||
items, _ = store.List(ctx)
|
||||
if len(items) != 1 || items[0].Version != 1 || items[0].Name != "portable" || m.confirmOpen {
|
||||
t.Fatal("destination publication failed", items, m.submitNotice)
|
||||
}
|
||||
updated, _ = m.openDrafts()
|
||||
m = updated.(Model)
|
||||
updated, _ = m.updateDrafts(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}})
|
||||
m = updated.(Model)
|
||||
updated, send = m.updateConfirm(tea.KeyMsg{Type: tea.KeyEnter})
|
||||
m = updated.(Model)
|
||||
send()
|
||||
updated, _ = m.Update(m.read()())
|
||||
m = updated.(Model)
|
||||
if !strings.Contains(m.submitNotice, "already exists") {
|
||||
t.Fatal("destination collision not guarded", m.submitNotice)
|
||||
}
|
||||
retained, err := os.ReadFile(d.Path + ".json")
|
||||
if err != nil || string(retained) != string(metadata) {
|
||||
t.Fatal("publication changed origin metadata", err)
|
||||
}
|
||||
text, err := draft.Read(d.Path)
|
||||
if err != nil || text != source {
|
||||
t.Fatal("publication changed source", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user