|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#28561 — the "All metadata" area's routes in the `/studio` |
| 5 | + * subtree (ADR-0146 D1), on the REAL `studioRoutes` the console mounts. |
| 6 | + * |
| 7 | + * - A package's area (`/studio/<package>/metadata/*`) is the pillar builder |
| 8 | + * with its area open: the surface draws the header and mounts the area, so |
| 9 | + * the route hands it `area="metadata"`. `:packageId/:tab` matches one |
| 10 | + * segment, so without this route an area URL such as |
| 11 | + * `/studio/crm/metadata/flow` matched nothing. |
| 12 | + * - The cross-package leg (`/studio/~all/metadata/*`) is the area alone, with |
| 13 | + * no package, in the frame the landing draws. Every other `~all` URL lands |
| 14 | + * on it, so none reaches the pillar builder as a package id. |
| 15 | + * - The entry gate decides first, as for every Studio URL. |
| 16 | + * |
| 17 | + * The builder module is mocked with markers (as in |
| 18 | + * `StudioRoute.lazyBuilder-11798.test.tsx`); the gate, the frame and the |
| 19 | + * router are real. |
| 20 | + */ |
| 21 | + |
| 22 | +import '@testing-library/jest-dom/vitest'; |
| 23 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 24 | +import { cleanup, render, screen } from '@testing-library/react'; |
| 25 | +import React from 'react'; |
| 26 | +import { MemoryRouter, Routes, Route, useLocation, useParams } from 'react-router-dom'; |
| 27 | + |
| 28 | +let auth = { isAuthenticated: true, isLoading: false, user: { id: 'u1' } as unknown }; |
| 29 | + |
| 30 | +let permissionsFetch: (input: unknown, init?: unknown) => Promise<Response> = async () => |
| 31 | + jsonResponse({ authenticated: true, systemPermissions: [] }); |
| 32 | + |
| 33 | +function jsonResponse(body: unknown, status = 200): Response { |
| 34 | + return { |
| 35 | + ok: status >= 200 && status < 300, |
| 36 | + status, |
| 37 | + headers: { get: () => null }, |
| 38 | + json: async () => body, |
| 39 | + } as unknown as Response; |
| 40 | +} |
| 41 | + |
| 42 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 43 | + ...(await importOriginal<Record<string, unknown>>()), |
| 44 | + useAuth: () => auth, |
| 45 | + createAuthenticatedFetch: () => (input: unknown, init?: unknown) => permissionsFetch(input, init), |
| 46 | +})); |
| 47 | + |
| 48 | +// `AuthGuard` reads `useAuth` through the auth package's own module graph; the |
| 49 | +// seam `StudioRoute.test.tsx` documents. |
| 50 | +vi.mock('../../../../packages/auth/src/useAuth', async (importOriginal) => ({ |
| 51 | + ...(await importOriginal<Record<string, unknown>>()), |
| 52 | + useAuth: () => auth, |
| 53 | +})); |
| 54 | + |
| 55 | +// The real barrel's surface is inherited (`check:vi-mock-inherit`); the route |
| 56 | +// constants (`STUDIO_ALL_PACKAGES_SEGMENT`, `STUDIO_METADATA_AREA`, |
| 57 | +// `studioMetadataAreaPath`) are the real ones. The barrel's own builder |
| 58 | +// components are overridden with a marker no case looks for: the route reaches |
| 59 | +// them only through `./studioBuilder`. |
| 60 | +vi.mock('@object-ui/app-shell', async (importOriginal) => ({ |
| 61 | + ...(await importOriginal<Record<string, unknown>>()), |
| 62 | + BuilderLanding: () => <div data-testid="barrel-builder" />, |
| 63 | + StudioDesignSurface: () => <div data-testid="barrel-builder" />, |
| 64 | + StudioMetadataArea: () => <div data-testid="barrel-builder" />, |
| 65 | + ConnectedShell: ({ children }: { children?: React.ReactNode }) => <>{children}</>, |
| 66 | + RequireOrganization: ({ children }: { children?: React.ReactNode }) => <>{children}</>, |
| 67 | + RedirectWithSplash: () => <div data-testid="redirect" />, |
| 68 | + LoadingFallback: () => <div data-testid="loading" />, |
| 69 | + LoadingScreen: () => <div data-testid="error-screen" />, |
| 70 | + getProductName: () => 'ObjectOS', |
| 71 | + useHomePath: () => '/home', |
| 72 | +})); |
| 73 | + |
| 74 | +function SurfaceMarker({ area }: { area?: string }) { |
| 75 | + const { packageId, tab } = useParams(); |
| 76 | + return ( |
| 77 | + <div data-testid="studio-pillar-builder" data-package={packageId} data-tab={tab ?? ''} data-area={area ?? ''} /> |
| 78 | + ); |
| 79 | +} |
| 80 | + |
| 81 | +vi.mock('./studioBuilder', () => ({ |
| 82 | + BuilderLanding: () => <div data-testid="studio-front-door" />, |
| 83 | + StudioDesignSurface: SurfaceMarker, |
| 84 | + StudioMetadataArea: ({ packageId }: { packageId: string | null }) => ( |
| 85 | + <div data-testid="studio-metadata-area" data-package={String(packageId)} /> |
| 86 | + ), |
| 87 | +})); |
| 88 | + |
| 89 | +import { STUDIO_ENTRY_CAPABILITY } from './studioEntry'; |
| 90 | +import { studioRoutes } from './StudioRoute'; |
| 91 | + |
| 92 | +function LocationProbe() { |
| 93 | + const { pathname, search } = useLocation(); |
| 94 | + return <div data-testid="pathname">{`${pathname}${search}`}</div>; |
| 95 | +} |
| 96 | + |
| 97 | +function renderStudio(url: string) { |
| 98 | + return render( |
| 99 | + <MemoryRouter initialEntries={[url]}> |
| 100 | + <LocationProbe /> |
| 101 | + <Routes> |
| 102 | + {studioRoutes} |
| 103 | + <Route path="/home" element={<div data-testid="home-launcher">home</div>} /> |
| 104 | + </Routes> |
| 105 | + </MemoryRouter>, |
| 106 | + ); |
| 107 | +} |
| 108 | + |
| 109 | +const answer = (systemPermissions: string[]) => async () => |
| 110 | + jsonResponse({ authenticated: true, userId: 'u1', systemPermissions, objects: {}, fields: {} }); |
| 111 | +const location = () => screen.getByTestId('pathname').textContent; |
| 112 | + |
| 113 | +beforeEach(() => { |
| 114 | + auth = { isAuthenticated: true, isLoading: false, user: { id: 'u1' } }; |
| 115 | + permissionsFetch = answer([STUDIO_ENTRY_CAPABILITY]); |
| 116 | + // The frame mounts the real `AppHeader`, whose feeds answer empty here. |
| 117 | + vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ data: [] }))); |
| 118 | +}); |
| 119 | + |
| 120 | +afterEach(() => { |
| 121 | + cleanup(); |
| 122 | + vi.unstubAllGlobals(); |
| 123 | +}); |
| 124 | + |
| 125 | +describe('/studio — the "All metadata" area\'s routes (objectstack#28561)', () => { |
| 126 | + it('a package\'s area is the pillar builder with its area open, the rest of the path its own', async () => { |
| 127 | + renderStudio('/studio/com.example.crm/metadata/flow/f1/history?package=com.example.crm'); |
| 128 | + const surface = await screen.findByTestId('studio-pillar-builder'); |
| 129 | + expect(surface).toHaveAttribute('data-package', 'com.example.crm'); |
| 130 | + expect(surface).toHaveAttribute('data-area', 'metadata'); |
| 131 | + expect(location()).toBe('/studio/com.example.crm/metadata/flow/f1/history?package=com.example.crm'); |
| 132 | + expect(screen.queryByTestId('barrel-builder')).toBeNull(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('CONTROL — a pillar is the pillar builder with no area', async () => { |
| 136 | + renderStudio('/studio/com.example.crm/data'); |
| 137 | + const surface = await screen.findByTestId('studio-pillar-builder'); |
| 138 | + expect(surface).toHaveAttribute('data-tab', 'data'); |
| 139 | + expect(surface).toHaveAttribute('data-area', ''); |
| 140 | + }); |
| 141 | + |
| 142 | + it('the cross-package leg is the area alone, with no package, in the landing\'s frame', async () => { |
| 143 | + renderStudio('/studio/~all/metadata/permission?scope=environment'); |
| 144 | + const area = await screen.findByTestId('studio-metadata-area'); |
| 145 | + expect(area).toHaveAttribute('data-package', 'null'); |
| 146 | + expect(screen.queryByTestId('studio-pillar-builder')).toBeNull(); |
| 147 | + // The console's own top bar frames it; its brand links home. |
| 148 | + expect(screen.getByRole('link', { name: 'ObjectOS home' })).toHaveAttribute('href', '/home'); |
| 149 | + expect(location()).toBe('/studio/~all/metadata/permission?scope=environment'); |
| 150 | + }); |
| 151 | + |
| 152 | + it.each(['/studio/~all', '/studio/~all/data', '/studio/~all/flow/f1'])( |
| 153 | + '%s lands on the cross-package area, never on the builder as a package id', |
| 154 | + async (url) => { |
| 155 | + renderStudio(url); |
| 156 | + expect(await screen.findByTestId('studio-metadata-area')).toBeInTheDocument(); |
| 157 | + expect(location()).toBe('/studio/~all/metadata'); |
| 158 | + expect(screen.queryByTestId('studio-pillar-builder')).toBeNull(); |
| 159 | + }, |
| 160 | + ); |
| 161 | + |
| 162 | + it.each(['/studio/~all/metadata', '/studio/com.example.crm/metadata'])( |
| 163 | + 'a principal the gate refuses gets neither leg: %s', |
| 164 | + async (url) => { |
| 165 | + permissionsFetch = answer(['setup.access']); |
| 166 | + renderStudio(url); |
| 167 | + expect(await screen.findByTestId('studio-access-required')).toBeInTheDocument(); |
| 168 | + expect(screen.queryByTestId('studio-metadata-area')).toBeNull(); |
| 169 | + expect(screen.queryByTestId('studio-pillar-builder')).toBeNull(); |
| 170 | + // The refusal is said where the caller asked. |
| 171 | + expect(location()).toBe(url); |
| 172 | + }, |
| 173 | + ); |
| 174 | +}); |
0 commit comments