diigo-cache: diigo counts pages from 0; migrate stored pages (schema v2)

Comparing a cache with diigo's RSS export: the size-100 walk had missed
exactly the 100 newest bookmarks (everything updated since 2026-09-16).
diigo's page_num is 0-based, and we sent our 1-based page number
unchanged, so "page 1" was really page 2 and the newest page was never
fetched. diigo's total (16744) was right; the "total-overstated"
reading was a misdiagnosis.

- page() asks for page_num = n - 1
- BREAKING for stored state, agreed beforehand, migrated in place:
  schema v2 renumbers every stored page +1 (pages and page_members, via
  negation so no number collides), once, recorded as kv "schema" = 2;
  nothing is refetched, page 1 just becomes a miss
- fill jobs record their numbering; a job saved without it fails ("page
  numbering changed …; start the fill again") instead of resuming
- the fake pages from 0 like diigo (7 tests failed until the fix);
  tests feed v1-shaped storage and a v1-numbered job to the new code
- the next-page peek guard stays; its comments no longer claim diigo
  overstates its total
- the fixed test clock moves to 2099: pinned to "today" its alarms came
  due in real time and fired on their own

Generated-by: Claude Opus 5.5
This commit is contained in:
dckc committed 2026-10-07 08:26:13 -05:00
1 parent e6deb9fda0
commit cffb1fa9b2
5 files changed
+116 -17

No files matched your search

+38 -1
View File
@@ -88,7 +88,7 @@ describe("page size", () => {
}
});
it("a short page whose next page is empty is the real end: diigo's total overstated, noted not refused", async () => {
it("a short page whose next page is empty is the real end: an overstated total is noted, not refused", async () => {
const spy = vi.spyOn(console, "log");
try {
const stub = testEnv.CACHE_DO.get(testEnv.CACHE_DO.newUniqueId());
@@ -122,6 +122,43 @@ describe("pacing", () => {
});
});
describe("page numbering (schema v2)", () => {
it("asks diigo for page_num n-1: page 1 is the newest page", async () => {
const stub = testEnv.CACHE_DO.get(testEnv.CACHE_DO.newUniqueId());
await stub.setup({ webappId: WEBAPP_ID });
const first = (await stub.page(1, PAGE_SIZE)).items.map((j) => (JSON.parse(j) as { title: string }).title);
expect(first[0]).toBe("bookmark 1");
});
it("migrates v1 storage: a stored 'page n' was really page n+1, so it is renumbered, not refetched", async () => {
const stub = testEnv.CACHE_DO.get(testEnv.CACHE_DO.newUniqueId());
await stub.setup({ webappId: WEBAPP_ID });
// the shape v1 left: tables, a page 1 holding what diigo's
// page_num=1 returned (really page 2), and no schema marker
await runInDurableObject(stub, (_obj, state) => {
const sql = state.storage.sql;
sql.exec(`create table items (item_id text primary key, url text not null, title text not null,
private integer not null, json text not null, updated text not null)`);
sql.exec("create table pages (page_num integer primary key, count integer not null, total integer, etched_at text not null)");
sql.exec("create table page_members (page_num integer not null, pos integer not null, item_id text not null, primary key (page_num, pos))");
sql.exec("insert into items values ('a', 'https://a.example/', 'old page one, first', 0, '{\"title\":\"A\"}', '1')");
sql.exec("insert into items values ('b', 'https://b.example/', 'old page one, second', 0, '{\"title\":\"B\"}', '1')");
sql.exec("insert into pages values (1, 100, 250, '2026-10-07T00:00:00.000Z')");
sql.exec("insert into page_members values (1, 1, 'a'), (1, 2, 'b')");
});
const before = await testEnv.WEBAPP.calls();
const two = await stub.page(2, PAGE_SIZE);
expect(two.cached).toBe(true);
expect(two.items.map((j) => (JSON.parse(j) as { title: string }).title)).toEqual(["A", "B"]);
expect(await testEnv.WEBAPP.calls()).toBe(before);
// page 1 was never fetched under v1: now it is a miss
expect((await stub.page(1, PAGE_SIZE)).cached).toBe(false);
await runInDurableObject(stub, (_obj, state) => {
expect(state.storage.kv.get("schema")).toBe(2);
});
});
});
describe("eviction", () => {
it("a cache miss leaves no undisposed RPC stubs to block eviction", async () => {
const stub = testEnv.CACHE_DO.get(testEnv.CACHE_DO.newUniqueId());