返回 DeepSeek-Reasonix
terminal_calls_test.go
根目录 / internal / provider / responses / terminal_calls_test.go
1 package responses
2
3 import (
4 "context"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "strings"
9 "testing"
10
11 "reasonix/internal/provider"
12 )
13
14 func TestTerminalCallsRejectUnsafeCompletion(t *testing.T) {
15 for _, tc := range []struct{ name, output string }{
16 {"arguments object", `{"type":"function_call","call_id":"call_1","name":"echo","arguments":{"secret":"must not appear"}}`},
17 {"arguments null", `{"type":"function_call","call_id":"call_1","name":"echo","arguments":null}`},
18 {"arguments missing", `{"type":"function_call","call_id":"call_1","name":"echo"}`},
19 {"missing call id", `{"type":"function_call","name":"echo","arguments":"{}"}`},
20 {"missing name", `{"type":"function_call","call_id":"call_1","arguments":"{}"}`},
21 {"in_progress item at completed terminal", `{"type":"function_call","call_id":"call_1","name":"echo","status":"in_progress","arguments":"{}"}`},
22 {"incomplete item at completed terminal", `{"type":"function_call","call_id":"call_1","name":"echo","status":"incomplete","arguments":"{}"}`},
23 } {
24 t.Run(tc.name, func(t *testing.T) {
25 chunks := chunksOf(t, `{"type":"response.output_text.delta","delta":"next step"}`, `{"type":"response.completed","response":{"id":"r","output":[`+tc.output+`]}}`)
26 var failed bool
27 for _, chunk := range chunks {
28 switch chunk.Type {
29 case provider.ChunkError:
30 failed = true
31 if !errors.Is(chunk.Err, ErrInvalidFunctionCall) || !errors.Is(fmt.Errorf("consumer: %w", chunk.Err), ErrInvalidFunctionCall) {
32 t.Errorf("invalid-call identity lost: %v", chunk.Err)
33 }
34 if errors.Is(chunk.Err, ErrUnfinishedFunctionCall) {
35 t.Error("protocol causes must stay distinct")
36 }
37 if provider.IsStreamInterrupted(chunk.Err) || provider.ClassifyRecovery(chunk.Err).Retryable {
38 t.Errorf("protocol error became retryable: %v", chunk.Err)
39 }
40 if strings.Contains(chunk.Err.Error(), "secret") {
41 t.Fatal("error leaked arguments")
42 }
43 case provider.ChunkDone:
44 t.Error("unsafe terminal emitted done")
45 }
46 }
47 if !failed {
48 t.Fatal("unsafe terminal silently succeeded")
49 }
50 })
51 }
52 }
53
54 func TestTerminalCallsRecognizeAlternateItemIdentity(t *testing.T) {
55 calls := map[string]*streamedCall{
56 "original": {id: "call_1", name: "echo"},
57 "terminal": {id: "call_1", name: "echo", arguments: "{}", completed: true},
58 "": {arguments: "{}"},
59 }
60 if err := terminalCallsError(nil, calls); err != nil {
61 t.Fatal(err)
62 }
63 }
64
65 func TestTerminalCallsKeepTextOnlyDONE(t *testing.T) {
66 chunks := chunksOf(t, `{"type":"response.output_text.delta","delta":"I will open the browser"}`, "[DONE]")
67 var done bool
68 for _, chunk := range chunks {
69 if chunk.Type == provider.ChunkError {
70 t.Fatal(chunk.Err)
71 }
72 done = done || chunk.Type == provider.ChunkDone
73 }
74 if !done {
75 t.Fatal("text-only DONE compatibility lost")
76 }
77 }
78
79 func TestTerminalCallsLeaveArgumentJSONToToolValidation(t *testing.T) {
80 // A string's JSON syntax is an existing tool-validation concern. This guard
81 // rejects wrong wire types rather than inventing a second argument repairer.
82 response := &sseResponse{Output: []json.RawMessage{json.RawMessage(`{"type":"function_call","call_id":"call_1","name":"echo","arguments":"{"}`)}}
83 calls := map[string]*streamedCall{"fc_1": {id: "call_1", name: "echo", arguments: "{", completed: true}}
84 if err := terminalCallsError(response, calls); err != nil {
85 t.Fatal(err)
86 }
87 }
88
89 func TestTerminalCallsCanceledSendDoesNotBlock(t *testing.T) {
90 ctx, cancel := context.WithCancel(context.Background())
91 cancel()
92 if checkTerminalCalls(ctx, make(chan provider.Chunk), nil, map[string]*streamedCall{"fc_1": {id: "call_1", name: "echo"}}) {
93 t.Fatal("unfinished call accepted")
94 }
95 }
96
97 func TestTerminalCallsRecoverOrphanDeltaWithDifferentItemID(t *testing.T) {
98 calls := toolCallsOf(t,
99 `{"type":"response.function_call_arguments.delta","item_id":"old_item","delta":"{}"}`,
100 `{"type":"response.completed","response":{"id":"r","output":[{"type":"function_call","id":"new_item","call_id":"call_1","name":"echo","arguments":"{}"}]}}`)
101 if len(calls) != 1 || calls[0].ID != "call_1" {
102 t.Fatalf("calls=%+v", calls)
103 }
104 for _, chunk := range chunksOf(t,
105 `{"type":"response.function_call_arguments.delta","item_id":"old_item","delta":"{}"}`,
106 `{"type":"response.completed","response":{"id":"r","output":[{"type":"function_call","id":"new_item","call_id":"call_1","name":"echo","arguments":"{}"}]}}`) {
107 if chunk.Type == provider.ChunkError {
108 t.Fatal(chunk.Err)
109 }
110 }
111 }
112
113 func TestTerminalCallsRejectArgumentsDoneWithoutIdentity(t *testing.T) {
114 for _, terminal := range []string{"[DONE]", `{"type":"response.completed","response":{"id":"r","output":[]}}`} {
115 failed := false
116 for _, chunk := range chunksOf(t, `{"type":"response.function_call_arguments.done","item_id":"fc_1","arguments":"{}","name":"echo"}`, terminal) {
117 if chunk.Type == provider.ChunkError {
118 failed = true
119 if !errors.Is(chunk.Err, ErrUnfinishedFunctionCall) || !errors.Is(fmt.Errorf("consumer: %w", chunk.Err), ErrUnfinishedFunctionCall) {
120 t.Errorf("unfinished-call identity lost: %v", chunk.Err)
121 }
122 if errors.Is(chunk.Err, ErrInvalidFunctionCall) {
123 t.Error("protocol causes must stay distinct")
124 }
125 }
126 if chunk.Type == provider.ChunkDone {
127 t.Error("unnamed call silently completed")
128 }
129 }
130 if !failed {
131 t.Fatal("want explicit protocol error")
132 }
133 }
134 }
135
135 lines GO