|
| 1 | +package orchestrator |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/codefionn/scriptschnell/internal/progress" |
| 10 | + "github.com/codefionn/scriptschnell/internal/session" |
| 11 | + "github.com/codefionn/scriptschnell/internal/tools" |
| 12 | +) |
| 13 | + |
| 14 | +func TestProcessToolCallsPreservesOrder(t *testing.T) { |
| 15 | + ctx := context.Background() |
| 16 | + sess := session.NewSession("test", ".") |
| 17 | + |
| 18 | + toolCalls := []map[string]interface{}{ |
| 19 | + { |
| 20 | + "id": "call-1", |
| 21 | + "type": "function", |
| 22 | + "function": map[string]interface{}{ |
| 23 | + "name": "fast-tool", |
| 24 | + "arguments": "{}", |
| 25 | + }, |
| 26 | + }, |
| 27 | + { |
| 28 | + "id": "call-2", |
| 29 | + "type": "function", |
| 30 | + "function": map[string]interface{}{ |
| 31 | + "name": "slow-tool", |
| 32 | + "arguments": "{}", |
| 33 | + }, |
| 34 | + }, |
| 35 | + } |
| 36 | + |
| 37 | + orch := &Orchestrator{} |
| 38 | + |
| 39 | + // Run calls with different latencies to ensure ordering is dictated by the |
| 40 | + // original call index, not completion time. |
| 41 | + execFn := func(ctx context.Context, call *tools.ToolCall, toolName string, progressCb progress.Callback, toolCallCb ToolCallCallback, toolResultCb ToolResultCallback, approved bool) (*tools.ToolResult, error) { |
| 42 | + if toolName == "slow-tool" { |
| 43 | + time.Sleep(15 * time.Millisecond) |
| 44 | + } |
| 45 | + return &tools.ToolResult{ |
| 46 | + ID: call.ID, |
| 47 | + Result: fmt.Sprintf("result-%s", call.ID), |
| 48 | + }, nil |
| 49 | + } |
| 50 | + |
| 51 | + if err := orch.processToolCalls(ctx, toolCalls, sess, nil, nil, nil, nil, execFn); err != nil { |
| 52 | + t.Fatalf("processToolCalls returned error: %v", err) |
| 53 | + } |
| 54 | + |
| 55 | + messages := sess.GetMessages() |
| 56 | + if len(messages) != 2 { |
| 57 | + t.Fatalf("expected 2 tool messages, got %d", len(messages)) |
| 58 | + } |
| 59 | + |
| 60 | + if messages[0].ToolID != "call-1" || messages[0].ToolName != "fast-tool" || messages[0].Content != "result-call-1" { |
| 61 | + t.Fatalf("unexpected first tool message: %+v", messages[0]) |
| 62 | + } |
| 63 | + |
| 64 | + if messages[1].ToolID != "call-2" || messages[1].ToolName != "slow-tool" || messages[1].Content != "result-call-2" { |
| 65 | + t.Fatalf("unexpected second tool message: %+v", messages[1]) |
| 66 | + } |
| 67 | +} |
0 commit comments