import type { AdapterContext } from "@rakazo/adapter-kit"; import { describe, expect, it, vi } from "vitest"; import { appendMarkdown, MarkdownMemoryStore } from "./index.js"; const context: AdapterContext = { operationId: "read-memory", traceId: "read-memory", spaceId: "workspace-1", userId: "user-1", signal: new AbortController().signal, }; describe("memory store contract shape", () => { it("declares markdown portability", () => { const store = new MarkdownMemoryStore({} as never); expect(store.describe().capabilities.markdownPortable).toBe(true); }); it("reads the most recently updated documents first", async () => { const updatedAt = new Date("2026-08-16T10:00:00.000Z"); const findMany = vi .fn() .mockResolvedValue([ { id: "memory-1", path: "facts.md", content: "A fact", revision: 3, updatedAt }, ]); const store = new MarkdownMemoryStore({ memoryDocument: { findMany } } as never); await expect(store.read({ scope: "bot", botId: "bot-1" }, context)).resolves.toEqual({ documents: [ { id: "memory-1", path: "facts.md", content: "A fact", revision: 3, updatedAt: updatedAt.toISOString(), }, ], }); expect(findMany).toHaveBeenCalledWith({ where: { spaceId: "workspace-1", userId: "user-1", scope: "bot", botId: "bot-1", }, orderBy: [{ updatedAt: "desc" }, { path: "asc" }], }); }); }); describe("appendMarkdown", () => { it("adds the note below a heading or paragraph, and right below a list", () => { expect(appendMarkdown("# Chief\n\n", "- 2026-10-03: Prefers tea")).toBe( "# Chief\n\n- 2026-10-03: Prefers tea\n", ); expect(appendMarkdown("- a\n- b", "- c")).toBe("- a\n- b\n- c\n"); expect(appendMarkdown("- a\n more about a\n", "- c")).toBe("- a\n more about a\n- c\n"); expect(appendMarkdown("", "- first")).toBe("- first\n"); }); it("leaves the document unchanged for an empty note", () => { expect(appendMarkdown("# Chief\n", " ")).toBe("# Chief\n"); }); });