|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Every member of the `security` service this plugin registers is either |
| 5 | + * DECLARED on `ISecurityService` (`@objectstack/spec/contracts`) or listed in |
| 6 | + * this file's ledger with a reason. A served member in neither turns this pin |
| 7 | + * red, by name. |
| 8 | + * |
| 9 | + * ## Why this pin exists |
| 10 | + * |
| 11 | + * The registered object is two pieces. The literal is typed against |
| 12 | + * `ISecurityService`, so the compiler refuses any member the contract does not |
| 13 | + * declare. The extension is merged on with `Object.assign` beside it, and |
| 14 | + * nothing types that half against the contract. Two cross-package seams were |
| 15 | + * served from the extension while the contract did not declare them: the |
| 16 | + * overlay discard the REST route calls, and the ownership-floor alternate seam |
| 17 | + * `service-storage` calls at boot. A contract reader could not see that either |
| 18 | + * seam existed, what it refused, or that a caller must feature-detect it. Both |
| 19 | + * are declared now; this pin keeps the next one from going undeclared. |
| 20 | + * |
| 21 | + * ## How the declared list stays equal to the interface |
| 22 | + * |
| 23 | + * `DECLARED_MEMBERS` is a test-local list, not a spec export, held to |
| 24 | + * `keyof ISecurityService` by a `satisfies` clause. A member added to the |
| 25 | + * interface and not listed here fails to compile (missing property); a name |
| 26 | + * listed here that the interface does not declare fails to compile (excess |
| 27 | + * property); and each entry's `required` / `optional` tag must match the |
| 28 | + * interface. The compile half runs in this package's `typecheck` |
| 29 | + * (`tsconfig.test.json` compiles every test here); vitest does not type-check. |
| 30 | + * A runtime list exported from `packages/spec` would have grown the published |
| 31 | + * surface to serve one test, and it could still drift from the interface |
| 32 | + * unless something like this clause held it there. |
| 33 | + * |
| 34 | + * ## What it does NOT pin |
| 35 | + * |
| 36 | + * Declared-but-not-served is legal for an OPTIONAL member: the contract lets a |
| 37 | + * partial implementation omit one, and callers feature-detect. So the reverse |
| 38 | + * direction is used only as this pin's non-vacuity control: every REQUIRED |
| 39 | + * member must be among the enumerated members, which fails if the boot stopped |
| 40 | + * reaching the registration or the enumeration stopped seeing the members. |
| 41 | + */ |
| 42 | + |
| 43 | +import { describe, it, expect, vi } from 'vitest'; |
| 44 | +import type { ISecurityService } from '@objectstack/spec/contracts'; |
| 45 | + |
| 46 | +import { SecurityPlugin } from './security-plugin.js'; |
| 47 | +import { discardPermissionSetOverlay } from './permission-set-overlay-discard.js'; |
| 48 | +import { OwnershipFloorAlternates } from './ownership-floor-alternates.js'; |
| 49 | + |
| 50 | +/** `optional` exactly when the interface declares the member with `?`. */ |
| 51 | +type DeclaredOptionality = { |
| 52 | + readonly [K in keyof ISecurityService]-?: object extends Pick<ISecurityService, K> ? 'optional' : 'required'; |
| 53 | +}; |
| 54 | + |
| 55 | +/** Every member `ISecurityService` declares — held equal to the interface by the compiler (see the header). */ |
| 56 | +const DECLARED_MEMBERS = { |
| 57 | + getReadFilter: 'required', |
| 58 | + getReadableFields: 'required', |
| 59 | + getMetadataReadableFields: 'optional', |
| 60 | + getQueryableFields: 'optional', |
| 61 | + getWritableFields: 'optional', |
| 62 | + resolvePermissionSetNames: 'required', |
| 63 | + resolvePermissionSetsForContext: 'optional', |
| 64 | + getEffectiveObjectPermissions: 'optional', |
| 65 | + canExport: 'required', |
| 66 | + canReadObject: 'optional', |
| 67 | + hasWriteBypass: 'required', |
| 68 | + resolveWriteScope: 'required', |
| 69 | + describeDelegationNarrowing: 'optional', |
| 70 | + checkAuthoredRowWrite: 'optional', |
| 71 | + explain: 'required', |
| 72 | + describeDelegableScope: 'required', |
| 73 | + listAudienceBindingSuggestions: 'required', |
| 74 | + confirmAudienceBindingSuggestion: 'required', |
| 75 | + dismissAudienceBindingSuggestion: 'required', |
| 76 | + discardPermissionSetOverlay: 'optional', |
| 77 | + contributeOwnershipFloorAlternates: 'optional', |
| 78 | +} as const satisfies DeclaredOptionality; |
| 79 | + |
| 80 | +/** |
| 81 | + * Members the registered service serves that are deliberately NOT part of the |
| 82 | + * contract — plugin-internal, no caller outside this package — each with the |
| 83 | + * reason. Empty: every served member is declared. An entry here is a decision |
| 84 | + * that a member stays off the contract; a member another package calls is a |
| 85 | + * contract, and belongs on `ISecurityService` instead. |
| 86 | + */ |
| 87 | +const SERVED_NOT_DECLARED: Readonly<Record<string, string>> = {}; |
| 88 | + |
| 89 | +/** |
| 90 | + * Every member name the object exposes, along its whole prototype chain up to |
| 91 | + * (not including) `Object.prototype`, enumerable or not, symbols included. A |
| 92 | + * class-backed service keeps its methods on the prototype, where `Object.keys` |
| 93 | + * would see nothing and this pin would pass over zero members. |
| 94 | + */ |
| 95 | +function servedMembers(service: object): string[] { |
| 96 | + const names = new Set<string>(); |
| 97 | + for (let o: object | null = service; o !== null && o !== Object.prototype; o = Object.getPrototypeOf(o)) { |
| 98 | + for (const key of Reflect.ownKeys(o)) { |
| 99 | + if (key === 'constructor') continue; |
| 100 | + names.add(typeof key === 'symbol' ? key.toString() : key); |
| 101 | + } |
| 102 | + } |
| 103 | + return [...names].sort(); |
| 104 | +} |
| 105 | + |
| 106 | +/** Boot the real plugin far enough to register `security`, and return what it registered. */ |
| 107 | +async function registeredSecurityService(): Promise<object> { |
| 108 | + const services: Record<string, unknown> = { |
| 109 | + manifest: { register: vi.fn() }, |
| 110 | + objectql: { |
| 111 | + registerMiddleware: vi.fn(), |
| 112 | + getSchema: () => undefined, |
| 113 | + }, |
| 114 | + metadata: { |
| 115 | + get: async () => undefined, |
| 116 | + list: async () => [], |
| 117 | + }, |
| 118 | + }; |
| 119 | + const registerService = vi.fn(); |
| 120 | + const ctx: Record<string, unknown> = { |
| 121 | + logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }, |
| 122 | + registerService, |
| 123 | + getService: (name: string) => { |
| 124 | + if (!(name in services)) throw new Error(`service not registered: ${name}`); |
| 125 | + return services[name]; |
| 126 | + }, |
| 127 | + }; |
| 128 | + const plugin = new SecurityPlugin(); |
| 129 | + await plugin.init(ctx as any); |
| 130 | + await plugin.start(ctx as any); |
| 131 | + const security = registerService.mock.calls.find((c: unknown[]) => c[0] === 'security')?.[1]; |
| 132 | + if (security === null || typeof security !== 'object') { |
| 133 | + throw new Error('the plugin did not register a `security` service object'); |
| 134 | + } |
| 135 | + return security; |
| 136 | +} |
| 137 | + |
| 138 | +describe('the registered `security` service serves only what ISecurityService declares', () => { |
| 139 | + it('every served member is declared on ISecurityService or ledgered here with a reason', async () => { |
| 140 | + const served = servedMembers(await registeredSecurityService()); |
| 141 | + |
| 142 | + // Non-vacuity: the required members are served, so the enumeration below |
| 143 | + // is looking at the real object. |
| 144 | + const required = Object.entries(DECLARED_MEMBERS) |
| 145 | + .filter(([, optionality]) => optionality === 'required') |
| 146 | + .map(([name]) => name); |
| 147 | + expect( |
| 148 | + required.filter((name) => !served.includes(name)), |
| 149 | + 'required ISecurityService members missing from the enumerated service', |
| 150 | + ).toEqual([]); |
| 151 | + |
| 152 | + const undeclared = served.filter( |
| 153 | + (name) => !Object.hasOwn(DECLARED_MEMBERS, name) && !Object.hasOwn(SERVED_NOT_DECLARED, name), |
| 154 | + ); |
| 155 | + expect( |
| 156 | + undeclared, |
| 157 | + 'served by the registered `security` service but neither declared on ISecurityService ' + |
| 158 | + '(packages/spec/src/contracts/security-service.ts) nor ledgered in SERVED_NOT_DECLARED with a reason', |
| 159 | + ).toEqual([]); |
| 160 | + }); |
| 161 | + |
| 162 | + it('the ledger names only members that are served and not declared', async () => { |
| 163 | + const served = servedMembers(await registeredSecurityService()); |
| 164 | + for (const [name, reason] of Object.entries(SERVED_NOT_DECLARED)) { |
| 165 | + expect(served, `ledger entry '${name}' is not served — delete it`).toContain(name); |
| 166 | + expect(Object.hasOwn(DECLARED_MEMBERS, name), `ledger entry '${name}' is declared — delete it`).toBe(false); |
| 167 | + expect(reason.trim().length, `ledger entry '${name}' carries no reason`).toBeGreaterThan(0); |
| 168 | + } |
| 169 | + }); |
| 170 | + |
| 171 | + it('the two extension members are served with the signatures the contract declares (compile-time)', () => { |
| 172 | + // The `Object.assign` half is not typed against the contract, so these two |
| 173 | + // witnesses are: each is the contract's member type, implemented by |
| 174 | + // delegating to the function the registered member delegates to. A |
| 175 | + // parameter the contract hands over that the implementation cannot take, |
| 176 | + // or a result the implementation returns that the contract does not |
| 177 | + // promise, stops this file compiling. Never invoked. |
| 178 | + const discard: NonNullable<ISecurityService['discardPermissionSetOverlay']> = (callerContext, id) => |
| 179 | + discardPermissionSetOverlay(null as never, callerContext, id); |
| 180 | + const contribute: NonNullable<ISecurityService['contributeOwnershipFloorAlternates']> = (plugin, alternates) => |
| 181 | + new OwnershipFloorAlternates().contribute(plugin, alternates); |
| 182 | + expect(typeof discard).toBe('function'); |
| 183 | + expect(typeof contribute).toBe('function'); |
| 184 | + }); |
| 185 | +}); |
0 commit comments