Files
archie/internal/webui/api_tasks_sandbox_test.go
T
sam cc0fba87b4
Quality / check (push) Canceled after 0s
feat(archied): read-only run-scoped sandbox view over the harness contract (#1662)
* feat(archied): read-only run-scoped sandbox view over the harness contract

Serve RunTarget: the daemon resolves a running task and attempt to its Kit
container and follows its output, refusing a finished or unknown task with a
stated reason. The dashboard adds a Sandbox tab behind the task log's read
authority. Takeover stays out of scope until the sod1 authority is wired.

* fix(archied): scope and close sandbox views correctly

Forward harness activity into container output, bind containers and dashboard views to their attempt, enforce the asserted org, and report unavailable dependencies and interrupted streams accurately.

Refs: archie-core-qi91, archie-core-252tj
2026-10-14 02:57:32 +11:00

87 lines
2.4 KiB
Go

package webui
import (
"context"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"golang.org/x/net/websocket"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
storecontract "github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/workflow"
)
type sandboxTasks struct{ storecontract.TaskStore }
func (sandboxTasks) TaskByID(context.Context, int64) (*workflow.Task, error) {
return &workflow.Task{ID: 7, Attempt: 2, Status: workflow.StatusRunning}, nil
}
type sandboxStream struct {
*io.PipeReader
closed chan struct{}
once sync.Once
}
func (s *sandboxStream) Close() error {
s.once.Do(func() { close(s.closed) })
return s.PipeReader.Close()
}
func (s *sandboxStream) OpenRun(context.Context, string, int64, int64) (io.ReadCloser, error) {
return s, nil
}
func TestSandboxStreamLifetime(t *testing.T) {
for _, name := range []string{"browser leaves quiet view", "output fails"} {
t.Run(name, func(t *testing.T) {
pr, pw := io.Pipe()
view := &sandboxStream{PipeReader: pr, closed: make(chan struct{})}
s := &Server{Store: sandboxTasks{}, SandboxView: view}
mux := http.NewServeMux()
mux.HandleFunc("GET /api/tasks/{id}/sandbox", s.handleTaskSandbox)
server := httptest.NewServer(mux)
defer server.Close()
defer view.Close()
defer pw.Close()
ws, err := websocket.Dial("ws"+strings.TrimPrefix(server.URL, "http")+"/api/tasks/7/sandbox?attempt=2", "", server.URL)
if err != nil {
t.Fatal(err)
}
defer ws.Close()
if err := ws.SetDeadline(time.Now().Add(2 * time.Second)); err != nil {
t.Fatal(err)
}
go func() {
_, _ = io.WriteString(pw, "ready")
if name == "output fails" {
_ = pw.CloseWithError(status.Error(codes.Unavailable, "lost sandbox output"))
}
}()
var message string
if err := websocket.Message.Receive(ws, &message); err != nil || message != "ready" {
t.Fatalf("initial output = %q, %v", message, err)
}
if name == "browser leaves quiet view" {
_ = ws.Close()
} else {
if err := websocket.Message.Receive(ws, &message); err != nil || !strings.Contains(message, "lost sandbox output") {
t.Fatalf("failure reason = %q, %v", message, err)
}
}
select {
case <-view.closed:
case <-time.After(time.Second):
t.Fatal("sandbox stream remained open")
}
})
}
}