Files
pipelines/internal/protocol/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

53 lines
1.4 KiB
Go

package protocol
import (
"fmt"
"reflect"
"testing"
)
func TestUnknownFieldsReportsOnlyUnmodeledSortedKeys(t *testing.T) {
if got := UnknownFields([]byte(`{"type":"pong"}`)); got != nil {
t.Fatalf("fully modeled frame reported %v", got)
}
got := UnknownFields([]byte(`{"type":"pong","zzz":1,"aaa":2}`))
if !reflect.DeepEqual(got, []string{"aaa", "zzz"}) {
t.Fatalf("unknown fields = %v, want sorted [aaa zzz]", got)
}
}
func TestUnknownFieldsIgnoresMalformedAndNonObjectFrames(t *testing.T) {
for _, data := range []string{
`not json`,
`[1,2,3]`,
`"a string"`,
`42`,
`null`,
} {
if got := UnknownFields([]byte(data)); got != nil {
t.Fatalf("frame %q reported unknown fields %v", data, got)
}
}
}
func TestKnownFieldsCachesTheModeledKeySet(t *testing.T) {
first := KnownFields()
second := KnownFields()
if len(first) == 0 {
t.Fatal("known field set is empty")
}
if _, ok := first["type"]; !ok {
t.Fatal("known field set is missing the modeled type key")
}
if _, ok := first["build"]; !ok {
t.Fatal("known field set is missing the modeled build key")
}
if _, ok := first["protocol"]; !ok {
t.Fatal("known field set is missing the modeled protocol key")
}
// The sync.Once must return the same cached map on every call.
if fmt.Sprintf("%p", first) != fmt.Sprintf("%p", second) {
t.Fatal("KnownFields re-derived the set instead of using the cache")
}
}