tests / visited-cache.test.ts
tests / visited-cache.test.ts
import { afterEach, describe, expect, it, vi } from "vitest";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { VisitedPageCache } from "../src/swarm/visited-cache";
import { CrawledSource } from "../src/types";
const roots: string[] = [];
function cachePath(): string {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "swarm-cache-test-"));
roots.push(dir);
return path.join(dir, "cache.json");
}
afterEach(() => { vi.useRealTimers(); for (const dir of roots.splice(0)) fs.rmSync(dir, { recursive: true, force: true }); });
function page(url: string, text: string): CrawledSource {
return { url, finalUrl: url, title: "Renewable energy research", description: text, text, wordCount: 100, published: null, outlinks: [], sourceQuery: "energy", workerRole: "academic", workerLabel: "worker", domainScore: 80, freshnessScore: 50, tier: "academic", relevanceScore: 0.8, origin: "web" };
}
describe("visited page cache", () => {
it("saves and retrieves relevant pages without rediscovering their URLs", () => {
const file = cachePath();
const cache = new VisitedPageCache(30, file);
cache.markVisited(page("https://example.org/a", "Photovoltaic energy storage research"));
cache.flush();
const restored = new VisitedPageCache(30, file);
expect(restored.searchRecent("photovoltaic energy storage", 3)).toHaveLength(1);
expect(restored.searchRecent("marine biodiversity", 3)).toHaveLength(0);
});
it("does not reuse pages after the retention period", () => {
vi.useFakeTimers();
const cache = new VisitedPageCache(1, cachePath());
cache.markVisited(page("https://example.org/a", "Photovoltaic energy storage research"));
vi.advanceTimersByTime(2 * 86400000);
expect(cache.searchRecent("photovoltaic energy storage", 3)).toEqual([]);
});
it("does not blacklist a URL for being irrelevant to a different query", () => {
const cache = new VisitedPageCache(30, cachePath());
cache.markRejected("https://example.org/a", "very-off-topic");
expect(cache.isRejected("https://example.org/a")).toBe(false);
cache.markRejected("https://example.org/a", "HTTP 403");
expect(cache.isRejected("https://example.org/a")).toBe(true);
});
});
import { afterEach, describe, expect, it, vi } from "vitest";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { VisitedPageCache } from "../src/swarm/visited-cache";
import { CrawledSource } from "../src/types";
const roots: string[] = [];
function cachePath(): string {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "swarm-cache-test-"));
roots.push(dir);
return path.join(dir, "cache.json");
}
afterEach(() => { vi.useRealTimers(); for (const dir of roots.splice(0)) fs.rmSync(dir, { recursive: true, force: true }); });
function page(url: string, text: string): CrawledSource {
return { url, finalUrl: url, title: "Renewable energy research", description: text, text, wordCount: 100, published: null, outlinks: [], sourceQuery: "energy", workerRole: "academic", workerLabel: "worker", domainScore: 80, freshnessScore: 50, tier: "academic", relevanceScore: 0.8, origin: "web" };
}
describe("visited page cache", () => {
it("saves and retrieves relevant pages without rediscovering their URLs", () => {
const file = cachePath();
const cache = new VisitedPageCache(30, file);
cache.markVisited(page("https://example.org/a", "Photovoltaic energy storage research"));
cache.flush();
const restored = new VisitedPageCache(30, file);
expect(restored.searchRecent("photovoltaic energy storage", 3)).toHaveLength(1);
expect(restored.searchRecent("marine biodiversity", 3)).toHaveLength(0);
});
it("does not reuse pages after the retention period", () => {
vi.useFakeTimers();
const cache = new VisitedPageCache(1, cachePath());
cache.markVisited(page("https://example.org/a", "Photovoltaic energy storage research"));
vi.advanceTimersByTime(2 * 86400000);
expect(cache.searchRecent("photovoltaic energy storage", 3)).toEqual([]);
});
it("does not blacklist a URL for being irrelevant to a different query", () => {
const cache = new VisitedPageCache(30, cachePath());
cache.markRejected("https://example.org/a", "very-off-topic");
expect(cache.isRejected("https://example.org/a")).toBe(false);
cache.markRejected("https://example.org/a", "HTTP 403");
expect(cache.isRejected("https://example.org/a")).toBe(true);
});
});