返回 DeepSeek-Reasonix
remote_inbox_test.go
根目录 / desktop / remote_inbox_test.go
1 package main
2
3 import (
4 "context"
5 "encoding/json"
6 "net/http"
7 "net/http/httptest"
8 "os"
9 "path/filepath"
10 "testing"
11 "time"
12
13 "reasonix/internal/config"
14 "reasonix/internal/control"
15 "reasonix/internal/event"
16 "reasonix/internal/serve"
17 "reasonix/internal/sessioninbox"
18 )
19
20 type remoteInboxRunner struct {
21 started chan string
22 release chan struct{}
23 }
24
25 func (r remoteInboxRunner) Run(ctx context.Context, input string) error {
26 r.started <- input
27 select {
28 case <-r.release:
29 return nil
30 case <-ctx.Done():
31 return ctx.Err()
32 }
33 }
34
35 type remoteInboxSink struct {
36 states chan event.RuntimeStateSnapshot
37 }
38
39 func (*remoteInboxSink) Emit(event.Event) {}
40 func (s *remoteInboxSink) RuntimeStateChanged(state event.RuntimeStateSnapshot) {
41 s.states <- state
42 }
43
44 func TestRemoteRuntimeInboxReceiptSnapshotAndConsumptionOverHTTP(t *testing.T) {
45 isolateDesktopUserDirs(t)
46 dir := t.TempDir()
47 path := filepath.Join(dir, "remote.jsonl")
48 if err := os.WriteFile(path, nil, 0o600); err != nil {
49 t.Fatal(err)
50 }
51 runner := remoteInboxRunner{started: make(chan string, 2), release: make(chan struct{}, 2)}
52 sink := &remoteInboxSink{states: make(chan event.RuntimeStateSnapshot, 64)}
53 ctrl := control.New(control.Options{SessionDir: dir, SessionPath: path, Runner: runner, Sink: sink})
54 t.Cleanup(func() {
55 defer ctrl.Close()
56 before := ctrl.RuntimeStateSnapshot()
57 if before.Phase != "executing" && before.Phase != "finishing" {
58 return
59 }
60 _ = ctrl.SetInboxPaused(true)
61 ctrl.Cancel()
62 deadline := time.NewTimer(5 * time.Second)
63 defer deadline.Stop()
64 for {
65 select {
66 case state := <-sink.states:
67 if state.Phase == "idle" && state.Revision > before.Revision {
68 return
69 }
70 case <-deadline.C:
71 t.Error("isolated inbox runner did not settle before cleanup")
72 return
73 }
74 }
75 })
76 server := httptest.NewServer(serve.New(ctrl, nil, config.ServeConfig{}).Handler())
77 defer server.Close()
78 a, tab := remoteRuntimeTestApp(server.Client())
79 tab.base, tab.routing.currentPath, tab.session.path = server.URL, path, path
80 ctrl.Send("hold first turn")
81 select {
82 case <-runner.started:
83 case <-time.After(5 * time.Second):
84 t.Fatal("first turn did not start")
85 }
86 receipt, err := a.EnqueueInboxFollowupWithInvocations(tab.id, "rich follow-up", "next input", nil, "remote-inbox-key")
87 if err != nil || receipt.ItemID == "" {
88 t.Fatalf("enqueue = %+v, %v", receipt, err)
89 }
90 snapshot, err := a.InboxSnapshot(tab.id)
91 if err != nil || snapshot.SessionPath != path || len(snapshot.Items) != 1 || snapshot.Items[0].ID != receipt.ItemID || snapshot.Items[0].Preview != "rich follow-up" || snapshot.MaxItems == 0 || snapshot.Bytes == 0 {
92 t.Fatalf("receipt did not resolve to durable composer snapshot: %+v, %v", snapshot, err)
93 }
94 runner.release <- struct{}{}
95 select {
96 case <-runner.started:
97 case <-time.After(5 * time.Second):
98 t.Fatal("durable follow-up was not dispatched")
99 }
100 active, err := a.InboxSnapshot(tab.id)
101 running := ctrl.RuntimeStateSnapshot()
102 if err != nil || len(active.Items) != 1 || active.Items[0].ID != receipt.ItemID || active.Items[0].State != "running" || active.Revision <= snapshot.Revision || !running.Running {
103 t.Fatalf("dispatched follow-up did not publish its composer-hidden state while still running: %+v, %v", active, err)
104 }
105 runner.release <- struct{}{}
106 deadline := time.NewTimer(5 * time.Second)
107 defer deadline.Stop()
108 for {
109 select {
110 case state := <-sink.states:
111 // The buffered sink also contains idle states from the first turn.
112 if state.Phase != "idle" || state.Revision <= running.Revision {
113 continue
114 }
115 consumed, err := a.InboxSnapshot(tab.id)
116 if err != nil || consumed.Items == nil || len(consumed.Items) != 0 || consumed.Revision <= snapshot.Revision {
117 t.Fatalf("consumption did not remove durable item: %+v, %v", consumed, err)
118 }
119 return
120 case <-deadline.C:
121 t.Fatal("follow-up did not settle")
122 }
123 }
124 }
125
126 func TestRemoteRuntimeInboxRejectsStaleResponseIdentity(t *testing.T) {
127 for _, change := range []string{"generation", "selection", "client", "base", "path", "rehydrating", "replaced", "closed"} {
128 t.Run(change, func(t *testing.T) {
129 started, release := make(chan struct{}), make(chan struct{})
130 client := &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
131 if req.URL.Path != "/inbox" || req.URL.Query().Get("session") != runtimeRemoteTestPath {
132 t.Errorf("unfenced inbox request: %s", req.URL)
133 }
134 close(started)
135 <-release
136 return remoteRuntimeTestResponse(req, http.StatusOK, `{"sessionPath":"`+runtimeRemoteTestPath+`","revision":4,"items":[]}`), nil
137 })}
138 a, tab := remoteRuntimeTestApp(client)
139 done := make(chan error, 1)
140 go func() { _, err := a.InboxSnapshot(tab.id); done <- err }()
141 <-started
142 a.remoteTabMu.Lock()
143 switch change {
144 case "generation":
145 tab.gen++
146 case "selection":
147 tab.selectionRevision++
148 case "client":
149 tab.client = &http.Client{}
150 case "base":
151 tab.base = "http://other.invalid"
152 case "path":
153 tab.routing.currentPath = "/other.jsonl"
154 case "rehydrating":
155 tab.routing.rehydratingPath = runtimeRemoteTestPath
156 case "replaced":
157 a.remoteTabs[tab.id] = &remoteTab{id: tab.id}
158 case "closed":
159 delete(a.remoteTabs, tab.id)
160 }
161 a.remoteTabMu.Unlock()
162 close(release)
163 if err := <-done; err == nil {
164 t.Fatal("stale remote inbox response was accepted")
165 }
166 })
167 }
168 }
169
170 func TestRemoteRuntimeInboxRejectsMissingOrWrongResponseSession(t *testing.T) {
171 for _, path := range []string{"", "/sessions/other.jsonl"} {
172 t.Run(path, func(t *testing.T) {
173 data, _ := json.Marshal(sessioninbox.InboxSnapshot{SessionPath: path})
174 a, tab := remoteRuntimeTestApp(&http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
175 return remoteRuntimeTestResponse(req, http.StatusOK, string(data)), nil
176 })})
177 if _, err := a.InboxSnapshot(tab.id); err == nil {
178 t.Fatal("unscoped remote inbox response was accepted")
179 }
180 })
181 }
182 }
183
183 lines GO