import { describe, expect, test } from "vitest"; import { EXCERPT_CHARS, makeExcerpt } from "./memory-excerpt"; const words = (n: number, w = "filler") => Array.from({ length: n }, () => w).join(" "); describe("makeExcerpt", () => { test("short content comes back whole, not truncated", () => { expect(makeExcerpt("a short fact")).toEqual({ excerpt: "a short fact", truncated: false }); }); test("collapses whitespace and newlines so the excerpt reads as one line", () => { expect(makeExcerpt("# Title\n\n- one\n- two\n").excerpt).toBe("# Title - one - two"); }); test("without a query, long content is cut from the start at a word boundary", () => { const { excerpt, truncated } = makeExcerpt(`opening line ${words(200)}`); expect(truncated).toBe(true); expect(excerpt.startsWith("opening line filler")).toBe(true); expect(excerpt.endsWith("…")).toBe(true); expect(excerpt.length).toBeLessThanOrEqual(EXCERPT_CHARS + 2); // Never splits a word: everything before the ellipsis is whole words. expect(excerpt.slice(0, -1).trim().split(" ").every((w) => ["opening", "line", "filler"].includes(w))).toBe(true); }); test("with a query, centres on the passage that matches it", () => { const content = `${words(150)} the kumquat marmalade recipe lives here ${words(150)}`; const { excerpt, truncated } = makeExcerpt(content, "kumquat marmalade"); expect(truncated).toBe(true); expect(excerpt).toContain("kumquat marmalade recipe"); expect(excerpt.startsWith("…")).toBe(true); expect(excerpt.endsWith("…")).toBe(true); }); test("prefers the window matching the most distinct query terms", () => { const content = `alpha ${words(100)} beta ${words(100)} alpha beta gamma together ${words(100)}`; expect(makeExcerpt(content, "alpha beta gamma").excerpt).toContain("alpha beta gamma together"); }); test("matching is case-insensitive and ignores stopwords", () => { // "the" would otherwise match at position 0 and win. const content = `the ${words(100)} WHP Launch plan ${words(100)}`; expect(makeExcerpt(content, "the whp launch").excerpt).toContain("WHP Launch plan"); }); test("falls back to the start when no query term appears (vector-only hit)", () => { const content = `opening line ${words(200)}`; expect(makeExcerpt(content, "zebra").excerpt.startsWith("opening line")).toBe(true); }); test("a match near the end still fills the window instead of returning a stub", () => { const content = `${words(200)} needle`; const { excerpt } = makeExcerpt(content, "needle"); expect(excerpt.endsWith("needle")).toBe(true); expect(excerpt.length).toBeGreaterThan(EXCERPT_CHARS - 20); }); // Regression from the live OLS memory: "WHP" and "partner" also appear // in an earlier sentence ("business partners … (1) WHP commercial // launch"), which tied the Founding Partner passage on raw term count // and won by coming first. Rare terms must outweigh common ones. test("weights rare query terms above ones that appear all over the memory", () => { const ols = 'OLS Digital LLC ("One Last Shot"): Josh\'s solo parent company (alongside a full-time SWE job, 10–20 hrs/week) for AnHonestHost (boutique hosting since 2014), WHP (Web Hosting Platform: containerized hosting with a Go control panel), Streamer Tools (TTRPG streaming toolkit, labor of love) and myTppy (NFC card plus link-in-bio). AI agents are treated as business partners: honest, direct, push back when something won\'t work.\n\n' + "Q1 2027 priorities: (1) WHP commercial launch early January 2027, goal 10 licensed installs at hosting providers; (2) AnHonestHost \"own your brand\" campaign for creators and small businesses, promoted by trusted creator friends on commission.\n\n" + 'WHP decisions (as of 2026-11-14): $20 or $25 per server (TBD), the same for physical or VM, no per-account tiers; includes a webmail client for Poste.io (hosts license Poste.io themselves, about $380/yr). x86 and ARM64 with identical features. Self-serve cPanel import built in (don\'t use "cPanel" in feature names, trademark). Founding Partner Program: first 10 hosts get 90 days free on 1 server, one-on-one onboarding (setup, test migrations, WHMCS integration) and a locked price; multi-server partners get the standard 30-day free period.'; const { excerpt } = makeExcerpt(ols, "WHP founding partner pricing"); expect(excerpt).toContain("Founding Partner Program"); // "pricing" should reach "locked price" in the same window. expect(excerpt).toContain("locked price"); }); test("matches simple inflections of a query word (pricing → price)", () => { const content = `${words(100)} we settled on a flat price per server ${words(100)}`; expect(makeExcerpt(content, "pricing").excerpt).toContain("flat price per server"); }); });