|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#37082 — one name per group across Studio. The left menu, the All |
| 5 | + * metadata directory's group headings and domain chips, and the domain badge |
| 6 | + * on a type or item page (`PageShell`, which Setup's datasource pages draw too) |
| 7 | + * name the `system` group Platform / 平台 and the `security` group Access / 访问 |
| 8 | + * (objectstack ADR-0084, amended 2026-11-24), in en and in zh-CN. |
| 9 | + * |
| 10 | + * Pinned over the real directory page and the real header, each word compared |
| 11 | + * with the menu's own (`studioMenuGroupLabel`); CONTROL: the `data` group keeps |
| 12 | + * Data / 数据, and the retired words System / 系统 and Security / 安全 are drawn |
| 13 | + * nowhere. |
| 14 | + */ |
| 15 | + |
| 16 | +import '@testing-library/jest-dom/vitest'; |
| 17 | +import * as React from 'react'; |
| 18 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 19 | +import { render, screen, cleanup, within } from '@testing-library/react'; |
| 20 | +import { MemoryRouter } from 'react-router-dom'; |
| 21 | +import { MetadataAreaHostContext, type MetadataAreaHost } from './metadataAreaHost'; |
| 22 | +import type { RichMetadataTypeEntry } from './useMetadata'; |
| 23 | + |
| 24 | +const PKG = 'com.example.showcase'; |
| 25 | + |
| 26 | +const entry = (type: string, domain: string): RichMetadataTypeEntry => |
| 27 | + ({ type, label: type, domain, allowEnvironmentOverlay: false, allowRuntimeCreate: true }) as RichMetadataTypeEntry; |
| 28 | + |
| 29 | +const ENTRIES: RichMetadataTypeEntry[] = [ |
| 30 | + entry('object', 'data'), |
| 31 | + entry('datasource', 'system'), |
| 32 | + entry('translation', 'system'), |
| 33 | + entry('permission', 'security'), |
| 34 | +]; |
| 35 | + |
| 36 | +const CLIENT = { list: async () => [] }; |
| 37 | + |
| 38 | +/** The locale the page reads; each case sets it before rendering. */ |
| 39 | +let LOCALE = 'en'; |
| 40 | + |
| 41 | +vi.mock('./useMetadata', async (importOriginal) => ({ |
| 42 | + ...(await importOriginal<Record<string, unknown>>()), |
| 43 | + useMetadataClient: () => CLIENT, |
| 44 | + useMetadataTypes: () => ({ loading: false, error: null, entries: ENTRIES }), |
| 45 | + useGlobalDiagnostics: () => ({ |
| 46 | + loading: false, |
| 47 | + error: null, |
| 48 | + summary: { entries: [], total: 0, scannedTypes: 0, scannedItems: 0 }, |
| 49 | + byType: {}, |
| 50 | + warnByType: {}, |
| 51 | + countsByType: {}, |
| 52 | + lockedByType: {}, |
| 53 | + packagesByType: Object.fromEntries(ENTRIES.map((e) => [e.type, [PKG]])), |
| 54 | + allPackages: [PKG], |
| 55 | + reload: () => {}, |
| 56 | + }), |
| 57 | +})); |
| 58 | +vi.mock('./i18n', async (importOriginal) => ({ |
| 59 | + ...(await importOriginal<Record<string, unknown>>()), |
| 60 | + useMetadataLocale: () => LOCALE, |
| 61 | +})); |
| 62 | +vi.stubGlobal( |
| 63 | + 'fetch', |
| 64 | + vi.fn(async () => |
| 65 | + new Response(JSON.stringify({ success: true, data: { packages: [] } }), { |
| 66 | + status: 200, |
| 67 | + headers: { 'Content-Type': 'application/json' }, |
| 68 | + }), |
| 69 | + ), |
| 70 | +); |
| 71 | + |
| 72 | +import { MetadataDirectoryPage } from './DirectoryPage'; |
| 73 | +import { PageShell } from './PageShell'; |
| 74 | +import { studioMenuGroupLabel } from '../studio-design/studioMenuGroupLabel'; |
| 75 | + |
| 76 | +afterEach(() => { |
| 77 | + cleanup(); |
| 78 | +}); |
| 79 | + |
| 80 | +const host: MetadataAreaHost = { packageId: PKG, readOnly: false, basePath: `/studio/${PKG}/metadata` }; |
| 81 | + |
| 82 | +const CASES = [ |
| 83 | + { locale: 'en', data: 'Data', platform: 'Platform', access: 'Access', retired: ['System', 'Security'] }, |
| 84 | + { locale: 'zh-CN', data: '数据', platform: '平台', access: '访问', retired: ['系统', '安全'] }, |
| 85 | +] as const; |
| 86 | + |
| 87 | +function renderDirectory() { |
| 88 | + return render( |
| 89 | + <MetadataAreaHostContext.Provider value={host}> |
| 90 | + <MemoryRouter> |
| 91 | + <MetadataDirectoryPage /> |
| 92 | + </MemoryRouter> |
| 93 | + </MetadataAreaHostContext.Provider>, |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +function renderShell(type: string, itemName?: string) { |
| 98 | + const e = ENTRIES.find((x) => x.type === type); |
| 99 | + return render( |
| 100 | + <MemoryRouter> |
| 101 | + <PageShell entry={e} itemName={itemName}> |
| 102 | + <div /> |
| 103 | + </PageShell> |
| 104 | + </MemoryRouter>, |
| 105 | + ); |
| 106 | +} |
| 107 | + |
| 108 | +describe.each(CASES)('one name per group ($locale, objectstack#37082)', (c) => { |
| 109 | + it('the menu names the groups Platform and Access', () => { |
| 110 | + expect(studioMenuGroupLabel('system', c.locale)).toBe(c.platform); |
| 111 | + expect(studioMenuGroupLabel('security', c.locale)).toBe(c.access); |
| 112 | + expect(studioMenuGroupLabel('data', c.locale)).toBe(c.data); |
| 113 | + }); |
| 114 | + |
| 115 | + it('the directory\'s group headings use the menu\'s names', async () => { |
| 116 | + LOCALE = c.locale; |
| 117 | + renderDirectory(); |
| 118 | + await screen.findByText('datasource', { selector: 'code' }); |
| 119 | + const headings = screen.getAllByRole('heading', { level: 2 }).map((h) => (h.textContent ?? '').trim()); |
| 120 | + expect(headings).toEqual( |
| 121 | + expect.arrayContaining([ |
| 122 | + `${studioMenuGroupLabel('data', c.locale)} (1)`, |
| 123 | + `${studioMenuGroupLabel('security', c.locale)} (1)`, |
| 124 | + `${studioMenuGroupLabel('system', c.locale)} (2)`, |
| 125 | + ]), |
| 126 | + ); |
| 127 | + expect(headings).toEqual(expect.arrayContaining([`${c.platform} (2)`, `${c.access} (1)`, `${c.data} (1)`])); |
| 128 | + for (const word of c.retired) expect(headings.join(' ')).not.toContain(word); |
| 129 | + }); |
| 130 | + |
| 131 | + it('the directory\'s domain chips use the menu\'s names', async () => { |
| 132 | + LOCALE = c.locale; |
| 133 | + renderDirectory(); |
| 134 | + await screen.findByText('datasource', { selector: 'code' }); |
| 135 | + expect(screen.getByRole('button', { name: `${studioMenuGroupLabel('system', c.locale)} (2)` })).toBeInTheDocument(); |
| 136 | + expect(screen.getByRole('button', { name: `${studioMenuGroupLabel('security', c.locale)} (1)` })).toBeInTheDocument(); |
| 137 | + expect(screen.getByRole('button', { name: `${c.platform} (2)` })).toBeInTheDocument(); |
| 138 | + expect(screen.getByRole('button', { name: `${c.access} (1)` })).toBeInTheDocument(); |
| 139 | + expect(screen.getByRole('button', { name: `${c.data} (1)` })).toBeInTheDocument(); |
| 140 | + for (const word of c.retired) { |
| 141 | + expect(screen.queryByRole('button', { name: new RegExp(`^${word} \\(`, 'u') })).toBeNull(); |
| 142 | + } |
| 143 | + }); |
| 144 | + |
| 145 | + it('a type page\'s badge uses the menu\'s names', () => { |
| 146 | + LOCALE = c.locale; |
| 147 | + renderShell('datasource'); |
| 148 | + const title = document.querySelector('h1')!.parentElement!; |
| 149 | + expect(within(title).getByText(studioMenuGroupLabel('system', c.locale))).toBeInTheDocument(); |
| 150 | + expect(within(title).getByText(c.platform)).toBeInTheDocument(); |
| 151 | + cleanup(); |
| 152 | + renderShell('permission'); |
| 153 | + expect(within(document.querySelector('h1')!.parentElement!).getByText(c.access)).toBeInTheDocument(); |
| 154 | + }); |
| 155 | + |
| 156 | + it('an item page\'s badge uses the menu\'s names (Setup\'s datasource item page draws this header)', () => { |
| 157 | + LOCALE = c.locale; |
| 158 | + renderShell('datasource', 'main_db'); |
| 159 | + const meta = screen.getByTestId('page-shell-header-meta'); |
| 160 | + expect(within(meta).getByText(studioMenuGroupLabel('system', c.locale))).toBeInTheDocument(); |
| 161 | + expect(within(meta).getByText(c.platform)).toBeInTheDocument(); |
| 162 | + cleanup(); |
| 163 | + renderShell('permission', 'sales_rep'); |
| 164 | + expect(within(screen.getByTestId('page-shell-header-meta')).getByText(c.access)).toBeInTheDocument(); |
| 165 | + for (const word of c.retired) expect(screen.getByTestId('page-shell-header-meta').textContent).not.toContain(word); |
| 166 | + }); |
| 167 | +}); |
0 commit comments