test / research-cache.test.js

import { test, after } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

const dataDir = mkdtempSync(join(tmpdir(), "rss-research-test-"));
process.env.RSS_RAG_DATA_DIR = dataDir;
const { getDb, closeDb } = await import("../lib/db.js");
const { cacheInfo, searchCache, getCachedItem } = await import("../lib/research-cache.js");
const db = getDb();
const now = Date.now();
const id = Number(db.prepare("INSERT INTO entries (guid, title, link, feed_url, pub_date, cached_at, content) VALUES (?, ?, ?, ?, ?, ?, ?)")
  .run("research-1", "Research article", "https://example.com/article", "https://example.com/feed", now, now, "Detailed orbital telescope evidence.").lastInsertRowid);
db.prepare("INSERT INTO chunks (entry_id, chunk_index, text) VALUES (?, 0, ?)").run(id, "Detailed orbital telescope evidence.");
after(() => { closeDb(); try { rmSync(dataDir, { recursive: true, force: true }); } catch {} });

test("research contract exposes stable IDs, source links, filters and bounded excerpts", async () => {
  const info = await cacheInfo();
  assert.equal(info.available, true);
  assert.equal(info.contractVersion, "1.0");
  assert.equal(info.itemCount, 1);
  const hits = await searchCache({ query: "telescope", source: "https://example.com/feed", since: new Date(now - 1000).toISOString(), limit: 100 });
  assert.equal(hits.length, 1);
  assert.equal(hits[0].pluginId, "mh825/rss-rag-cache");
  assert.equal(hits[0].itemId, String(id));
  assert.equal(hits[0].sourceUrl, "https://example.com/article");
  assert.match((await getCachedItem(hits[0].itemId)).excerpt, /telescope/);
  assert.deepEqual(await searchCache({ query: "telescope", source: "https://other.example/feed" }), []);
  assert.deepEqual(await searchCache({ query: "telescope", since: new Date(now + 1000).toISOString() }), []);
  assert.equal(await getCachedItem("999999"), null);
  await assert.rejects(searchCache({ query: "x", since: "not a date" }));
  await assert.rejects(getCachedItem("1 OR 1=1"));
});