| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/control" |
| 10 | "reasonix/internal/provider" |
| 11 | ) |
| 12 | |
| 13 | func TestRemoteToolRecoveryKeepsIdentityAndNeverReplaysUnknownPost(t *testing.T) { |
| 14 | posts := 0 |
| 15 | client := &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) { |
| 16 | if req.URL.Path != "/tool-recovery" || req.Header.Get(expectedSessionPathHeader) != runtimeRemoteTestPath { |
| 17 | t.Fatalf("unfenced recovery request: %s %v", req.URL, req.Header) |
| 18 | } |
| 19 | if req.Method == http.MethodGet { |
| 20 | return remoteRuntimeTestResponse(req, 200, remoteRuntimeTestJSON(t, control.ToolRecoverySnapshot{SessionPath: runtimeRemoteTestPath, RuntimeEpoch: "epoch", Revision: "revision", Calls: []provider.ToolCallRecord{}})), nil |
| 21 | } |
| 22 | posts++ |
| 23 | var body control.ToolRecoveryRequest |
| 24 | if err := json.NewDecoder(req.Body).Decode(&body); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | if body.AttemptID != "attempt" || body.InspectionID != "inspection" || body.RuntimeEpoch != "epoch" || body.Revision != "revision" { |
| 28 | t.Fatalf("identity changed: %+v", body) |
| 29 | } |
| 30 | return nil, errors.New("response lost after commit") |
| 31 | })} |
| 32 | a, tab := remoteRuntimeTestApp(client) |
| 33 | v, err := a.GetToolRecoveryForTab(tab.id) |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | _, err = a.ResolveToolRecoveryForTab(tab.id, control.ToolRecoveryRequest{SessionPath: v.SessionPath, RuntimeEpoch: v.RuntimeEpoch, Revision: v.Revision, AttemptID: "attempt", InspectionID: "inspection", Action: "confirm"}) |
| 38 | if err == nil || posts != 1 { |
| 39 | t.Fatalf("unknown post replayed: posts=%d err=%v", posts, err) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func TestRemoteToolRecoveryRejectsOtherSessionSnapshot(t *testing.T) { |
| 44 | client := &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) { |
| 45 | return remoteRuntimeTestResponse(req, 200, `{"sessionPath":"/different","runtimeEpoch":"epoch","revision":"revision","calls":[]}`), nil |
| 46 | })} |
| 47 | a, tab := remoteRuntimeTestApp(client) |
| 48 | if _, err := a.GetToolRecoveryForTab(tab.id); err == nil { |
| 49 | t.Fatal("another session's snapshot was accepted") |
| 50 | } |
| 51 | } |
| 52 |