8c1eea53a9
Exchange a build/protocol identity so an older CLI can no longer talk to a newer native server silently: - protocol carries ProtocolVersion and every server frame (including the unsolicited welcome) is stamped with the server build and protocol, so a client that never pings still learns the server identity. - buildinfo.Compare classifies the relationship as InSync/Warn/Refuse and names both builds; the client Watch reports one warning per connection and returns a hard error when the server speaks a newer, uninterpretable protocol so the caller never acts on the frame. - client.ReceiveNotice decodes the raw frame so an unmodeled top-level field produces a one-time "unrecognized field(s)" notice instead of being dropped silently (the case that hid the artifacts field). - CLI routes warnings to stderr, keeps machine JSON stdout clean and fails non-zero on a refused mismatch; ping/connection print the client/server relationship. The TUI surfaces the same notices through the shared log. - scripts/setup-native.py now warns when an older `pipelines` earlier on PATH shadows prefix/bin/pipelines, naming both paths and versions, without installing or modifying anything else. - Docs, the packaged debugging skill reference and README no longer imply the installed CLI always matches the server. Tests cover the comparison table, unknown-field detection and caching, the server identity stamping on pong and welcome, the CLI/TUI reporting paths and the installer PATH shadowing warning.
122 lines
4.9 KiB
Go
122 lines
4.9 KiB
Go
package tui
|
|
|
|
import (
|
|
"context"
|
|
"git.a-wan.net/alan/pipelines/internal/buildinfo"
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"git.a-wan.net/alan/pipelines/internal/server"
|
|
"git.a-wan.net/alan/pipelines/internal/wstest"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestEditorPublicationSurfacesBuildMismatchNotice proves a short-lived TUI
|
|
// command client routes its build/protocol notice through the shared notice
|
|
// path instead of dropping it (the artifacts-field regression).
|
|
func TestEditorPublicationSurfacesBuildMismatchNotice(t *testing.T) {
|
|
stale := buildinfo.Info{Version: "v0-stale-server", Commit: "deadbeef"}
|
|
endpoint := wstest.Server(t, func(request protocol.Message) protocol.Message {
|
|
switch request.Type {
|
|
case protocol.List:
|
|
return protocol.Message{Type: protocol.List, Build: &stale, Protocol: protocol.ProtocolVersion,
|
|
Pipelines: []protocol.PipelineSummary{{ID: 7, Name: "test", Version: 1}}}
|
|
case protocol.GetPipeline:
|
|
return protocol.Message{Type: protocol.GetPipeline,
|
|
Detail: &protocol.PipelineDetail{PipelineSummary: protocol.PipelineSummary{ID: 7, Name: "test", Version: 1}, Source: "published"}}
|
|
}
|
|
return protocol.Message{Type: protocol.Pong}
|
|
})
|
|
edit := editedMsg{name: "test", source: "published", baseVersion: 0}
|
|
result := publicationCheck(context.Background(), endpoint, edit, 0)
|
|
if len(result.notices) != 1 {
|
|
t.Fatalf("short-lived client dropped the build mismatch notice: %+v", result.notices)
|
|
}
|
|
m := New(context.Background(), endpoint)
|
|
updated, _ := m.finishEditorPublication(result)
|
|
m = updated.(Model)
|
|
if !strings.Contains(strings.Join(m.lines, "\n"), "warning:") {
|
|
t.Fatalf("notice not routed to the shared TUI notice path: %q", m.lines)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|