返回 DeepSeek-Reasonix
session_missing_test.go
根目录 / internal / plugin / session_missing_test.go
1 package plugin
2
3 import (
4 "encoding/json"
5 "net/http"
6 "net/http/httptest"
7 "sync/atomic"
8 "testing"
9 )
10
11 func TestHTTPTransportEstablishedSessionless404DoesNotRebuild(t *testing.T) {
12 var initializeCount atomic.Int32
13 var listCount atomic.Int32
14 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
15 if r.Method == http.MethodGet {
16 w.WriteHeader(http.StatusMethodNotAllowed)
17 return
18 }
19 var req struct {
20 ID *int `json:"id"`
21 Method string `json:"method"`
22 }
23 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
24 http.Error(w, "bad body", http.StatusBadRequest)
25 return
26 }
27 switch req.Method {
28 case "initialize":
29 initializeCount.Add(1)
30 writeHTTPRPCResult(w, req.ID, map[string]any{
31 "protocolVersion": testLegacyProtocolVersion,
32 "serverInfo": map[string]any{"name": "sessionless", "version": "1"},
33 })
34 case "notifications/initialized":
35 w.WriteHeader(http.StatusAccepted)
36 case "tools/list":
37 listCount.Add(1)
38 http.Error(w, "not found", http.StatusNotFound)
39 default:
40 http.Error(w, "unknown method", http.StatusBadRequest)
41 }
42 }))
43 defer srv.Close()
44
45 transport, err := newHTTPTransport(Spec{Name: "sessionless", Type: "http", URL: srv.URL})
46 if err != nil {
47 t.Fatal(err)
48 }
49 defer transport.close()
50 if _, err := transport.call(t.Context(), "tools/list", map[string]any{}); err == nil {
51 t.Fatal("tools/list unexpectedly succeeded")
52 }
53 if got := initializeCount.Load(); got != 1 {
54 t.Fatalf("initialize count = %d, want no session rebuild", got)
55 }
56 if got := listCount.Load(); got != 1 {
57 t.Fatalf("tools/list count = %d, want no replay", got)
58 }
59 transport.mu.Lock()
60 generations := transport.nextGeneration
61 transport.mu.Unlock()
62 if generations != 1 {
63 t.Fatalf("supervisor generations = %d, want 1", generations)
64 }
65 diagnostics := transport.sessionDiagnostics()
66 if diagnostics.LastErrorKind != SessionErrorProtocol {
67 t.Fatalf("last error kind = %q, want %q", diagnostics.LastErrorKind, SessionErrorProtocol)
68 }
69 }
70
70 lines GO