|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { withEmptyHint } from "./empty-hint.js"; |
| 3 | +import type { ToolResult } from "../tools/types.js"; |
| 4 | + |
| 5 | +const empty = (hint?: string): ToolResult => ({ rows: [], columns: ["a"], hint }); |
| 6 | +const filled = (hint?: string): ToolResult => ({ rows: [{ a: "1" }], columns: ["a"], hint }); |
| 7 | + |
| 8 | +// Riproduce la risoluzione del path MCP (server.ts formatResult, escluso DEFAULT_EMPTY) |
| 9 | +const mcpHint = (result: ToolResult, emptyHint?: string) => result.hint ?? emptyHint; |
| 10 | + |
| 11 | +describe("withEmptyHint", () => { |
| 12 | + it("risultato vuoto senza hint dinamico usa l'emptyHint statico", () => { |
| 13 | + const out = withEmptyHint(empty(), "STATICO"); |
| 14 | + expect(out.hint).toBe("STATICO"); |
| 15 | + expect(out.rows).toEqual([]); |
| 16 | + }); |
| 17 | + |
| 18 | + it("l'hint dinamico ha precedenza sull'emptyHint statico", () => { |
| 19 | + const out = withEmptyHint(empty("DINAMICO"), "STATICO"); |
| 20 | + expect(out.hint).toBe("DINAMICO"); |
| 21 | + }); |
| 22 | + |
| 23 | + it("risultato non vuoto non riceve alcun hint", () => { |
| 24 | + const out = withEmptyHint(filled(), "STATICO"); |
| 25 | + expect(out.hint).toBeUndefined(); |
| 26 | + }); |
| 27 | + |
| 28 | + it("vuoto senza hint dinamico né emptyHint non produce hint", () => { |
| 29 | + const out = withEmptyHint(empty(), undefined); |
| 30 | + expect(out.hint).toBeUndefined(); |
| 31 | + }); |
| 32 | + |
| 33 | + it("non muta l'oggetto risultato in ingresso", () => { |
| 34 | + const input = empty(); |
| 35 | + const out = withEmptyHint(input, "STATICO"); |
| 36 | + expect(input.hint).toBeUndefined(); |
| 37 | + expect(out).not.toBe(input); |
| 38 | + }); |
| 39 | + |
| 40 | + it("hint dinamico stringa vuota (falsy ma non nullish) non viene sovrascritto", () => { |
| 41 | + const out = withEmptyHint(empty(""), "STATICO"); |
| 42 | + expect(out.hint).toBe(""); |
| 43 | + }); |
| 44 | + |
| 45 | + it("parità con il path MCP (result.hint ?? emptyHint)", () => { |
| 46 | + const cases: Array<[ToolResult, string | undefined]> = [ |
| 47 | + [empty(), "STATICO"], |
| 48 | + [empty("DINAMICO"), "STATICO"], |
| 49 | + [empty(""), "STATICO"], |
| 50 | + [empty(), undefined], |
| 51 | + ]; |
| 52 | + for (const [result, emptyHint] of cases) { |
| 53 | + expect(withEmptyHint(result, emptyHint).hint).toBe(mcpHint(result, emptyHint)); |
| 54 | + } |
| 55 | + }); |
| 56 | +}); |
0 commit comments