|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Nothing the auth family writes at start is written before the protocol-19 |
| 5 | + * boot guard has judged the database (ADR-0159 D7) — declared, not incidental. |
| 6 | + * |
| 7 | + * `AuthPlugin.start()` and `IdentityObjectsPlugin.start()` write identity rows |
| 8 | + * (the Default Organization under `single`, ADR-0131 D3). The guard |
| 9 | + * (`@objectstack/runtime`'s `FormTypeSwitchBootGuardPlugin`) judges in its own |
| 10 | + * `start()`, and a database it refuses must be left as it found it, so both |
| 11 | + * plugins name it in `optionalDependencies`, beside the v18 guard |
| 12 | + * (`default-organization-guard-ordering.pin.test.ts`). This file proves the |
| 13 | + * declaration is what moves the order: a hostile registry (the writer used |
| 14 | + * BEFORE the guard) still starts the guard first, and the same registry with |
| 15 | + * the declaration removed does not. The real guard on a real kernel is |
| 16 | + * `runtime/src/form-type-switch-boot-guard.kernel.test.ts`. |
| 17 | + * |
| 18 | + * The guard is a NAME-ONLY stub carrying the real guard's own order-if-present |
| 19 | + * edges: this package does not depend on the runtime, and |
| 20 | + * `resolvePluginOrder` reads only the `OrderablePlugin` surface. |
| 21 | + */ |
| 22 | + |
| 23 | +import { describe, expect, it } from 'vitest'; |
| 24 | +import { resolvePluginOrder } from '@objectstack/core'; |
| 25 | +import type { OrderablePlugin } from '@objectstack/core'; |
| 26 | +import { AuthPlugin } from './auth-plugin.js'; |
| 27 | +import { IdentityObjectsPlugin } from './identity-objects-plugin.js'; |
| 28 | + |
| 29 | +// Pay the first transform of the dist-resolved dependency `auth-plugin`'s |
| 30 | +// reach loads through a dynamic `import()` at MODULE LOAD, never inside a |
| 31 | +// case's clocked window (`scripts/check-test-source-alias.mjs`). |
| 32 | +import '@objectstack/core/egress'; |
| 33 | + |
| 34 | +const GUARD_PLUGIN = 'com.objectstack.form-type-switch-boot-guard'; |
| 35 | +const OWNERSHIP_GUARD_PLUGIN = 'com.objectstack.organization-ownership-boot-guard'; |
| 36 | +const ENGINE_PLUGIN = 'com.objectstack.engine.objectql'; |
| 37 | + |
| 38 | +const writers: Array<[string, () => OrderablePlugin]> = [ |
| 39 | + ['AuthPlugin', () => new AuthPlugin({ secret: 'test-secret-at-least-32-chars-long!!' }) as unknown as OrderablePlugin], |
| 40 | + ['IdentityObjectsPlugin', () => new IdentityObjectsPlugin() as unknown as OrderablePlugin], |
| 41 | +]; |
| 42 | + |
| 43 | +/** The guard as the resolver sees it: its name and its own order-if-present edges. */ |
| 44 | +const guardStub = (): OrderablePlugin => ({ |
| 45 | + name: GUARD_PLUGIN, |
| 46 | + optionalDependencies: [ENGINE_PLUGIN, 'com.objectstack.platform-objects', OWNERSHIP_GUARD_PLUGIN], |
| 47 | +}); |
| 48 | + |
| 49 | +const orderOf = (...plugins: OrderablePlugin[]): string[] => |
| 50 | + resolvePluginOrder(new Map(plugins.map((p) => [p.name, p]))).map((p) => p.name); |
| 51 | + |
| 52 | +describe.each(writers)('%s writes its identity rows only after the protocol-19 boot guard has judged', (_label, make) => { |
| 53 | + it('declares the guard as an OPTIONAL dependency', () => { |
| 54 | + const writer = make(); |
| 55 | + expect(writer.optionalDependencies ?? []).toContain(GUARD_PLUGIN); |
| 56 | + expect(writer.dependencies ?? []).not.toContain(GUARD_PLUGIN); |
| 57 | + }); |
| 58 | + |
| 59 | + it('⭐ the declaration moves the order: used BEFORE the guard, it still starts after it', () => { |
| 60 | + const writer = make(); |
| 61 | + const ordered = orderOf({ name: ENGINE_PLUGIN }, writer, guardStub()); |
| 62 | + expect(ordered.indexOf(GUARD_PLUGIN)).toBeLessThan(ordered.indexOf(writer.name)); |
| 63 | + }); |
| 64 | + |
| 65 | + it('…and the declaration is what does it: forget it and the writer starts first', () => { |
| 66 | + const writer = make(); |
| 67 | + writer.optionalDependencies = (writer.optionalDependencies ?? []).filter((d) => d !== GUARD_PLUGIN); |
| 68 | + const ordered = orderOf({ name: ENGINE_PLUGIN }, writer, guardStub()); |
| 69 | + expect(ordered.indexOf(writer.name)).toBeLessThan(ordered.indexOf(GUARD_PLUGIN)); |
| 70 | + }); |
| 71 | + |
| 72 | + it('beside the v18 guard: used before both, it starts after both, the older guard judging first', () => { |
| 73 | + const writer = make(); |
| 74 | + const ordered = orderOf( |
| 75 | + { name: ENGINE_PLUGIN }, |
| 76 | + writer, |
| 77 | + guardStub(), |
| 78 | + { name: OWNERSHIP_GUARD_PLUGIN, optionalDependencies: [ENGINE_PLUGIN] }, |
| 79 | + ); |
| 80 | + expect(ordered.indexOf(OWNERSHIP_GUARD_PLUGIN)).toBeLessThan(ordered.indexOf(GUARD_PLUGIN)); |
| 81 | + expect(ordered.indexOf(GUARD_PLUGIN)).toBeLessThan(ordered.indexOf(writer.name)); |
| 82 | + }); |
| 83 | + |
| 84 | + it('order-if-present: a kernel that composes no guard resolves as before', () => { |
| 85 | + const writer = make(); |
| 86 | + expect(orderOf({ name: ENGINE_PLUGIN }, writer)).toContain(writer.name); |
| 87 | + }); |
| 88 | +}); |
0 commit comments