Skip to content

Commit 19a48b3

Browse files
committed
test(console): pin push registration, the router wiring and the before-sign-out steps
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L6bhgWPe8iKWPE5j7CLjdF
1 parent f52567f commit 19a48b3

4 files changed

Lines changed: 535 additions & 9 deletions

File tree

‎ui/apps/console/src/__tests__/App.nativeShellMode-29039.test.tsx‎

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,20 @@ import { describe, expect, it, vi, afterEach, beforeEach } from 'vitest';
2626
import { render, screen, cleanup, act } from '@testing-library/react';
2727
import type { ReactNode } from 'react';
2828
import {
29+
NATIVE_CONNECTION_REMOVED_EVENT,
2930
NATIVE_HOST_GLOBAL,
3031
NATIVE_NAVIGATE_EVENT,
3132
resetNativeHostEventsForTests,
3233
type ObjectStackNative,
3334
} from '../../../../packages/mobile/src/nativeHost';
3435
import { resetNativeHostWatchForTests } from '../../../../packages/mobile/src/useNativeHost';
36+
import {
37+
NATIVE_PUSH_PROMPT_DELAY_MS,
38+
NATIVE_PUSH_REGISTRATION_KEY,
39+
resetNativePushRegistrationForTests,
40+
} from '../nativePushRegistration';
3541

36-
const { passthrough, stub, shellState } = vi.hoisted(() => {
42+
const { passthrough, stub, shellState, authState } = vi.hoisted(() => {
3743
const passthrough = ({ children }: { children?: ReactNode }) => <>{children}</>;
3844
const stub = (testid: string) => () => <div data-testid={testid} />;
3945
// objectstack#29750: what the shell's navigation context and the metadata
@@ -45,7 +51,10 @@ const { passthrough, stub, shellState } = vi.hoisted(() => {
4551
loading: false,
4652
appStatus: undefined as 'idle' | 'loading' | 'ready' | 'error' | undefined,
4753
};
48-
return { passthrough, stub, shellState };
54+
// objectstack#29042: who the auth context names. The default is what this
55+
// file's other cases read: a user id and no session answer.
56+
const authState = { value: { user: { id: 'u1' } } as Record<string, unknown> };
57+
return { passthrough, stub, shellState, authState };
4958
});
5059

5160
vi.mock('@object-ui/app-shell', async (importOriginal) => ({
@@ -90,7 +99,7 @@ vi.mock('@object-ui/app-shell', async (importOriginal) => ({
9099
vi.mock('@object-ui/auth', async (importOriginal) => ({
91100
...(await importOriginal<Record<string, unknown>>()),
92101
AuthProvider: passthrough,
93-
useAuth: () => ({ user: { id: 'u1' } }),
102+
useAuth: () => authState.value,
94103
}));
95104

96105
vi.mock('../AppContent', () => ({
@@ -166,6 +175,9 @@ beforeEach(() => {
166175
shellState.apps = [];
167176
shellState.loading = false;
168177
shellState.appStatus = undefined;
178+
authState.value = { user: { id: 'u1' } };
179+
resetNativePushRegistrationForTests();
180+
sessionStorage.clear();
169181
window.history.pushState({}, '', '/login');
170182
});
171183

@@ -358,3 +370,76 @@ describe('the catch-all hosts an app-relative first URL under an app', () => {
358370
expect(window.location.pathname).toBe('/home');
359371
});
360372
});
373+
374+
/**
375+
* objectstack#29042 — the router wiring of push registration: the route the
376+
* router is on decides when the token is asked for, and the shell's removal of
377+
* this connection is answered with the unregister, under the Console's session.
378+
*/
379+
describe('push registration through the router wiring', () => {
380+
const signedIn = { user: { id: 'u1' }, session: { token: 'session' }, isAuthenticated: true };
381+
382+
function deviceCalls(fetchMock: ReturnType<typeof vi.fn>): string[] {
383+
return fetchMock.mock.calls
384+
.map(([url, init]) => `${(init as RequestInit | undefined)?.method ?? 'GET'} ${String(url)}`)
385+
.filter((call) => call.includes('/api/v1/notifications/devices'));
386+
}
387+
388+
function stubFetch() {
389+
const fetchMock = vi.fn(async (_url: string, init?: RequestInit) => ({
390+
ok: true,
391+
status: 200,
392+
headers: new Headers(),
393+
json: async () => ({
394+
success: true,
395+
data: init?.method === 'POST' ? { deviceId: 'install-7', success: true } : { success: true },
396+
}),
397+
}));
398+
vi.stubGlobal('fetch', fetchMock);
399+
return fetchMock;
400+
}
401+
402+
afterEach(() => {
403+
vi.unstubAllGlobals();
404+
});
405+
406+
it('Home asks the shell for the token once the delay has passed, and registers it', async () => {
407+
const fetchMock = stubFetch();
408+
authState.value = signedIn;
409+
installBridge();
410+
const bridge = window[NATIVE_HOST_GLOBAL as keyof Window] as unknown as ObjectStackNative;
411+
const getPushToken = vi.spyOn(bridge, 'getPushToken').mockResolvedValue({ token: 'tok-1', vendor: 'fcm' });
412+
window.history.pushState({}, '', '/home');
413+
render(<App />);
414+
await screen.findByTestId('home-page');
415+
expect(getPushToken).not.toHaveBeenCalled();
416+
await vi.waitFor(
417+
() => expect(deviceCalls(fetchMock)).toEqual([expect.stringMatching(/^POST .*\/api\/v1\/notifications\/devices$/)]),
418+
{ timeout: NATIVE_PUSH_PROMPT_DELAY_MS + 2000 },
419+
);
420+
expect(getPushToken).toHaveBeenCalledTimes(1);
421+
});
422+
423+
it('the shell removing this connection waits for the unregister of the registered device', async () => {
424+
const fetchMock = stubFetch();
425+
authState.value = signedIn;
426+
sessionStorage.setItem(
427+
NATIVE_PUSH_REGISTRATION_KEY,
428+
JSON.stringify({ userId: 'u1', token: 'tok-1', vendor: 'fcm', deviceId: 'install-7' }),
429+
);
430+
installBridge();
431+
render(<App />);
432+
await screen.findByTestId('login-page');
433+
434+
const held: Promise<unknown>[] = [];
435+
const event = new Event(NATIVE_CONNECTION_REMOVED_EVENT);
436+
Object.assign(event, { waitUntil: (promise: Promise<unknown>) => held.push(promise) });
437+
act(() => {
438+
window.dispatchEvent(event);
439+
});
440+
expect(held).toHaveLength(1);
441+
await held[0];
442+
expect(deviceCalls(fetchMock)).toEqual([expect.stringMatching(/^DELETE .*\/api\/v1\/notifications\/devices\/install-7$/)]);
443+
expect(sessionStorage.getItem(NATIVE_PUSH_REGISTRATION_KEY)).toBeNull();
444+
});
445+
});

0 commit comments

Comments
 (0)