80 lines
2.9 KiB
Go
80 lines
2.9 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestGraphEvidenceSubscribeSnapshot(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
defer cancel()
|
|
store, handler, ids := eventStore(t, ctx, "a", "b", "c")
|
|
definition, err := store.Get(ctx, ids[0], 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
id, _, err := store.CreateRun(ctx, definition, "graph-snapshot", "workspace", false, nil, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for index, key := range []string{"a", "b", "c"} {
|
|
sequence := index + 1
|
|
if err := store.BeginStep(ctx, id, sequence, key, map[string]any{"prompt": "PRIVATE-PROMPT"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
status, outcome, target := "completed", "success", "b"
|
|
if key == "b" {
|
|
target = "c"
|
|
}
|
|
if key == "c" {
|
|
status, outcome, target = "failed", "", ""
|
|
}
|
|
if err := store.FinishStep(ctx, id, protocol.RunStep{Sequence: sequence, Status: status, Outcome: outcome, Target: target, Transcript: "PRIVATE-TRANSCRIPT"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
hub := newRunEventHub()
|
|
defer hub.unsubscribeClient("snapshot-test")
|
|
var messages []protocol.Message
|
|
if !handleRunEvents(protocol.Message{Type: protocol.Subscribe}, hub, handler.runner, "snapshot-test", func(message protocol.Message) error { messages = append(messages, message); return nil }) {
|
|
t.Fatal("unhandled subscribe")
|
|
}
|
|
if len(messages) != 2 || messages[0].RunEvent == nil || messages[1].Type != protocol.Subscribe {
|
|
t.Fatalf("snapshot/ack: %+v", messages)
|
|
}
|
|
evidence := messages[0].RunEvent.GraphEvidence
|
|
if evidence == nil || !evidence.Complete || len(evidence.Visits) != 3 || evidence.Visits[2].Status != "failed" {
|
|
t.Fatalf("missing persisted graph evidence: %+v", evidence)
|
|
}
|
|
encoded, _ := json.Marshal(messages[0])
|
|
if strings.Contains(string(encoded), "PRIVATE") {
|
|
t.Fatal(string(encoded))
|
|
}
|
|
}
|
|
|
|
func TestCoalescedGraphVisitsRetainEveryStage(t *testing.T) {
|
|
hub := newRunEventHub()
|
|
hub.interval = time.Hour
|
|
sub := hub.subscribe("graph-test", "", nil, func(protocol.Message) error { return nil })
|
|
defer sub.close()
|
|
// Hold subsequent frames in the coalescer without relying on wall-clock races.
|
|
hub.emit(protocol.RunEventFrame{RunID: 1, Status: "running"}, false)
|
|
for sequence, stage := range []string{"a", "b", "c"} {
|
|
hub.emit(protocol.RunEventFrame{RunID: 1, Step: &protocol.RunStepDelta{Sequence: sequence + 1, Stage: stage, Status: "completed", Outcome: "success", Target: "next", Diagnostic: "PRIVATE"}}, false)
|
|
}
|
|
hub.mu.Lock()
|
|
pending := hub.runs[1].pending
|
|
hub.stopTimerLocked(hub.runs[1])
|
|
hub.mu.Unlock()
|
|
if pending == nil || len(pending.GraphVisits) != 3 || pending.Step.Stage != "c" {
|
|
t.Fatalf("coalescing lost evidence: %+v", pending)
|
|
}
|
|
if pending.GraphVisits[0].Stage != "a" || pending.GraphVisits[1].Stage != "b" {
|
|
t.Fatal(pending.GraphVisits)
|
|
}
|
|
}
|