|
| 1 | +import React from "react"; |
| 2 | +import { readFileSync } from "node:fs"; |
| 3 | +import { describe, expect, it } from "vitest"; |
| 4 | +import { renderToStaticMarkup } from "react-dom/server"; |
| 5 | + |
| 6 | +import { buildTraceTimeline } from "../trace-timeline"; |
| 7 | +import { createInitialLiveRun, reduceLiveRunEvent } from "../live-run-state"; |
| 8 | +import { |
| 9 | + parseSchemaToolResult, |
| 10 | + parseSqlToolResult, |
| 11 | +} from "../tool-result-normalize"; |
| 12 | +import { |
| 13 | + ToolFailureResult, |
| 14 | + ToolFormattedResult, |
| 15 | +} from "../tool-result-format"; |
| 16 | +import { |
| 17 | + resolveToolFailurePresentation, |
| 18 | + toolResultLooksLikeError, |
| 19 | +} from "../tool-call-display"; |
| 20 | +import { toolResultFixtures } from "../tool-display-fixtures"; |
| 21 | + |
| 22 | +const renderTool = (toolName: string, result: unknown, variant: "chat" | "console" = "console") => |
| 23 | + renderToStaticMarkup( |
| 24 | + ToolFormattedResult({ toolName, result, variant, showRawFallback: false }), |
| 25 | + ); |
| 26 | + |
| 27 | +describe("observation-wrapped chat vs console parity", () => { |
| 28 | + const wrappedCases = [ |
| 29 | + { |
| 30 | + tool: "list_data_sources", |
| 31 | + result: toolResultFixtures.list_data_sources.observationWrapped, |
| 32 | + expectText: "API DuckDB Demo", |
| 33 | + }, |
| 34 | + { |
| 35 | + tool: "inspect_schema", |
| 36 | + result: toolResultFixtures.inspect_schema.observationWrapped, |
| 37 | + expectText: "orders", |
| 38 | + }, |
| 39 | + { |
| 40 | + tool: "run_sql_readonly", |
| 41 | + result: toolResultFixtures.run_sql_readonly.observationWrapped, |
| 42 | + expectText: "128", |
| 43 | + }, |
| 44 | + ] as const; |
| 45 | + |
| 46 | + it.each(wrappedCases)("renders wrapped $tool in chat and console", ({ tool, result, expectText }) => { |
| 47 | + expect(renderTool(tool, result, "chat")).toContain(expectText); |
| 48 | + expect(renderTool(tool, result, "console")).toContain(expectText); |
| 49 | + }); |
| 50 | + |
| 51 | + it("parses stringified payloads for chat card data paths", () => { |
| 52 | + expect( |
| 53 | + parseSqlToolResult(JSON.stringify(toolResultFixtures.run_sql_readonly.observationWrapped)), |
| 54 | + ).toMatchObject({ columns: ["total_orders"], row_count: 1 }); |
| 55 | + expect( |
| 56 | + parseSchemaToolResult(JSON.stringify(toolResultFixtures.inspect_schema.observationWrapped)), |
| 57 | + ).toMatchObject({ tables: [{ name: "orders" }] }); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe("failure presentation parity", () => { |
| 62 | + it("recognizes protocol and delivery errors", () => { |
| 63 | + const protocol = JSON.stringify(toolResultFixtures.protocolError); |
| 64 | + const delivery = JSON.stringify(toolResultFixtures.toolNotDelivered); |
| 65 | + expect(toolResultLooksLikeError(protocol)).toBe(true); |
| 66 | + expect(toolResultLooksLikeError(delivery)).toBe(true); |
| 67 | + expect(resolveToolFailurePresentation(protocol).title).toBe("Result sync failed"); |
| 68 | + expect(resolveToolFailurePresentation(delivery).title).toBe("Result not delivered"); |
| 69 | + }); |
| 70 | + |
| 71 | + it("renders shared failure UI for console and chat paths", () => { |
| 72 | + const protocol = JSON.stringify(toolResultFixtures.protocolError); |
| 73 | + const markup = renderToStaticMarkup( |
| 74 | + ToolFailureResult({ toolName: "run_sql_readonly", result: protocol }), |
| 75 | + ); |
| 76 | + expect(markup).toContain("Result sync failed"); |
| 77 | + expect(markup).toContain("run_sql_readonly"); |
| 78 | + expect(markup).not.toContain('"status": "error"'); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe("empty and malformed tool payloads", () => { |
| 83 | + it("shows a zero-row message for empty SQL results", () => { |
| 84 | + const markup = renderTool("run_sql_readonly", toolResultFixtures.run_sql_readonly.emptyRows); |
| 85 | + expect(markup).toContain("The query returned no rows."); |
| 86 | + expect(markup).toContain("Rows"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("keeps the middle-column SQL card on the shared result renderer", () => { |
| 90 | + const pageSource = readFileSync(new URL("../page.tsx", import.meta.url), "utf8"); |
| 91 | + const sqlCardSource = pageSource.slice( |
| 92 | + pageSource.indexOf("function SqlToolCard"), |
| 93 | + pageSource.indexOf("type SchemaColumn"), |
| 94 | + ); |
| 95 | + expect(sqlCardSource).toContain("<ToolFormattedResult"); |
| 96 | + expect(sqlCardSource).toContain("toolName={name}"); |
| 97 | + expect(sqlCardSource).toContain('variant="chat"'); |
| 98 | + }); |
| 99 | + |
| 100 | + it("returns null parsers for malformed schema payloads", () => { |
| 101 | + expect(parseSchemaToolResult(JSON.stringify({ datasource_id: "x" }))).toBeNull(); |
| 102 | + }); |
| 103 | +}); |
| 104 | + |
| 105 | +describe("trace-aligned tool result rendering", () => { |
| 106 | + it("formats workspace tool raw results the same way trace cards should", () => { |
| 107 | + const markup = renderToStaticMarkup( |
| 108 | + ToolFormattedResult({ |
| 109 | + toolName: "write_file", |
| 110 | + result: toolResultFixtures.write_file, |
| 111 | + variant: "console", |
| 112 | + showRawFallback: false, |
| 113 | + }), |
| 114 | + ); |
| 115 | + expect(markup).toContain("reports/summary.md"); |
| 116 | + }); |
| 117 | + |
| 118 | + it("uses shared failure UI for errored trace payloads", () => { |
| 119 | + const protocol = JSON.stringify(toolResultFixtures.protocolError); |
| 120 | + const markup = renderToStaticMarkup( |
| 121 | + ToolFailureResult({ toolName: "run_sql_readonly", result: protocol }), |
| 122 | + ); |
| 123 | + expect(markup).toContain("Result sync failed"); |
| 124 | + }); |
| 125 | + |
| 126 | + it("shows schema chips from wrapped restore payloads in timeline data", () => { |
| 127 | + let run = reduceLiveRunEvent(createInitialLiveRun(), { type: "RUN_STARTED" }); |
| 128 | + run = reduceLiveRunEvent(run, { |
| 129 | + type: "TOOL_CALL_START", |
| 130 | + toolCallId: "tc-schema-wrap", |
| 131 | + toolCallName: "inspect_schema", |
| 132 | + }); |
| 133 | + run = reduceLiveRunEvent(run, { |
| 134 | + type: "TOOL_CALL_RESULT", |
| 135 | + toolCallId: "tc-schema-wrap", |
| 136 | + toolCallName: "inspect_schema", |
| 137 | + result: JSON.stringify(toolResultFixtures.inspect_schema.observationWrapped), |
| 138 | + }); |
| 139 | + run = reduceLiveRunEvent(run, { type: "RUN_FINISHED" }); |
| 140 | + |
| 141 | + const entry = buildTraceTimeline(run).find((item) => item.toolCallId === "tc-schema-wrap"); |
| 142 | + expect(entry?.schemaTables?.[0]?.name).toBe("orders"); |
| 143 | + }); |
| 144 | +}); |
0 commit comments