|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#35416 — the console root starts a record route's reads. |
| 5 | + * |
| 6 | + * `AppContent` mounts below the session's permission read (`/auth/me/permissions`), |
| 7 | + * so the page list and the package object list a record route waits for used |
| 8 | + * to leave one round trip after that read answered. The root's prefetch |
| 9 | + * (`useAppObjectScopePrefetch`, mounted beside the permission read) now asks |
| 10 | + * them as soon as the app list names the app: the page list on every record |
| 11 | + * route, and the package list beside it under the same rule `AppContent` |
| 12 | + * follows (objectstack#35455: not when the bundle answers a miss). |
| 13 | + * |
| 14 | + * Only the root's prefetch is mounted here, so every read below is one the |
| 15 | + * root asked before `AppContent` exists. |
| 16 | + * |
| 17 | + * CONTROLs: an object's list route asks no page list; a bundle that is a miss |
| 18 | + * starts no package list beside the page list. |
| 19 | + */ |
| 20 | + |
| 21 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 22 | +import { act, cleanup, render } from '@testing-library/react'; |
| 23 | +import { MemoryRouter } from 'react-router-dom'; |
| 24 | +import { MetadataProvider } from '../../providers/MetadataProvider'; |
| 25 | +import { useAppObjectScopePrefetch } from '../appObjectScope'; |
| 26 | + |
| 27 | +const CRM = 'app.crm'; |
| 28 | +const APPS = [{ name: 'crm', _packageId: CRM, navigation: [] }]; |
| 29 | +const OBJECTS = [{ name: 'crm_account', _packageId: CRM, fields: {} }]; |
| 30 | + |
| 31 | +type Adapter = Parameters<typeof MetadataProvider>[0]['adapter']; |
| 32 | + |
| 33 | +/** Every metadata read is held until `release`; `pending` names the held ones. */ |
| 34 | +function makeHeldAdapter(options: { bundleOnTheWire: boolean }) { |
| 35 | + const held: Array<{ label: string; go: () => void }> = []; |
| 36 | + const hold = <T,>(label: string, answer: () => T) => |
| 37 | + new Promise<T>((resolve) => held.push({ label, go: () => resolve(answer()) })); |
| 38 | + const list = (type: string, packageId?: string) => { |
| 39 | + const all: any[] = type === 'app' ? APPS : type === 'object' ? OBJECTS : []; |
| 40 | + return { type, items: structuredClone(packageId ? all.filter((o) => o._packageId === packageId) : all) }; |
| 41 | + }; |
| 42 | + const adapter = { |
| 43 | + clearCache: vi.fn(), |
| 44 | + invalidateCache: vi.fn(), |
| 45 | + getObjectSchema: (name: string) => hold(`object/${name}`, () => OBJECTS.find((o) => o.name === name)), |
| 46 | + getClient: () => ({ |
| 47 | + meta: { |
| 48 | + getItems: (type: string, opts?: { packageId?: string; names?: readonly string[] }) => |
| 49 | + hold( |
| 50 | + opts?.names ? `${type}?names=${opts.names.join(',')}` : opts?.packageId ? `${type}?package=${opts.packageId}` : type, |
| 51 | + () => list(type, opts?.packageId), |
| 52 | + ), |
| 53 | + getItem: (type: string, name: string) => hold(`${type}/${name}`, () => ({ item: undefined })), |
| 54 | + }, |
| 55 | + }), |
| 56 | + // A bundle still on the wire: the provider answers its reads `pending`. |
| 57 | + ...(options.bundleOnTheWire |
| 58 | + ? { peekAppBundle: () => ({ status: 'loading', settled: new Promise(() => undefined) }) } |
| 59 | + : {}), |
| 60 | + } as unknown as Adapter; |
| 61 | + return { adapter, pending: () => held.map((h) => h.label).sort(), release: () => held.splice(0).forEach((h) => h.go()) }; |
| 62 | +} |
| 63 | + |
| 64 | +function RootPrefetch() { |
| 65 | + useAppObjectScopePrefetch(); |
| 66 | + return null; |
| 67 | +} |
| 68 | + |
| 69 | +const settle = async () => { |
| 70 | + for (let i = 0; i < 6; i++) await act(() => new Promise((resolve) => setTimeout(resolve, 0))); |
| 71 | +}; |
| 72 | + |
| 73 | +/** The reads the root asks in each wave, for `waveCount` waves. */ |
| 74 | +async function rootWaves(url: string, bundleOnTheWire: boolean, waveCount = 2): Promise<string[][]> { |
| 75 | + sessionStorage.clear(); |
| 76 | + const transport = makeHeldAdapter({ bundleOnTheWire }); |
| 77 | + render( |
| 78 | + <MetadataProvider adapter={transport.adapter}> |
| 79 | + <MemoryRouter initialEntries={[url]}> |
| 80 | + <RootPrefetch /> |
| 81 | + </MemoryRouter> |
| 82 | + </MetadataProvider>, |
| 83 | + ); |
| 84 | + const seen: string[][] = []; |
| 85 | + await settle(); |
| 86 | + for (let i = 0; i < waveCount; i++) { |
| 87 | + seen.push(transport.pending()); |
| 88 | + transport.release(); |
| 89 | + await settle(); |
| 90 | + } |
| 91 | + return seen; |
| 92 | +} |
| 93 | + |
| 94 | +afterEach(() => cleanup()); |
| 95 | + |
| 96 | +describe('objectstack#35416 — the console root starts a record route\'s reads', () => { |
| 97 | + it('a record route: the app list, then the page list and the package list together, before AppContent mounts', async () => { |
| 98 | + const [first, second] = await rootWaves('/apps/crm/crm_account/record/r1', true); |
| 99 | + expect(first).toEqual(['app']); |
| 100 | + expect(second).toEqual(expect.arrayContaining(['page', `object?package=${CRM}`])); |
| 101 | + }); |
| 102 | + |
| 103 | + it("CONTROL: a bundle that is a miss starts the page list alone, as AppContent's rule says (objectstack#35455)", async () => { |
| 104 | + const [first, second] = await rootWaves('/apps/crm/crm_account/record/r1', false); |
| 105 | + expect(first).toEqual(['app']); |
| 106 | + expect(second).toContain('page'); |
| 107 | + expect(second).not.toContain(`object?package=${CRM}`); |
| 108 | + }); |
| 109 | + |
| 110 | + it("CONTROL: an object's list route asks no page list at the root", async () => { |
| 111 | + const waves = await rootWaves('/apps/crm/crm_account', true, 3); |
| 112 | + expect(waves.flat()).not.toContain('page'); |
| 113 | + expect(waves[1]).toContain(`object?package=${CRM}`); |
| 114 | + }); |
| 115 | +}); |
0 commit comments