history.test.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { describe, expect, test } from "bun:test"
  2. import { isDuplicateEntry, MAX_HISTORY_ENTRIES, parsePromptHistory, type PromptInfo } from "../../src/prompt/history"
  3. const entry = (input: string, parts: PromptInfo["parts"] = []): PromptInfo => ({ input, parts })
  4. describe("prompt history", () => {
  5. test("recovers valid JSONL entries around corruption", () => {
  6. expect(parsePromptHistory(`${JSON.stringify(entry("one"))}\nnot-json\n${JSON.stringify(entry("two"))}\n`)).toEqual([
  7. entry("one"),
  8. entry("two"),
  9. ])
  10. })
  11. test("retains only the newest entries", () => {
  12. const input = Array.from({ length: MAX_HISTORY_ENTRIES + 5 }, (_, index) =>
  13. JSON.stringify(entry(String(index))),
  14. ).join("\n")
  15. const result = parsePromptHistory(input)
  16. expect(result).toHaveLength(MAX_HISTORY_ENTRIES)
  17. expect(result[0]?.input).toBe("5")
  18. })
  19. test("dedupes only identical consecutive entries", () => {
  20. expect(isDuplicateEntry(undefined, entry("hello"))).toBe(false)
  21. expect(isDuplicateEntry(entry("hello"), entry("hello"))).toBe(true)
  22. expect(isDuplicateEntry(entry("foo"), entry("bar"))).toBe(false)
  23. expect(isDuplicateEntry({ ...entry("ls"), mode: "normal" }, { ...entry("ls"), mode: "shell" })).toBe(false)
  24. })
  25. test("does not dedupe entries with different parts", () => {
  26. const a = entry("describe this", [
  27. { type: "file", mime: "image/png", filename: "a.png", url: "data:image/png;base64,AAA" },
  28. ])
  29. const b = entry("describe this", [
  30. { type: "file", mime: "image/png", filename: "b.png", url: "data:image/png;base64,BBB" },
  31. ])
  32. expect(isDuplicateEntry(a, b)).toBe(false)
  33. })
  34. })