mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-18 14:35:55 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Task text contains links to pull requests and other work. > - A link has useful status that should be visible when a task opens. > - The current cache lives in the server process and the browser. > - A new browser or server restart has no stored status to show. > - This pull request stores link status in the database and reads it in batches. > - Idle page work updates stale status without a hover or a delay to task data. ## Linked Issues or Issue Description **Problem or motivation** PR status is unknown in a new browser until the user points to each link. The process cache cannot supply saved status after a server restart. Related work: #20593 and #20654. A search found no other PR for database link caching. **Proposed solution** Store status by company, viewer and canonical resource URL. Read saved status in one indexed batch query. After document load and core request settlement, use browser idle time to update missing or stale status. Use the same query for hover and idle work. **Alternatives considered** Adding provider calls to task reads would delay task loading. Company-wide reuse of personal GitHub results would cross viewer access boundaries. A periodic server worker would create work when no page needs the result. **Roadmap alignment** This extends the shipped rich-link feature. It does not duplicate a roadmap project or add a new provider integration. ## What Changed - Add an additive, repeat-safe `link_previews` migration. Store channel, resource kind, free-form status, JSON metadata and freshness. The table has no task relationship or GitHub status enum. - Bound each company/viewer cache to 1,000 rows across concurrent server instances. Remove rows expired for over 30 days during writes. Limit provider lookups to 600 requests per minute per scope/process. Keep cached batch reads available during backoff. - Add a database store owned by the link service. Reuse canonical results across tasks. Keep company and viewer isolation. Save hover and idle results. Preserve last known status during an outage and cache unavailable results. - Add a board-only batch GET endpoint, including viewer-role access. It reads at most 50 URLs with one indexed select and makes no provider calls. Both endpoints use private, no-store HTTP responses. - Add a page coordinator for mounted rich links. Reuse browser data, share pending reads, and defer provider work until document load, core request settlement and browser idle time. Limit automatic work to 200 unique links and three requests in flight. Remove queued work when links leave the page. Keep encoded batch URLs below 6 KB. - Keep exact navigation URLs, status styling, anchored cards and keyboard access. Add a page-load Storybook case and browser regression. The link service owns cache policy and provider calls. The browser coordinator owns page timing. The shared contract adds only the batch limit and response shape. The database adds storage because process and browser caches cannot serve a new browser session. Plugin resolver registration is outside this change; storage accepts future provider channels and status values. ## Verification - **All 93 affected tests pass:** 40 link service, real PostgreSQL and HTTP cases, plus 53 OpenAPI cases. Two SQL regressions and one HTTP case failed before the storage repair. Concurrent writers now retain at most 1,000 rows per company/viewer; cleanup removes long-expired rows and keeps recent stale status. Request backoff preserves cached reads and isolates companies/viewers. The earlier eight concurrent hydration regressions also pass. Focused store/service/unit types pass. Commands: `pnpm exec vitest run server/src/__tests__/link-previews.test.ts server/src/__tests__/link-preview-store.integration.test.ts server/src/__tests__/link-preview-routes.test.ts --maxWorkers=1` and `pnpm exec vitest run server/src/__tests__/openapi-routes.test.ts --maxWorkers=1`. - **All 50 affected server tests passed before the later server-only repairs** on the master-integrated source: 20 recovery/deadline cases, one authenticated review-counts HTTP case, 10 link-service cases, 13 real PostgreSQL cache cases and six link-route cases. Command: `pnpm exec vitest run server/src/__tests__/issue-recovery-deadlines.test.ts server/src/__tests__/tool-connection-review-counts-http.test.ts server/src/__tests__/link-previews.test.ts server/src/__tests__/link-preview-store.integration.test.ts server/src/__tests__/link-preview-routes.test.ts --maxWorkers=1`. - Reproduced the CI watchdog assertion locally twice before the test repair. Ordinary assignment recovery can win `wakeReason` while the watchdog wake coalesces into the same successful run with its full mandate. The test now verifies the persisted watchdog identity, watched issue and stopped fingerprint, plus exactly one review run. All 21 recovery/HTTP cases passed before integrating master; all 50 affected cases pass after integration. No runtime behavior was changed for this repair. - **Fresh static Storybook build and all 22 browser cases pass**, with two workers and retries disabled. They cover states, anchored hover, keyboard access, exact navigation, duplicate requests, reload persistence, expiry and saved-status hydration followed by one idle refresh. The first browser attempt could not launch because this session lacked Chromium; installing the expected browser resolved setup. That attempt had no behavioral verdict. - The cache repair remains covered by four PostgreSQL red/green regressions for expired memory and newer durable status during provider failure/timeout, plus check-time precedence and failed-read recovery. Earlier UI/database/focused server type and migration safety/compatibility/replay checks remain unchanged-input evidence. - Master `77ddf260bf9a3de3dcb8456a60f4f809d67401fd` is integrated cleanly. Link migration 0489 and its snapshot chain are unchanged. SDK source validation checks 545 inputs with no stale source identities or changed runtime/lock pins. Whitespace validation passes. - `pnpm verify --explain` returns exit 2 and requires broad CI. No full local suite or deployed acceptance is claimed. Earlier local full-server type checks exceeded memory; current-head CI supplies broad type/build/test coverage. - [Previous CI 43900336895](https://github.com/paperclipai/paperclip/actions/runs/43900336895) passed full types/build but failed on embedded PostgreSQL startup (`ECONNREFUSED`), the watchdog assertion above, and unrelated plan-task browser cleanup (activity-log foreign key on company deletion). One failed-jobs retry was requested. Its browser shard 7 now passes without code changes; the exact deletion-race cause remains unproven. The watchdog assertion is repaired in this revision. The previous failures remain recorded, not counted as an overall pass. - [Prior CI 43905482848](https://github.com/paperclipai/paperclip/actions/runs/43905482848) passed full types, build and all eight browser shards. Several test jobs were cancelled; the run has no overall pass. Its concurrent update finding is repaired. [Next CI 43909197530](https://github.com/paperclipai/paperclip/actions/runs/43909197530) also passed types, build and all browser shards, but several server jobs were cancelled. Greptile and Paperclip Review gave that revision 5/5. Its security scan found unbounded retained cache rows. The quota, retention and request-limit repair above addresses that finding. All ten review threads are resolved. - [Current CI 43912947278](https://github.com/paperclipai/paperclip/actions/runs/43912947278), fresh code reviews and a fresh security scan are pending on `335d89ef1668dcdeb66bb2059512a7c2da29e273`. Auto-merge is disabled until the current revision clears its gates. Published head: `335d89ef1668dcdeb66bb2059512a7c2da29e273`. Local head: `b37b87c704a2ba8a65233c689a6a743f4011ce14`. Both have tree `643ebd19b9729b1e6a6bb695442269c3db32be3a`; GitHub tree creation verified equality. ## Risks - Each company/viewer retains at most 1,000 rows. Writes remove excess rows and entries whose freshness expired over 30 days ago. Inactive scopes remain bounded and are cleaned on the next write. Company deletion removes all rows. No periodic worker runs. Scope writes use a transaction lock; the existing deadline and concurrency budget cover that work. - Last known status can be stale until idle or hover work completes. Provider failures keep that status through the negative-cache window. - Automatic enrichment adds bounded provider traffic. It runs only for mounted supported links, once per mount, with the existing timeout and server concurrency limit. - Cache storage is viewer-scoped because provider results can be private. New viewers load their own results. This prevents reuse across users with different repository access. ## Model Used OpenAI Codex, based on GPT-6, with reasoning, repository tools, code execution and browser automation. The runtime does not expose the exact serving model ID or context-window size. No subagents were used. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing> Co-authored-by: Paperclip <Paperclip-Paperclip@users.noreply.github.com>
254 lines
16 KiB
TypeScript
254 lines
16 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import { readFile } from "node:fs/promises";
|
|
import { companies, linkPreviews } from "@paperclipai/db";
|
|
import { recognizeLinkPreview } from "@paperclipai/shared";
|
|
import { sql } from "drizzle-orm";
|
|
import { expect, it, vi } from "vitest";
|
|
import { describeEmbeddedPostgres, useEmbeddedPostgres } from "./helpers/route-test-harness.js";
|
|
import { createLinkPreviewStore } from "../services/link-preview-store.js";
|
|
import { createLinkPreviewService } from "../services/link-previews.js";
|
|
|
|
const url = "https://github.com/acme/web/pull/42";
|
|
const target = recognizeLinkPreview(url)!;
|
|
const preview = { target, title: "Durable PR", state: "merged" as const, author: "alice", updatedAt: null };
|
|
|
|
describeEmbeddedPostgres("durable link preview cache", () => {
|
|
const context = useEmbeddedPostgres("link-previews-");
|
|
async function fixture() {
|
|
const companyId = randomUUID();
|
|
await context.db.insert(companies).values({ id: companyId, name: "Link cache", issuePrefix: companyId });
|
|
return { companyId, userId: "alice" };
|
|
}
|
|
it("bounds retained rows across concurrent store instances and isolates their quotas", async () => {
|
|
const scope = await fixture();
|
|
const other = { ...scope, userId: "bob" };
|
|
const now = Date.now();
|
|
const seed = Array.from({ length: 1_000 }, (_, n) => ({
|
|
companyId: scope.companyId, viewerId: scope.userId, channel: "github", resourceKind: "pull_request",
|
|
canonicalUrl: url + n, status: null, metadata: {}, checkedAt: new Date(now - 1_000 + n), expiresAt: new Date(now + 60_000),
|
|
}));
|
|
await context.db.insert(linkPreviews).values([...seed, { ...seed[0], viewerId: other.userId }]);
|
|
const first = createLinkPreviewStore(context.db);
|
|
const second = createLinkPreviewStore(context.db);
|
|
await Promise.all(Array.from({ length: 8 }, (_, n) => (n % 2 ? first : second).write(scope,
|
|
recognizeLinkPreview(url + (1_000 + n))!, { result: { preview: null, maxAgeMs: 60_000 }, expiresAt: now + 60_000 }, now + n)));
|
|
const rows = await context.db.select().from(linkPreviews).where(sql`${linkPreviews.companyId} = ${scope.companyId}`);
|
|
expect(rows.filter(row => row.viewerId === scope.userId)).toHaveLength(1_000);
|
|
expect(rows.filter(row => row.viewerId === other.userId)).toHaveLength(1);
|
|
const saved = await first.read(scope, [recognizeLinkPreview(url + "0")!, recognizeLinkPreview(url + "1007")!]);
|
|
expect(saved.has(url + "0")).toBe(false);
|
|
expect(saved.has(url + "1007")).toBe(true);
|
|
});
|
|
it("removes long-expired rows on writes and preserves recent stale status", async () => {
|
|
const scope = await fixture();
|
|
const now = Date.now();
|
|
const expired = now - 31 * 24 * 60 * 60_000;
|
|
await context.db.insert(linkPreviews).values([
|
|
{ companyId: scope.companyId, viewerId: scope.userId, channel: "github", resourceKind: "pull_request", canonicalUrl: url,
|
|
status: "merged", metadata: { title: preview.title }, checkedAt: new Date(expired - 60_000), expiresAt: new Date(expired) },
|
|
{ companyId: scope.companyId, viewerId: scope.userId, channel: "github", resourceKind: "pull_request", canonicalUrl: url + "1",
|
|
status: "merged", metadata: { title: preview.title }, checkedAt: new Date(now - 120_000), expiresAt: new Date(now - 60_000) },
|
|
]);
|
|
const store = createLinkPreviewStore(context.db);
|
|
await store.write(scope, recognizeLinkPreview(url + "2")!, { result: { preview: null, maxAgeMs: 60_000 }, expiresAt: now + 60_000 }, now);
|
|
const saved = await store.read(scope, [target, recognizeLinkPreview(url + "1")!, recognizeLinkPreview(url + "2")!]);
|
|
expect(saved.has(url)).toBe(false);
|
|
expect(saved.get(url + "1")?.result.preview?.state).toBe("merged");
|
|
expect(saved.has(url + "2")).toBe(true);
|
|
});
|
|
it.each(["failure", "success", "timeout", "stale timeout"])("keeps another replica's concurrent hydration after provider %s", async (settlement) => {
|
|
const scope = await fixture();
|
|
const store = createLinkPreviewStore(context.db);
|
|
let now = 1_000;
|
|
const oldPreview = { ...preview, state: "open" as const };
|
|
await store.write(scope, target, { result: { preview: oldPreview, maxAgeMs: 1_000 }, expiresAt: 2_000 }, now);
|
|
let finish!: (value: { preview: typeof oldPreview | null; maxAgeMs: number }) => void;
|
|
let started!: () => void;
|
|
const providerStarted = new Promise<void>(done => { started = done; });
|
|
const resolve = vi.fn(() => new Promise<{ preview: typeof oldPreview | null; maxAgeMs: number }>(done => { finish = done; started(); }));
|
|
const write = vi.fn(store.write);
|
|
const first = createLinkPreviewService([{ key: "github", resolve }], { store: { ...store, write }, now: () => now, timeoutMs: 1_000 });
|
|
await first.cached(scope, [url]);
|
|
now = 3_000;
|
|
const pending = first.get(scope, url);
|
|
await providerStarted;
|
|
now = 4_000;
|
|
const expiresAt = settlement === "stale timeout" ? 3_500 : 34_000;
|
|
await store.write(scope, target, { result: { preview, maxAgeMs: 30_000 }, expiresAt }, now);
|
|
expect((await first.cached(scope, [url])).results[url].preview).toEqual(preview);
|
|
if (!settlement.includes("timeout")) finish({ preview: settlement === "success" ? oldPreview : null, maxAgeMs: 60_000 });
|
|
expect(await pending).toEqual({ preview, maxAgeMs: Math.max(0, expiresAt - now) });
|
|
// A second batch checks that the process did not retain the older status.
|
|
expect((await first.cached(scope, [url])).results[url].preview).toEqual(preview);
|
|
const saved = (await store.read(scope, [target])).get(url)!;
|
|
expect(saved.result.preview).toEqual(preview);
|
|
expect(saved.checkedAt).toBe(4_000);
|
|
expect(saved.expiresAt).toBe(expiresAt);
|
|
expect(write).not.toHaveBeenCalled();
|
|
finish({ preview: null, maxAgeMs: 60_000 });
|
|
});
|
|
it.each(["open", "negative"])("rechecks expired %s memory before calling the provider", async (initialState) => {
|
|
const scope = await fixture();
|
|
const store = createLinkPreviewStore(context.db);
|
|
let now = 1_000;
|
|
const resolve = vi.fn(async () => ({ preview: initialState === "open" ? { ...preview, state: "open" as const } : null, maxAgeMs: 60_000 }));
|
|
const first = createLinkPreviewService([{ key: "github", resolve }], { store, now: () => now });
|
|
await first.get(scope, url);
|
|
now = 62_000;
|
|
const other = createLinkPreviewService([{ key: "github", resolve: async () => ({ preview, maxAgeMs: 60_000 }) }], { store, now: () => now });
|
|
await other.get(scope, url);
|
|
resolve.mockRejectedValue(new Error("provider unavailable"));
|
|
|
|
expect((await first.get(scope, url)).preview).toEqual(preview);
|
|
expect(resolve).toHaveBeenCalledTimes(1);
|
|
const saved = (await store.read(scope, [target])).get(url)!;
|
|
expect(saved.result.preview).toEqual(preview);
|
|
expect(saved.expiresAt).toBe(122_000);
|
|
});
|
|
it.each(["failure", "timeout"])("retains the newest stale stored preview on provider %s", async (failure) => {
|
|
const scope = await fixture();
|
|
const store = createLinkPreviewStore(context.db);
|
|
let now = 1_000;
|
|
let requests = 0;
|
|
const resolve = async (_target: import("@paperclipai/shared").LinkPreviewTarget, _scope: import("../services/link-previews.js").LinkPreviewScope, signal: AbortSignal) => {
|
|
if (++requests === 1) return { preview: { ...preview, state: "open" as const }, maxAgeMs: 60_000 };
|
|
if (failure === "failure") throw new Error("provider unavailable");
|
|
return new Promise<never>((_done, reject) => signal.addEventListener("abort", () => reject(new Error("timeout"))));
|
|
};
|
|
const first = createLinkPreviewService([{ key: "github", resolve }], { store, now: () => now, timeoutMs: 100 });
|
|
await first.get(scope, url);
|
|
now = 62_000;
|
|
await store.write(scope, target, { result: { preview, maxAgeMs: 1_000 }, expiresAt: 63_000 }, now);
|
|
now = 64_000;
|
|
expect((await first.get(scope, url)).preview).toEqual(preview);
|
|
await vi.waitFor(async () => expect((await store.read(scope, [target])).get(url)?.expiresAt).toBe(124_000));
|
|
expect((await store.read(scope, [target])).get(url)?.result.preview).toEqual(preview);
|
|
});
|
|
it("compares check times rather than cache lifetimes when hydrating", async () => {
|
|
const scope = await fixture();
|
|
const store = createLinkPreviewStore(context.db);
|
|
let now = 1_000;
|
|
await store.write(scope, target, { result: { preview: { ...preview, state: "open" }, maxAgeMs: 60_000 }, expiresAt: 61_000 }, now);
|
|
const service = createLinkPreviewService([{ key: "github", resolve: async () => ({ preview: null, maxAgeMs: 60_000 }) }], { store, now: () => now });
|
|
await service.cached(scope, [url]);
|
|
await store.write(scope, target, { result: { preview, maxAgeMs: 1_000 }, expiresAt: 4_000 }, 3_000);
|
|
now = 5_000;
|
|
expect((await service.cached(scope, [url])).results[url]).toEqual({ preview, maxAgeMs: 0 });
|
|
});
|
|
it("keeps a failed durable read's fallback local and accepts recovered stored status", async () => {
|
|
const scope = await fixture();
|
|
const store = createLinkPreviewStore(context.db);
|
|
const read = vi.fn(store.read);
|
|
let now = 1_000;
|
|
const resolve = vi.fn(async () => ({ preview: { ...preview, state: "open" as const }, maxAgeMs: 60_000 }));
|
|
const first = createLinkPreviewService([{ key: "github", resolve }], { store: { ...store, read }, now: () => now });
|
|
await first.get(scope, url);
|
|
now = 62_000;
|
|
await store.write(scope, target, { result: { preview, maxAgeMs: 60_000 }, expiresAt: 122_000 }, now);
|
|
now = 64_000;
|
|
read.mockRejectedValueOnce(new Error("temporary database outage"));
|
|
resolve.mockRejectedValueOnce(new Error("provider unavailable"));
|
|
expect((await first.get(scope, url)).preview?.state).toBe("open");
|
|
const saved = (await store.read(scope, [target])).get(url)!;
|
|
expect(saved.result.preview).toEqual(preview);
|
|
expect(saved.expiresAt).toBe(122_000);
|
|
expect((await first.cached(scope, [url])).results[url].preview).toEqual(preview);
|
|
});
|
|
it.each(["missing", "failed"])("reads another replica's saved status after a %s batch read", async (initialRead) => {
|
|
const scope = await fixture();
|
|
const store = createLinkPreviewStore(context.db);
|
|
const read = vi.fn(store.read);
|
|
if (initialRead === "failed") read.mockRejectedValueOnce(new Error("temporary database outage"));
|
|
const write = vi.fn(store.write);
|
|
const resolve = vi.fn(async () => { throw new Error("provider unavailable"); });
|
|
const first = createLinkPreviewService([{ key: "github", resolve }], { store: { read, write } });
|
|
expect(await first.cached(scope, [url])).toEqual({ results: {} });
|
|
|
|
const other = createLinkPreviewService([{ key: "github", resolve: async () => ({ preview, maxAgeMs: 60_000 }) }], { store });
|
|
await other.get(scope, url);
|
|
expect((await first.get(scope, url)).preview).toEqual(preview);
|
|
expect(read).toHaveBeenCalledTimes(2);
|
|
expect(resolve).not.toHaveBeenCalled();
|
|
expect(write).not.toHaveBeenCalled();
|
|
expect((await store.read(scope, [target])).get(url)?.result.preview).toEqual(preview);
|
|
});
|
|
it("survives service recreation, reuses canonical URLs across tasks, and isolates viewers/companies", async () => {
|
|
const scope = await fixture();
|
|
const store = createLinkPreviewStore(context.db);
|
|
const resolve = vi.fn(async () => ({ preview, maxAgeMs: 60_000 }));
|
|
const make = () => createLinkPreviewService([{ key: "github", resolve }], { store });
|
|
await make().get(scope, url);
|
|
const restored = await make().cached(scope, [url, "https://www.github.com/ACME/WEB/pull/42/files#review"]);
|
|
expect(Object.keys(restored.results)).toEqual([url]);
|
|
expect(restored.results[url].preview).toMatchObject({ title: preview.title, state: "merged", target: { canonicalUrl: url } });
|
|
expect(restored.results[url].maxAgeMs).toBeGreaterThan(0);
|
|
await make().get(scope, url);
|
|
expect(resolve).toHaveBeenCalledTimes(1);
|
|
expect(await make().cached({ ...scope, userId: "bob" }, [url])).toEqual({ results: {} });
|
|
expect(await make().cached(await fixture(), [url])).toEqual({ results: {} });
|
|
});
|
|
it("returns stale status immediately, refreshes on demand, and persists negative freshness", async () => {
|
|
const scope = await fixture();
|
|
const store = createLinkPreviewStore(context.db);
|
|
let now = 1_000;
|
|
const resolve = vi.fn(async () => ({ preview, maxAgeMs: 60_000 }));
|
|
const make = () => createLinkPreviewService([{ key: "github", resolve }], { store, now: () => now });
|
|
await make().get(scope, url);
|
|
now += 61_000;
|
|
expect((await make().cached(scope, [url])).results[url]).toEqual({ preview, maxAgeMs: 0 });
|
|
expect(resolve).toHaveBeenCalledTimes(1);
|
|
resolve.mockRejectedValueOnce(new Error("outage"));
|
|
expect((await make().get(scope, url)).preview).toEqual(preview);
|
|
await make().get(scope, url);
|
|
expect(resolve).toHaveBeenCalledTimes(2);
|
|
const missing = url + "1";
|
|
resolve.mockRejectedValueOnce(new Error("private"));
|
|
await make().get(scope, missing);
|
|
await make().get(scope, missing);
|
|
expect(resolve).toHaveBeenCalledTimes(3);
|
|
expect((await make().cached(scope, [missing])).results[missing].preview).toBeNull();
|
|
});
|
|
it("persists provider timeout freshness across service restarts without extending the response", async () => {
|
|
const scope = await fixture();
|
|
const database = createLinkPreviewStore(context.db);
|
|
let saved!: () => void;
|
|
const persisted = new Promise<void>(done => { saved = done; });
|
|
const store = { ...database, write: async (...args: Parameters<typeof database.write>) => { await database.write(...args); saved(); } };
|
|
const resolve = vi.fn((_target: import("@paperclipai/shared").LinkPreviewTarget, _scope: import("../services/link-previews.js").LinkPreviewScope, signal: AbortSignal) => new Promise<never>((_done, reject) => {
|
|
signal.addEventListener("abort", () => reject(new Error("timeout")));
|
|
}));
|
|
const first = createLinkPreviewService([{ key: "github", resolve }], { store, timeoutMs: 100 });
|
|
expect((await first.get(scope, url)).preview).toBeNull();
|
|
await persisted;
|
|
const restarted = createLinkPreviewService([{ key: "github", resolve }], { store });
|
|
expect((await restarted.get(scope, url)).preview).toBeNull();
|
|
expect(resolve).toHaveBeenCalledTimes(1);
|
|
expect((await restarted.cached(scope, [url])).results[url].maxAgeMs).toBeGreaterThan(0);
|
|
});
|
|
it("reads a 50-link page in one indexed select and does not require task relationships", async () => {
|
|
const scope = await fixture();
|
|
const store = createLinkPreviewStore(context.db);
|
|
const targets = Array.from({ length: 50 }, (_, n) => recognizeLinkPreview(url + n)!);
|
|
await Promise.all(targets.map(t => store.write(scope, t, { result: { preview: { ...preview, target: t }, maxAgeMs: 60_000 }, expiresAt: 61_000 }, 1_000)));
|
|
const select = vi.spyOn(context.db, "select");
|
|
expect((await store.read(scope, targets)).size).toBe(50);
|
|
expect(select).toHaveBeenCalledTimes(1);
|
|
select.mockRestore();
|
|
const plan = await context.db.execute(sql`EXPLAIN SELECT * FROM link_previews WHERE company_id = ${scope.companyId} AND viewer_id = 'alice' AND canonical_url IN (${sql.join(targets.map(t => sql`${t.canonicalUrl}`), sql`, `)})`);
|
|
expect(JSON.stringify(plan)).toContain("link_previews_scope_url_uq");
|
|
});
|
|
it("supports other channels/statuses and older writers cannot replace newer results", async () => {
|
|
const scope = await fixture();
|
|
const store = createLinkPreviewStore(context.db);
|
|
const deployment = { ...target, provider: "vercel", kind: "deployment", canonicalUrl: "https://vercel.com/acme/build/123" };
|
|
await context.db.insert(linkPreviews).values({ companyId: scope.companyId, viewerId: scope.userId, channel: deployment.provider,
|
|
resourceKind: deployment.kind, canonicalUrl: deployment.canonicalUrl, status: "deployed", metadata: { deploymentId: "123", region: "iad1" }, checkedAt: new Date(), expiresAt: new Date() });
|
|
await store.write(scope, target, { result: { preview, maxAgeMs: 60_000 }, expiresAt: 65_000 }, 5_000);
|
|
await store.write(scope, target, { result: { preview: { ...preview, state: "open" }, maxAgeMs: 60_000 }, expiresAt: 61_000 }, 1_000);
|
|
expect((await store.read(scope, [target])).get(url)?.result.preview?.state).toBe("merged");
|
|
const migration = await readFile(new URL("../../../packages/db/src/migrations/0489_real_sage.sql", import.meta.url), "utf8");
|
|
for (const statement of migration.split("--> statement-breakpoint")) if (statement.trim()) await context.db.execute(sql.raw(statement));
|
|
expect((await store.read(scope, [target])).get(url)?.result.preview?.state).toBe("merged");
|
|
});
|
|
});
|