mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-07-02 11:01:36 +02:00
24e578db5f
Co-authored-by: Gregory Schier <gschier1990@gmail.com>
52 lines
1.4 KiB
TypeScript
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",
|
|
});
|
|
});
|
|
});
|