|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#30588 — the marketplace pages' strings are a lazy namespace, and |
| 5 | + * a visitor reading the console in another language never sees them in |
| 6 | + * English or as raw keys while it loads. |
| 7 | + * |
| 8 | + * `marketplace.*` (bar the families Studio's packages page reads) left the ten |
| 9 | + * language packs for `@object-ui/i18n/namespaces/marketplace`, fetched when a |
| 10 | + * marketplace page mounts. Both pages wrap their bodies in |
| 11 | + * `MarketplaceNamespaceGate`, which renders the copy-free load skeleton |
| 12 | + * (`marketplace-resolving`) until the namespace is in, then the page — in the |
| 13 | + * console's language from the first frame that has text. A catalogue that |
| 14 | + * cannot be fetched is said in the console's own resident error words, with |
| 15 | + * Try again. |
| 16 | + * |
| 17 | + * Every text node the page ever adds is recorded (a MutationObserver over the |
| 18 | + * render container, attached before the first render), so "never English, |
| 19 | + * never a raw key" is checked over the whole sequence, not only its end. |
| 20 | + * |
| 21 | + * ## The registry is put back in its production state |
| 22 | + * |
| 23 | + * The DOM setup calls `registerBuiltInLocales()`, which registers every |
| 24 | + * language COMPLETE — the namespace included — so the page would never need a |
| 25 | + * fetch. That is the all-ten door's promise, not what a console visitor gets. |
| 26 | + * This file re-registers each language as its pack alone, at module scope — |
| 27 | + * the registry a console that awaited `preloadBootstrapLocale()` holds. The |
| 28 | + * `dom` project isolates each file's module graph, so this stays here. |
| 29 | + * |
| 30 | + * Harness: `MarketplacePage.searchPlaceholder-10900.test.tsx`'s and |
| 31 | + * `MarketplacePackagePage.guardOrder.test.tsx`'s — the real runtime-config |
| 32 | + * boot, an admin viewer, the API module stubbed — under a real `I18nProvider`. |
| 33 | + */ |
| 34 | +import '@testing-library/jest-dom/vitest'; |
| 35 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 36 | +import type { ReactElement } from 'react'; |
| 37 | +import { render, screen, fireEvent, cleanup, act } from '@testing-library/react'; |
| 38 | + |
| 39 | +const arChunk = vi.hoisted(() => ({ fails: false, requests: 0 })); |
| 40 | + |
| 41 | +vi.mock('react-router-dom', () => ({ |
| 42 | + useNavigate: () => vi.fn(), |
| 43 | + useParams: () => ({ packageId: 'com.acme.helpdesk' }), |
| 44 | +})); |
| 45 | + |
| 46 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 47 | + ...(await importOriginal<Record<string, unknown>>()), |
| 48 | + useWorkspaceAdminStatus: () => ({ isAdmin: true, isResolved: true }), |
| 49 | +})); |
| 50 | + |
| 51 | +const PKG = { |
| 52 | + id: 'pkg_1', |
| 53 | + manifest_id: 'com.acme.helpdesk', |
| 54 | + display_name: 'Service Desk', |
| 55 | + description: 'Tickets and SLAs', |
| 56 | + category: null, |
| 57 | + latest_version: null, |
| 58 | +}; |
| 59 | + |
| 60 | +vi.mock('../marketplaceApi', () => ({ |
| 61 | + listMarketplacePackages: async () => ({ items: [PKG] }), |
| 62 | + getMarketplacePackage: async () => ({ package: PKG, versions: [] }), |
| 63 | + getCloudInstallationInfo: async () => null, |
| 64 | + listLocalInstalls: async () => [], |
| 65 | + listOrgPackages: async () => ({ items: [] }), |
| 66 | + listInstalledPackages: async () => ({ items: [] }), |
| 67 | + installPackage: async () => ({}), |
| 68 | + installLocal: async () => ({}), |
| 69 | + uninstallLocal: async () => ({}), |
| 70 | + listCloudEnvironments: async () => [], |
| 71 | + listInstallableOrgIds: async () => [], |
| 72 | + cloudConsoleUrl: () => '', |
| 73 | + reseedSampleData: async () => ({ ok: true }), |
| 74 | + purgeSampleData: async () => ({ ok: true }), |
| 75 | + reseedLocalSampleData: async () => ({ ok: true }), |
| 76 | + purgeLocalSampleData: async () => ({ ok: true }), |
| 77 | +})); |
| 78 | + |
| 79 | +vi.mock('../../../assistant/assistantBus', () => ({ emitMetadataRefresh: () => {} })); |
| 80 | +vi.mock('../../../providers/MetadataProvider', () => ({ useMetadata: () => ({ refresh: () => {} }) })); |
| 81 | +vi.mock('../../../components/SuggestedBindingsPanel', () => ({ SuggestedBindingsPanel: () => null })); |
| 82 | + |
| 83 | +// The Arabic catalogue's chunk, failing while a case says so. |
| 84 | +vi.mock('@object-ui/i18n/namespaces/marketplace/ar', async (importOriginal) => { |
| 85 | + arChunk.requests += 1; |
| 86 | + if (arChunk.fails) throw new Error('chunk offline'); |
| 87 | + return importOriginal(); |
| 88 | +}); |
| 89 | + |
| 90 | +import { I18nProvider } from '@object-ui/i18n'; |
| 91 | +import { builtInLocales } from '@object-ui/i18n/locales'; |
| 92 | +import { BUILT_IN_LANGUAGE_CODES, registerBuiltInLocale } from '@object-ui/i18n/locales/registry'; |
| 93 | +import en from '@object-ui/i18n/locales/en'; |
| 94 | +import zh from '@object-ui/i18n/locales/zh'; |
| 95 | +import ja from '@object-ui/i18n/locales/ja'; |
| 96 | +import ko from '@object-ui/i18n/locales/ko'; |
| 97 | +import de from '@object-ui/i18n/locales/de'; |
| 98 | +import fr from '@object-ui/i18n/locales/fr'; |
| 99 | +import es from '@object-ui/i18n/locales/es'; |
| 100 | +import pt from '@object-ui/i18n/locales/pt'; |
| 101 | +import ru from '@object-ui/i18n/locales/ru'; |
| 102 | +import ar from '@object-ui/i18n/locales/ar'; |
| 103 | +import { initRuntimeConfig, resetRuntimeConfigForTesting } from '../../../runtime-config'; |
| 104 | +import { MarketplacePage } from '../MarketplacePage'; |
| 105 | +import { MarketplacePackagePage } from '../MarketplacePackagePage'; |
| 106 | + |
| 107 | +const PACKS: Record<string, Record<string, unknown>> = { en, zh, ja, ko, de, fr, es, pt, ru, ar }; |
| 108 | +for (const code of BUILT_IN_LANGUAGE_CODES) registerBuiltInLocale(code, PACKS[code]); |
| 109 | + |
| 110 | +const ENGLISH = builtInLocales.en.marketplace; |
| 111 | +const RAW_KEY = /\bmarketplace\.[A-Za-z]/; |
| 112 | + |
| 113 | +beforeEach(async () => { |
| 114 | + arChunk.fails = false; |
| 115 | + resetRuntimeConfigForTesting(); |
| 116 | + vi.stubGlobal( |
| 117 | + 'fetch', |
| 118 | + vi.fn(async () => ({ |
| 119 | + ok: true, |
| 120 | + status: 200, |
| 121 | + json: async () => ({ |
| 122 | + cloudUrl: 'https://cloud.objectos.ai', |
| 123 | + singleEnvironment: true, |
| 124 | + features: { installLocal: true, marketplace: true, aiStudio: true, autoPublishAiBuilds: true }, |
| 125 | + branding: { productName: 'ObjectOS', productShortName: 'ObjectOS' }, |
| 126 | + }), |
| 127 | + })), |
| 128 | + ); |
| 129 | + await initRuntimeConfig(); |
| 130 | +}); |
| 131 | + |
| 132 | +afterEach(() => { |
| 133 | + cleanup(); |
| 134 | + vi.unstubAllGlobals(); |
| 135 | + resetRuntimeConfigForTesting(); |
| 136 | +}); |
| 137 | + |
| 138 | +/** Render `page` in `language`, recording the text of every node it ever adds. */ |
| 139 | +function renderRecording(page: ReactElement, language: string) { |
| 140 | + const container = document.createElement('div'); |
| 141 | + document.body.appendChild(container); |
| 142 | + const texts: string[] = []; |
| 143 | + const observer = new MutationObserver((records) => { |
| 144 | + for (const record of records) { |
| 145 | + if (record.type === 'characterData') texts.push(record.target.textContent ?? ''); |
| 146 | + for (const node of Array.from(record.addedNodes)) texts.push(node.textContent ?? ''); |
| 147 | + } |
| 148 | + }); |
| 149 | + observer.observe(container, { childList: true, subtree: true, characterData: true }); |
| 150 | + const result = render( |
| 151 | + <I18nProvider config={{ defaultLanguage: language, detectBrowserLanguage: false }} persistLanguage={false}> |
| 152 | + {page} |
| 153 | + </I18nProvider>, |
| 154 | + { container }, |
| 155 | + ); |
| 156 | + // The first commit is synchronous; record it whatever the observer batched. |
| 157 | + texts.push(container.textContent ?? ''); |
| 158 | + return { |
| 159 | + ...result, |
| 160 | + /** Everything the page has shown so far, once the observer has delivered. */ |
| 161 | + async shown(): Promise<string[]> { |
| 162 | + await act(async () => { |
| 163 | + await Promise.resolve(); |
| 164 | + }); |
| 165 | + for (const record of observer.takeRecords()) { |
| 166 | + for (const node of Array.from(record.addedNodes)) texts.push(node.textContent ?? ''); |
| 167 | + } |
| 168 | + return texts; |
| 169 | + }, |
| 170 | + }; |
| 171 | +} |
| 172 | + |
| 173 | +describe('the marketplace pages wait for their strings (objectstack#30588)', () => { |
| 174 | + it('zh-CN: the catalogue page shows the load skeleton, then Chinese, and never English or a raw key', async () => { |
| 175 | + const zhMarketplace = builtInLocales.zh.marketplace; |
| 176 | + const view = renderRecording(<MarketplacePage />, 'zh-CN'); |
| 177 | + expect(screen.getByTestId('marketplace-resolving')).toBeInTheDocument(); |
| 178 | + |
| 179 | + expect(await screen.findByText(zhMarketplace.title)).toBeInTheDocument(); |
| 180 | + expect(screen.getByText(zhMarketplace.subtitle)).toBeInTheDocument(); |
| 181 | + |
| 182 | + const shown = (await view.shown()).join('\n'); |
| 183 | + expect(shown).not.toContain(ENGLISH.title); |
| 184 | + expect(shown).not.toContain(ENGLISH.subtitle); |
| 185 | + expect(shown).not.toMatch(RAW_KEY); |
| 186 | + }); |
| 187 | + |
| 188 | + it('de: the package page shows the load skeleton, then German, and never English or a raw key', async () => { |
| 189 | + const deMarketplace = builtInLocales.de.marketplace; |
| 190 | + const view = renderRecording(<MarketplacePackagePage />, 'de'); |
| 191 | + expect(screen.getByTestId('marketplace-resolving')).toBeInTheDocument(); |
| 192 | + |
| 193 | + expect((await screen.findAllByText(deMarketplace.back)).length).toBeGreaterThan(0); |
| 194 | + |
| 195 | + const shown = (await view.shown()).join('\n'); |
| 196 | + expect(shown).not.toContain(ENGLISH.back); |
| 197 | + expect(shown).not.toMatch(RAW_KEY); |
| 198 | + }); |
| 199 | + |
| 200 | + it('ar: a catalogue that cannot be fetched is said in resident words, and Try again fetches it', async () => { |
| 201 | + arChunk.fails = true; |
| 202 | + const view = renderRecording(<MarketplacePage />, 'ar'); |
| 203 | + |
| 204 | + const failed = await screen.findByTestId('marketplace-namespace-failed'); |
| 205 | + expect(failed).toHaveTextContent(ar.console.errors.somethingWentWrong); |
| 206 | + expect(arChunk.requests).toBe(1); |
| 207 | + |
| 208 | + arChunk.fails = false; |
| 209 | + await act(async () => { |
| 210 | + fireEvent.click(screen.getByRole('button', { name: ar.console.errors.tryAgain })); |
| 211 | + }); |
| 212 | + expect(await screen.findByText(builtInLocales.ar.marketplace.title)).toBeInTheDocument(); |
| 213 | + expect(arChunk.requests).toBe(2); |
| 214 | + |
| 215 | + const shown = (await view.shown()).join('\n'); |
| 216 | + expect(shown).not.toContain(ENGLISH.title); |
| 217 | + expect(shown).not.toMatch(RAW_KEY); |
| 218 | + }); |
| 219 | +}); |
0 commit comments