Skip to content
16 changes: 16 additions & 0 deletions .changeset/28085-home-boot-reads-once.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@object-ui/app-shell': patch
'@object-ui/console': patch
---

fix(app-shell): Home sends the deployment notice, authorized-clients and API-key reads once per load

Clause-②: no
Metadata-protocol: none

On a built Console, a cold load of Home sent `GET /api/settings/deployment/notice` twice and `GET /api/v1/auth/authorized-clients` and the `sys_api_key` list three times each. The repeats were not concurrent, so sharing an in-flight request could not remove them. Each had its own cause:

- **Home was mounted twice.** Home waits on the launcher's `pending` flag so that it never draws an app tile that `/me/apps` then hides. The `LauncherAppsProvider` set that flag from its effect, which runs after the first commit. So the first frame had nothing pending and nothing narrowed: Home drew its page, then switched to its loading state, then drew the page again. Everything inside it mounted twice, and the deployment notice and the Connect an AI Assistant card sent their reads twice. The first read is now `pending` from the render that makes it due. Home draws its page once, after the answer, and a hidden app's tile is never drawn in that first frame.
- **Two readers of one answer.** Home draws the Connect an AI Assistant card and the administrator's first-run checklist together. Both read "has this person connected an assistant" through `useConnectedAccess`, and each sent its own two requests. Every reader mounted at the same time now shares one pair of reads. A change to either record re-reads it once for all of them. The shared answer is dropped with its last reader, so a page mounted later reads afresh.

A list page load already read every URL once on a built Console, so nothing changes there.
181 changes: 181 additions & 0 deletions ui/apps/console/src/__tests__/App.listPageBootReadsOnce-28085.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* objectstack#28085 — a console list page load asks `auth/get-session`,
* `meta/app`, `auth/me/permissions` and `me/apps` once each.
*
* ## What is real here
*
* The boot order `main.tsx` runs: the pre-React `preflightAuth` validating the
* stored Bearer, then `App` — the real `AuthProvider` with its own better-auth
* client, `AuthGuard`, `ProtectedRoute`, `ConsoleShell`, `ConnectedShell` with
* the real `ObjectStackAdapter` and `MetadataProvider`, `RequireOrganization`,
* `ConsolePermissionsScope` (the `/me/permissions` provider) and the console's
* `LauncherAppsProvider` (`/me/apps`). The network is one stubbed global
* `fetch` that records every URL and answers it, so a count is of requests that
* would reach the wire. No `React.StrictMode`: a built console does not replay
* effects, and the doubling this card was filed on was that replay behind the
* Vite dev server.
*
* The app's own content is a stub: the four reads under test are the shell's,
* which every list page shares, not the list view's.
*/

import '@testing-library/jest-dom/vitest';
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { render, screen, cleanup, waitFor, act } from '@testing-library/react';
import type { ReactNode } from 'react';
// Paid at collection, not inside the mock factory's clocked first call.
import '@object-ui/react';
import '@object-ui/app-shell';

const ORG = { id: 'org_1', name: 'Acme', slug: 'acme' };
const USER = { id: 'u1', name: 'Ada', email: 'ada@example.test' };
const TOKEN = 'tok_valid';
const SESSION_BODY = {
user: USER,
session: { id: 's_1', userId: USER.id, token: TOKEN, activeOrganizationId: ORG.id, expiresAt: '2099-01-01T00:00:00.000Z' },
};

/** The one app the deployment serves; the list page under test is one of its objects. */
const APPS = [{ name: 'crm', label: 'CRM', active: true, isDefault: true }];
const LIST_PAGE = '/apps/crm/crm_contact';

function json(body: unknown, status = 200): Response {
return new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } });
}

const urlOf = (input: RequestInfo | URL): string =>
typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;

const calls: string[] = [];
const fetchStub = vi.fn(async (input: RequestInfo | URL) => {
const url = urlOf(input);
calls.push(url);
if (url.includes('/auth/get-session')) return json(SESSION_BODY);
if (url.includes('/auth/organization/list')) return json([ORG]);
if (url.includes('/auth/organization/get-active-member')) {
return json({ id: 'm_1', organizationId: ORG.id, userId: USER.id, role: 'member' });
}
if (url.includes('/auth/me/permissions')) {
return json({
authenticated: true, userId: USER.id, tenantId: ORG.id, positions: [], permissionSets: [], objects: {}, fields: {}, hasAnyQueue: false,
});
}
if (url.includes('/auth/')) return json({});
if (url.includes('/api/v1/discovery')) {
return json({
success: true,
data: { version: '1.0.0', apiName: 'v1', routes: { data: '/api/v1/data', metadata: '/api/v1/meta' }, services: {} },
});
}
if (url.includes('/me/apps')) return json({ apps: APPS });
if (/\/api\/v1\/meta\/app(\?|$)/.test(url)) return json({ success: true, data: { type: 'app', items: APPS } });
if (url.includes('/api/v1/meta/')) return json({ success: true, data: { items: [] } });
if (url.includes('/api/v1/data/')) return json({ success: true, data: { records: [], total: 0 } });
return json({ success: true, data: {} });
});

