Something went wrong. Try again.
Your thinking partner.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778import { describe, expect, test } from "bun:test";
import type { SDKMessage } from "@letta-ai/letta-agent-sdk/client";
import type { TranscriptItem } from "./model";import { applyStreamBatch, stabilizeTranscriptItems } from "./streamProjection";
describe("stream projection batching", () => { test("applies every event but projects once", () => { let applied = 0; let projected = 0; const snapshot = { value: 1 }; const result = applyStreamBatch( { apply: () => { applied++; } }, [{}, {}, {}] as SDKMessage[], snapshot, (current) => { projected++; return current; }, ); expect(applied).toBe(3); expect(projected).toBe(1); expect(result).toBe(snapshot); });
test("reuses unchanged rows and replaces changed rows", () => { const settled = { kind: "assistant", id: "a1", text: "done" } as TranscriptItem; const changing = { kind: "assistant", id: "a2", text: "hel", streaming: true } as TranscriptItem; const previous = new Map([[settled.id, settled], [changing.id, changing]]);
const result = stabilizeTranscriptItems(previous, [ { ...settled }, { ...changing, text: "hello" }, ]);
expect(result.items[0]).toBe(settled); expect(result.items[1]).not.toBe(changing); expect(result.items[1]).toMatchObject({ text: "hello" }); });
test("keeps a long settled transcript stable when only the live tail changes", () => { const rows = Array.from({ length: 1000 }, (_, index) => ({ kind: "assistant" as const, id: `a${index}`, text: `message ${index}`, ...(index === 999 ? { streaming: true } : {}), })) as TranscriptItem[]; const previous = new Map(rows.map((row) => [row.id, row])); const next = rows.map((row, index) => ({ ...row, ...(index === 999 ? { text: "updated live tail" } : {}), })) as TranscriptItem[];
const result = stabilizeTranscriptItems(previous, next);
expect(result.items.slice(0, -1).every((row, index) => row === rows[index])).toBe(true); expect(result.items[999]).not.toBe(rows[999]); });
test("memoizes unchanged channel metadata and replaces changed metadata", () => { const previous = { kind: "user", id: "u1", text: "hello", originLabel: "Discord · Ada", channelAttachments: "Image · one.png", channelReply: "Reply to Grace\nold", } as TranscriptItem; const stable = stabilizeTranscriptItems(new Map([[previous.id, previous]]), [{ ...previous }]); expect(stable.items[0]).toBe(previous);
const changed = stabilizeTranscriptItems(stable.byId, [{ ...previous, channelReply: "Reply to Grace\nnew" }]); expect(changed.items[0]).not.toBe(previous); expect(changed.items[0]).toMatchObject({ channelReply: "Reply to Grace\nnew" }); });});