|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The package page's local Install opens a confirm that says what the install |
| 5 | + * adds before anything is installed (objectstack#30325): how many objects, |
| 6 | + * permission sets and sample records, and which permission sets it will |
| 7 | + * suggest granting to Everyone. Its Install is the one click that installs. |
| 8 | + * |
| 9 | + * The counts come from the manifest answer the page already reads for the |
| 10 | + * install modes (`GET /api/v1/marketplace/packages/:id/versions/latest/manifest`, |
| 11 | + * the body install-local installs): one read serves both. |
| 12 | + * |
| 13 | + * `marketplaceApi` is NOT mocked: one stubbed `fetch` answers by path, so the |
| 14 | + * requests counted are the ones the page puts on the wire. `t` echoes the KEY |
| 15 | + * and the interpolated name, count and set. |
| 16 | + */ |
| 17 | + |
| 18 | +import '@testing-library/jest-dom/vitest'; |
| 19 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 20 | +import { act, cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'; |
| 21 | + |
| 22 | +vi.mock('react-router-dom', () => ({ |
| 23 | + useNavigate: () => vi.fn(), |
| 24 | + useParams: () => ({ packageId: 'pkg_crm' }), |
| 25 | +})); |
| 26 | + |
| 27 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 28 | + ...(await importOriginal<Record<string, unknown>>()), |
| 29 | + useWorkspaceAdminStatus: () => ({ isAdmin: true, isResolved: true }), |
| 30 | +})); |
| 31 | + |
| 32 | +vi.mock('@object-ui/i18n', async (importOriginal) => ({ |
| 33 | + ...(await importOriginal<Record<string, unknown>>()), |
| 34 | + useObjectTranslation: () => ({ |
| 35 | + t: (key: string, o?: Record<string, unknown>) => { |
| 36 | + const facts = ['name', 'count', 'set'].filter((k) => o?.[k] !== undefined).map((k) => `${k}=${String(o?.[k])}`); |
| 37 | + return facts.length > 0 ? `«${key}|${facts.join('|')}»` : `«${key}»`; |
| 38 | + }, |
| 39 | + language: 'en', |
| 40 | + }), |
| 41 | +})); |
| 42 | + |
| 43 | +vi.mock('../../../assistant/assistantBus', () => ({ emitMetadataRefresh: () => {} })); |
| 44 | +vi.mock('../../../providers/MetadataProvider', () => ({ useMetadata: () => ({ refresh: async () => {} }) })); |
| 45 | +vi.mock('../../../components/SuggestedBindingsPanel', () => ({ |
| 46 | + SuggestedBindingsPanel: () => <div data-testid="suggested-bindings" />, |
| 47 | +})); |
| 48 | +vi.mock('../ConnectorCredentialsStep', () => ({ |
| 49 | + ConnectorCredentialsStep: () => <div data-testid="credential-step" />, |
| 50 | +})); |
| 51 | + |
| 52 | +import { initRuntimeConfig, resetRuntimeConfigForTesting } from '../../../runtime-config'; |
| 53 | +import { MarketplacePackagePage } from '../MarketplacePackagePage'; |
| 54 | + |
| 55 | +const PKG = 'app.acme.crm'; |
| 56 | +const INSTALL_PATH = '/api/v1/marketplace/install-local'; |
| 57 | +const MANIFEST_PATH = '/api/v1/marketplace/packages/pkg_crm/versions/latest/manifest'; |
| 58 | +const CATALOG_PACKAGE = { |
| 59 | + package: { id: 'pkg_crm', manifest_id: PKG, display_name: 'Acme CRM', latest_version: { id: 'ver_6', version: '6.0.0' } }, |
| 60 | + versions: [{ id: 'ver_6', version: '6.0.0' }], |
| 61 | +}; |
| 62 | +const records = (n: number) => Array.from({ length: n }, (_, i) => ({ name: `r${i}` })); |
| 63 | + |
| 64 | +/** A flattened marketplace bundle, the shape a published app's version serves. */ |
| 65 | +const BUNDLE = { |
| 66 | + manifest: { id: PKG, version: '6.0.0', type: 'app', name: 'Acme CRM' }, |
| 67 | + objects: [{ name: 'crm_account' }, { name: 'crm_contact' }, { name: 'crm_lead' }], |
| 68 | + permissions: [{ name: 'crm_user', isDefault: true }, { name: 'crm_admin' }], |
| 69 | + data: [ |
| 70 | + { object: 'crm_account', externalId: 'name', records: records(5) }, |
| 71 | + { object: 'crm_contact', externalId: 'email', records: records(4) }, |
| 72 | + ], |
| 73 | +}; |
| 74 | + |
| 75 | +const MANAGED = { manifestId: PKG, version: '6.0.0', versionId: 'ver_6', installedAt: '2026-11-05T00:00:00Z', upgradedFrom: null, hotLoaded: true }; |
| 76 | + |
| 77 | +const answer = (status: number, body: unknown) => ({ |
| 78 | + ok: status >= 200 && status < 300, |
| 79 | + status, |
| 80 | + statusText: '', |
| 81 | + headers: new Headers({ 'content-type': 'application/json' }), |
| 82 | + json: async () => body, |
| 83 | + text: async () => JSON.stringify(body), |
| 84 | +}); |
| 85 | + |
| 86 | +let calls: string[] = []; |
| 87 | + |
| 88 | +async function boot(manifest: { status: number; body?: unknown }) { |
| 89 | + calls = []; |
| 90 | + resetRuntimeConfigForTesting(); |
| 91 | + vi.stubGlobal('fetch', vi.fn(async (input: unknown, init?: { method?: string }) => { |
| 92 | + const path = new URL(String(input), 'http://console.test').pathname; |
| 93 | + const method = (init?.method ?? 'GET').toUpperCase(); |
| 94 | + calls.push(`${method} ${path}`); |
| 95 | + if (path === '/api/v1/runtime/config') { |
| 96 | + return answer(200, { |
| 97 | + cloudUrl: '', singleEnvironment: true, |
| 98 | + features: { installLocal: true, marketplace: true, aiStudio: true, autoPublishAiBuilds: true }, |
| 99 | + branding: { productName: 'ObjectOS', productShortName: 'ObjectOS' }, |
| 100 | + }); |
| 101 | + } |
| 102 | + if (path === INSTALL_PATH && method === 'GET') return answer(200, { success: true, data: { items: [], total: 0 } }); |
| 103 | + if (path === INSTALL_PATH && method === 'POST') return answer(200, { success: true, data: MANAGED }); |
| 104 | + if (path === MANIFEST_PATH) { |
| 105 | + return manifest.status === 200 |
| 106 | + ? answer(200, { success: true, data: { manifest: manifest.body, version: '6.0.0', version_id: 'ver_6' } }) |
| 107 | + : answer(manifest.status, { success: false, error: { code: 'NOT_FOUND', message: 'no manifest' } }); |
| 108 | + } |
| 109 | + if (path === '/api/v1/marketplace/packages/pkg_crm') return answer(200, { success: true, data: CATALOG_PACKAGE }); |
| 110 | + return answer(404, { success: false, error: { code: 'NOT_FOUND', message: `no route ${method} ${path}` } }); |
| 111 | + })); |
| 112 | + await initRuntimeConfig(); |
| 113 | +} |
| 114 | + |
| 115 | +const posts = () => calls.filter((c) => c === `POST ${INSTALL_PATH}`); |
| 116 | + |
| 117 | +/** Render, settle the manifest read, and click the page's Install. */ |
| 118 | +async function openConfirm() { |
| 119 | + render(<MarketplacePackagePage />); |
| 120 | + const button = await screen.findByRole('button', { name: '«marketplace.action.install»' }); |
| 121 | + await waitFor(() => expect(calls).toContain(`GET ${MANIFEST_PATH}`)); |
| 122 | + await act(async () => {}); |
| 123 | + fireEvent.click(button); |
| 124 | + return screen.findByTestId('install-preview-dialog'); |
| 125 | +} |
| 126 | + |
| 127 | +afterEach(() => { |
| 128 | + cleanup(); |
| 129 | + vi.unstubAllGlobals(); |
| 130 | + resetRuntimeConfigForTesting(); |
| 131 | +}); |
| 132 | + |
| 133 | +describe('the local Install confirms what it adds first (objectstack#30325)', () => { |
| 134 | + it('Install opens the confirm with the counts and the suggested grant, and installs nothing yet', async () => { |
| 135 | + await boot({ status: 200, body: BUNDLE }); |
| 136 | + const dialog = await openConfirm(); |
| 137 | + |
| 138 | + expect(within(dialog).getByText('«marketplace.install.preview.title|name=Acme CRM»')).toBeInTheDocument(); |
| 139 | + expect(within(dialog).getByText('«marketplace.install.preview.intro»')).toBeInTheDocument(); |
| 140 | + expect(within(dialog).getByTestId('install-preview-objects')).toHaveTextContent('3'); |
| 141 | + expect(within(dialog).getByTestId('install-preview-permission-sets')).toHaveTextContent('2'); |
| 142 | + expect(within(dialog).getByTestId('install-preview-sample-records')).toHaveTextContent('9'); |
| 143 | + expect(within(dialog).getByText('«marketplace.install.preview.sampleNote»')).toBeInTheDocument(); |
| 144 | + expect(within(dialog).getByTestId('install-preview-suggestions')).toHaveTextContent( |
| 145 | + '«marketplace.install.preview.suggestsEveryone|set=crm_user»', |
| 146 | + ); |
| 147 | + expect(posts()).toHaveLength(0); |
| 148 | + // One manifest read served the modes and the counts. |
| 149 | + expect(calls.filter((c) => c === `GET ${MANIFEST_PATH}`)).toHaveLength(1); |
| 150 | + }); |
| 151 | + |
| 152 | + it('its Install installs once, as the button did, and the result is drawn', async () => { |
| 153 | + await boot({ status: 200, body: BUNDLE }); |
| 154 | + await openConfirm(); |
| 155 | + fireEvent.click(screen.getByTestId('install-preview-confirm')); |
| 156 | + |
| 157 | + expect(await screen.findByText('«marketplace.install.localSuccess|name=Acme CRM»')).toBeInTheDocument(); |
| 158 | + expect(posts()).toHaveLength(1); |
| 159 | + expect(screen.queryByTestId('install-preview-dialog')).not.toBeInTheDocument(); |
| 160 | + }); |
| 161 | + |
| 162 | + it('Cancel installs nothing', async () => { |
| 163 | + await boot({ status: 200, body: BUNDLE }); |
| 164 | + const dialog = await openConfirm(); |
| 165 | + fireEvent.click(within(dialog).getByRole('button', { name: '«common.cancel»' })); |
| 166 | + |
| 167 | + await waitFor(() => expect(screen.queryByTestId('install-preview-dialog')).not.toBeInTheDocument()); |
| 168 | + await act(async () => {}); |
| 169 | + expect(posts()).toHaveLength(0); |
| 170 | + }); |
| 171 | + |
| 172 | + it('a scoped sample dataset reads as an upper bound', async () => { |
| 173 | + await boot({ status: 200, body: { ...BUNDLE, data: [{ object: 'crm_account', locale: ['zh-CN'], records: records(6) }] } }); |
| 174 | + const dialog = await openConfirm(); |
| 175 | + |
| 176 | + expect(within(dialog).getByTestId('install-preview-sample-records')).toHaveTextContent('«marketplace.install.preview.atMost|count=6»'); |
| 177 | + }); |
| 178 | + |
| 179 | + it('no sample records and no default set: no sample note and no suggestion line', async () => { |
| 180 | + await boot({ status: 200, body: { manifest: BUNDLE.manifest, objects: BUNDLE.objects, permissions: [{ name: 'crm_admin' }] } }); |
| 181 | + const dialog = await openConfirm(); |
| 182 | + |
| 183 | + expect(within(dialog).getByTestId('install-preview-sample-records')).toHaveTextContent('0'); |
| 184 | + expect(within(dialog).queryByText('«marketplace.install.preview.sampleNote»')).not.toBeInTheDocument(); |
| 185 | + expect(within(dialog).queryByTestId('install-preview-suggestions')).not.toBeInTheDocument(); |
| 186 | + }); |
| 187 | + |
| 188 | + it('a manifest that cannot be read: the confirm says so, and its Install still installs', async () => { |
| 189 | + await boot({ status: 404 }); |
| 190 | + const dialog = await openConfirm(); |
| 191 | + |
| 192 | + expect(within(dialog).getByText('«marketplace.install.preview.unreadable»')).toBeInTheDocument(); |
| 193 | + expect(within(dialog).queryByTestId('install-preview')).not.toBeInTheDocument(); |
| 194 | + fireEvent.click(within(dialog).getByTestId('install-preview-confirm')); |
| 195 | + await waitFor(() => expect(posts()).toHaveLength(1)); |
| 196 | + }); |
| 197 | +}); |
0 commit comments