@jant/core 0.8.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/dist/{app-CFsmRVJN.js → app--uxnaIRV.js} +427 -245
  2. package/dist/client/.vite/manifest.json +2 -2
  3. package/dist/client/_assets/{client-DTLBOHqD.js → client-BT6kpcuG.js} +1 -1
  4. package/dist/client/_assets/{client-auth-D6Ix6SmE.js → client-auth-begiT1tq.js} +1 -1
  5. package/dist/index.js +1 -1
  6. package/dist/node.js +2 -2
  7. package/package.json +1 -1
  8. package/src/__tests__/fixtures/releases/0.8.0/site-export/README.md +130 -0
  9. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/2thjq/_index.md +25 -0
  10. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/2u5e5/_index.md +35 -0
  11. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/41q27/0x4nv/index.md +18 -0
  12. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/41q27/_index.md +44 -0
  13. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/41q27/faqyt/index.md +30 -0
  14. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/41q27/the-potting-mix-guide-i-keep-rereading/index.md +20 -0
  15. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/5kayp/_index.md +23 -0
  16. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/5ynfs/_index.md +31 -0
  17. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/5ynfs/hu597/index.md +21 -0
  18. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/_index.md +5 -0
  19. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/a-night-heron-field-guide-for-my-walk-folder/_index.md +31 -0
  20. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/a-quiet-saturday-after-a-busy-week/_index.md +36 -0
  21. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/archive/_index.md +8 -0
  22. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/are-na-is-the-only-bookmark-pile-i-still-revisit/_index.md +30 -0
  23. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/bought-too-many-lemons-again/_index.md +37 -0
  24. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/city-walks/_index.md +12 -0
  25. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/cleanshot-for-screenshots-ocr-and-screen-recording-on-the-mac/_index.md +33 -0
  26. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/cleanshotmac-shang-de-jie-tu-ocr-lu-ping-gong-ju/_index.md +32 -0
  27. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/co344/_index.md +25 -0
  28. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/collections/_index.md +5 -0
  29. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/current-tea-rotation/_index.md +34 -0
  30. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/derek-sivers-still-writes-the-kind-of-internet-i-want-more-of/_index.md +30 -0
  31. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/desk-but-better/_index.md +25 -0
  32. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/e0kcg/_index.md +22 -0
  33. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/egi3c/_index.md +24 -0
  34. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/evf9j/_index.md +152 -0
  35. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/featured/_index.md +8 -0
  36. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/from-basho/_index.md +25 -0
  37. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/from-epictetus/_index.md +25 -0
  38. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/from-marcus-aurelius/_index.md +25 -0
  39. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/from-seneca/_index.md +26 -0
  40. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/from-thoreau/_index.md +25 -0
  41. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/gkbvv/2cl6d/index.md +18 -0
  42. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/gkbvv/3zqv7/index.md +30 -0
  43. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/gkbvv/5oxsv/index.md +18 -0
  44. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/gkbvv/_index.md +39 -0
  45. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/gwob9/_index.md +25 -0
  46. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/home-cooking/_index.md +12 -0
  47. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/hz729/_index.md +24 -0
  48. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/inaturalist-ai-zi-ran-she-qu/_index.md +44 -0
  49. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/j4v0q/_index.md +35 -0
  50. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/k278t/_index.md +25 -0
  51. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/k4eee/_index.md +22 -0
  52. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/k5vh8/_index.md +32 -0
  53. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/k5vh8/a-reading-log-idea-i-might-steal/index.md +20 -0
  54. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/k5vh8/a83qu/index.md +18 -0
  55. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/k5vh8/from-seneca-5hibo/index.md +21 -0
  56. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/kan-qi-lai-bi-jiao-ku-de-gong-si-men/_index.md +41 -0
  57. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/l2m7y/_index.md +29 -0
  58. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/lan-ya-yao-kong-qi-chuang-shang-yue-du-de-fan-ye-shen-qi/_index.md +57 -0
  59. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/mynoise-for-rainy-afternoons/_index.md +30 -0
  60. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/n09le/_index.md +23 -0
  61. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/ni-fu-jin-de-wei-ji-bai-ke-tiao-mu/_index.md +42 -0
  62. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/nts-archives-are-good-cleaning-music/_index.md +30 -0
  63. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/qgfcu/_index.md +32 -0
  64. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/qgfcu/c91j0/index.md +18 -0
  65. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/qgfcu/qyucy/index.md +18 -0
  66. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/qgfcu/wsgck/index.md +18 -0
  67. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/rain-makes-my-neighborhood-look-better/_index.md +46 -0
  68. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/reading-notes/_index.md +12 -0
  69. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/s4r3e/_index.md +25 -0
  70. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/serious-eats-on-tomato-and-egg-soup/_index.md +30 -0
  71. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/te812/_index.md +44 -0
  72. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/te812/cxiu6/index.md +18 -0
  73. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/te812/t77ms/index.md +18 -0
  74. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/te812/the-soup-trick-i-always-forget/index.md +20 -0
  75. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/telk3/_index.md +30 -0
  76. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/telk3/from-thoreau-pawt2/index.md +21 -0
  77. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/the-five-minute-reset-is-still-the-best-habit-i-have/_index.md +36 -0
  78. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/the-hallway-light-took-12-minutes-and-3-months/_index.md +32 -0
  79. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/the-recurse-center-manual-is-weirdly-calming/_index.md +30 -0
  80. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/tiny-projects/_index.md +12 -0
  81. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/tong-wang-xing-fu-zhi-lu/_index.md +68 -0
  82. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/tou-ma-yan-jiang-ju-le-bu-chu-ti-yan/_index.md +62 -0
  83. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/useful-links/_index.md +12 -0
  84. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/weekend-notes/_index.md +12 -0
  85. package/src/__tests__/fixtures/releases/0.8.0/site-export/content/what-i-actually-use-my-notebook-for/_index.md +52 -0
  86. package/src/__tests__/fixtures/releases/0.8.0/site-export/data/jant.toml +124 -0
  87. package/src/__tests__/fixtures/releases/0.8.0/site-export/hugo.toml +45 -0
  88. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/_redirects +25 -0
  89. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/15d51730a4f7-med_01kn8jtq3penf98tfq4gzmh9g9.png +0 -0
  90. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01kn8jtx8qenf98tm9ahxr48sw.webp +0 -0
  91. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01kn8jv0jdenf98tp6qr9zgjjc.webp +0 -0
  92. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01kn8jv452enf98trbkwrvhvgd.webp +0 -0
  93. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01kn8jvb16enf98txp7p0pf5ge.webp +0 -0
  94. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01kn8jve5nenf98tzap11gpxzr.webp +0 -0
  95. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01kn8jvq58enf98v538w5vsqp4.webp +0 -0
  96. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01kn8jvsaeenf98v6ct4gjzppc.webp +0 -0
  97. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01kn8jw26cenf98vc7k0ppz5k8.webp +0 -0
  98. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m313q1hme4e8cmby77dn66yy.webp +0 -0
  99. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m313q2w7e4e8cmc20036yh66.webp +0 -0
  100. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m313q7d2e4e8cmdeqjmyafby.webp +0 -0
  101. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m313q887e4e8cmdjssych7yh.webp +0 -0
  102. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167jx2f6ct88pkz45j3ycp-poster.webp +0 -0
  103. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167jx2f6ct88pkz45j3ycp.mp4 +0 -0
  104. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167mbdf6ct88pr4ga1b9kf-poster.webp +0 -0
  105. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167mbdf6ct88pr4ga1b9kf.mp4 +0 -0
  106. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167nsvf6ct88q1anhz4svb-poster.webp +0 -0
  107. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167nsvf6ct88q1anhz4svb.mp4 +0 -0
  108. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167qaxf6ct88qfndwzxx83-poster.webp +0 -0
  109. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167qaxf6ct88qfndwzxx83.mp4 +0 -0
  110. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167sm0f6ct88qw5brtsgcw.webp +0 -0
  111. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167tf8f6ct88r169s51j49.webp +0 -0
  112. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167vaaf6ct88rbx5ztenwb.webp +0 -0
  113. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167w51f6ct88rp19pybkw1.webp +0 -0
  114. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167x0nf6ct88rzajb84tw3.webp +0 -0
  115. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167xxbf6ct88s5t2300bjk.webp +0 -0
  116. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167yq5f6ct88s9pjwt5fxg.webp +0 -0
  117. package/src/__tests__/fixtures/releases/0.8.0/site-export/static/media/med_01m3167zg4f6ct88sge8jp28ja.webp +0 -0
  118. package/src/__tests__/fixtures/releases/0.8.0/site-export/themes/jant/static/apple-touch-icon.png +0 -0
  119. package/src/__tests__/fixtures/releases/0.8.0/site-export/themes/jant/static/custom.css +0 -0
  120. package/src/__tests__/fixtures/releases/0.8.0/site-export/themes/jant/static/favicon.ico +0 -0
  121. package/src/__tests__/fixtures/releases/0.8.0/site-export/wrangler.jsonc +24 -0
  122. package/src/__tests__/fixtures/releases/0.8.0/snapshot/db.sql +322 -0
  123. package/src/__tests__/fixtures/releases/0.8.0/snapshot/meta.json +11 -0
  124. package/src/__tests__/helpers/cursor-walk.ts +52 -0
  125. package/src/__tests__/release-fixtures.test.ts +6 -2
  126. package/src/lib/__tests__/post-list-cursor.test.ts +105 -0
  127. package/src/lib/__tests__/timeline.test.ts +14 -14
  128. package/src/lib/post-list-cursor.ts +211 -0
  129. package/src/routes/api/__tests__/mcp.test.ts +58 -0
  130. package/src/routes/api/__tests__/posts.test.ts +79 -0
  131. package/src/routes/api/posts.ts +5 -9
  132. package/src/routes/api/public/__tests__/archive.test.ts +261 -0
  133. package/src/routes/api/public/__tests__/posts.test.ts +344 -19
  134. package/src/routes/api/public/archive.ts +18 -16
  135. package/src/routes/api/public/posts.ts +15 -16
  136. package/src/services/__tests__/post.test.ts +342 -36
  137. package/src/services/mcp.ts +6 -11
  138. package/src/services/post.ts +513 -319
  139. package/src/ui/shared/Pagination.tsx +1 -1
