90 lines
3.6 KiB
Go
90 lines
3.6 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"git.a-wan.net/alan/pipelines/internal/stage"
|
|
)
|
|
|
|
func TestExportWaitEventCoalescingClearsOnlyOnLifecycleTransition(t *testing.T) {
|
|
frame := protocol.RunEventFrame{RunID: 7, Status: "waiting", ExportPendingSequence: 2}
|
|
mergeRunEventFrame(&frame, &protocol.RunEventFrame{RunID: 7, Artifacts: 1})
|
|
if frame.ExportPendingSequence != 2 {
|
|
t.Fatal("repair-only frame cleared export pending")
|
|
}
|
|
mergeRunEventFrame(&frame, &protocol.RunEventFrame{RunID: 7, Status: "running"})
|
|
if frame.ExportPendingSequence != 0 || frame.Status != "running" {
|
|
t.Fatalf("continuation did not clear export wait: %+v", frame)
|
|
}
|
|
}
|
|
|
|
func TestExportWaitPrivateRepairResumesWithoutTerminalRegression(t *testing.T) {
|
|
h, s, id, first, second := gateRunner(t, func(context.Context, stage.SessionExportRequest) (stage.SessionExportResult, error) {
|
|
return stage.SessionExportResult{}, errors.New("offline")
|
|
}, false)
|
|
h.runner.wg.Wait()
|
|
ctx := context.Background()
|
|
job, err := s.SessionExportJob(ctx, id, 1)
|
|
if err != nil || job == nil {
|
|
t.Fatalf("missing recovery checkpoint: %v", err)
|
|
}
|
|
name := "session-repair-1-private-test.html"
|
|
result, err := validGateExport(ctx, stage.SessionExportRequest{ArtifactsDir: h.runner.turnPaths(id, 1, "one").TurnDir, FileName: name})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
evidence := &protocol.SessionExport{Status: "complete", SessionID: job.SessionID, Backend: job.Backend, Directory: job.Directory, Source: "native_export", HTML: name, HTMLBytes: result.HTMLBytes, Messages: result.Messages}
|
|
if err := s.RecordDeliverable(ctx, id, 1, protocol.StageArtifact{Name: name, SizeBytes: result.HTMLBytes, ContentType: "text/html"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.RecordSessionExportRepair(ctx, id, 1, evidence); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Reconciliation recognizes a durable manual repair without another export.
|
|
if claimed, err := s.ClaimSessionExportJob(ctx, time.Now().Add(time.Hour), time.Minute); err != nil || claimed != nil {
|
|
t.Fatalf("repair triggered a redundant export: %+v %v", claimed, err)
|
|
}
|
|
if err := h.runner.resumeExportGates(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h.runner.wg.Wait()
|
|
if err := h.runner.resumeExportGates(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
d, err := h.runner.getRun(id)
|
|
if err != nil || d.Status != "succeeded" || d.ExportPendingSequence != 0 || first.Load() != 1 || second.Load() != 1 {
|
|
t.Fatalf("private repair replayed or lost continuation: %+v %v", d, err)
|
|
}
|
|
input, err := os.ReadFile(h.runner.turnPaths(id, 2, "two").InputPath)
|
|
if err != nil || !strings.Contains(string(input), "computed output") || strings.Contains(string(input), name) {
|
|
t.Fatalf("private export leaked or computed output lost: %q %v", input, err)
|
|
}
|
|
}
|
|
|
|
func TestExportWaitSubscriptionSnapshotCarriesPendingSequence(t *testing.T) {
|
|
h, _, id, _, _ := gateRunner(t, func(context.Context, stage.SessionExportRequest) (stage.SessionExportResult, error) {
|
|
return stage.SessionExportResult{}, errors.New("offline")
|
|
}, false)
|
|
h.runner.wg.Wait()
|
|
hub := newRunEventHub()
|
|
defer hub.unsubscribeClient("export-wait-test")
|
|
var snapshot *protocol.RunEventFrame
|
|
if !handleRunEvents(protocol.Message{Type: protocol.Subscribe, RunIDs: []int64{id}}, hub, h.runner, "export-wait-test", func(message protocol.Message) error {
|
|
if message.RunEvent != nil {
|
|
snapshot = message.RunEvent
|
|
}
|
|
return nil
|
|
}) {
|
|
t.Fatal("subscribe not handled")
|
|
}
|
|
if snapshot == nil || snapshot.Status != "waiting" || snapshot.ExportPendingSequence != 1 || snapshot.FinishedAt != "" {
|
|
t.Fatalf("snapshot lost nonterminal export wait: %+v", snapshot)
|
|
}
|
|
}
|