|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#35196 — the served Studio's door to package management. |
| 5 | + * |
| 6 | + * Ruling 甲 (objectstack#25098) puts the unfiltered package list in Studio |
| 7 | + * only. The `@objectstack/studio` app whose Overview → Packages entry names it |
| 8 | + * is not default-loaded, so on a default boot the Studio is the console's own, |
| 9 | + * at `/studio`, and its landing is the door: each package card carries Manage, |
| 10 | + * which opens the package's detail sheet (the one the top-bar switcher's |
| 11 | + * "Package info & settings" opens), and the title row links to the unfiltered |
| 12 | + * Packages page. |
| 13 | + * |
| 14 | + * Read off the real `BuilderLanding` and the real `PackageDetailSheet`. The |
| 15 | + * sheet's Disable is the served `disableable` verdict and its Enable follows a |
| 16 | + * disabled record, so the cases hold one package of each kind, with a |
| 17 | + * read-only package the server does not let be disabled as the control. |
| 18 | + */ |
| 19 | + |
| 20 | +import '@testing-library/jest-dom/vitest'; |
| 21 | +// The source under test reaches `import('@objectstack/lint')`: paid here, at collection, never inside a test's clock. |
| 22 | +import '@objectstack/lint'; |
| 23 | +import * as React from 'react'; |
| 24 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 25 | +import { cleanup, fireEvent, render, screen, within } from '@testing-library/react'; |
| 26 | +import { MemoryRouter, Route, Routes } from 'react-router-dom'; |
| 27 | +import { initRuntimeConfig, resetRuntimeConfigForTesting } from '../../runtime-config'; |
| 28 | + |
| 29 | +/** A code-loaded module of an installed app: the server serves `disableable: true`. */ |
| 30 | +const MODULE = { id: 'com.example.multi.orders', name: 'Multi-Package Orders' }; |
| 31 | +/** A code-loaded app: read-only, and the disable door refuses it. */ |
| 32 | +const APP = { id: 'com.example.multi.core', name: 'Multi-Package Core' }; |
| 33 | +/** A module already switched off. */ |
| 34 | +const DISABLED = { id: 'com.example.multi.extras', name: 'Multi-Package Extras' }; |
| 35 | +/** The author's own writable base. */ |
| 36 | +const OWN = { id: 'com.acme.mine', name: 'My App' }; |
| 37 | + |
| 38 | +const entries = [ |
| 39 | + { ...MODULE, writable: false, namespace: 'crm' }, |
| 40 | + { ...APP, writable: false, namespace: 'crm' }, |
| 41 | + { ...DISABLED, writable: false, namespace: 'crm' }, |
| 42 | + { ...OWN, writable: true, namespace: 'mine' }, |
| 43 | +]; |
| 44 | + |
| 45 | +/** `GET /api/v1/packages` as the wire serves it: the full rows the sheet opens on. */ |
| 46 | +const servedRows = [ |
| 47 | + { manifest: { ...MODULE, version: '1.0.0', type: 'module' }, source: 'code', writable: false, disableable: true, enabled: true, status: 'installed' }, |
| 48 | + { manifest: { ...APP, version: '1.0.0', type: 'app' }, source: 'code', writable: false, disableable: false, enabled: true, status: 'installed' }, |
| 49 | + { manifest: { ...DISABLED, version: '1.0.0', type: 'module' }, source: 'code', writable: false, disableable: true, enabled: false, status: 'disabled' }, |
| 50 | + { manifest: { ...OWN, version: '0.1.0', type: 'app' }, source: 'authored', writable: true, disableable: true, enabled: true, status: 'installed' }, |
| 51 | +]; |
| 52 | + |
| 53 | +vi.mock('./packages-io', async (importOriginal) => { |
| 54 | + const mod = await importOriginal<typeof import('./packages-io')>(); |
| 55 | + return { |
| 56 | + ...mod, |
| 57 | + fetchPackages: vi.fn(async () => entries), |
| 58 | + fetchListedPackageIds: vi.fn(async () => new Set(entries.map((e) => e.id))), |
| 59 | + }; |
| 60 | +}); |
| 61 | + |
| 62 | +// The create dialog is closed throughout; its form stack is not under test. |
| 63 | +vi.mock('../metadata-admin/PackageFormDialog', () => ({ PackageFormDialog: () => null })); |
| 64 | + |
| 65 | +import { BuilderLanding, STUDIO_ALL_PACKAGES_PATH } from './BuilderLanding'; |
| 66 | + |
| 67 | +const json = (body: unknown, status = 200) => |
| 68 | + ({ ok: status < 400, status, json: async () => body, text: async () => JSON.stringify(body) }) as Response; |
| 69 | + |
| 70 | +afterEach(() => { |
| 71 | + cleanup(); |
| 72 | + vi.unstubAllGlobals(); |
| 73 | + resetRuntimeConfigForTesting(); |
| 74 | +}); |
| 75 | + |
| 76 | +async function renderLanding() { |
| 77 | + resetRuntimeConfigForTesting(); |
| 78 | + vi.stubGlobal( |
| 79 | + 'fetch', |
| 80 | + vi.fn(async (input: RequestInfo | URL) => { |
| 81 | + const url = String(input); |
| 82 | + if (/\/api\/v1\/packages(\?|$)/.test(url)) return json({ success: true, data: { packages: servedRows } }); |
| 83 | + if (url.includes('/api/v1/meta/_drafts')) return json({ drafts: [] }); |
| 84 | + return json({ features: { installLocal: false, marketplace: false } }); |
| 85 | + }), |
| 86 | + ); |
| 87 | + await initRuntimeConfig(); |
| 88 | + render( |
| 89 | + <MemoryRouter initialEntries={['/studio']}> |
| 90 | + <Routes> |
| 91 | + <Route path="/studio" element={<BuilderLanding />} /> |
| 92 | + </Routes> |
| 93 | + </MemoryRouter>, |
| 94 | + ); |
| 95 | + await screen.findByText(MODULE.name); |
| 96 | +} |
| 97 | + |
| 98 | +/** The card for `id`, and its Manage button. */ |
| 99 | +function manageOn(id: string): HTMLElement { |
| 100 | + const card = document.querySelector<HTMLElement>(`[data-package-id="${id}"]`); |
| 101 | + if (!card) throw new Error(`no card for ${id}`); |
| 102 | + return within(card).getByTestId('studio-landing-manage'); |
| 103 | +} |
| 104 | + |
| 105 | +/** Opens `id`'s sheet from its card and answers the sheet's action names. */ |
| 106 | +async function openSheet(id: string, name: string): Promise<string[]> { |
| 107 | + fireEvent.click(manageOn(id)); |
| 108 | + const sheet = await screen.findByRole('dialog'); |
| 109 | + await within(sheet).findByText(name); |
| 110 | + return within(sheet) |
| 111 | + .getAllByRole('button') |
| 112 | + .map((b) => (b.textContent ?? '').trim()) |
| 113 | + .filter(Boolean); |
| 114 | +} |
| 115 | + |
| 116 | +describe('the Studio landing is the served door to package management (objectstack#35196)', () => { |
| 117 | + it('a code-loaded module the server lets be disabled opens a sheet with Disable', async () => { |
| 118 | + await renderLanding(); |
| 119 | + expect(await openSheet(MODULE.id, MODULE.name)).toContain('Disable'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('control: a read-only app the disable door refuses opens a sheet without Disable', async () => { |
| 123 | + await renderLanding(); |
| 124 | + const actions = await openSheet(APP.id, APP.name); |
| 125 | + expect(actions).not.toContain('Disable'); |
| 126 | + expect(actions).not.toContain('Enable'); |
| 127 | + }); |
| 128 | + |
| 129 | + it('a disabled package opens a sheet with Enable, so switching it back on stays one click from the landing', async () => { |
| 130 | + await renderLanding(); |
| 131 | + const actions = await openSheet(DISABLED.id, DISABLED.name); |
| 132 | + expect(actions).toContain('Enable'); |
| 133 | + expect(actions).not.toContain('Disable'); |
| 134 | + }); |
| 135 | + |
| 136 | + it("every card carries Manage, the author's own apps included", async () => { |
| 137 | + await renderLanding(); |
| 138 | + for (const { id } of entries) expect(manageOn(id)).toBeInTheDocument(); |
| 139 | + expect(await openSheet(OWN.id, OWN.name)).toContain('Disable'); |
| 140 | + }); |
| 141 | + |
| 142 | + it('the title row links to the unfiltered Packages page', async () => { |
| 143 | + await renderLanding(); |
| 144 | + const link = screen.getByTestId('studio-landing-all-packages'); |
| 145 | + expect(link).toHaveAttribute('href', '/apps/setup/component/developer/packages'); |
| 146 | + expect(STUDIO_ALL_PACKAGES_PATH).toBe('/apps/setup/component/developer/packages'); |
| 147 | + // Unfiltered: no `source` query narrows it to the marketplace view. |
| 148 | + expect(link.getAttribute('href')).not.toContain('source='); |
| 149 | + }); |
| 150 | +}); |
0 commit comments