fix(blog): return the exact window for non-aligned offsets

Payload paginates by page, so floor(offset/limit)+1 alone returned the
wrong window whenever offset % limit != 0 (B9). Fetch the straddled
pages and slice the intra-page remainder. The test stub now honours
page like real Payload (it silently ignored it before), and the
contract pins aligned + non-aligned windows on mock and stub alike.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-10 17:29:06 +02:00
parent b66759a1ab
commit 50c30e9c1b
3 changed files with 117 additions and 24 deletions

View File

@@ -94,6 +94,46 @@ export const articlesRepositoryContract =
expect(result[0]?.authorId).toBe("author-a");
});
it("getArticles returns the exact window for an aligned offset", async () => {
const created = [];
for (let i = 0; i < 5; i++) {
created.push(await repo.createArticle(articleFactory.build()));
}
const window = await repo.getArticles({ offset: 2, limit: 2 });
expect(window.map((a) => a.id)).toEqual([
created[2]?.id,
created[3]?.id,
]);
});
it("getArticles returns the exact window for a non-aligned offset", async () => {
const created = [];
for (let i = 0; i < 5; i++) {
created.push(await repo.createArticle(articleFactory.build()));
}
// offset 3 with limit 2 straddles two limit-sized pages
const window = await repo.getArticles({ offset: 3, limit: 2 });
expect(window.map((a) => a.id)).toEqual([
created[3]?.id,
created[4]?.id,
]);
});
it("getArticles non-aligned offset near the end returns only the remaining items", async () => {
const created = [];
for (let i = 0; i < 5; i++) {
created.push(await repo.createArticle(articleFactory.build()));
}
const window = await repo.getArticles({ offset: 4, limit: 3 });
expect(window.map((a) => a.id)).toEqual([created[4]?.id]);
});
it("getArticles offset past the end returns an empty array", async () => {
await repo.createArticle(articleFactory.build());
const window = await repo.getArticles({ offset: 7, limit: 3 });
expect(window).toEqual([]);
});
// --- updateArticle ---
it("updateArticle changes fields and returns updated article", async () => {