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

129 lines
4.2 KiB
Go

package client
import (
"context"
"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 TestCallRefusesNewerServerProtocol(t *testing.T) {
server := buildinfo.Info{Version: "v9-server", Commit: "deadbeef"}
endpoint := wstest.Server(t, func(protocol.Message) protocol.Message {
return protocol.Message{Type: protocol.Pong, Build: &server, Protocol: protocol.ProtocolVersion + 5}
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
c, err := Open(ctx, endpoint)
if err != nil {
t.Fatal(err)
}
defer c.Close()
var warnings int
c.Report(func(buildinfo.Relationship) { warnings++ })
_, err = c.Call(ctx, protocol.Message{Type: protocol.Ping})
if err == nil {
t.Fatal("newer server protocol was not refused")
}
client := buildinfo.Current()
for _, needle := range []string{client.Version, server.Version, fmt.Sprint(protocol.ProtocolVersion + 5), fmt.Sprint(protocol.ProtocolVersion)} {
if !strings.Contains(err.Error(), needle) {
t.Fatalf("refusal %q does not name %q", err.Error(), needle)
}
}
if warnings != 0 {
t.Fatalf("refusal reported %d warnings instead of failing fast", warnings)
}
}
func TestCallWarnsOnceOnDifferingBuild(t *testing.T) {
server := buildinfo.Info{Version: "v0-older-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()
c, err := Open(ctx, endpoint)
if err != nil {
t.Fatal(err)
}
defer c.Close()
var messages []string
c.Report(func(relationship buildinfo.Relationship) { messages = append(messages, relationship.Message) })
for i := 0; i < 3; i++ {
if _, err := c.Call(ctx, protocol.Message{Type: protocol.Ping}); err != nil {
t.Fatal(err)
}
}
if len(messages) != 1 {
t.Fatalf("differing build reported %d notices, want exactly one: %v", len(messages), messages)
}
client := buildinfo.Current()
for _, needle := range []string{client.Version, server.Version} {
if !strings.Contains(messages[0], needle) {
t.Fatalf("warning %q does not name %q", messages[0], needle)
}
}
}
func TestCallReportsUnmodeledFieldOnce(t *testing.T) {
payload := map[string]any{"type": protocol.Pong, "future_field": true, "another": 2}
client := buildinfo.Current()
payload["build"] = client
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()
c, err := Open(ctx, endpoint)
if err != nil {
t.Fatal(err)
}
defer c.Close()
var notices []string
c.Report(func(relationship buildinfo.Relationship) { notices = append(notices, relationship.Message) })
for i := 0; i < 2; i++ {
response, err := c.Call(ctx, protocol.Message{Type: protocol.Ping})
if err != nil {
t.Fatal(err)
}
if response.Type != protocol.Pong {
t.Fatalf("unexpected response %+v", response)
}
}
if len(notices) != 1 {
t.Fatalf("unmodeled fields reported %d notices, want one: %v", len(notices), notices)
}
for _, needle := range []string{"another", "future_field", "unrecognized"} {
if !strings.Contains(notices[0], needle) {
t.Fatalf("unknown-field notice %q does not name %q", notices[0], needle)
}
}
}
func TestReceiveNoticeDoesNotFlagFullyModeledFrames(t *testing.T) {
endpoint := wstest.Server(t, func(protocol.Message) protocol.Message {
server := buildinfo.Current()
return protocol.Message{Type: protocol.Pong, Build: &server, Protocol: protocol.ProtocolVersion}
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
c, err := Open(ctx, endpoint)
if err != nil {
t.Fatal(err)
}
defer c.Close()
var notices int
c.Report(func(buildinfo.Relationship) { notices++ })
if _, err := c.Call(ctx, protocol.Message{Type: protocol.Ping}); err != nil {
t.Fatal(err)
}
if notices != 0 {
t.Fatalf("an in-sync, fully modeled frame produced %d notices", notices)
}
}