|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * objectstack-ai/objectstack#34740 — every object route answers an object the |
| 11 | + * reader cannot open with ONE sentence that neither asserts nor reveals whether |
| 12 | + * the object exists. |
| 13 | + * |
| 14 | + * The server leaves an object the caller cannot read out of every schema list |
| 15 | + * and answers a describe of it as an absent name (ruling 6787890900, amending |
| 16 | + * ADR-0106), so the console receives both cases as the same absence. Each |
| 17 | + * route's face draws `console.objectView.objectNotFoundDescription` ("does not |
| 18 | + * exist, or you do not have access to it"). The record route's face, the fourth, |
| 19 | + * is pinned in `RecordDetailView.harness.test.tsx` under the same issue number. |
| 20 | + * |
| 21 | + * Two names per route: `crm_lead`, an object the server withheld (absent from the |
| 22 | + * served list while a readable sibling is present), and `no_such_object`, a name |
| 23 | + * that exists nowhere. Both, in en and zh-CN, must read the same sentence with |
| 24 | + * only the name changed. |
| 25 | + * |
| 26 | + * The route cells read the sentence from the pack, so they pin which key each |
| 27 | + * face draws and that both names read alike; the premise cell pins the wording. |
| 28 | + * Measured: the create face put back on `empty.objectNotFoundDescription` reds |
| 29 | + * both create cells (that key is gone, so the raw key renders); the en sentence |
| 30 | + * put back to "does not exist in the current configuration" reds the premise |
| 31 | + * cell and leaves the route cells green. |
| 32 | + */ |
| 33 | + |
| 34 | +import '@testing-library/jest-dom/vitest'; |
| 35 | +import * as React from 'react'; |
| 36 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 37 | +import { render, cleanup, screen } from '@testing-library/react'; |
| 38 | +import { MemoryRouter, Routes, Route } from 'react-router-dom'; |
| 39 | +import { createI18n, I18nProvider } from '@object-ui/i18n'; |
| 40 | +import { builtInLocales } from '@object-ui/i18n/locales'; |
| 41 | + |
| 42 | +vi.mock('@object-ui/permissions', async (importOriginal) => { |
| 43 | + const actual = await importOriginal<typeof import('@object-ui/permissions')>(); |
| 44 | + return { |
| 45 | + ...actual, |
| 46 | + usePermissions: () => ({ |
| 47 | + check: () => ({ allowed: true }), |
| 48 | + checkField: () => true, |
| 49 | + getFieldPermissions: () => [], |
| 50 | + getRowFilter: () => undefined, |
| 51 | + getObjectApiOperations: () => undefined, |
| 52 | + roles: [], |
| 53 | + isLoaded: false, |
| 54 | + hasCapabilities: () => true, |
| 55 | + can: () => true, |
| 56 | + cannot: () => false, |
| 57 | + }), |
| 58 | + useFieldPermissions: () => ({ canRead: () => true, canWrite: () => true, permissions: [] }), |
| 59 | + }; |
| 60 | +}); |
| 61 | + |
| 62 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 63 | + ...(await importOriginal<Record<string, unknown>>()), |
| 64 | + useAuth: () => ({ user: { id: 'u1', name: 'Ada' }, activeOrganization: null, getAuthConfig: async () => ({ features: {} }) }), |
| 65 | + useWorkspaceAdminStatus: () => ({ isAdmin: false, isResolved: true }), |
| 66 | + createAuthenticatedFetch: () => vi.fn(), |
| 67 | +})); |
| 68 | + |
| 69 | +vi.mock('./RecordDetailView', () => ({ RecordDetailView: () => null })); |
| 70 | +vi.mock('./CreateViewDialog', () => ({ CreateViewDialog: () => null })); |
| 71 | +vi.mock('@object-ui/plugin-form', async (importOriginal) => ({ |
| 72 | + ...(await importOriginal<Record<string, unknown>>()), |
| 73 | + ObjectForm: () => <div data-testid="object-form" />, |
| 74 | +})); |
| 75 | +vi.mock('./metadata-admin/useMetadata', () => ({ useMetadataClient: () => ({}) })); |
| 76 | + |
| 77 | +/** The served list: the readable sibling only. `crm_lead` was withheld by the server. */ |
| 78 | +const READABLE = { name: 'crm_account', label: 'Account', fields: { name: { type: 'text', label: 'Name' } } }; |
| 79 | +const SERVED = [READABLE]; |
| 80 | +vi.mock('../providers/MetadataProvider', () => ({ useMetadata: () => ({ objects: SERVED, loading: false }) })); |
| 81 | +vi.mock('../providers/AdapterProvider', () => ({ useAdapter: () => null })); |
| 82 | + |
| 83 | +// `ObjectView` reaches a dynamic `import('@objectstack/lint')`: pay its load at |
| 84 | +// collection, never inside a case's clocked window (`check:test-source-alias`). |
| 85 | +import '@objectstack/lint'; |
| 86 | +import { ObjectView } from './ObjectView'; |
| 87 | +import { ObjectDataPage } from './ObjectDataPage'; |
| 88 | +import { RecordFormPage } from './RecordFormPage'; |
| 89 | +import { ExpressionProvider } from '../providers/ExpressionProvider'; |
| 90 | + |
| 91 | +type Lang = 'en' | 'zh-CN'; |
| 92 | +const PACK: Record<Lang, 'en' | 'zh-Hans'> = { en: 'en', 'zh-CN': 'zh-Hans' }; |
| 93 | +const WITHHELD = 'crm_lead'; |
| 94 | +const MISSING = 'no_such_object'; |
| 95 | + |
| 96 | +/** The one sentence, from the pack the reader's language loads. */ |
| 97 | +function sentence(lang: Lang, objectName: string): string { |
| 98 | + const pack = builtInLocales[PACK[lang]] as { console: { objectView: { objectNotFoundDescription: string } } }; |
| 99 | + return pack.console.objectView.objectNotFoundDescription.replace('{{objectName}}', objectName); |
| 100 | +} |
| 101 | + |
| 102 | +const dataSource = { |
| 103 | + find: vi.fn(async () => ({ data: [], total: 0 })), |
| 104 | + findOne: vi.fn(async () => null), |
| 105 | + create: vi.fn(async () => ({})), |
| 106 | + update: vi.fn(async () => ({})), |
| 107 | + delete: vi.fn(async () => ({})), |
| 108 | + getObjectSchema: vi.fn(async () => null), |
| 109 | +} as never; |
| 110 | + |
| 111 | +const ROUTES = { |
| 112 | + list: { path: '/apps/:appName/:objectName', url: (o: string) => `/apps/crm/${o}`, |
| 113 | + element: () => <ObjectView dataSource={dataSource} objects={SERVED} onEdit={() => {}} /> }, |
| 114 | + data: { path: '/apps/:appName/:objectName/data', url: (o: string) => `/apps/crm/${o}/data`, |
| 115 | + element: () => <ObjectDataPage dataSource={dataSource} objects={SERVED} /> }, |
| 116 | + create: { path: '/apps/:appName/:objectName/new', url: (o: string) => `/apps/crm/${o}/new`, |
| 117 | + element: () => <RecordFormPage mode="create" /> }, |
| 118 | +} as const; |
| 119 | + |
| 120 | +/** The absent-object face's text on `route` for `objectName`, in `lang`. */ |
| 121 | +async function faceText(route: keyof typeof ROUTES, lang: Lang, objectName: string): Promise<string> { |
| 122 | + const i18n = createI18n({ defaultLanguage: lang, detectBrowserLanguage: false }); |
| 123 | + expect(i18n.language).toBe(lang); |
| 124 | + const r = ROUTES[route]; |
| 125 | + render( |
| 126 | + <I18nProvider instance={i18n} persistLanguage={false}> |
| 127 | + <ExpressionProvider user={{ id: 'u1', name: 'Ada', profile: 'member' }}> |
| 128 | + <MemoryRouter initialEntries={[r.url(objectName)]}> |
| 129 | + <Routes> |
| 130 | + <Route path={r.path} element={r.element()} /> |
| 131 | + </Routes> |
| 132 | + </MemoryRouter> |
| 133 | + </ExpressionProvider> |
| 134 | + </I18nProvider>, |
| 135 | + ); |
| 136 | + const face = await screen.findByTestId('object-not-found', {}, { timeout: 5000 }); |
| 137 | + const text = face.textContent ?? ''; |
| 138 | + cleanup(); |
| 139 | + return text; |
| 140 | +} |
| 141 | + |
| 142 | +beforeEach(() => { |
| 143 | + vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({ data: [] }), { |
| 144 | + status: 200, headers: { 'content-type': 'application/json' }, |
| 145 | + }))); |
| 146 | +}); |
| 147 | + |
| 148 | +afterEach(() => { |
| 149 | + cleanup(); |
| 150 | + vi.unstubAllGlobals(); |
| 151 | + vi.clearAllMocks(); |
| 152 | +}); |
| 153 | + |
| 154 | +describe('objectRouteAbsence-34740 — one neutral sentence on the list, data and create routes', () => { |
| 155 | + it('the premise: the sentence names both readings, in both packs, and no other face key is left', () => { |
| 156 | + expect(sentence('en', 'x')).toBe('The object "x" does not exist, or you do not have access to it.'); |
| 157 | + expect(sentence('zh-CN', 'x')).toBe('对象"x"不存在,或者您无权访问它。'); |
| 158 | + for (const pack of ['en', 'zh-Hans'] as const) { |
| 159 | + expect((builtInLocales[pack] as { empty: Record<string, unknown> }).empty.objectNotFoundDescription).toBeUndefined(); |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + for (const route of Object.keys(ROUTES) as Array<keyof typeof ROUTES>) { |
| 164 | + for (const lang of ['en', 'zh-CN'] as const) { |
| 165 | + it(`${route} · ${lang}: a withheld object and a missing name read the same sentence`, async () => { |
| 166 | + const withheld = await faceText(route, lang, WITHHELD); |
| 167 | + const missing = await faceText(route, lang, MISSING); |
| 168 | + expect(withheld).toContain(sentence(lang, WITHHELD)); |
| 169 | + expect(missing).toContain(sentence(lang, MISSING)); |
| 170 | + // Byte-identical once the name is taken out: nothing on the face tells the two apart. |
| 171 | + expect(withheld.replaceAll(WITHHELD, 'NAME')).toBe(missing.replaceAll(MISSING, 'NAME')); |
| 172 | + }); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + it('CONTROL: the readable sibling draws no absent-object face on the create route', async () => { |
| 177 | + const i18n = createI18n({ defaultLanguage: 'en', detectBrowserLanguage: false }); |
| 178 | + render( |
| 179 | + <I18nProvider instance={i18n} persistLanguage={false}> |
| 180 | + <MemoryRouter initialEntries={[`/apps/crm/${READABLE.name}/new`]}> |
| 181 | + <Routes> |
| 182 | + <Route path={ROUTES.create.path} element={ROUTES.create.element()} /> |
| 183 | + </Routes> |
| 184 | + </MemoryRouter> |
| 185 | + </I18nProvider>, |
| 186 | + ); |
| 187 | + expect(await screen.findByTestId('object-form')).toBeInTheDocument(); |
| 188 | + expect(screen.queryByTestId('object-not-found')).toBeNull(); |
| 189 | + }); |
| 190 | +}); |
0 commit comments