|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * Smoke / regression test: every published `@object-ui/plugin-*` MUST share |
| 6 | + * the single `ComponentRegistry` singleton exported from `@object-ui/core`. |
| 7 | + * |
| 8 | + * If a plugin's bundle accidentally inlines `@object-ui/core`, it ends up with |
| 9 | + * its own private `ComponentRegistry` — components registered by that plugin |
| 10 | + * become invisible to other plugins, and downstream apps see `undefined` |
| 11 | + * lookups across plugin boundaries. |
| 12 | + * |
| 13 | + * This test imports the **built dist** of two representative plugins via |
| 14 | + * explicit relative paths (bypassing vitest's source aliases) and asserts |
| 15 | + * that the components they register are visible on the same singleton. |
| 16 | + */ |
| 17 | + |
| 18 | +import path from 'node:path'; |
| 19 | +import { existsSync } from 'node:fs'; |
| 20 | +import { pathToFileURL } from 'node:url'; |
| 21 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 22 | + |
| 23 | +import { ComponentRegistry } from '../Registry.js'; |
| 24 | + |
| 25 | +const PLUGIN_GRID_DIST = path.resolve( |
| 26 | + __dirname, |
| 27 | + '../../../../plugin-grid/dist/index.js', |
| 28 | +); |
| 29 | +const PLUGIN_LIST_DIST = path.resolve( |
| 30 | + __dirname, |
| 31 | + '../../../../plugin-list/dist/index.js', |
| 32 | +); |
| 33 | + |
| 34 | +const distAvailable = |
| 35 | + existsSync(PLUGIN_GRID_DIST) && existsSync(PLUGIN_LIST_DIST); |
| 36 | + |
| 37 | +describe.skipIf(!distAvailable)( |
| 38 | + 'ComponentRegistry singleton across plugin dist bundles', |
| 39 | + () => { |
| 40 | + const baselineSize = ComponentRegistry.getAllConfigs().length; |
| 41 | + |
| 42 | + beforeAll(async () => { |
| 43 | + // Import via file URL so Node treats them as ESM modules and the |
| 44 | + // workspace aliases (which only apply to bare specifiers) are bypassed. |
| 45 | + await import(pathToFileURL(PLUGIN_GRID_DIST).href); |
| 46 | + await import(pathToFileURL(PLUGIN_LIST_DIST).href); |
| 47 | + }); |
| 48 | + |
| 49 | + afterAll(() => { |
| 50 | + // Best-effort cleanup: drop registry entries the plugins added so this |
| 51 | + // test doesn't pollute other tests in the same vitest worker. |
| 52 | + const components = (ComponentRegistry as unknown as { |
| 53 | + components: Map<string, unknown>; |
| 54 | + }).components; |
| 55 | + for (const key of [ |
| 56 | + 'plugin-grid:object-grid', |
| 57 | + 'object-grid', |
| 58 | + 'view:grid', |
| 59 | + 'plugin-grid:import-wizard', |
| 60 | + 'import-wizard', |
| 61 | + 'plugin-list:list-view', |
| 62 | + 'list-view', |
| 63 | + 'view:list', |
| 64 | + ]) { |
| 65 | + components.delete(key); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + it('plugin-grid registers object-grid into the shared singleton', () => { |
| 70 | + const cfg = |
| 71 | + ComponentRegistry.getConfig('plugin-grid:object-grid') ?? |
| 72 | + ComponentRegistry.getConfig('object-grid'); |
| 73 | + expect(cfg).toBeDefined(); |
| 74 | + expect(cfg?.component).toBeTruthy(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('plugin-list registers list-view into the shared singleton', () => { |
| 78 | + const cfg = |
| 79 | + ComponentRegistry.getConfig('plugin-list:list-view') ?? |
| 80 | + ComponentRegistry.getConfig('list-view'); |
| 81 | + expect(cfg).toBeDefined(); |
| 82 | + expect(cfg?.component).toBeTruthy(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('plugin-list can resolve a component registered by plugin-grid', () => { |
| 86 | + // Cross-plugin lookup — the actual bug scenario reported downstream. |
| 87 | + const fromList = |
| 88 | + ComponentRegistry.get('plugin-grid:object-grid') ?? |
| 89 | + ComponentRegistry.get('object-grid'); |
| 90 | + expect(fromList).toBeDefined(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('importing both plugins increases registry size on the singleton', () => { |
| 94 | + expect(ComponentRegistry.getAllConfigs().length).toBeGreaterThan( |
| 95 | + baselineSize, |
| 96 | + ); |
| 97 | + }); |
| 98 | + }, |
| 99 | +); |
0 commit comments