Initial pipelines server, CLI, TUI, and Go installation
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"github.com/coder/websocket/wsjson"
|
||||
"pipelines/internal/pipeline"
|
||||
"pipelines/internal/protocol"
|
||||
"pipelines/internal/stage"
|
||||
)
|
||||
|
||||
func TestRunWebSocketRemainsResponsiveAndAuditable(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "run.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
if _, err := store.Submit(ctx, "ws", pipeline.Template([]string{"one"}, true)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, _ := store.List(ctx)
|
||||
handler := Handler(ctx, store).(*runHandler)
|
||||
defer func() { cancel(); WaitRuns(handler) }()
|
||||
entered := make(chan struct{})
|
||||
handler.runner.execute = func(ctx context.Context, _ stage.Definition, _ stage.Values) (stage.Result, error) {
|
||||
close(entered)
|
||||
<-ctx.Done()
|
||||
return stage.Result{}, ctx.Err()
|
||||
}
|
||||
srv := httptest.NewServer(handler)
|
||||
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()
|
||||
var snapshot protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &snapshot); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request := func(message protocol.Message) protocol.Message {
|
||||
t.Helper()
|
||||
if err := wsjson.Write(ctx, conn, message); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.ID != message.ID {
|
||||
t.Fatal("response ID mismatch")
|
||||
}
|
||||
return response
|
||||
}
|
||||
invalid := request(protocol.Message{Type: protocol.RunPipeline, ID: "invalid", PipelineID: items[0].ID, RequestKey: "invalid"})
|
||||
if invalid.Type != protocol.Error {
|
||||
t.Fatal("accepted unpinned run")
|
||||
}
|
||||
missingPrompt := request(protocol.Message{Type: protocol.RunPipeline, ID: "missing-prompt", PipelineID: items[0].ID, Version: 1, RequestKey: "missing-prompt"})
|
||||
if missingPrompt.Type != protocol.Error || !strings.Contains(missingPrompt.Error, "user prompt") {
|
||||
t.Fatal("run accepted missing user prompt")
|
||||
}
|
||||
started := request(protocol.Message{Type: protocol.RunPipeline, ID: "start", PipelineID: items[0].ID, Version: 1, RequestKey: "ws-once", Input: map[string]any{"prompt": "test"}})
|
||||
if started.Type != protocol.RunStarted || started.RunDetail == nil {
|
||||
t.Fatal("run not started", started)
|
||||
}
|
||||
<-entered
|
||||
if response := request(protocol.Message{Type: protocol.Ping, ID: "ping"}); response.Type != protocol.Pong {
|
||||
t.Fatal("active run blocked ping")
|
||||
}
|
||||
if response := request(protocol.Message{Type: protocol.ListRuns, ID: "list"}); response.Type != protocol.Runs || len(response.Runs) != 1 {
|
||||
t.Fatal("run list missing")
|
||||
}
|
||||
if response := request(protocol.Message{Type: protocol.CancelRun, ID: "cancel", RunID: started.RunDetail.ID}); response.Type != protocol.Run {
|
||||
t.Fatal("cancel rejected")
|
||||
}
|
||||
handler.runner.wg.Wait()
|
||||
result := request(protocol.Message{Type: protocol.GetRun, ID: "inspect", RunID: started.RunDetail.ID})
|
||||
if result.RunDetail == nil || result.RunDetail.Status != "canceled" || len(result.RunDetail.Steps) != 1 || result.RunDetail.Steps[0].Input["prompt"] != "test" {
|
||||
t.Fatal("missing canceled audit", result)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user