package codex import ( "context" "encoding/json" "strings" "testing" "git.a-wan.net/alan/pipelines/internal/protocol" ) func TestStructuredToolHistoryIsNotDropped(t *testing.T) { chat := modelTestChat(t, func(method string, _ json.RawMessage) any { if method != "thread/turns/list" { t.Errorf("unexpected method: %s", method) } return json.RawMessage(`{"data":[{"items":[ {"id":"mcp","type":"mcpToolCall","server":"fixture","tool":"lookup","status":"failed","arguments":{"query":"test"},"error":{"message":"controlled failure"}}, {"id":"code","type":"dynamicToolCall","namespace":"functions","tool":"execute","status":"completed","arguments":{"code":"return 1"},"contentItems":[{"type":"inputText","text":"1"}]}, {"id":"output","type":"functionCallOutput","name":"execute","output":"full code-mode output"}, {"id":"future","type":"newProviderTool","input":{"safe":"visible"},"status":"completed"} ]}],"nextCursor":null}`) }) page, err := chat.Messages(context.Background(), fixtureThread, 50, "") if err != nil || len(page.Messages) != 4 { t.Fatalf("tool history missing: %+v %v", page, err) } first := page.Messages[0].Parts[0] if first.Name != "fixture/lookup" || first.State.Status != protocol.ChatToolError || first.State.Error.Message != "controlled failure" { t.Fatalf("structured MCP failure lost: %+v", first) } if page.Messages[1].Parts[0].Name != "functions/execute" || !strings.Contains(page.Messages[1].Parts[0].State.Content[0].Text, "1") { t.Fatal("code-mode output lost") } if page.Messages[2].Parts[0].State.Content[0].Text != "full code-mode output" { t.Fatal("function output lost") } if page.Messages[3].Parts[0].Name != "newProviderTool" { t.Fatal("unknown tool silently dropped") } } func TestLiveStructuredMCPErrorIsRenderable(t *testing.T) { c := &Chat{subs: map[string]map[chan protocol.ChatEventFrame]struct{}{}} ch := c.subscribe(fixtureThread) defer c.unsubscribe(fixtureThread, ch) c.handleItem(fixtureThread, "", json.RawMessage(`{"id":"mcp","type":"mcpToolCall","server":"fixture","tool":"lookup","status":"failed","error":{"message":"controlled error"}}`), true) select { case frame := <-ch: if frame.Part == nil || frame.Part.State.Error == nil || frame.Part.State.Error.Message != "controlled error" { t.Fatalf("bad tool frame: %+v", frame) } default: t.Fatal("structured tool event disappeared") } } func TestHistoryPaginationPreservesOpaqueCursorAndOrder(t *testing.T) { c := modelTestChat(t, func(method string, params json.RawMessage) any { var p map[string]any json.Unmarshal(params, &p) if method != "thread/turns/list" || p["cursor"] != "opaque cursor" || p["limit"] != float64(100) || p["itemsView"] != "full" { t.Errorf("wrong history request: %s %s", method, params) } return json.RawMessage(`{"data":[{"items":[{"id":"new","type":"agentMessage","text":"new"}]},{"items":[{"id":"old","type":"agentMessage","text":"old"}]}],"nextCursor":"next opaque"}`) }) page, err := c.Messages(context.Background(), fixtureThread, 10000, "opaque cursor") if err != nil || len(page.Messages) != 2 || page.Messages[0].ID != "old" || page.Messages[1].ID != "new" || !page.HasMore || page.NextCursor != "next opaque" { t.Fatalf("history=%+v err=%v", page, err) } }