05ae530442
Install current verified harness releases inside Docker, preserve persistent accounts and settings, and provide separate interactive and pipeline model defaults with terminal onboarding and fuzzy pickers. Add Docker-aware authoring transport, Pi stage execution, bounded workflow controls, reliable final-result readback and usage settlement, and finite short-lived CLI pipe draining. Keep dated installation evidence and packaged authoring guidance consistent. Verified with a real Docker installation, both harness stages and interactive authors, the built-in plan/implement/review starter, independent host race/vet checks, installer tests, and packaged skill tests. Generated release receipts stay ignored in clean checkouts.
100 lines
2.6 KiB
Go
100 lines
2.6 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"log"
|
|
"net/http/httptest"
|
|
"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/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)
|
|
}
|
|
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())
|
|
}
|
|
}
|
|
}
|