Files
pipelines/internal/buildinfo/identity_test.go
T
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

109 lines
3.4 KiB
Go

package buildinfo
import (
"fmt"
"strings"
"testing"
)
func TestCompareClassifiesBuildRelationship(t *testing.T) {
client := Info{Version: "v1-client", Commit: "aaaa"}
server := Info{Version: "v2-server", Commit: "bbbb"}
cases := []struct {
name string
clientProtocol int
serverProtocol int
server *Info
severity Severity
}{
{"identical", 1, 1, &client, InSync},
{"different build same protocol", 1, 1, &server, Warn},
{"server newer protocol", 1, 2, &server, Refuse},
{"nil server", 1, 0, nil, Warn},
{"legacy server protocol", 1, 0, &server, Warn},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := Compare(client, tc.clientProtocol, tc.serverProtocol, tc.server)
if got.Severity != tc.severity {
t.Fatalf("severity = %v, want %v (%q)", got.Severity, tc.severity, got.Message)
}
if got.Severity == InSync {
if got.Message != "" {
t.Fatalf("in-sync relationship must carry no message: %q", got.Message)
}
return
}
if strings.TrimSpace(got.Message) == "" {
t.Fatal("warning/refusal must name the mismatch")
}
})
}
}
func TestCompareWarningNamesBothBuilds(t *testing.T) {
client := Info{Version: "v1-client", Commit: "aaaa"}
server := Info{Version: "v2-server", Commit: "bbbb"}
relation := Compare(client, 1, 1, &server)
if relation.Severity != Warn {
t.Fatalf("severity = %v", relation.Severity)
}
for _, needle := range []string{client.Version, server.Version} {
if !strings.Contains(relation.Message, needle) {
t.Fatalf("warning %q does not name %q", relation.Message, needle)
}
}
}
func TestCompareRefusalNamesBothProtocolsAndBuilds(t *testing.T) {
client := Info{Version: "v1-client", Commit: "aaaa"}
server := Info{Version: "v2-server", Commit: "bbbb"}
relation := Compare(client, 1, 3, &server)
if relation.Severity != Refuse {
t.Fatalf("severity = %v, want Refuse", relation.Severity)
}
for _, needle := range []string{client.Version, server.Version, "3", "1"} {
if !strings.Contains(relation.Message, needle) {
t.Fatalf("refusal %q does not name %q", relation.Message, needle)
}
}
}
func TestCompareDistinguishesDirtyPointer(t *testing.T) {
base := Info{Version: "v1", Commit: "aaaa"}
clean := Info{Version: "v1", Commit: "aaaa", Dirty: boolPtr(false)}
dirty := Info{Version: "v1", Commit: "aaaa", Dirty: boolPtr(true)}
if got := Compare(base, 1, 1, &base); got.Severity != InSync {
t.Fatalf("identical missing-dirty identities must be in sync: %+v", got)
}
if got := Compare(clean, 1, 1, &dirty); got.Severity != Warn {
t.Fatalf("differing dirty state must warn: %+v", got)
}
if got := Compare(base, 1, 1, &clean); got.Severity != Warn {
t.Fatalf("nil dirty is distinct from an explicit value: %+v", got)
}
if got := Compare(clean, 1, 1, &clean); got.Severity != InSync {
t.Fatalf("identical explicit dirty identities must be in sync: %+v", got)
}
}
func TestDescribeNamesMissingBuildAsUnavailable(t *testing.T) {
if got := Describe(nil); got != "unavailable" {
t.Fatalf("Describe(nil) = %q", got)
}
info := Info{Version: "v1", Commit: "abc"}
if got := Describe(&info); !strings.Contains(got, "v1") || !strings.Contains(got, "abc") {
t.Fatalf("Describe = %q", got)
}
}
func TestSeverityZeroIsInSync(t *testing.T) {
var zero Severity
if zero != InSync {
t.Fatalf("zero severity = %v, want InSync", zero)
}
_ = fmt.Sprint(zero)
}