Files
alan 8c1eea53a9 Report a stale client and refuse an unreadable newer server
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.
2026-10-16 18:37:01 +01:00

105 lines
4.0 KiB
Go

package cli
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strings"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/buildinfo"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/wstest"
)
func TestCLIWarnsOnceOnDifferingBuildAndKeepsJSONParseable(t *testing.T) {
server := buildinfo.Info{Version: "v0-stale-server", Commit: "deadbeef"}
endpoint := wstest.Server(t, func(protocol.Message) protocol.Message {
return protocol.Message{Type: protocol.Pong, Build: &server, Protocol: protocol.ProtocolVersion}
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var out, errOut bytes.Buffer
app := App{Out: &out, Err: &errOut, In: strings.NewReader("")}
if err := app.Run(ctx, []string{"--url", endpoint, "ping", "--json"}); err != nil {
t.Fatal(err)
}
// stdout must stay valid JSON.
var decoded protocol.Message
if err := json.Unmarshal(out.Bytes(), &decoded); err != nil {
t.Fatalf("stdout is not parseable JSON: %v\n%s", err, out.String())
}
if decoded.Build == nil || decoded.Build.Version != server.Version {
t.Fatalf("stdout lost the server build: %+v", decoded.Build)
}
warnings := 0
for _, line := range strings.Split(strings.TrimSpace(errOut.String()), "\n") {
if strings.HasPrefix(line, "warning:") {
warnings++
client := buildinfo.Current()
for _, needle := range []string{client.Version, server.Version} {
if !strings.Contains(line, needle) {
t.Fatalf("warning %q does not name %q", line, needle)
}
}
}
}
if warnings != 1 {
t.Fatalf("differing build produced %d stderr warnings, want exactly one:\n%s", warnings, errOut.String())
}
t.Logf("stdout:\n%s", out.String())
t.Logf("stderr:\n%s", strings.TrimSpace(errOut.String()))
// Human ping prints the relationship too, without hiding the server build.
var humanOut, humanErr bytes.Buffer
human := App{Out: &humanOut, Err: &humanErr, In: strings.NewReader("")}
if err := human.Run(ctx, []string{"--url", endpoint, "ping"}); err != nil {
t.Fatal(err)
}
if !strings.Contains(humanOut.String(), "Note:") || !strings.Contains(humanOut.String(), server.Version) {
t.Fatalf("human ping omitted the client/server relationship:\n%s", humanOut.String())
}
}
func TestCLINoticesUnmodeledFieldInsteadOfDroppingIt(t *testing.T) {
payload := map[string]any{"type": protocol.Pong, "future_field": true}
payload["build"] = buildinfo.Current()
payload["protocol"] = protocol.ProtocolVersion
endpoint := wstest.Raw(t, func(protocol.Message) map[string]any { return payload })
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var out, errOut bytes.Buffer
app := App{Out: &out, Err: &errOut, In: strings.NewReader("")}
if err := app.Run(ctx, []string{"--url", endpoint, "ping"}); err != nil {
t.Fatal(err)
}
if !strings.Contains(errOut.String(), "future_field") || !strings.Contains(errOut.String(), "unrecognized") {
t.Fatalf("unmodeled field was silent: stderr=%q stdout=%q", errOut.String(), out.String())
}
t.Logf("stderr:\n%s", strings.TrimSpace(errOut.String()))
}
func TestCLIRefusesNewerServerProtocol(t *testing.T) {
server := buildinfo.Info{Version: "v9-future-server", Commit: "deadbeef"}
endpoint := wstest.Server(t, func(protocol.Message) protocol.Message {
return protocol.Message{Type: protocol.Pong, Build: &server, Protocol: protocol.ProtocolVersion + 7}
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var out, errOut bytes.Buffer
app := App{Out: &out, Err: &errOut, In: strings.NewReader("")}
err := app.Run(ctx, []string{"--url", endpoint, "ping", "--json"})
if err == nil {
t.Fatal("a newer server protocol did not fail the command")
}
client := buildinfo.Current()
for _, needle := range []string{client.Version, server.Version, fmt.Sprint(protocol.ProtocolVersion + 7), fmt.Sprint(protocol.ProtocolVersion)} {
if !strings.Contains(err.Error(), needle) {
t.Fatalf("refusal %q does not name %q", err.Error(), needle)
}
}
t.Logf("exit error: %v", err)
}