@@ -1,6 +1,9 @@
1
1
  import { describe, expect, it } from "vitest";
2
2
  import { createTestApp } from "../../../../__tests__/helpers/app.js";
3
+ import { walkPostPages } from "../../../../__tests__/helpers/cursor-walk.js";
4
+ import { createEntityId } from "../../../../lib/ids.js";
3
5
  import { publicArchiveApiRoutes } from "../archive.js";
6
+ import { publicPostsApiRoutes } from "../posts.js";
4
7
 
5
8
  describe("Public Archive API Routes", () => {
6
9
  describe("GET /api/public/archive", () => {
@@ -234,6 +237,70 @@ describe("Public Archive API Routes", () => {
234
237
  ]);
235
238
  });
236
239
 
240
+ // The failure this guards: with no time axis named, the list fell back to
241
+ // `lastActivityAt`, so a reply lifted a long-published root to the top of
242
+ // an endpoint documented as newest-published-first.
243
+ it("orders by publication date, not by the latest reply", async () => {
244
+ const { app, services } = createTestApp({ authenticated: false });
245
+ app.route("/api/public/archive", publicArchiveApiRoutes);
246
+
247
+ const oldest = await services.posts.create({
248
+ format: "note",
249
+ title: "Published January 2024",
250
+ bodyMarkdown: "oldest",
251
+ publishedAt: Date.UTC(2024, 0, 1) / 1000,
252
+ });
253
+ const extended = await services.posts.create({
254
+ format: "note",
255
+ title: "Published November 2024, replied to in 2026",
256
+ bodyMarkdown: "old root",
257
+ publishedAt: Date.UTC(2024, 10, 19) / 1000,
258
+ });
259
+ await services.posts.create({
260
+ format: "note",
261
+ bodyMarkdown: "new reply",
262
+ replyToId: extended.id,
263
+ publishedAt: Date.UTC(2026, 8, 26) / 1000,
264
+ });
265
+ const middle = await services.posts.create({
266
+ format: "note",
267
+ title: "Published March 2025",
268
+ bodyMarkdown: "middle",
269
+ publishedAt: Date.UTC(2025, 2, 1) / 1000,
270
+ });
271
+ const newest = await services.posts.create({
272
+ format: "note",
273
+ title: "Published December 2025",
274
+ bodyMarkdown: "newest",
275
+ publishedAt: Date.UTC(2025, 11, 1) / 1000,
276
+ });
277
+ const expected = [newest.id, middle.id, extended.id, oldest.id];
278
+
279
+ const all = await app.request("/api/public/archive");
280
+ expect(all.status).toBe(200);
281
+ const allBody = await all.json();
282
+ expect(allBody.posts.map((post: { id: string }) => post.id)).toEqual(
283
+ expected,
284
+ );
285
+
286
+ // One post per page puts a cursor on every boundary, including the two
287
+ // where publication and activity order disagree. A cursor comparison on
288
+ // a different axis from the ORDER BY would skip or repeat a post here.
289
+ const walked: string[] = [];
290
+ let cursor: string | null = null;
291
+ for (let page = 0; page <= expected.length; page++) {
292
+ const query: string = cursor ? `&cursor=${cursor}` : "";
293
+ const res = await app.request(`/api/public/archive?limit=1${query}`);
294
+ expect(res.status).toBe(200);
295
+ const body = await res.json();
296
+ walked.push(...body.posts.map((post: { id: string }) => post.id));
297
+ cursor = body.nextCursor;
298
+ if (cursor === null) break;
299
+ }
300
+ expect(cursor).toBeNull();
301
+ expect(walked).toEqual(expected);
302
+ });
303
+
237
304
  it("filters by year using publishedAt", async () => {
238
305
  const { app, services } = createTestApp({ authenticated: false });
239
306
  app.route("/api/public/archive", publicArchiveApiRoutes);
@@ -470,4 +537,198 @@ describe("Public Archive API Routes", () => {
470
537
  expect(res.status).toBe(400);
471
538
  });
472
539
  });
540
+
541
+ describe("cursors", () => {
542
+ function setup() {
543
+ const { app, services } = createTestApp({ authenticated: false });
544
+ app.route("/api/public/archive", publicArchiveApiRoutes);
545
+ app.route("/api/public/posts", publicPostsApiRoutes);
546
+ return { app, services };
547
+ }
548
+
549
+ /** Four roots published a day apart, oldest first. */
550
+ async function publishDays(services: ReturnType<typeof setup>["services"]) {
551
+ const created = [];
552
+ for (let day = 1; day <= 4; day++) {
553
+ created.push(
554
+ await services.posts.create({
555
+ format: "note",
556
+ title: `Day ${day}`,
557
+ bodyMarkdown: `day ${day}`,
558
+ publishedAt: Date.UTC(2025, 0, day) / 1000,
559
+ }),
560
+ );
561
+ }
562
+ return created;
563
+ }
564
+
565
+ async function page(
566
+ app: ReturnType<typeof setup>["app"],
567
+ path: string,
568
+ ): Promise<{ ids: string[]; nextCursor: string | null }> {
569
+ const res = await app.request(path);
570
+ expect(res.status).toBe(200);
571
+ const body = await res.json();
572
+ return {
573
+ ids: body.posts.map((post: { id: string }) => post.id),
574
+ nextCursor: body.nextCursor,
575
+ };
576
+ }
577
+
578
+ it("continues after the post the cursor was taken on is deleted", async () => {
579
+ const { app, services } = setup();
580
+ const [day1, day2, day3, day4] = await publishDays(services);
581
+
582
+ const first = await page(app, "/api/public/archive?limit=2");
583
+ expect(first.ids).toEqual([day4?.id, day3?.id]);
584
+ await services.posts.delete(day3?.id ?? "");
585
+
586
+ const second = await page(
587
+ app,
588
+ `/api/public/archive?limit=2&cursor=${first.nextCursor}`,
589
+ );
590
+ expect(second.ids).toEqual([day2?.id, day1?.id]);
591
+ expect(second.nextCursor).toBeNull();
592
+ });
593
+
594
+ it("skips nothing when the cursor post is re-dated mid-walk", async () => {
595
+ const { app, services } = setup();
596
+ const [day1, day2, day3, day4] = await publishDays(services);
597
+
598
+ const first = await page(app, "/api/public/archive?limit=2");
599
+ expect(first.ids).toEqual([day4?.id, day3?.id]);
600
+
601
+ // The author moves the page's last post before every other one. Resuming
602
+ // from where it now sits would return nothing more.
603
+ await services.posts.update(day3?.id ?? "", {
604
+ publishedAt: Date.UTC(2024, 0, 1) / 1000,
605
+ });
606
+ expect((await page(app, "/api/public/archive")).ids).toEqual([
607
+ day4?.id,
608
+ day2?.id,
609
+ day1?.id,
610
+ day3?.id,
611
+ ]);
612
+
613
+ const rest = await walkPostPages(
614
+ app,
615
+ "/api/public/archive",
616
+ 2,
617
+ first.nextCursor,
618
+ );
619
+ // Day 3 comes round again: its sort key changed mid-walk, which the
620
+ // paging guarantee does not cover. Nothing that stayed put is skipped.
621
+ expect(rest).toEqual([day2?.id, day1?.id, day3?.id]);
622
+ });
623
+
624
+ it("answers a private post's or a draft's ID as it does an unknown one", async () => {
625
+ const { app, services } = setup();
626
+ await publishDays(services);
627
+ const hidden = await services.posts.create({
628
+ format: "note",
629
+ title: "Private",
630
+ bodyMarkdown: "private",
631
+ visibility: "private",
632
+ });
633
+ const draft = await services.posts.create({
634
+ format: "note",
635
+ title: "Draft",
636
+ bodyMarkdown: "draft",
637
+ status: "draft",
638
+ });
639
+
640
+ const answers = [];
641
+ for (const id of [hidden.id, draft.id, createEntityId("post")]) {
642
+ for (const path of ["/api/public/archive", "/api/public/posts"]) {
643
+ const res = await app.request(`${path}?cursor=${id}`);
644
+ expect(res.status).toBe(400);
645
+ const body = await res.json();
646
+ expect(body.code).toBe("VALIDATION_ERROR");
647
+ answers.push(body.error);
648
+ }
649
+ }
650
+ // Nothing in the answer tells a real post from one that never existed.
651
+ expect(new Set(answers).size).toBe(1);
652
+ });
653
+
654
+ it("still resumes from a bare post ID", async () => {
655
+ const { app, services } = setup();
656
+ const [day1, day2, day3] = await publishDays(services);
657
+
658
+ const res = await page(app, `/api/public/archive?cursor=${day3?.id}`);
659
+ expect(res.ids).toEqual([day2?.id, day1?.id]);
660
+ });
661
+
662
+ it("rejects a cursor it can't read", async () => {
663
+ const { app, services } = setup();
664
+ await publishDays(services);
665
+
666
+ for (const cursor of ["garbage", "eyJ2IjoxfQ", "pst_", "%7B%7D"]) {
667
+ const res = await app.request(`/api/public/archive?cursor=${cursor}`);
668
+ expect(res.status).toBe(400);
669
+ expect((await res.json()).code).toBe("VALIDATION_ERROR");
670
+ }
671
+ });
672
+
673
+ it("rejects a cursor from a list in another order", async () => {
674
+ const { app, services } = setup();
675
+ await publishDays(services);
676
+ await services.posts.create({
677
+ format: "note",
678
+ title: "Featured",
679
+ bodyMarkdown: "featured",
680
+ featured: true,
681
+ });
682
+
683
+ // The archive reads publication order; `/api/public/posts` reads
684
+ // activity with pinned posts first.
685
+ const archive = await page(app, "/api/public/archive?limit=1");
686
+ const posts = await app.request(
687
+ `/api/public/posts?cursor=${archive.nextCursor}`,
688
+ );
689
+ expect(posts.status).toBe(400);
690
+ expect((await posts.json()).error).toMatch(/different order/);
691
+
692
+ // A cursor records a position in an order, not the filters around it:
693
+ // Featured-only reads the same order, so the position carries over.
694
+ const featured = await app.request(
695
+ `/api/public/archive?cursor=${archive.nextCursor}&visibility=featured`,
696
+ );
697
+ expect(featured.status).toBe(200);
698
+ });
699
+
700
+ it("walks every order it serves the same as one unpaged request", async () => {
701
+ const { app, services } = setup();
702
+ await publishDays(services);
703
+ const sameDay = Date.UTC(2025, 5, 1) / 1000;
704
+ for (const title of ["Tie A", "Tie B", "Tie C"]) {
705
+ await services.posts.create({
706
+ format: "note",
707
+ title,
708
+ bodyMarkdown: title,
709
+ publishedAt: sameDay,
710
+ featured: title !== "Tie C",
711
+ });
712
+ }
713
+ await services.posts.create({
714
+ format: "note",
715
+ title: "Pinned, and pinning doesn't count here",
716
+ bodyMarkdown: "pinned",
717
+ pinned: true,
718
+ publishedAt: Date.UTC(2023, 0, 1) / 1000,
719
+ });
720
+
721
+ for (const path of [
722
+ "/api/public/archive",
723
+ "/api/public/archive?visibility=featured",
724
+ "/api/public/archive?format=note&title=any",
725
+ ]) {
726
+ const expected = (await page(app, path)).ids;
727
+ expect(expected.length).toBeGreaterThan(1);
728
+ for (const limit of [1, 2, 3]) {
729
+ expect(await walkPostPages(app, path, limit)).toEqual(expected);
730
+ }
731
+ }
732
+ });
733
+ });
473
734
  });
