test: preserve UI diff fixture recovery
This commit is contained in:
@@ -0,0 +1,256 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/stage"
|
||||
)
|
||||
|
||||
// TestExportUIDiffFixture writes a deliberately isolated fixture only when its
|
||||
// destination is explicitly supplied. It gives UI review a durable scratch
|
||||
// database with real runner snapshots, rather than a warning-only incidental
|
||||
// run. It refuses an existing export so it cannot replace any prior data.
|
||||
func TestExportUIDiffFixture(t *testing.T) {
|
||||
dir := os.Getenv("PIPELINES_UI_DIFF_FIXTURE_DIR")
|
||||
if dir == "" {
|
||||
t.Skip("set PIPELINES_UI_DIFF_FIXTURE_DIR to export the scratch UI fixture")
|
||||
}
|
||||
dir, err := filepath.Abs(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(dir, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
db := filepath.Join(dir, "ui-diff-fixture.db")
|
||||
workspace := filepath.Join(dir, "workspace")
|
||||
metadata := filepath.Join(dir, "ui-diff-fixture.json")
|
||||
for _, path := range []string{db, workspace, metadata} {
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
t.Fatalf("refusing to replace existing fixture path %s", path)
|
||||
} else if !os.IsNotExist(err) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := os.Mkdir(workspace, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(workspace, "notes.txt"), []byte("removed line\npreserved line\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
store, err := pipeline.Open(db)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
source := pipeline.Template([]string{"prepare", "review"}, true)
|
||||
if _, err := store.Submit(ctx, "ui-diff-fixture", source); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, err := store.List(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r, err := newRunner(ctx, store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.execute = func(_ context.Context, definition stage.Definition, _ stage.Values) (stage.Result, error) {
|
||||
switch definition.Key {
|
||||
case "prepare":
|
||||
if err := os.WriteFile(filepath.Join(workspace, "notes.txt"), []byte("preserved line\nadded line\n"), 0600); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(workspace, "created.txt"), []byte("created fixture file\n"), 0600); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
case "review":
|
||||
if err := os.WriteFile(filepath.Join(workspace, "notes.txt"), []byte("preserved line\nadded line\nreviewed line\n"), 0600); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
if err := os.Remove(filepath.Join(workspace, "created.txt")); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(workspace, "final.txt"), []byte("final fixture file\n"), 0600); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
default:
|
||||
return stage.Result{}, fmt.Errorf("unexpected fixture stage %q", definition.Key)
|
||||
}
|
||||
return stage.Result{Outcome: "success"}, nil
|
||||
}
|
||||
const title = "Readable fixture title: added and removed lines across multiple captured files"
|
||||
run, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "ui-diff-fixture", Workspace: workspace, Title: title, Input: map[string]any{"prompt": "create deterministic UI diff fixture"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.wg.Wait()
|
||||
detail, err := store.GetRun(ctx, run.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if detail.Status != "succeeded" || detail.Title != title || len(detail.Steps) != 2 {
|
||||
t.Fatalf("fixture run is incomplete: %+v", detail)
|
||||
}
|
||||
var patches []string
|
||||
paths := make(map[string]bool)
|
||||
kinds := make(map[string]bool)
|
||||
for _, step := range detail.Steps {
|
||||
if step.FileChanges == nil || len(step.FileChanges.Files) < 2 {
|
||||
t.Fatalf("fixture step lacks multiple captured files: %+v", step)
|
||||
}
|
||||
for _, file := range step.FileChanges.Files {
|
||||
patches = append(patches, file.Patch)
|
||||
paths[file.Path] = true
|
||||
kinds[file.Kind] = true
|
||||
}
|
||||
}
|
||||
for _, path := range []string{"notes.txt", "created.txt", "final.txt"} {
|
||||
if !paths[path] {
|
||||
t.Fatalf("fixture omits expected path %q: %+v", path, paths)
|
||||
}
|
||||
}
|
||||
for _, kind := range []string{"added", "modified", "deleted"} {
|
||||
if !kinds[kind] {
|
||||
t.Fatalf("fixture omits expected change kind %q: %+v", kind, kinds)
|
||||
}
|
||||
}
|
||||
if joined := strings.Join(patches, "\n"); !strings.Contains(joined, "-removed line") || !strings.Contains(joined, "+added line") || !strings.Contains(joined, "+reviewed line") {
|
||||
t.Fatalf("fixture did not retain known hunk lines: %q", joined)
|
||||
}
|
||||
if err := store.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
readback, serverCommand, getRunCommand := readUIDiffFixtureThroughBranchServer(t, dir, db, run.ID)
|
||||
if len(readback.Steps) != len(detail.Steps) {
|
||||
t.Fatalf("get_run returned %d steps, want %d", len(readback.Steps), len(detail.Steps))
|
||||
}
|
||||
readbackJSON, err := json.MarshalIndent(readback, "", " ")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(readbackJSON), `"file_changes"`) || !strings.Contains(string(readbackJSON), `"patch"`) {
|
||||
t.Fatalf("get_run response omitted file patches: %s", readbackJSON)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "get-run-response.json"), append(readbackJSON, '\n'), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
output := struct {
|
||||
FixtureID string `json:"fixture_id"`
|
||||
Database string `json:"database"`
|
||||
Workspace string `json:"workspace"`
|
||||
RunID int64 `json:"run_id"`
|
||||
Title string `json:"title"`
|
||||
Expected []string `json:"expected"`
|
||||
ServerCommand string `json:"server_command"`
|
||||
GetRunCommand string `json:"get_run_command"`
|
||||
Readback string `json:"readback"`
|
||||
}{
|
||||
FixtureID: "ui-diff-fixture",
|
||||
Database: db,
|
||||
Workspace: workspace,
|
||||
RunID: run.ID,
|
||||
Title: title,
|
||||
Expected: []string{"notes.txt", "created.txt", "final.txt", "-removed line", "+added line", "+reviewed line"},
|
||||
ServerCommand: serverCommand,
|
||||
GetRunCommand: getRunCommand,
|
||||
Readback: filepath.Join(dir, "get-run-response.json"),
|
||||
}
|
||||
data, err := json.MarshalIndent(output, "", " ")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(metadata, append(data, '\n'), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// readUIDiffFixtureThroughBranchServer starts binaries built from this branch
|
||||
// against the exported database. The CLI readback is what UI review will use,
|
||||
// so a handler in the test process is not evidence for this fixture.
|
||||
func readUIDiffFixtureThroughBranchServer(t *testing.T, dir, db string, runID int64) (*protocol.RunDetail, string, string) {
|
||||
t.Helper()
|
||||
repo, err := filepath.Abs("../..")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
serverBinary := filepath.Join(dir, "pipelines-server")
|
||||
cliBinary := filepath.Join(dir, "pipelines")
|
||||
for _, build := range [][]string{{"build", "-o", serverBinary, "./cmd/pipelines-server"}, {"build", "-o", cliBinary, "./cmd/pipelines"}} {
|
||||
cmd := exec.Command("go", build...)
|
||||
cmd.Dir = repo
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("build branch binary %q: %v\n%s", build[len(build)-1], err, output)
|
||||
}
|
||||
}
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
addr := listener.Addr().String()
|
||||
if err := listener.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
logPath := filepath.Join(dir, "pipelines-server.log")
|
||||
logFile, err := os.OpenFile(logPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
server := exec.Command(serverBinary, "-addr", addr, "-db", db)
|
||||
server.Stdout = logFile
|
||||
server.Stderr = logFile
|
||||
if err := server.Start(); err != nil {
|
||||
_ = logFile.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_ = server.Process.Signal(os.Interrupt)
|
||||
if err := server.Wait(); err != nil {
|
||||
t.Errorf("stop fixture server: %v", err)
|
||||
}
|
||||
_ = logFile.Close()
|
||||
})
|
||||
url := "ws://" + addr + "/connect"
|
||||
args := []string{"--url", url, "inspect", fmt.Sprint(runID), "--json"}
|
||||
deadline := time.Now().Add(10 * time.Second)
|
||||
var output []byte
|
||||
for {
|
||||
command := exec.Command(cliBinary, args...)
|
||||
output, err = command.CombinedOutput()
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("branch CLI get_run: %v\n%s", err, output)
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
var readback protocol.RunDetail
|
||||
if err := json.Unmarshal(bytes.TrimSpace(output), &readback); err != nil {
|
||||
t.Fatalf("decode branch CLI get_run output: %v\n%s", err, output)
|
||||
}
|
||||
for _, step := range readback.Steps {
|
||||
if step.FileChanges == nil {
|
||||
t.Fatalf("branch CLI get_run omitted file changes: %s", output)
|
||||
}
|
||||
for _, file := range step.FileChanges.Files {
|
||||
if file.Patch == "" {
|
||||
t.Fatalf("branch CLI get_run omitted patch for %q: %s", file.Path, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
return &readback, strings.Join(append([]string{serverBinary}, "-addr", addr, "-db", db), " "), strings.Join(append([]string{cliBinary}, args...), " ")
|
||||
}
|
||||
Reference in New Issue
Block a user