|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `AuthPluginOptions.registerOrganizationRecordPage`: a host that ships its own |
| 5 | + * default record page for `sys_organization` composes without plugin-auth's. |
| 6 | + * |
| 7 | + * ## The rule this option serves |
| 8 | + * |
| 9 | + * One `isDefault: true` record page per object per deployment, a second refused |
| 10 | + * at registration with `DEFAULT_RECORD_PAGE_CONFLICT` (maintainer ruling A on |
| 11 | + * objectstack#23015, record 6134056653). Ruling A keeps one page "by |
| 12 | + * overlay-by-name or by composition"; this option is the composition route. It |
| 13 | + * is not a precedence ladder and not a registration-order rule: with it set, |
| 14 | + * plugin-auth registers no organization page at all, so the host's page is the |
| 15 | + * single default whichever plugin initialises first. |
| 16 | + * |
| 17 | + * ## The three pins, each with its control |
| 18 | + * |
| 19 | + * 1. With the option (`false`), the host's page is the single default record |
| 20 | + * page of `sys_organization` and the boot succeeds, in either plugin order. |
| 21 | + * Control: the same composition without the option is refused (pin 3). |
| 22 | + * 2. Without it, plugin-auth's `sys_organization_detail` registers as today. |
| 23 | + * Control: with the option and no host page, `sys_organization` has no |
| 24 | + * default record page, so the assertion can tell the two apart. |
| 25 | + * `sys_user_detail` registers in both. |
| 26 | + * 3. With both pages and no option, `DEFAULT_RECORD_PAGE_CONFLICT` still |
| 27 | + * refuses the boot, naming both pages, in either plugin order. Control: an |
| 28 | + * explicit `true` is today's behaviour and is refused the same way. |
| 29 | + * |
| 30 | + * ## The composition |
| 31 | + * |
| 32 | + * The cheapest real one: a real `ObjectKernel`, a real ObjectQL engine on |
| 33 | + * in-memory SQLite, the real `AuthPlugin`, and a host plugin in the place of a |
| 34 | + * product package (the cloud control plane's tenant service is the first such |
| 35 | + * host) registering its own organization page through the `manifest` service, |
| 36 | + * as `AppPlugin.init()` does. The refusal is the registry's own, reached |
| 37 | + * through that door. |
| 38 | + * |
| 39 | + * ## Resolution |
| 40 | + * |
| 41 | + * `AuthPlugin` is imported from SOURCE (relative). `ObjectKernel`, |
| 42 | + * `ObjectQLPlugin` and `SqlDriver` are the fixed instruments, read from |
| 43 | + * `dist/` as this package's other kernel tests read them, and so is |
| 44 | + * `@objectstack/spec/ui`'s `defaultRecordPageObject`, the one reading of "the |
| 45 | + * default record page of object X" the registry's refusal asks too. |
| 46 | + */ |
| 47 | + |
| 48 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 49 | +import { ObjectKernel, type Plugin, type PluginContext } from '@objectstack/core'; |
| 50 | +import { ObjectQLPlugin, type ObjectQL } from '@objectstack/objectql'; |
| 51 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 52 | +import { defaultRecordPageObject } from '@objectstack/spec/ui'; |
| 53 | +import { AuthPlugin, type AuthPluginOptions } from './auth-plugin.js'; |
| 54 | +import { AUTH_PLUGIN_ID } from './manifest.js'; |
| 55 | + |
| 56 | +const HOST_PACKAGE_ID = 'com.example.host-tenant'; |
| 57 | +const HOST_PAGE = 'host_organization_detail'; |
| 58 | +const AUTH_PAGE = 'sys_organization_detail'; |
| 59 | + |
| 60 | +/** Env the auth plugin's boot branches on; cleared per case so the default `single` posture holds. */ |
| 61 | +const ENV_KEYS = ['OS_TENANCY_POSTURE', 'OS_MULTI_ORG_ENABLED', 'OS_PLATFORM_OWNER_EMAIL'] as const; |
| 62 | + |
| 63 | +type Holder = { kind: 'package'; packageId: string; page: string } | { kind: 'environment'; page: string }; |
| 64 | +type Refusal = Error & { code?: string; status?: number; object?: string; incoming?: Holder; existing?: Holder }; |
| 65 | + |
| 66 | +/** Publishes an in-memory SQLite driver the way a datasource plugin does. */ |
| 67 | +function sqliteDriverPlugin(): Plugin { |
| 68 | + return { |
| 69 | + name: 'test.driver.sqlite', |
| 70 | + type: 'standard', |
| 71 | + version: '1.0.0', |
| 72 | + async init(ctx: PluginContext) { |
| 73 | + ctx.registerService( |
| 74 | + 'driver.default', |
| 75 | + new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }), |
| 76 | + ); |
| 77 | + }, |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * A host shipping its own default record page for `sys_organization`, |
| 83 | + * registered through the `manifest` service in `init()` as `AppPlugin.init()` |
| 84 | + * does. No edge to `AuthPlugin`, so the order the case uses it in is the order |
| 85 | + * the two initialise in. |
| 86 | + */ |
| 87 | +function hostPlugin(): Plugin { |
| 88 | + return { |
| 89 | + name: HOST_PACKAGE_ID, |
| 90 | + type: 'app', |
| 91 | + version: '1.0.0', |
| 92 | + dependencies: ['com.objectstack.engine.objectql'], |
| 93 | + async init(ctx: PluginContext) { |
| 94 | + ctx.getService<{ register(m: unknown): unknown }>('manifest').register({ |
| 95 | + id: HOST_PACKAGE_ID, |
| 96 | + name: 'host-tenant', |
| 97 | + version: '1.0.0', |
| 98 | + type: 'app', |
| 99 | + pages: [ |
| 100 | + { |
| 101 | + name: HOST_PAGE, |
| 102 | + label: 'Workspace', |
| 103 | + type: 'record', |
| 104 | + object: 'sys_organization', |
| 105 | + isDefault: true, |
| 106 | + regions: [], |
| 107 | + }, |
| 108 | + ], |
| 109 | + }); |
| 110 | + }, |
| 111 | + }; |
| 112 | +} |
| 113 | + |
| 114 | +const authPlugin = (options: Pick<AuthPluginOptions, 'registerOrganizationRecordPage'> = {}) => |
| 115 | + new AuthPlugin({ secret: 'test-secret-at-least-32-chars-long', baseUrl: 'http://localhost:3000', ...options }); |
| 116 | + |
| 117 | +let kernel: ObjectKernel | undefined; |
| 118 | +const savedEnv: Partial<Record<(typeof ENV_KEYS)[number], string | undefined>> = {}; |
| 119 | + |
| 120 | +beforeEach(() => { |
| 121 | + for (const key of ENV_KEYS) { |
| 122 | + savedEnv[key] = process.env[key]; |
| 123 | + delete process.env[key]; |
| 124 | + } |
| 125 | +}); |
| 126 | + |
| 127 | +afterEach(async () => { |
| 128 | + try { |
| 129 | + await kernel?.shutdown(); |
| 130 | + } catch { |
| 131 | + /* a refused boot leaves the kernel stopped */ |
| 132 | + } |
| 133 | + kernel = undefined; |
| 134 | + for (const key of ENV_KEYS) { |
| 135 | + if (savedEnv[key] === undefined) delete process.env[key]; |
| 136 | + else process.env[key] = savedEnv[key]; |
| 137 | + } |
| 138 | +}); |
| 139 | + |
| 140 | +/** Boot the composition with `plugins` used in the given order; resolves to the engine or the refusal. */ |
| 141 | +async function boot(plugins: Plugin[]): Promise<{ ql?: ObjectQL; refusal?: Refusal }> { |
| 142 | + await kernel?.shutdown().catch(() => undefined); |
| 143 | + kernel = new ObjectKernel({ logger: { level: 'silent' }, gracefulShutdown: false }); |
| 144 | + await kernel.use(sqliteDriverPlugin()); |
| 145 | + await kernel.use(new ObjectQLPlugin()); |
| 146 | + for (const p of plugins) await kernel.use(p); |
| 147 | + try { |
| 148 | + await kernel.bootstrap(); |
| 149 | + } catch (e) { |
| 150 | + return { refusal: e as Refusal }; |
| 151 | + } |
| 152 | + return { ql: kernel.getService<ObjectQL>('objectql') }; |
| 153 | +} |
| 154 | + |
| 155 | +/** Every default record page of `object` in the booted registry: its name and the package that registered it. */ |
| 156 | +function defaultsOf(ql: ObjectQL, object: string): Array<{ name: string; packageId: string }> { |
| 157 | + return ql.registry |
| 158 | + .listItems<{ name: string; _packageId: string }>('page') |
| 159 | + .filter((p) => defaultRecordPageObject(p) === object) |
| 160 | + .map((p) => ({ name: p.name, packageId: p._packageId })); |
| 161 | +} |
| 162 | + |
| 163 | +const pkgPage = (packageId: string, page: string): Holder => ({ kind: 'package', packageId, page }); |
| 164 | + |
| 165 | +/** The ADR-0112 envelope of the refusal and the two pages it names. */ |
| 166 | +function expectConflict(refusal: Refusal | undefined, incoming: Holder, existing: Holder) { |
| 167 | + expect(refusal, 'the boot was refused').toBeDefined(); |
| 168 | + expect({ code: refusal!.code, status: refusal!.status }).toEqual({ code: 'DEFAULT_RECORD_PAGE_CONFLICT', status: 422 }); |
| 169 | + expect(refusal!.object).toBe('sys_organization'); |
| 170 | + expect(refusal!.incoming).toEqual(incoming); |
| 171 | + expect(refusal!.existing).toEqual(existing); |
| 172 | +} |
| 173 | + |
| 174 | +describe('registerOrganizationRecordPage: a host with its own sys_organization default record page', () => { |
| 175 | + it('pin 1: with the option, the host\'s page is the single default and the boot succeeds, in either order', async () => { |
| 176 | + for (const order of ['auth first', 'host first'] as const) { |
| 177 | + const auth = authPlugin({ registerOrganizationRecordPage: false }); |
| 178 | + const { ql, refusal } = await boot(order === 'auth first' ? [auth, hostPlugin()] : [hostPlugin(), auth]); |
| 179 | + |
| 180 | + expect(refusal, `${order}: the boot succeeds`).toBeUndefined(); |
| 181 | + expect(defaultsOf(ql!, 'sys_organization'), order).toEqual([{ name: HOST_PAGE, packageId: HOST_PACKAGE_ID }]); |
| 182 | + expect(ql!.registry.listItems<{ name: string }>('page', AUTH_PLUGIN_ID).map((p) => p.name), order).not.toContain( |
| 183 | + AUTH_PAGE, |
| 184 | + ); |
| 185 | + // `SysUserDetailPage` is unchanged by the option. |
| 186 | + expect(defaultsOf(ql!, 'sys_user'), order).toEqual([{ name: 'sys_user_detail', packageId: AUTH_PLUGIN_ID }]); |
| 187 | + } |
| 188 | + }); |
| 189 | + |
| 190 | + it('pin 2: without the option, plugin-auth\'s sys_organization_detail registers as today', async () => { |
| 191 | + const today = await boot([authPlugin()]); |
| 192 | + expect(today.refusal).toBeUndefined(); |
| 193 | + expect(defaultsOf(today.ql!, 'sys_organization')).toEqual([{ name: AUTH_PAGE, packageId: AUTH_PLUGIN_ID }]); |
| 194 | + expect(defaultsOf(today.ql!, 'sys_user')).toEqual([{ name: 'sys_user_detail', packageId: AUTH_PLUGIN_ID }]); |
| 195 | + |
| 196 | + // Control: the option, with no page of the host's own, leaves the object |
| 197 | + // with no default record page — the reading above is not vacuous. |
| 198 | + const omitted = await boot([authPlugin({ registerOrganizationRecordPage: false })]); |
| 199 | + expect(omitted.refusal).toBeUndefined(); |
| 200 | + expect(defaultsOf(omitted.ql!, 'sys_organization')).toEqual([]); |
| 201 | + expect(defaultsOf(omitted.ql!, 'sys_user')).toEqual([{ name: 'sys_user_detail', packageId: AUTH_PLUGIN_ID }]); |
| 202 | + }); |
| 203 | + |
| 204 | + it('pin 3: with both pages and no option, DEFAULT_RECORD_PAGE_CONFLICT still refuses the boot, in either order', async () => { |
| 205 | + const authFirst = await boot([authPlugin(), hostPlugin()]); |
| 206 | + expect(authFirst.ql).toBeUndefined(); |
| 207 | + expectConflict(authFirst.refusal, pkgPage(HOST_PACKAGE_ID, HOST_PAGE), pkgPage(AUTH_PLUGIN_ID, AUTH_PAGE)); |
| 208 | + |
| 209 | + const hostFirst = await boot([hostPlugin(), authPlugin()]); |
| 210 | + expect(hostFirst.ql).toBeUndefined(); |
| 211 | + expectConflict(hostFirst.refusal, pkgPage(AUTH_PLUGIN_ID, AUTH_PAGE), pkgPage(HOST_PACKAGE_ID, HOST_PAGE)); |
| 212 | + |
| 213 | + // Control: an explicit `true` is today's behaviour, refused the same way. |
| 214 | + const explicit = await boot([authPlugin({ registerOrganizationRecordPage: true }), hostPlugin()]); |
| 215 | + expect(explicit.ql).toBeUndefined(); |
| 216 | + expectConflict(explicit.refusal, pkgPage(HOST_PACKAGE_ID, HOST_PAGE), pkgPage(AUTH_PLUGIN_ID, AUTH_PAGE)); |
| 217 | + }); |
| 218 | +}); |
0 commit comments