| 1 | package serve |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "reasonix/internal/config" |
| 11 | "reasonix/internal/control" |
| 12 | "reasonix/internal/provider" |
| 13 | ) |
| 14 | |
| 15 | func TestProtocolRecoveryHTTPRejectsMissingAndStaleTokens(t *testing.T) { |
| 16 | bc := NewBroadcaster() |
| 17 | got := make(chan string, 1) |
| 18 | ctrl := control.New(control.Options{Runner: fakeRunner{got: got}, Sink: bc}) |
| 19 | defer ctrl.Close() |
| 20 | srv := httptest.NewServer(New(ctrl, bc, config.ServeConfig{}).Handler()) |
| 21 | defer srv.Close() |
| 22 | for _, tc := range []struct { |
| 23 | body string |
| 24 | status int |
| 25 | }{ |
| 26 | {`{"action":"protocol_recovery"}`, 400}, |
| 27 | {`{"action":"protocol_recovery","recoveryId":"stale","input":"/new"}`, 409}, |
| 28 | {`{"action":"protocol_recovery","recoveryId":"stale","input":"/model other"}`, 409}, |
| 29 | {`{"action":"protocol_recovery","recoveryId":"stale","format":"json_object"}`, 400}, |
| 30 | } { |
| 31 | resp, err := http.Post(srv.URL+"/submit", "application/json", strings.NewReader(tc.body)) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | resp.Body.Close() |
| 36 | if resp.StatusCode != tc.status { |
| 37 | t.Fatalf("body=%s status=%d", tc.body, resp.StatusCode) |
| 38 | } |
| 39 | } |
| 40 | select { |
| 41 | case input := <-got: |
| 42 | t.Fatalf("stale action reached runner: %q", input) |
| 43 | default: |
| 44 | } |
| 45 | } |
| 46 | func TestProtocolAndSearchHistoryPreserveDisplayWithoutProof(t *testing.T) { |
| 47 | raw, _ := json.Marshal(provider.ProtocolRecoveryRecord{Version: 1, ID: "one", State: "pending", Fingerprint: "hash", Prefix: 1}) |
| 48 | got := historyMessages([]provider.Message{{LocalOnly: true, ProtocolRecovery: raw}, {Role: provider.RoleAssistant, Content: "summary", ServerSearch: []provider.ServerSearchCall{{ID: "s", SourcesStatus: provider.SourcesNotProvided, Raw: json.RawMessage(`{"opaque":"secret-proof"}`)}}}}) |
| 49 | b, _ := json.Marshal(got) |
| 50 | if !strings.Contains(string(b), "not_provided") || !strings.Contains(string(b), "protocolRecovery") || strings.Contains(string(b), "secret-proof") { |
| 51 | t.Fatalf("history=%s", b) |
| 52 | } |
| 53 | } |
| 54 |