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

140 lines
4.0 KiB
Go

package server
import (
"bytes"
"context"
"log"
"net/http/httptest"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/coder/websocket"
"github.com/coder/websocket/wsjson"
"git.a-wan.net/alan/pipelines/internal/buildinfo"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
type logBuffer struct {
mu sync.Mutex
buffer bytes.Buffer
}
func (b *logBuffer) Write(p []byte) (int, error) {
b.mu.Lock()
defer b.mu.Unlock()
return b.buffer.Write(p)
}
func (b *logBuffer) String() string {
b.mu.Lock()
defer b.mu.Unlock()
return b.buffer.String()
}
func TestPersistentConnection(t *testing.T) {
var logs logBuffer
previousOutput := log.Writer()
log.SetOutput(&logs)
defer log.SetOutput(previousOutput)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
srv := httptest.NewServer(Handler(ctx))
defer srv.Close()
conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect", nil)
if err != nil {
t.Fatal(err)
}
defer conn.CloseNow()
for _, request := range []protocol.Message{
{Type: protocol.Ping, ID: "1"},
{Type: "unknown", ID: "2"},
{Type: protocol.Ping, ID: "3"},
} {
before := time.Now().UTC().Truncate(time.Second)
if err := wsjson.Write(ctx, conn, request); err != nil {
t.Fatal(err)
}
var response protocol.Message
if err := wsjson.Read(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if response.ID != request.ID {
t.Fatalf("ID = %q, want %q", response.ID, request.ID)
}
if request.Type != protocol.Ping {
if response.Type != protocol.Error || response.Error == "" {
t.Fatalf("expected error response, got %+v", response)
}
continue
}
if response.Type != protocol.Pong {
t.Fatalf("expected pong, got %+v", response)
}
wantBuild := buildinfo.Current()
if response.Build == nil || response.Build.Version != wantBuild.Version || response.Build.Commit != wantBuild.Commit {
t.Fatalf("pong missing server build identity: %+v", response.Build)
}
if response.Protocol != protocol.ProtocolVersion {
t.Fatalf("pong protocol = %d, want %d", response.Protocol, protocol.ProtocolVersion)
}
stamp, err := time.Parse(time.RFC3339, response.Time)
if err != nil {
t.Fatal(err)
}
if stamp.Before(before) || stamp.After(time.Now().UTC()) || stamp.Nanosecond() != 0 || !strings.HasSuffix(response.Time, "Z") {
t.Fatalf("invalid current UTC time to seconds: %q", response.Time)
}
}
for _, expected := range []string{
`command received: client=`,
`type="ping" id="1"`,
`response sent: client=`,
`type="pong" id="1" time="`,
`type="error" id="2"`,
} {
if !strings.Contains(logs.String(), expected) {
t.Errorf("missing log %q in %s", expected, logs.String())
}
}
}
// TestWelcomeEnvelopeCarriesServerIdentity proves a client that never pings
// still learns the server build and protocol from the unsolicited welcome.
func TestWelcomeEnvelopeCarriesServerIdentity(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "identity.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
handler := Handler(ctx, store)
srv := httptest.NewServer(handler)
defer srv.Close()
defer func() { cancel(); WaitRuns(handler) }()
conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect", nil)
if err != nil {
t.Fatal(err)
}
defer conn.CloseNow()
var welcome protocol.Message
if err := wsjson.Read(ctx, conn, &welcome); err != nil {
t.Fatal(err)
}
if welcome.Type != protocol.PipelineList {
t.Fatalf("first frame = %q, want welcome pipelines list", welcome.Type)
}
wantBuild := buildinfo.Current()
if welcome.Build == nil || welcome.Build.Version != wantBuild.Version || welcome.Build.Commit != wantBuild.Commit {
t.Fatalf("welcome missing server build identity: %+v", welcome.Build)
}
if welcome.Protocol != protocol.ProtocolVersion {
t.Fatalf("welcome protocol = %d, want %d", welcome.Protocol, protocol.ProtocolVersion)
}
}