tests / companion-cache.test.ts
import { describe, expect, it, vi } from "vitest";
import { collectCompanionSources, createCompanionCacheSearch, createPluginToolOpener } from "../src/swarm/companion-cache";
const PLUGIN = "mh825/telegram-rag-cache";
const ITEM = { itemId: "channel/42", title: "Cache report", excerpt: "Upstream evidence from the public post.",
sourceUrl: "https://t.me/example/42", cachedAt: "2026-11-25T10:00:00Z", publishedAt: "2026-11-25T09:00:00Z" };
const context = (signal = new AbortController().signal) => ({ signal, status: vi.fn(), warn: vi.fn() });
describe("native companion cache", () => {
it("calls only the approved cache search tool and keeps provenance when live feeds are empty", async () => {
const search = vi.fn().mockResolvedValue({ contractVersion: 1, items: [ITEM, ITEM] });
const refresh = vi.fn();
const dispose = vi.fn();
const pluginTools = vi.fn().mockResolvedValue({ tools: [
{ name: "refresh", implementation: refresh },
{ name: "search_cache", implementation: search },
], [Symbol.dispose]: dispose });
const runSearch = createCompanionCacheSearch(createPluginToolOpener({ plugins: { pluginTools } }));
const result = await collectCompanionSources({ topic: "public post", companionCachePlugins: [
"khtsly/persistent-memory", PLUGIN, PLUGIN, "mh825/rss-rag-cache",
], maxExternalToolCalls: 1 }, ["public post"], [], runSearch, context(), Date.now() + 3000);
expect(pluginTools.mock.calls).toEqual([[PLUGIN]]);
expect(search).toHaveBeenCalledWith({ query: "public post", limit: 5 }, expect.any(Object));
expect(refresh).not.toHaveBeenCalled();
expect(dispose).toHaveBeenCalledOnce();
expect(result.feedCount).toBe(1);
expect(result.sources).toEqual([expect.objectContaining({ url: ITEM.sourceUrl, published: "2026-11-25T09:00:00.000Z",
companionCache: { pluginId: PLUGIN, itemId: ITEM.itemId, cachedAt: "2026-11-25T10:00:00.000Z" } })]);
});
it("rejects incompatible contract data and scopes public results to the requested domains", async () => {
const pluginTools = vi.fn().mockResolvedValue({ tools: [{ name: "search_cache", implementation: () => ({
contractVersion: 2, items: [ITEM],
}) }] });
const runSearch = createCompanionCacheSearch(createPluginToolOpener({ plugins: { pluginTools } }));
await expect(runSearch(PLUGIN, { query: "post" }, context())).resolves.toMatchObject({ items: [], error: expect.stringMatching(/contract v1/) });
pluginTools.mockResolvedValue({ tools: [{ name: "search_cache", implementation: () => ({
contractVersion: 1, items: [ITEM, { ...ITEM, itemId: "other", sourceUrl: "http://127.0.0.1/private" }],
}) }] });
const result = await collectCompanionSources({ topic: "post", companionCachePlugins: [PLUGIN], allowedDomains: ["example.org"] },
["post"], [], runSearch, context(), Date.now() + 3000);
expect(result.sources).toEqual([]);
});
it("returns on cancellation and disposes a session that opens late", async () => {
const controller = new AbortController();
const dispose = vi.fn();
const implementation = vi.fn();
const session = { tools: [{ name: "search_cache", implementation }], [Symbol.dispose]: dispose };
let resolveSession!: (value: typeof session) => void;
const opening = new Promise<typeof session>((resolve) => { resolveSession = resolve; });
const runSearch = createCompanionCacheSearch(createPluginToolOpener({ plugins: { pluginTools: () => opening } }));
const result = runSearch(PLUGIN, { query: "post" }, context(controller.signal));
controller.abort();
await expect(result).resolves.toMatchObject({ items: [], error: "Timed out or cancelled" });
resolveSession(session);
await opening;
expect(implementation).not.toHaveBeenCalled();
expect(dispose).toHaveBeenCalledOnce();
});
it("disposes an opened session even if the tool ignores cancellation", async () => {
const controller = new AbortController();
const dispose = vi.fn();
let started!: () => void;
const running = new Promise<void>((resolve) => { started = resolve; });
const implementation = () => { started(); return new Promise(() => {}); };
const runSearch = createCompanionCacheSearch(createPluginToolOpener({ plugins: {
pluginTools: async () => ({ tools: [{ name: "search_cache", implementation }], [Symbol.dispose]: dispose }),
} }));
const result = runSearch(PLUGIN, { query: "post" }, context(controller.signal));
await running;
controller.abort();
await expect(result).resolves.toMatchObject({ items: [], error: "Timed out or cancelled" });
expect(dispose).toHaveBeenCalledOnce();
});
});