vi.mock('@object-ui/app-shell', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
ConsoleToaster: () => null,
DefaultHomeLayout: ({ children }: { children?: ReactNode }) => <>{children}</>,
DefaultHomePage: () => <div data-testid="home-page" />,
DefaultAppContent: () => <div data-testid="app-content" />,
}));

vi.mock('../LocalizationFetchProvider', () => ({
LocalizationFetchProvider: ({ children }: { children?: ReactNode }) => <>{children}</>,
}));
vi.mock('../pages/settings/FirstRunChecklist', () => ({ FirstRunChecklist: () => null }));
vi.mock('../components/SetupRoute', () => ({ SetupRoute: () => null }));
vi.mock('../components/FormPage', () => ({ FormPage: () => null }));
vi.mock('../components/InternalFormRoute', () => ({ InternalFormRoute: () => null, InAppFormRoute: () => null }));
vi.mock('../components/MetadataHmrReloader', () => ({ MetadataHmrReloader: () => null }));
vi.mock('../components/FaviconSync', () => ({ FaviconSync: () => null }));
vi.mock('../pages/SharedRecordPage', () => ({ default: () => null }));
vi.mock('../pages/auth/LoginPage', () => ({ LoginPage: () => <div data-testid="login-page" /> }));
vi.mock('../pages/auth/RegisterPage', () => ({ RegisterPage: () => null }));
vi.mock('../pages/auth/ForgotPasswordPage', () => ({ ForgotPasswordPage: () => null }));
vi.mock('../pages/auth/ResetPasswordPage', () => ({ ResetPasswordPage: () => null }));
vi.mock('../pages/auth/SetPasswordPage', () => ({ SetPasswordPage: () => null }));
vi.mock('../pages/auth/VerifyEmailPage', () => ({ VerifyEmailPage: () => null }));
vi.mock('../pages/auth/VerifyEmailPromptPage', () => ({ VerifyEmailPromptPage: () => null }));
vi.mock('../pages/auth/OAuthConsentPage', () => ({ OAuthConsentPage: () => null }));
vi.mock('../pages/auth/DeviceAuthPage', () => ({ DeviceAuthPage: () => null }));
vi.mock('../dev/DevMasterDetail', () => ({ DevMasterDetail: () => null }));
vi.mock('../dev/DevLists', () => ({ DevLists: () => null }));
vi.mock('../dev/DevModal', () => ({ DevModal: () => null }));
vi.mock('../dev/DevLookup', () => ({ DevLookup: () => null }));
vi.mock('../dev/DevRowActions', () => ({ DevRowActions: () => null }));

import { App } from '../App';
import { preflightAuth } from '../lib/auth-preflight';

const pathOf = (url: string) => {
try {
return new URL(url, 'http://console.test').pathname;
} catch {
return url;
}
};
const readsOf = (test: (path: string) => boolean) => calls.filter((u) => test(pathOf(u)));
const FOUR = {
'auth/get-session': (p: string) => p.endsWith('/api/v1/auth/get-session'),
'meta/app': (p: string) => p.endsWith('/api/v1/meta/app'),
'auth/me/permissions': (p: string) => p.endsWith('/api/v1/auth/me/permissions'),
'me/apps': (p: string) => p.endsWith('/api/v1/me/apps'),
} as const;
const countFour = () =>
Object.fromEntries(Object.entries(FOUR).map(([name, test]) => [name, readsOf(test).length]));

/** Let the effects and the answers they await run out (a count of one needs it). */
async function settle() {
for (let i = 0; i < 10; i++) {
await act(async () => {
await new Promise((r) => setTimeout(r, 10));
});
}
}

/** The boot `main.tsx` runs, landing on a list page. */
async function openListPage() {
localStorage.setItem('auth-session-token', TOKEN);
window.history.pushState({}, '', LIST_PAGE);
await preflightAuth('/api/v1/auth');
render(<App />);
await waitFor(() => expect(screen.getByTestId('app-content')).toBeInTheDocument(), { timeout: 5000 });
// Each read is sent from an effect, which a busy run can flush after the DOM
// shows the page: wait until all four have gone out, then let the load run out.
await waitFor(() => {
for (const test of Object.values(FOUR)) expect(readsOf(test).length).toBeGreaterThan(0);
});
await settle();
}

