|
1 | 1 | import { readFileSync } from "node:fs"; |
| 2 | +import type { UIMessage } from "@ai-sdk/react"; |
2 | 3 | import { describe, expect, it } from "vitest"; |
| 4 | +import { blocksFor, winningInvestigationOccurrences } from "./DashboardAgentMessages"; |
3 | 5 | import { reuseWinners, sameOccurrences } from "./investigation-winners"; |
4 | 6 |
|
5 | 7 | const source = readFileSync(new URL("./DashboardAgentMessages.tsx", import.meta.url), "utf8"); |
@@ -47,3 +49,87 @@ describe("investigation winners identity", () => { |
47 | 49 | expect(source).not.toMatch(/=\s*winningInvestigationOccurrences\(stripped\)/); |
48 | 50 | }); |
49 | 51 | }); |
| 52 | + |
| 53 | +/** |
| 54 | + * The winner pass runs once per streamed token over the whole transcript, so it must |
| 55 | + * not touch report payloads. `output` is a counting getter because a report parse is |
| 56 | + * otherwise silent: it returns `null` on a bad payload rather than throwing. |
| 57 | + */ |
| 58 | +function countingReportPart(vm: unknown) { |
| 59 | + let reads = 0; |
| 60 | + const part = { |
| 61 | + type: "tool-get_report", |
| 62 | + state: "output-available", |
| 63 | + toolCallId: "toolcall_1", |
| 64 | + get output() { |
| 65 | + reads++; |
| 66 | + return { vm }; |
| 67 | + }, |
| 68 | + }; |
| 69 | + return { part: part as unknown as UIMessage["parts"][number], reads: () => reads }; |
| 70 | +} |
| 71 | + |
| 72 | +const VALID_VM = { |
| 73 | + title: "health", |
| 74 | + scope: "prod", |
| 75 | + period: "last 1h", |
| 76 | + generatedAt: "2026-07-27T10:15:00.000Z", |
| 77 | + windowMinutes: 60, |
| 78 | + summary: { severity: "ok", statements: [] }, |
| 79 | +}; |
| 80 | + |
| 81 | +describe("the winner pass does not parse report blocks", () => { |
| 82 | + it("leaves a report part's payload untouched", () => { |
| 83 | + const valid = countingReportPart(VALID_VM); |
| 84 | + // Would fail `reportBlockSchema`: no `generatedAt`, no `windowMinutes`. |
| 85 | + const invalid = countingReportPart({ title: "health" }); |
| 86 | + |
| 87 | + const messages = [ |
| 88 | + { id: "m1", role: "assistant", parts: [valid.part, invalid.part] }, |
| 89 | + ] as unknown as UIMessage[]; |
| 90 | + |
| 91 | + let winners: Map<string, string> | undefined; |
| 92 | + expect(() => (winners = winningInvestigationOccurrences(messages))).not.toThrow(); |
| 93 | + |
| 94 | + expect(winners!.size).toBe(0); |
| 95 | + expect(valid.reads()).toBe(0); |
| 96 | + expect(invalid.reads()).toBe(0); |
| 97 | + }); |
| 98 | + |
| 99 | + it("still parses the same part when the turn renders it", () => { |
| 100 | + const valid = countingReportPart(VALID_VM); |
| 101 | + const blocks = blocksFor(valid.part); |
| 102 | + |
| 103 | + expect(valid.reads()).toBeGreaterThan(0); |
| 104 | + expect(blocks).toHaveLength(1); |
| 105 | + expect((blocks![0] as { type: string }).type).toBe("report"); |
| 106 | + }); |
| 107 | + |
| 108 | + it("still finds investigation winners emitted by the view tools", () => { |
| 109 | + const messages = [ |
| 110 | + { |
| 111 | + id: "m1", |
| 112 | + role: "assistant", |
| 113 | + parts: [ |
| 114 | + countingReportPart(VALID_VM).part, |
| 115 | + { |
| 116 | + type: "tool-render_view", |
| 117 | + output: { blocks: [{ type: "investigation", id: "inv_1", revision: 0 }] }, |
| 118 | + }, |
| 119 | + ], |
| 120 | + }, |
| 121 | + { |
| 122 | + id: "m2", |
| 123 | + role: "assistant", |
| 124 | + parts: [ |
| 125 | + { |
| 126 | + type: "data-view", |
| 127 | + data: { blocks: [{ type: "investigation", id: "inv_1", revision: 1 }] }, |
| 128 | + }, |
| 129 | + ], |
| 130 | + }, |
| 131 | + ] as unknown as UIMessage[]; |
| 132 | + |
| 133 | + expect(winningInvestigationOccurrences(messages)).toEqual(new Map([["inv_1", "m2:0"]])); |
| 134 | + }); |
| 135 | +}); |
0 commit comments