| 1 | package responses |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/provider" |
| 9 | ) |
| 10 | |
| 11 | // Message ids are local transcript identity. Both the request body and the |
| 12 | // stateful-continuation digest must ignore them, or a reload would force a |
| 13 | // full replay and lose previous_response_id reuse. |
| 14 | func TestMessageIDNeverReachesResponsesWire(t *testing.T) { |
| 15 | plain := []provider.Message{ |
| 16 | {Role: provider.RoleSystem, Content: "sys"}, |
| 17 | {Role: provider.RoleUser, Content: "list the files"}, |
| 18 | {Role: provider.RoleAssistant, ToolCalls: []provider.ToolCall{{ID: "call_1", Name: "ls", Arguments: `{"path":"."}`}}}, |
| 19 | {Role: provider.RoleTool, Content: "main.go", ToolCallID: "call_1", Name: "ls"}, |
| 20 | {Role: provider.RoleAssistant, Content: "done"}, |
| 21 | } |
| 22 | tagged := append([]provider.Message(nil), plain...) |
| 23 | for i := range tagged { |
| 24 | tagged[i].ID = "01ARZ3NDEKTSV4RRFFQ69G5FA" + string(rune('A'+i)) |
| 25 | } |
| 26 | c := New(Config{Name: "responses", BaseURL: "https://example.com", Model: "model"}).(*client) |
| 27 | plainBody, _, _ := c.buildRequestBody(provider.Request{Messages: plain}) |
| 28 | taggedBody, _, _ := c.buildRequestBody(provider.Request{Messages: tagged}) |
| 29 | want, err := json.Marshal(plainBody) |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | got, err := json.Marshal(taggedBody) |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | if !bytes.Equal(want, got) { |
| 38 | t.Fatalf("message ids changed request bytes\nplain=%s\ntagged=%s", want, got) |
| 39 | } |
| 40 | if bytes.Contains(got, []byte("01ARZ3NDEKTSV4RRFFQ69G5FA")) { |
| 41 | t.Fatalf("message id leaked into request: %s", got) |
| 42 | } |
| 43 | if c.conversationDigest(plain) != c.conversationDigest(tagged) { |
| 44 | t.Fatal("conversationDigest must ignore message ids") |
| 45 | } |
| 46 | } |
| 47 |