|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// The MERGED-ORDER pin for Setup → Integrations. |
| 4 | +// |
| 5 | +// --------------------------------------------------------------------------- |
| 6 | +// What this pins |
| 7 | +// --------------------------------------------------------------------------- |
| 8 | +// `SETUP_APP` declares `group_integrations` as an empty anchor (ADR-0029 D7); |
| 9 | +// every entry in it is contributed at runtime by the package that owns the |
| 10 | +// capability, and `SchemaRegistry.applyNavContributions` orders the |
| 11 | +// contributions by their `priority`, lowest first. Today that reads: |
| 12 | +// |
| 13 | +// Webhooks / HTTP Deliveries `@objectstack/plugin-webhooks` |
| 14 | +// Datasources `@objectstack/service-datasource` |
| 15 | +// Connect an Agent `@objectstack/mcp` |
| 16 | +// Connector Credentials `@objectstack/service-automation` |
| 17 | +// Cloud Connection `@objectstack/cloud-connection` |
| 18 | +// |
| 19 | +// Each contributor states the group's other priorities in its own header so |
| 20 | +// that its number sorts where it means to — and nothing held those headers to |
| 21 | +// the code. A later contributor could renumber one of the five and silently |
| 22 | +// reorder the menu an administrator reads. This file is that hold. |
| 23 | +// |
| 24 | +// ⛔ The priorities are READ from the real contributions, never copied here as |
| 25 | +// literals: a pin that restated `110` / `120` would pass beside a source that |
| 26 | +// had moved, as long as nobody re-ran the arithmetic. What the file does state |
| 27 | +// is the ORDER of the ids, which is the user-visible fact under protection. |
| 28 | +// |
| 29 | +// The first two contributors share one priority, so the fold's stable sort |
| 30 | +// leaves their relative order to registration order, which belongs to the |
| 31 | +// composition root's plugin list and not to any priority. That tier is pinned |
| 32 | +// as a set; every tier boundary is pinned as an order. |
| 33 | +// |
| 34 | +// --------------------------------------------------------------------------- |
| 35 | +// Why it lives in `packages/cli/test/` |
| 36 | +// --------------------------------------------------------------------------- |
| 37 | +// The same reason as `connect-agent-both-halves-wire.pin.test.ts` beside it: |
| 38 | +// this is a COMPOSED fact across five packages and the registry fold, and |
| 39 | +// `packages/cli` is the only workspace package that depends on all of them. |
| 40 | +// |
| 41 | +// Two of the five register their contribution from inside `init()` through |
| 42 | +// the `manifest` service rather than exporting it, so they are booted here on |
| 43 | +// a context that serves exactly that service — the shape |
| 44 | +// `packages/cli/scripts/check-app-nav-i18n.mjs` boots the same two with. |
| 45 | +// `@objectstack/service-automation` registers its bundle at `kernel:ready`, |
| 46 | +// which this context never fires, so its exported bundle is read directly, as |
| 47 | +// that gate does too. |
| 48 | + |
| 49 | +import { describe, expect, it } from 'vitest'; |
| 50 | +import { CLOUD_CONNECTION_UI_BUNDLE } from '@objectstack/cloud-connection'; |
| 51 | +import { CONNECT_AGENT_UI_BUNDLE } from '@objectstack/mcp'; |
| 52 | +import { SchemaRegistry } from '@objectstack/objectql'; |
| 53 | +import { SETUP_APP, SETUP_NAV_CONTRIBUTIONS } from '@objectstack/platform-objects/apps'; |
| 54 | +import { WebhookOutboxPlugin } from '@objectstack/plugin-webhooks'; |
| 55 | +import { CONNECTOR_CREDENTIALS_UI_BUNDLE } from '@objectstack/service-automation'; |
| 56 | +import { DatasourceAdminServicePlugin } from '@objectstack/service-datasource'; |
| 57 | + |
| 58 | +type AnyRec = Record<string, any>; |
| 59 | + |
| 60 | +const GROUP = 'group_integrations'; |
| 61 | + |
| 62 | +/** |
| 63 | + * The group, in the order an administrator reads it. Each inner array is one |
| 64 | + * priority tier; ids inside a tier are compared as a set (see the header). |
| 65 | + */ |
| 66 | +const EXPECTED_TIERS: readonly (readonly string[])[] = [ |
| 67 | + ['nav_webhooks', 'nav_http_deliveries', 'nav_datasources'], |
| 68 | + ['nav_connect_agent'], |
| 69 | + ['nav_connector_credentials'], |
| 70 | + ['nav_cloud_connection'], |
| 71 | +]; |
| 72 | + |
| 73 | +/** |
| 74 | + * The manifests a plugin registers from `init()`, captured off a context that |
| 75 | + * serves the `manifest` service and inert, complete stand-ins for the rest of |
| 76 | + * the interface (`packages/core/src/types.ts`), so a missing member cannot be |
| 77 | + * misread as a plugin that contributes nothing. |
| 78 | + */ |
| 79 | +async function manifestsRegisteredBy(plugin: { init(ctx: any): Promise<void> | void }): Promise<AnyRec[]> { |
| 80 | + const manifests: AnyRec[] = []; |
| 81 | + const ctx = { |
| 82 | + getService: (name: string) => (name === 'manifest' ? { register: (m: AnyRec) => manifests.push(m) } : undefined), |
| 83 | + registerService: () => {}, |
| 84 | + registerServiceFactory: () => {}, |
| 85 | + replaceService: () => {}, |
| 86 | + getServiceScoped: async () => undefined, |
| 87 | + getServices: () => new Map(), |
| 88 | + hook: () => {}, |
| 89 | + trigger: async () => {}, |
| 90 | + logger: { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} }, |
| 91 | + getKernel: () => undefined, |
| 92 | + }; |
| 93 | + await plugin.init(ctx); |
| 94 | + return manifests; |
| 95 | +} |
| 96 | + |
| 97 | +/** Every Setup contribution the composition carries, tagged with its package. */ |
| 98 | +async function setupContributions(): Promise<Array<{ packageId: string; contribution: AnyRec }>> { |
| 99 | + const sources: Array<[string, AnyRec[]]> = [ |
| 100 | + ['@objectstack/plugin-webhooks', await manifestsRegisteredBy(new WebhookOutboxPlugin({ autoEnqueue: false }))], |
| 101 | + ['@objectstack/service-datasource', await manifestsRegisteredBy(new DatasourceAdminServicePlugin({}) as any)], |
| 102 | + ['@objectstack/mcp', [CONNECT_AGENT_UI_BUNDLE as AnyRec]], |
| 103 | + ['@objectstack/service-automation', [CONNECTOR_CREDENTIALS_UI_BUNDLE as AnyRec]], |
| 104 | + ['@objectstack/cloud-connection', [CLOUD_CONNECTION_UI_BUNDLE as AnyRec]], |
| 105 | + ]; |
| 106 | + const out: Array<{ packageId: string; contribution: AnyRec }> = SETUP_NAV_CONTRIBUTIONS.map((contribution) => ({ |
| 107 | + packageId: '@objectstack/platform-objects', |
| 108 | + contribution: contribution as AnyRec, |
| 109 | + })); |
| 110 | + for (const [packageId, manifests] of sources) { |
| 111 | + for (const manifest of manifests) { |
| 112 | + for (const contribution of manifest?.navigationContributions ?? []) { |
| 113 | + if (contribution?.app === 'setup') out.push({ packageId, contribution }); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + return out; |
| 118 | +} |
| 119 | + |
| 120 | +/** The children of one nav group in the merged app, by id. */ |
| 121 | +function groupChildren(app: AnyRec | undefined, groupId: string): string[] { |
| 122 | + const group = (app?.navigation ?? []).find((item: AnyRec) => item?.id === groupId); |
| 123 | + return (group?.children ?? []).map((child: AnyRec) => String(child?.id)); |
| 124 | +} |
| 125 | + |
| 126 | +describe('Setup → Integrations: the merged order follows each contributor\'s priority', () => { |
| 127 | + it('serves the five contributors in priority order, read from the contributions themselves', async () => { |
| 128 | + const contributions = await setupContributions(); |
| 129 | + |
| 130 | + // `structuredClone`: `registerApp` writes the `_lock` envelope onto |
| 131 | + // what it is handed (ADR-0010 §3.7); the module constant stays clean. |
| 132 | + const registry = new SchemaRegistry({ multiTenant: false, collisionPolicy: 'error' }); |
| 133 | + (registry as AnyRec).logLevel = 'silent'; |
| 134 | + registry.registerApp(structuredClone(SETUP_APP), '@objectstack/platform-objects'); |
| 135 | + for (const { packageId, contribution } of contributions) { |
| 136 | + registry.registerAppNavContribution(contribution as any, packageId); |
| 137 | + } |
| 138 | + |
| 139 | + const merged = groupChildren(registry.getApp('setup'), GROUP); |
| 140 | + |
| 141 | + // ── The order an administrator reads ────────────────────────────── |
| 142 | + expect(merged).toHaveLength(EXPECTED_TIERS.flat().length); |
| 143 | + let offset = 0; |
| 144 | + for (const tier of EXPECTED_TIERS) { |
| 145 | + expect(new Set(merged.slice(offset, offset + tier.length))).toEqual(new Set(tier)); |
| 146 | + offset += tier.length; |
| 147 | + } |
| 148 | + |
| 149 | + // ── The priorities behind it, read where they are declared ──────── |
| 150 | + // Every id in the group, mapped to the priority of the contribution |
| 151 | + // that carries it — and to that contribution's package, so a red names |
| 152 | + // who moved. |
| 153 | + const declared = new Map<string, { priority: number; packageId: string }>(); |
| 154 | + for (const { packageId, contribution } of contributions) { |
| 155 | + if (contribution.group !== GROUP) continue; |
| 156 | + for (const item of contribution.items ?? []) { |
| 157 | + declared.set(String(item.id), { priority: contribution.priority, packageId }); |
| 158 | + } |
| 159 | + } |
| 160 | + // Nothing contributes into the group that this pin does not order. |
| 161 | + expect([...declared.keys()].sort()).toEqual([...EXPECTED_TIERS.flat()].sort()); |
| 162 | + |
| 163 | + const tierPriority = EXPECTED_TIERS.map((tier) => { |
| 164 | + const priorities = new Set(tier.map((id) => declared.get(id)?.priority)); |
| 165 | + expect(priorities.size, `${tier.join(', ')} share one priority`).toBe(1); |
| 166 | + const [priority] = priorities; |
| 167 | + expect(typeof priority, `${tier.join(', ')} declare a numeric priority`).toBe('number'); |
| 168 | + return { tier, priority: priority as number }; |
| 169 | + }); |
| 170 | + for (let i = 1; i < tierPriority.length; i += 1) { |
| 171 | + const before = tierPriority[i - 1]; |
| 172 | + const after = tierPriority[i]; |
| 173 | + const owner = (id: string) => declared.get(id)?.packageId; |
| 174 | + expect( |
| 175 | + before.priority < after.priority, |
| 176 | + `${before.tier[0]} (${owner(before.tier[0])}, ${before.priority}) sorts before ` + |
| 177 | + `${after.tier[0]} (${owner(after.tier[0])}, ${after.priority})`, |
| 178 | + ).toBe(true); |
| 179 | + } |
| 180 | + }); |
| 181 | +}); |
0 commit comments