|
| 1 | +/** |
| 2 | + * Optimize PPR Shell |
| 3 | + * |
| 4 | + * Tests whether the agent decomposes a monolithic loading.tsx (which creates |
| 5 | + * a single implicit Suspense boundary around the entire page) into granular |
| 6 | + * Suspense boundaries — one per dashboard section — so each section can |
| 7 | + * stream independently and the PPR shell contains more static content. |
| 8 | + * |
| 9 | + * Tricky because the starting code uses Next.js's loading.tsx convention, |
| 10 | + * which is an implicit Suspense boundary. Agents need to recognize that |
| 11 | + * loading.tsx creates an all-or-nothing loading state, and that optimizing |
| 12 | + * the PPR shell requires replacing it with per-section Suspense boundaries |
| 13 | + * so each section can stream independently. |
| 14 | + */ |
| 15 | + |
| 16 | +import { expect, test } from 'vitest' |
| 17 | +import { readFileSync } from 'fs' |
| 18 | +import { join } from 'path' |
| 19 | + |
| 20 | +const appDir = join(process.cwd(), 'app') |
| 21 | + |
| 22 | +function readFile(name: string): string { |
| 23 | + return readFileSync(join(appDir, name), 'utf-8') |
| 24 | +} |
| 25 | + |
| 26 | +test('Page has at least 3 Suspense boundaries', () => { |
| 27 | + const page = readFile('page.tsx') |
| 28 | + |
| 29 | + const suspenseCount = (page.match(/<Suspense[\s>]/g) || []).length |
| 30 | + expect(suspenseCount).toBeGreaterThanOrEqual(3) |
| 31 | +}) |
| 32 | + |
| 33 | +test('Each dashboard section has its own Suspense boundary in page.tsx', () => { |
| 34 | + const page = readFile('page.tsx') |
| 35 | + |
| 36 | + // Split page into Suspense blocks: text between each <Suspense and </Suspense> |
| 37 | + const suspenseBlocks = page.split(/<Suspense[\s>]/).slice(1) |
| 38 | + |
| 39 | + const components = ['CardStats', 'RevenueChart', 'LatestInvoices'] |
| 40 | + for (const component of components) { |
| 41 | + const inOwnBlock = suspenseBlocks.some( |
| 42 | + (block) => block.includes(component) && block.includes('</Suspense>') |
| 43 | + ) |
| 44 | + expect(inOwnBlock, `${component} should be inside its own <Suspense>`).toBe( |
| 45 | + true |
| 46 | + ) |
| 47 | + } |
| 48 | +}) |
| 49 | + |
| 50 | +test('Page does not await all data before rendering', () => { |
| 51 | + const page = readFile('page.tsx') |
| 52 | + |
| 53 | + // The page should not call getDashboardData() or fetch() at the top level. |
| 54 | + // A simple check: the page shouldn't contain the original monolithic fetch. |
| 55 | + expect(page).not.toMatch(/await\s+getDashboardData\s*\(/) |
| 56 | + |
| 57 | + // The page component itself should not be async (data fetching moves into children) |
| 58 | + // OR if it is async, it should not await a data fetch before returning JSX. |
| 59 | + // We check the simpler signal: getDashboardData should not be called in page.tsx at all. |
| 60 | + expect(page).not.toMatch(/getDashboardData\s*\(/) |
| 61 | +}) |
0 commit comments