|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The package page draws the `422 DEPENDENCY_MISSING` install refusal |
| 5 | + * (ADR-0141 D2) in the author's terms — 「请先安装 HotCRM 6.x」 — one line per |
| 6 | + * unmet entry, from the refusal's own facts (objectstack#25792 item 3). |
| 7 | + * |
| 8 | + * `marketplaceApi` is NOT mocked: one stubbed `fetch` answers by path, so the |
| 9 | + * facts the page draws are the ones that crossed the wire. `t` echoes the KEY |
| 10 | + * and the interpolated facts: the claim is WHICH line each entry gets and that |
| 11 | + * it carries that entry's id, range and version. |
| 12 | + */ |
| 13 | + |
| 14 | +import '@testing-library/jest-dom/vitest'; |
| 15 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 16 | +import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'; |
| 17 | + |
| 18 | +vi.mock('react-router-dom', () => ({ |
| 19 | + useNavigate: () => vi.fn(), |
| 20 | + useParams: () => ({ packageId: 'pkg_psa' }), |
| 21 | +})); |
| 22 | + |
| 23 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 24 | + ...(await importOriginal<Record<string, unknown>>()), |
| 25 | + useWorkspaceAdminStatus: () => ({ isAdmin: true, isResolved: true }), |
| 26 | +})); |
| 27 | + |
| 28 | +vi.mock('@object-ui/i18n', async (importOriginal) => ({ |
| 29 | + ...(await importOriginal<Record<string, unknown>>()), |
| 30 | + useObjectTranslation: () => ({ |
| 31 | + t: (key: string, o?: Record<string, unknown>) => { |
| 32 | + const facts = ['dependency', 'range', 'installedVersion'].filter((k) => o?.[k] !== undefined).map((k) => `${k}=${String(o?.[k])}`); |
| 33 | + return facts.length > 0 ? `«${key}|${facts.join('|')}»` : `«${key}»`; |
| 34 | + }, |
| 35 | + language: 'en', |
| 36 | + }), |
| 37 | +})); |
| 38 | + |
| 39 | +vi.mock('../../../assistant/assistantBus', () => ({ emitMetadataRefresh: () => {} })); |
| 40 | +vi.mock('../../../providers/MetadataProvider', () => ({ useMetadata: () => ({ refresh: async () => {} }) })); |
| 41 | +vi.mock('../../../components/SuggestedBindingsPanel', () => ({ |
| 42 | + SuggestedBindingsPanel: () => <div data-testid="suggested-bindings" />, |
| 43 | +})); |
| 44 | + |
| 45 | +import { initRuntimeConfig, resetRuntimeConfigForTesting } from '../../../runtime-config'; |
| 46 | +import { MarketplacePackagePage } from '../MarketplacePackagePage'; |
| 47 | + |
| 48 | +const INSTALL_PATH = '/api/v1/marketplace/install-local'; |
| 49 | +const MANIFEST_PATH = '/api/v1/marketplace/packages/pkg_psa/versions/latest/manifest'; |
| 50 | +const CATALOG_PACKAGE = { |
| 51 | + package: { id: 'pkg_psa', manifest_id: 'com.acme.psa', display_name: 'PSA', latest_version: { id: 'ver_1', version: '1.0.0' } }, |
| 52 | + versions: [{ id: 'ver_1', version: '1.0.0' }], |
| 53 | +}; |
| 54 | +const SERVER_MESSAGE = 'SERVER-ENGLISH sentinel: Install refused: a declared dependency is not satisfied in this environment.'; |
| 55 | + |
| 56 | +let installBody: unknown; |
| 57 | + |
| 58 | +const answer = (status: number, body: unknown) => ({ |
| 59 | + ok: status >= 200 && status < 300, |
| 60 | + status, |
| 61 | + statusText: '', |
| 62 | + headers: new Headers({ 'content-type': 'application/json' }), |
| 63 | + json: async () => body, |
| 64 | + text: async () => JSON.stringify(body), |
| 65 | +}); |
| 66 | + |
| 67 | +async function boot() { |
| 68 | + resetRuntimeConfigForTesting(); |
| 69 | + vi.stubGlobal('fetch', vi.fn(async (input: unknown, init?: { method?: string }) => { |
| 70 | + const path = new URL(String(input), 'http://console.test').pathname; |
| 71 | + const method = (init?.method ?? 'GET').toUpperCase(); |
| 72 | + if (path === '/api/v1/runtime/config') { |
| 73 | + return answer(200, { |
| 74 | + cloudUrl: '', singleEnvironment: true, |
| 75 | + features: { installLocal: true, marketplace: true, aiStudio: true, autoPublishAiBuilds: true }, |
| 76 | + branding: { productName: 'ObjectOS', productShortName: 'ObjectOS' }, |
| 77 | + }); |
| 78 | + } |
| 79 | + if (path === INSTALL_PATH && method === 'GET') return answer(200, { success: true, data: { items: [], total: 0 } }); |
| 80 | + if (path === INSTALL_PATH && method === 'POST') return answer(422, installBody); |
| 81 | + if (path === MANIFEST_PATH) { |
| 82 | + return answer(200, { success: true, data: { manifest: { id: 'com.acme.psa', version: '1.0.0', type: 'app' }, version: '1.0.0', version_id: 'ver_1' } }); |
| 83 | + } |
| 84 | + if (path === '/api/v1/marketplace/packages/pkg_psa') return answer(200, { success: true, data: CATALOG_PACKAGE }); |
| 85 | + return answer(404, { success: false, error: { code: 'NOT_FOUND', message: `no route ${method} ${path}` } }); |
| 86 | + })); |
| 87 | + await initRuntimeConfig(); |
| 88 | +} |
| 89 | + |
| 90 | +async function install() { |
| 91 | + render(<MarketplacePackagePage />); |
| 92 | + const button = await screen.findByRole('button', { name: '«marketplace.action.install»' }); |
| 93 | + await act(async () => {}); |
| 94 | + fireEvent.click(button); |
| 95 | +} |
| 96 | + |
| 97 | +const refusal = (details: unknown) => ({ |
| 98 | + success: false, |
| 99 | + error: { code: 'DEPENDENCY_MISSING', httpStatus: 422, message: SERVER_MESSAGE, details }, |
| 100 | +}); |
| 101 | + |
| 102 | +beforeEach(() => { installBody = undefined; }); |
| 103 | +afterEach(() => { |
| 104 | + cleanup(); |
| 105 | + vi.unstubAllGlobals(); |
| 106 | + resetRuntimeConfigForTesting(); |
| 107 | +}); |
| 108 | + |
| 109 | +describe('the package page draws DEPENDENCY_MISSING from its facts (objectstack#25792)', () => { |
| 110 | + it('one line per unmet entry under the heading, in place of the server\'s English sentence', async () => { |
| 111 | + installBody = refusal({ |
| 112 | + compatible: false, |
| 113 | + issues: [ |
| 114 | + { severity: 'error', category: 'dependency_missing', message: 'not installed' }, |
| 115 | + { severity: 'error', category: 'dependency_missing', message: 'out of range' }, |
| 116 | + ], |
| 117 | + unmet: [ |
| 118 | + { packageId: 'com.acme.psa', dependency: 'app.objectstack.hotcrm', range: '6.x', found: 'absent' }, |
| 119 | + { packageId: 'com.acme.psa', dependency: 'com.acme.billing', range: '^2.1.0', found: 'out-of-range', installedVersion: '1.4.0' }, |
| 120 | + ], |
| 121 | + }); |
| 122 | + await boot(); |
| 123 | + await install(); |
| 124 | + |
| 125 | + const note = await screen.findByText(/marketplace\.install\.dependencyMissing\.heading/); |
| 126 | + expect(note.textContent!.split('\n')).toEqual([ |
| 127 | + '«marketplace.install.dependencyMissing.heading»', |
| 128 | + '• «marketplace.install.dependencyMissing.absent|dependency=app.objectstack.hotcrm|range=6.x|installedVersion=—»', |
| 129 | + '• «marketplace.install.dependencyMissing.outOfRange|dependency=com.acme.billing|range=^2.1.0|installedVersion=1.4.0»', |
| 130 | + ]); |
| 131 | + expect(screen.queryByText(SERVER_MESSAGE)).not.toBeInTheDocument(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('CONTROL: a refusal without facts it can read keeps the envelope\'s own message', async () => { |
| 135 | + installBody = refusal({ compatible: false, issues: [] }); |
| 136 | + await boot(); |
| 137 | + await install(); |
| 138 | + |
| 139 | + expect(await screen.findByText(SERVER_MESSAGE)).toBeInTheDocument(); |
| 140 | + expect(screen.queryByText(/dependencyMissing/)).not.toBeInTheDocument(); |
| 141 | + }); |
| 142 | +}); |
0 commit comments