Files
yaak-mountain-loop/crates/yaak-sse/summary.test.ts
T
baofeidyz 24e578db5f Add SSE response summary helpers (#466)
Co-authored-by: Gregory Schier <gschier1990@gmail.com>
2026-07-01 12:33:03 -07:00

52 lines
1.4 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { computeSseSummary, extractSseValueAtPath } from "./summary";
describe("extractSseValueAtPath", () => {
it("supports simple paths", () => {
expect(
extractSseValueAtPath(
JSON.stringify({ choices: [{ delta: { content: "hello" } }] }),
"$.choices[0].delta.content",
),
).toBe("hello");
});
it("supports full JSONPath expressions", () => {
expect(
extractSseValueAtPath(
JSON.stringify({
choices: [
{ delta: { role: "assistant" } },
{ delta: { content: "hello" } },
{ delta: { content: " world" } },
],
}),
"$.choices[*].delta.content",
),
).toBe("hello world");
});
it("returns null when a JSONPath expression has no matches", () => {
expect(extractSseValueAtPath(JSON.stringify({ delta: {} }), "$.delta.text")).toBeNull();
});
});
describe("computeSseSummary", () => {
it("concatenates JSONPath matches across SSE messages", () => {
expect(
computeSseSummary(
[
`data: ${JSON.stringify({ choices: [{ delta: { content: "hello" } }] })}`,
"",
`data: ${JSON.stringify({ choices: [{ delta: { content: " world" } }] })}`,
"",
].join("\n"),
"$.choices[*].delta.content",
),
).toEqual({
fragmentCount: 2,
summary: "hello world",
});
});
});