import { describe, expect, it } from "vitest"; import { collectCatalog } from "../../../src/features/tech-log/application/collect-catalog.ts"; import type { CatalogQuery, StudioGateway, } from "../../../src/features/tech-log/application/ports/studio-gateway.ts"; import type { CatalogPage } from "../../../src/features/tech-log/contracts/studio/contract.ts"; type Entry = CatalogPage["items"][number]; function entry(index: number): Entry { return { id: `00000000-0000-4000-8000-${String(index).padStart(12, "0")}`, type: "RELATION", label: `기록 ${index}`, dependencyRevision: `relation:${index}`, }; } /** 서버를 흉내 낸다 — 한 번에 100개까지만 주고, 더 있으면 커서를 함께 준다. */ function gatewayHolding(total: number) { const all = Array.from({ length: total }, (_, index) => entry(index)); const calls: Array = []; const gateway: Pick = { getCatalog(query: CatalogQuery) { calls.push(query.cursor); const limit = query.limit ?? 20; const from = query.cursor === undefined ? 0 : Number(query.cursor); const items = all.slice(from, from + limit); const next = from + limit; return Promise.resolve({ items, nextCursor: next < all.length ? String(next) : null, }); }, }; return { gateway, calls }; } describe("catalog 는 커서를 끝까지 따라간다", () => { /* 화면들은 `limit: 100` 으로 한 번만 부르고 있었다. 100 은 계약의 상한이라 더 크게 부를 수도 없어서, 기록이 101개가 된 날 101번째부터는 관계 대상 선택에 아예 나타나지 않았다 — 화면에는 아무 표시도 없이 "고를 수 없는 기록" 이 생겼다. */ it("상한을 넘는 기록도 빠짐없이 모은다", async () => { const { gateway, calls } = gatewayHolding(105); const items = await collectCatalog(gateway, { type: "RELATION" }); expect(items).toHaveLength(105); expect(new Set(items.map((item) => item.id)).size).toBe(105); expect(items.at(-1)?.label).toBe("기록 104"); expect(calls).toEqual([undefined, "100"]); }); it("한 쪽으로 끝나면 한 번만 부른다", async () => { const { gateway, calls } = gatewayHolding(4); const items = await collectCatalog(gateway, { type: "PROJECT" }); expect(items).toHaveLength(4); expect(calls).toEqual([undefined]); }); /** 서버가 커서를 계속 주더라도 화면이 영원히 기다리지 않는다. */ it("커서가 끝나지 않아도 멈춘다", async () => { let calls = 0; const endless: Pick = { getCatalog() { calls += 1; return Promise.resolve({ items: [entry(calls)], nextCursor: `cursor-${calls}` }); }, }; const items = await collectCatalog(endless, { type: "TOPIC" }); expect(calls).toBe(50); expect(items).toHaveLength(50); }); });