beforeEach(() => {
calls.length = 0;
fetchStub.mockClear();
vi.stubGlobal('fetch', fetchStub);
localStorage.clear();
sessionStorage.clear();
delete (globalThis as unknown as Record<symbol, unknown>)[Symbol.for('objectui.authPreflightSession')];
});
afterEach(() => {
cleanup();
vi.unstubAllGlobals();
window.history.pushState({}, '', '/');
});

describe('a console list page load asks each of its four boot reads once (objectstack#28085)', () => {
it('get-session, meta/app, me/permissions and me/apps go out once each', async () => {
await openListPage();
expect(window.location.pathname).toBe(LIST_PAGE);
expect(countFour()).toEqual({
'auth/get-session': 1,
'meta/app': 1,
'auth/me/permissions': 1,
'me/apps': 1,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import '@testing-library/jest-dom/vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react';
import { act, cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react';

vi.mock('@object-ui/i18n', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
Expand Down Expand Up @@ -60,7 +60,7 @@ vi.mock('@object-ui/auth', async (importOriginal) => ({

import { notifyDataChanged } from '@object-ui/react';
import { ConnectAgent } from './ConnectAgentWidget';
import { combineConnectedReads, type ConnectedEntry } from './connectedAccess';
import { combineConnectedReads, useConnectedAccess, type ConnectedEntry } from './connectedAccess';

type Row = Record<string, unknown>;
type Client = {
Expand Down Expand Up @@ -303,3 +303,58 @@ describe('§6 combineConnectedReads', () => {
expect(result.state === 'ready' && result.entries.map((e) => e.id)).toEqual(['d', 'b', 'a', 'c']);
});
});

/**
* objectstack#28085 — Home mounts more than one reader of this answer (the
* Connect an AI Assistant card and the administrator's checklist), and they
* mount at different moments of one load. Every reader mounted at the same
* time shares one pair of reads; a page mounted after the last reader left
* reads afresh.
*/
describe('§7 one pair of reads per page, however many readers', () => {
function Reader({ id }: { id: string }) {
const access = useConnectedAccess();
return <div data-testid={id}>{access.state === 'ready' ? String(access.connected) : access.state}</div>;
}

it('two readers mounted together send each request once', async () => {
granted = [DESKTOP];
render(<><Reader id="a" /><Reader id="b" /></>);
await waitFor(() => expect(screen.getByTestId('a')).toHaveTextContent('true'));
expect(screen.getByTestId('b')).toHaveTextContent('true');
expect(listMock).toHaveBeenCalledTimes(1);
expect(findMock).toHaveBeenCalledTimes(1);
});

it('a reader mounting after the answer landed joins it instead of asking again', async () => {
granted = [DESKTOP];
const { rerender } = render(<Reader id="a" />);
await waitFor(() => expect(screen.getByTestId('a')).toHaveTextContent('true'));
rerender(<><Reader id="a" /><Reader id="late" /></>);
expect(screen.getByTestId('late')).toHaveTextContent('true');
expect(listMock).toHaveBeenCalledTimes(1);
expect(findMock).toHaveBeenCalledTimes(1);
});

it('a change to either record re-reads it once for every reader', async () => {
render(<><Reader id="a" /><Reader id="b" /></>);
await waitFor(() => expect(screen.getByTestId('a')).toHaveTextContent('false'));
keyRows = [{ ...LIVE_KEY }];
act(() => notifyDataChanged({ objectName: 'sys_api_key' }));
await waitFor(() => expect(screen.getByTestId('b')).toHaveTextContent('true'));
expect(screen.getByTestId('a')).toHaveTextContent('true');
expect(findMock).toHaveBeenCalledTimes(2);
expect(listMock).toHaveBeenCalledTimes(1);
});

it('a page mounted after every reader left reads afresh', async () => {
const first = render(<Reader id="a" />);
await waitFor(() => expect(screen.getByTestId('a')).toHaveTextContent('false'));
first.unmount();
granted = [DESKTOP];
render(<Reader id="again" />);
await waitFor(() => expect(screen.getByTestId('again')).toHaveTextContent('true'));
expect(listMock).toHaveBeenCalledTimes(2);
expect(findMock).toHaveBeenCalledTimes(2);
});
});
Loading
Loading