@@ -1,7 +1,9 @@
1
1
  import { describe, expect, it } from "vitest";
2
2
  import { eq } from "drizzle-orm";
3
3
  import { createTestApp } from "../../../../__tests__/helpers/app.js";
4
- import { posts } from "../../../../db/schema.js";
4
+ import { walkPostPages } from "../../../../__tests__/helpers/cursor-walk.js";
5
+ import { posts, threadCollections } from "../../../../db/schema.js";
6
+ import { createEntityId } from "../../../../lib/ids.js";
5
7
  import { publicPostsApiRoutes } from "../posts.js";
6
8
 
7
9
  describe("Public Posts API Routes", () => {
@@ -120,6 +122,104 @@ describe("Public Posts API Routes", () => {
120
122
  expect(body.nextCursor).toBeTruthy();
121
123
  });
122
124
 
125
+ it("walks pinned posts and bumped Threads the same as one unpaged request", async () => {
126
+ const { app, services } = createTestApp({ authenticated: false });
127
+ app.route("/api/public/posts", publicPostsApiRoutes);
128
+
129
+ const day = (n: number) => Date.UTC(2025, 0, n) / 1000;
130
+ await services.posts.create({
131
+ format: "note",
132
+ title: "Pinned",
133
+ bodyMarkdown: "pinned",
134
+ publishedAt: day(1),
135
+ pinnedAt: day(20),
136
+ });
137
+ await services.posts.create({
138
+ format: "note",
139
+ title: "Pinned at the same moment",
140
+ bodyMarkdown: "pinned too",
141
+ publishedAt: day(2),
142
+ pinnedAt: day(20),
143
+ });
144
+ const bumped = await services.posts.create({
145
+ format: "note",
146
+ title: "Old root, new reply",
147
+ bodyMarkdown: "bumped",
148
+ publishedAt: day(3),
149
+ });
150
+ await services.posts.create({
151
+ format: "note",
152
+ bodyMarkdown: "reply",
153
+ replyToId: bumped.id,
154
+ publishedAt: day(15),
155
+ });
156
+ for (const title of ["Tie A", "Tie B", "Tie C"]) {
157
+ await services.posts.create({
158
+ format: "note",
159
+ title,
160
+ bodyMarkdown: title,
161
+ publishedAt: day(10),
162
+ });
163
+ }
164
+ await services.posts.create({
165
+ format: "note",
166
+ title: "Hidden from Latest",
167
+ bodyMarkdown: "hidden",
168
+ visibility: "latest_hidden",
169
+ publishedAt: day(11),
170
+ });
171
+
172
+ const res = await app.request("/api/public/posts");
173
+ const expected = (await res.json()).posts.map(
174
+ (post: { id: string }) => post.id,
175
+ );
176
+ expect(expected).toHaveLength(6);
177
+ for (const limit of [1, 2, 4]) {
178
+ expect(await walkPostPages(app, "/api/public/posts", limit)).toEqual(
179
+ expected,
180
+ );
181
+ }
182
+ });
183
+
184
+ it("keeps paging after the cursor post is deleted, and reads old ID cursors", async () => {
185
+ const { app, services } = createTestApp({ authenticated: false });
186
+ app.route("/api/public/posts", publicPostsApiRoutes);
187
+
188
+ const created = [];
189
+ for (let n = 1; n <= 4; n++) {
190
+ created.push(
191
+ await services.posts.create({
192
+ format: "note",
193
+ title: `Post ${n}`,
194
+ bodyMarkdown: `post ${n}`,
195
+ publishedAt: Date.UTC(2025, 0, n) / 1000,
196
+ }),
197
+ );
198
+ }
199
+ const [first, second, third, fourth] = created;
200
+
201
+ const page1 = await (
202
+ await app.request("/api/public/posts?limit=2")
203
+ ).json();
204
+ expect(page1.posts.map((post: { id: string }) => post.id)).toEqual([
205
+ fourth?.id,
206
+ third?.id,
207
+ ]);
208
+ await services.posts.delete(third?.id ?? "");
209
+ expect(
210
+ await walkPostPages(app, "/api/public/posts", 2, page1.nextCursor),
211
+ ).toEqual([second?.id, first?.id]);
212
+
213
+ // The bare post ID earlier releases returned as `nextCursor`.
214
+ const legacy = await app.request(
215
+ `/api/public/posts?cursor=${fourth?.id}`,
216
+ );
217
+ expect(legacy.status).toBe(200);
218
+ expect(
219
+ (await legacy.json()).posts.map((post: { id: string }) => post.id),
220
+ ).toEqual([second?.id, first?.id]);
221
+ });
222
+
123
223
  it("returns markdown instead of rendered fields when content=markdown", async () => {
124
224
  const { app, services } = createTestApp({ authenticated: false });
125
225
  app.route("/api/public/posts", publicPostsApiRoutes);
@@ -363,28 +463,253 @@ describe("Public Posts API Routes", () => {
363
463
  const middle = await createRatedThread("Middle", 2000, 3);
364
464
  const lowest = await createRatedThread("Lowest", 3000, 1);
365
465
 
366
- const firstRes = await app.request(
367
- "/api/public/posts?collection=rated-threads&limit=1",
368
- );
369
- expect(firstRes.status).toBe(200);
370
- const first = await firstRes.json();
371
- expect(first.posts[0].id).toBe(highest.id);
372
- expect(first.nextCursor).toBe(highest.id);
466
+ expect(
467
+ await walkPostPages(
468
+ app,
469
+ "/api/public/posts?collection=rated-threads",
470
+ 1,
471
+ ),
472
+ ).toEqual([highest.id, middle.id, lowest.id]);
473
+ });
474
+
475
+ it("walks a collection in each order the same as one unpaged request", async () => {
476
+ const { app, services, db } = createTestApp({ authenticated: false });
477
+ app.route("/api/public/posts", publicPostsApiRoutes);
478
+
479
+ const collection = await services.collections.create({
480
+ slug: "walked",
481
+ title: "Walked",
482
+ });
483
+ const day = (n: number) => Date.UTC(2025, 0, n) / 1000;
484
+ const thread = async (
485
+ title: string,
486
+ publishedAt: number,
487
+ extra: {
488
+ rating?: number;
489
+ visibility?: "private" | "latest_hidden";
490
+ } = {},
491
+ ) =>
492
+ services.posts.create({
493
+ format: "note",
494
+ title,
495
+ bodyMarkdown: title,
496
+ collectionIds: [collection.id],
497
+ publishedAt,
498
+ ...extra,
499
+ });
373
500
 
374
- const secondRes = await app.request(
375
- `/api/public/posts?collection=rated-threads&limit=1&cursor=${first.nextCursor}`,
501
+ // Pins tie on the pin time, then order by each sort's own keys.
502
+ const pinnedA = await thread("Pinned A", day(1), { rating: 2 });
503
+ const pinnedB = await thread("Pinned B", day(2));
504
+ for (const pinned of [pinnedA, pinnedB]) {
505
+ await db
506
+ .update(threadCollections)
507
+ .set({ pinnedAt: day(30) })
508
+ .where(eq(threadCollections.threadId, pinned.id));
509
+ }
510
+ // A reply moves this Thread up under newest and rating, not oldest.
511
+ const bumped = await thread("Bumped", day(3), { rating: 4 });
512
+ await services.posts.create({
513
+ format: "note",
514
+ bodyMarkdown: "reply",
515
+ replyToId: bumped.id,
516
+ publishedAt: day(20),
517
+ rating: 5,
518
+ });
519
+ // Ties on publication time, activity, and rating.
520
+ for (const title of ["Tie A", "Tie B", "Tie C"]) {
521
+ await thread(title, day(10), { rating: 3 });
522
+ }
523
+ await thread("Unrated", day(12));
524
+ await thread("Private", day(13), { visibility: "private" });
525
+ await thread("Hidden from Latest", day(14), {
526
+ visibility: "latest_hidden",
527
+ });
528
+
529
+ for (const sort of ["newest", "oldest", "rating_desc"]) {
530
+ const path = `/api/public/posts?collection=walked&sort=${sort}`;
531
+ const res = await app.request(`${path}&limit=100`);
532
+ const body = await res.json();
533
+ const expected = body.posts.map((post: { id: string }) => post.id);
534
+ expect(body.nextCursor).toBeNull();
535
+ expect(expected).toHaveLength(7);
536
+ expect(expected.slice(0, 2).sort()).toEqual(
537
+ [pinnedA.id, pinnedB.id].sort(),
538
+ );
539
+ for (const limit of [1, 2, 3]) {
540
+ expect(await walkPostPages(app, path, limit), sort).toEqual(expected);
541
+ }
542
+ }
543
+ });
544
+
545
+ it("keeps paging a collection after the cursor Thread leaves it", async () => {
546
+ const { app, services } = createTestApp({ authenticated: false });
547
+ app.route("/api/public/posts", publicPostsApiRoutes);
548
+
549
+ const collection = await services.collections.create({
550
+ slug: "shrinking",
551
+ title: "Shrinking",
552
+ });
553
+ const roots = [];
554
+ for (let n = 1; n <= 7; n++) {
555
+ roots.push(
556
+ await services.posts.create({
557
+ format: "note",
558
+ title: `Thread ${n}`,
559
+ bodyMarkdown: `thread ${n}`,
560
+ collectionIds: [collection.id],
561
+ publishedAt: Date.UTC(2025, 0, n) / 1000,
562
+ }),
563
+ );
564
+ }
565
+ const ids = roots.map((root) => root.id).reverse();
566
+ const path = "/api/public/posts?collection=shrinking&limit=2";
567
+ const readPage = async (cursor?: string) => {
568
+ const res = await app.request(
569
+ cursor ? `${path}&cursor=${encodeURIComponent(cursor)}` : path,
570
+ );
571
+ expect(res.status).toBe(200);
572
+ return (await res.json()) as {
573
+ posts: { id: string }[];
574
+ nextCursor: string | null;
575
+ };
576
+ };
577
+ const idsOf = (page: { posts: { id: string }[] }) =>
578
+ page.posts.map((post) => post.id);
579
+
580
+ // Each page ends on a Thread that is gone before the next request:
581
+ // deleted, unpublished, then taken out of the collection.
582
+ const page1 = await readPage();
583
+ expect(idsOf(page1)).toEqual(ids.slice(0, 2));
584
+ await services.posts.delete(ids[1] ?? "");
585
+
586
+ const page2 = await readPage(page1.nextCursor ?? undefined);
587
+ expect(idsOf(page2)).toEqual(ids.slice(2, 4));
588
+ await services.posts.update(ids[3] ?? "", { status: "draft" });
589
+
590
+ const page3 = await readPage(page2.nextCursor ?? undefined);
591
+ expect(idsOf(page3)).toEqual(ids.slice(4, 6));
592
+ await services.collections.removeThread(collection.id, ids[5] ?? "");
593
+
594
+ const page4 = await readPage(page3.nextCursor ?? undefined);
595
+ expect(idsOf(page4)).toEqual(ids.slice(6));
596
+ expect(page4.nextCursor).toBeNull();
597
+ });
598
+
599
+ it("resumes a collection from a bare root ID it lists, and rejects any other", async () => {
600
+ const { app, services } = createTestApp({ authenticated: false });
601
+ app.route("/api/public/posts", publicPostsApiRoutes);
602
+
603
+ const collection = await services.collections.create({
604
+ slug: "legacy",
605
+ title: "Legacy",
606
+ });
607
+ const inCollection = async (title: string, n: number) =>
608
+ services.posts.create({
609
+ format: "note",
610
+ title,
611
+ bodyMarkdown: title,
612
+ collectionIds: [collection.id],
613
+ publishedAt: Date.UTC(2025, 0, n) / 1000,
614
+ });
615
+ const first = await inCollection("First", 1);
616
+ const second = await inCollection("Second", 2);
617
+ const third = await inCollection("Third", 3);
618
+
619
+ // The bare root ID earlier releases returned as `nextCursor`.
620
+ const legacy = await app.request(
621
+ `/api/public/posts?collection=legacy&cursor=${third.id}`,
376
622
  );
377
- expect(secondRes.status).toBe(200);
378
- const second = await secondRes.json();
379
- expect(second.posts[0].id).toBe(middle.id);
380
- expect(second.nextCursor).toBe(middle.id);
623
+ expect(legacy.status).toBe(200);
624
+ const body = await legacy.json();
625
+ expect(body.posts.map((post: { id: string }) => post.id)).toEqual([
626
+ second.id,
627
+ first.id,
628
+ ]);
629
+ expect(body.nextCursor).toBeNull();
630
+
631
+ const privateRoot = await services.posts.create({
632
+ format: "note",
633
+ title: "Private",
634
+ bodyMarkdown: "private",
635
+ collectionIds: [collection.id],
636
+ visibility: "private",
637
+ });
638
+ const draftRoot = await services.posts.create({
639
+ format: "note",
640
+ title: "Draft",
641
+ bodyMarkdown: "draft",
642
+ collectionIds: [collection.id],
643
+ status: "draft",
644
+ });
645
+ const elsewhere = await services.posts.create({
646
+ format: "note",
647
+ title: "Not in the collection",
648
+ bodyMarkdown: "elsewhere",
649
+ });
650
+ const deleted = await inCollection("Deleted", 4);
651
+ await services.posts.delete(deleted.id);
652
+
653
+ const answers = [];
654
+ for (const id of [
655
+ privateRoot.id,
656
+ draftRoot.id,
657
+ elsewhere.id,
658
+ deleted.id,
659
+ createEntityId("post"),
660
+ ]) {
661
+ const res = await app.request(
662
+ `/api/public/posts?collection=legacy&cursor=${id}`,
663
+ );
664
+ expect(res.status).toBe(400);
665
+ const error = await res.json();
666
+ expect(error.code).toBe("VALIDATION_ERROR");
667
+ answers.push(error.error);
668
+ }
669
+ // Nothing in the answer tells a private Thread from one that never
670
+ // existed.
671
+ expect(new Set(answers).size).toBe(1);
672
+ });
673
+
674
+ it("rejects a cursor from a collection in another order, or from Latest", async () => {
675
+ const { app, services } = createTestApp({ authenticated: false });
676
+ app.route("/api/public/posts", publicPostsApiRoutes);
677
+
678
+ const collection = await services.collections.create({
679
+ slug: "ordered",
680
+ title: "Ordered",
681
+ });
682
+ for (const n of [1, 2, 3]) {
683
+ await services.posts.create({
684
+ format: "note",
685
+ title: `Thread ${n}`,
686
+ bodyMarkdown: `thread ${n}`,
687
+ collectionIds: [collection.id],
688
+ publishedAt: Date.UTC(2025, 0, n) / 1000,
689
+ });
690
+ }
691
+ const cursorOf = async (path: string) =>
692
+ (await (await app.request(path)).json()).nextCursor as string;
381
693
 
382
- const thirdRes = await app.request(
383
- `/api/public/posts?collection=rated-threads&limit=1&cursor=${second.nextCursor}`,
694
+ const newest = await cursorOf(
695
+ "/api/public/posts?collection=ordered&sort=newest&limit=1",
384
696
  );
385
- expect(thirdRes.status).toBe(200);
386
- const third = await thirdRes.json();
387
- expect(third.posts[0].id).toBe(lowest.id);
697
+ const latest = await cursorOf("/api/public/posts?limit=1");
698
+ for (const path of [
699
+ `/api/public/posts?collection=ordered&sort=oldest&cursor=${newest}`,
700
+ `/api/public/posts?collection=ordered&cursor=${latest}`,
701
+ `/api/public/posts?cursor=${newest}`,
702
+ ]) {
703
+ const res = await app.request(path);
704
+ expect(res.status, path).toBe(400);
705
+ expect((await res.json()).error).toMatch(/different order/);
706
+ }
707
+ for (const cursor of ["garbage", "eyJ2IjoxfQ", "pst_"]) {
708
+ const res = await app.request(
709
+ `/api/public/posts?collection=ordered&cursor=${cursor}`,
710
+ );
711
+ expect(res.status).toBe(400);
712
+ }
388
713
  });
389
714
 
390
715
  it("keeps root format and Latest visibility filters for Collections", async () => {