| 1 | package provider |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | // TestSanitizeDuplicateToolCallIDsKeepsEachResult probes whether a malformed |
| 6 | // history with two tool calls sharing one id round-trips both results. The map |
| 7 | // keyed on id collapses them, so the loop guard's safety net silently drops one. |
| 8 | func TestSanitizeDuplicateToolCallIDsKeepsEachResult(t *testing.T) { |
| 9 | msgs := []Message{ |
| 10 | {Role: RoleUser, Content: "go"}, |
| 11 | {Role: RoleAssistant, ToolCalls: []ToolCall{ |
| 12 | {ID: "dup", Name: "read_file"}, |
| 13 | {ID: "dup", Name: "grep"}, |
| 14 | }}, |
| 15 | {Role: RoleTool, ToolCallID: "dup", Name: "read_file", Content: "FILE-RESULT"}, |
| 16 | {Role: RoleTool, ToolCallID: "dup", Name: "grep", Content: "GREP-RESULT"}, |
| 17 | } |
| 18 | out := SanitizeToolPairing(msgs) |
| 19 | |
| 20 | var got []string |
| 21 | for _, m := range out { |
| 22 | if m.Role == RoleTool { |
| 23 | got = append(got, m.Content) |
| 24 | } |
| 25 | } |
| 26 | if len(got) != 2 { |
| 27 | t.Fatalf("want 2 tool results, got %d: %v", len(got), got) |
| 28 | } |
| 29 | if got[0] == got[1] { |
| 30 | t.Errorf("both tool results collapsed to the same content %q — one was lost", got[0]) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // TestSanitizeEmptyToolCallIDs probes two calls with empty ids — same collapse |
| 35 | // risk, and the placeholder/backfill path keys on "" too. |
| 36 | func TestSanitizeEmptyToolCallIDsKeepsEachResult(t *testing.T) { |
| 37 | msgs := []Message{ |
| 38 | {Role: RoleAssistant, ToolCalls: []ToolCall{ |
| 39 | {ID: "", Name: "a"}, |
| 40 | {ID: "", Name: "b"}, |
| 41 | }}, |
| 42 | {Role: RoleTool, ToolCallID: "", Name: "a", Content: "RESULT-A"}, |
| 43 | {Role: RoleTool, ToolCallID: "", Name: "b", Content: "RESULT-B"}, |
| 44 | } |
| 45 | out := SanitizeToolPairing(msgs) |
| 46 | |
| 47 | var got []string |
| 48 | for _, m := range out { |
| 49 | if m.Role == RoleTool { |
| 50 | got = append(got, m.Content) |
| 51 | } |
| 52 | } |
| 53 | if len(got) != 2 || got[0] == got[1] { |
| 54 | t.Errorf("empty-id pair collapsed: %v", got) |
| 55 | } |
| 56 | } |
| 57 |