From 990c2903e68108124020448dd88ad434a77142e3 Mon Sep 17 00:00:00 2001 From: Owen Date: Wed, 11 Nov 2026 21:00:48 -0700 Subject: [PATCH] Fix checked review queue looping through retired page aliases --- apps/web/app/services/page-precheck.server.ts | 42 +++++++++--- apps/web/tests/services/page-precheck.test.ts | 68 +++++++++++++++++++ 2 files changed, 100 insertions(+), 10 deletions(-) create mode 100644 apps/web/tests/services/page-precheck.test.ts diff --git a/apps/web/app/services/page-precheck.server.ts b/apps/web/app/services/page-precheck.server.ts index b876a355..2bc529c0 100644 --- a/apps/web/app/services/page-precheck.server.ts +++ b/apps/web/app/services/page-precheck.server.ts @@ -1,7 +1,7 @@ import type { SupabaseClient } from "@supabase/supabase-js"; import { PRECHECK_MAX, UNREVIEWED_SECTIONS, isReviewable } from "../lib/page-review-types"; import { SITEMAP_SECTIONS, sitemapEntries, type SitemapSection } from "./sitemap.server"; -import { reviewKind, reviewPath } from "./page-reviews.server"; +import { reviewKind, reviewPath, sectionOf } from "./page-reviews.server"; import { checkPage } from "./page-check.server"; /** @@ -37,22 +37,44 @@ export async function queuePrecheck(supabase: SupabaseClient, section: SitemapSe return picked.length; } -/** How the queue stands: pages still waiting for their check, and checked pages nobody has reviewed yet. */ +/** Checked pages still in the public review inventory, oldest first. A merged/renamed/hidden page's + * historical review stays intact, but must not keep sending staff through a redirect to a page they finished. + * Use the same inventory as queuePrecheck, and the same list for both the counter and Next. */ +async function readyCheckedPaths(supabase: SupabaseClient): Promise { + const rows: { path: string }[] = []; + for (let offset = 0; ; offset += 1000) { + const { data, error } = await supabase.from("page_reviews").select("path") + .is("status", null).is("check_requested_at", null).not("ai_checked_at", "is", null) + .order("ai_checked_at", { ascending: true }).order("path", { ascending: true }).range(offset, offset + 999); + if (error) throw new Error("Could not load checked pages."); + rows.push(...(data || [])); + if (!data || data.length < 1000) break; + } + if (!rows.length) return []; + const sections = new Set(["pages"]); + for (const { path } of rows) { + const section = sectionOf(path); + if (section) sections.add(section); + } + // Do not turn a failed inventory read into an apparently empty/finished queue. + const lists = await Promise.all([...sections].map((section) => sitemapEntries(supabase, section))); + const current = new Set(lists.flat().map((entry) => reviewPath(entry.path)).filter((path) => path && isReviewable(path))); + return rows.map(({ path }) => path).filter((path) => current.has(path)); +} + +/** How the queue stands: pages waiting for their check, and current checked pages nobody has reviewed yet. */ export async function precheckState(supabase: SupabaseClient) { const [queued, ready] = await Promise.all([ supabase.from("page_reviews").select("path", { count: "exact", head: true }).not("check_requested_at", "is", null), - supabase.from("page_reviews").select("path", { count: "exact", head: true }) - .is("status", null).is("check_requested_at", null).not("ai_checked_at", "is", null), + readyCheckedPaths(supabase), ]); - return { queued: queued.count ?? 0, ready: ready.count ?? 0 }; + if (queued.error) throw new Error("Could not load queued pages."); + return { queued: queued.count ?? 0, ready: ready.length }; } -/** The next checked page nobody has reviewed (oldest check first), other than the one just finished. */ +/** The next current checked page nobody has reviewed, excluding the page just finished or skipped. */ export async function nextCheckedPath(supabase: SupabaseClient, from: string | null): Promise { - const { data } = await supabase.from("page_reviews").select("path") - .is("status", null).is("check_requested_at", null).not("ai_checked_at", "is", null) - .neq("path", from || "").order("ai_checked_at", { ascending: true }).limit(1); - return data?.[0]?.path ?? null; + return (await readyCheckedPaths(supabase)).find((path) => path !== from) ?? null; } /** The cron's share of the queue: checks the oldest queued pages, a few at once, and clears each one's mark. */ diff --git a/apps/web/tests/services/page-precheck.test.ts b/apps/web/tests/services/page-precheck.test.ts new file mode 100644 index 00000000..96d8bb09 --- /dev/null +++ b/apps/web/tests/services/page-precheck.test.ts @@ -0,0 +1,68 @@ +import { beforeEach, expect, it, vi } from "vitest"; +import { nextCheckedPath, precheckState } from "~/services/page-precheck.server"; +import { sitemapEntries } from "~/services/sitemap.server"; + +vi.mock("~/services/page-check.server", () => ({ checkPage: vi.fn() })); +vi.mock("~/services/sitemap.server", () => ({ + SITEMAP_SECTIONS: ["pages", "matters", "meetings", "documents", "properties"], + sitemapEntries: vi.fn(), +})); + +type Row = { path: string; status: string | null; check_requested_at: string | null; ai_checked_at: string | null }; +const checked = (path: string, status: string | null = null): Row => ({ path, status, check_requested_at: null, ai_checked_at: "2026-11-03" }); +// In-memory query adapter: exercise filtering and pagination against stored rows, without live records or AI. +function database(rows: Row[]) { + return { from: () => { + let selected = [...rows], head = false; + const query: any = { + select: (_: string, options?: { head?: boolean }) => { head = !!options?.head; return query; }, + is: (key: keyof Row, value: null) => { selected = selected.filter((row) => row[key] === value); return query; }, + not: (key: keyof Row, _: string, value: null) => { selected = selected.filter((row) => row[key] !== value); return query; }, + order: () => query, + range: (from: number, to: number) => Promise.resolve({ data: selected.slice(from, to + 1), error: null }), + then: (resolve: any) => Promise.resolve({ data: head ? null : selected, count: selected.length, error: null }).then(resolve), + }; + return query; + } } as any; +} + +beforeEach(() => { vi.mocked(sitemapEntries).mockReset(); }); + +it("drains the checked queue without revisiting merged aliases of an already-reviewed page", async () => { + const old = "/matters/design-guidelines-for-carriage-houses"; + const canonical = "/matters/carriage-house-design-guidelines-and-separation-requirements"; + const next = "/matters/another-topic"; + const rows = [checked(old), checked(canonical, "reviewed"), checked(next), checked("/matters/hidden-topic")]; + vi.mocked(sitemapEntries).mockResolvedValue([{ path: canonical }, { path: next }] as any); + const db = database(rows); + expect(await precheckState(db)).toEqual({ queued: 0, ready: 1 }); + expect(await nextCheckedPath(db, null)).toBe(next); + expect(await nextCheckedPath(db, next)).toBeNull(); + rows[2].status = "reviewed"; + expect(await precheckState(db)).toEqual({ queued: 0, ready: 0 }); + expect(await nextCheckedPath(db, null)).toBeNull(); + expect(rows[0]).toEqual(checked(old)); // retain the historical review, not a data cleanup +}); + +it("keeps current reviewable detail pages and excludes indexes, reviewed and queued pages", async () => { + const paths = ["/matters", "/meetings/a-meeting", "/properties/a-home"]; + vi.mocked(sitemapEntries).mockImplementation(async (_, section) => + paths.filter((path) => section === "pages" ? path === "/matters" : path.startsWith(`/${section}/`)).map((path) => ({ path })) as any); + const db = database([...paths.map((p) => checked(p)), checked("/matters/reviewed", "reviewed"), + { ...checked("/matters/queued"), check_requested_at: "2026-11-12" }, { ...checked("/matters/unchecked"), ai_checked_at: null }]); + expect(await precheckState(db)).toEqual({ queued: 1, ready: 2 }); + expect(await nextCheckedPath(db, "/matters")).toBe("/meetings/a-meeting"); +}); + +it("can reach a current page beyond a full batch of retired paths", async () => { + const current = "/matters/current"; + vi.mocked(sitemapEntries).mockResolvedValue([{ path: current }] as any); + const db = database([...Array.from({ length: 1000 }, (_, i) => checked(`/matters/retired-${i}`)), checked(current)]); + expect(await nextCheckedPath(db, null)).toBe(current); + expect((await precheckState(db)).ready).toBe(1); +}); + +it("does not report a finished queue when the public inventory fails", async () => { + vi.mocked(sitemapEntries).mockRejectedValue(new Error("inventory unavailable")); + await expect(precheckState(database([checked("/matters/current")]))).rejects.toThrow("inventory unavailable"); +});