返回 DeepSeek-Reasonix
runtime_state_test.go
根目录 / internal / event / runtime_state_test.go
1 package event
2
3 import (
4 "encoding/json"
5 "reflect"
6 "sync/atomic"
7 "testing"
8 "time"
9 )
10
11 type runtimeStateCaptureSink struct {
12 states chan RuntimeStateSnapshot
13 events atomic.Int32
14 }
15
16 func (s *runtimeStateCaptureSink) Emit(Event) { s.events.Add(1) }
17 func (s *runtimeStateCaptureSink) RuntimeStateChanged(state RuntimeStateSnapshot) { s.states <- state }
18
19 func TestRuntimeStateForwardsOutsideTranscriptEvents(t *testing.T) {
20 for _, kind := range []string{"direct", "sync", "coalesce", "audit", "combined"} {
21 t.Run(kind, func(t *testing.T) {
22 capture := &runtimeStateCaptureSink{states: make(chan RuntimeStateSnapshot, 1)}
23 var sink Sink = capture
24 switch kind {
25 case "sync":
26 sink = Sync(sink)
27 case "coalesce":
28 sink = Coalesce(sink, time.Millisecond)
29 case "audit":
30 sink = runtimeAuditTestSink{AuditForwarder: AuditForwarder{Inner: sink}}
31 case "combined":
32 sink = Sync(Coalesce(runtimeAuditTestSink{AuditForwarder: AuditForwarder{Inner: sink}}, time.Millisecond))
33 }
34 want := RuntimeStateSnapshot{SchemaVersion: 1, RuntimeEpoch: "test-runtime", Revision: 9, Phase: "finishing", Running: true, TurnID: "test-turn"}
35 PublishRuntimeState(sink, want)
36 select {
37 case got := <-capture.states:
38 if !reflect.DeepEqual(got, want) {
39 t.Fatalf("wrapper changed snapshot: got=%+v want=%+v", got, want)
40 }
41 case <-time.After(5 * time.Second):
42 t.Fatal("wrapper swallowed runtime capability")
43 }
44 if capture.events.Load() != 0 {
45 t.Fatal("runtime notification entered the transcript event channel")
46 }
47 })
48 }
49 }
50
51 type runtimeAuditTestSink struct{ AuditForwarder }
52
53 func (s runtimeAuditTestSink) Emit(e Event) { s.Inner.Emit(e) }
54
55 func TestRuntimeStateJSONExplicitZeroValues(t *testing.T) {
56 raw, err := json.Marshal(RuntimeStateSnapshot{SchemaVersion: 1, RuntimeEpoch: "test", Revision: 1, Phase: "idle"})
57 if err != nil {
58 t.Fatal(err)
59 }
60 var fields map[string]any
61 if err := json.Unmarshal(raw, &fields); err != nil {
62 t.Fatal(err)
63 }
64 for _, key := range []string{"running", "pendingPrompt", "cancelRequested", "cancellable"} {
65 if value, ok := fields[key]; !ok || value != false {
66 t.Fatalf("%s must explicitly clear stale state: %s", key, raw)
67 }
68 }
69 if value, ok := fields["backgroundJobs"]; !ok || value != float64(0) {
70 t.Fatalf("backgroundJobs must explicitly clear stale count: %s", raw)
71 }
72 }
73
74 func TestRuntimeStatePublishAcceptsLegacyAndNilSinks(t *testing.T) {
75 var typedNil *runtimeStateCaptureSink
76 for _, sink := range []Sink{nil, typedNil, Discard, FuncSink(func(Event) { t.Fatal("legacy sink received a synthetic event") })} {
77 PublishRuntimeState(sink, RuntimeStateSnapshot{Phase: "idle"})
78 }
79 }
80
80 lines GO