|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#26596, #26698] The metadata-change subscription contract: which signals |
| 5 | + * drop the security plugin's metadata-derived caches, and which do not. |
| 6 | + * |
| 7 | + * The HTTP-level proof that a member's verdict follows a runtime change is |
| 8 | + * `packages/qa/dogfood/test/security-verdict-follows-runtime-metadata-change.dogfood.test.ts`; |
| 9 | + * this file pins the seam it rests on. |
| 10 | + */ |
| 11 | + |
| 12 | +import { describe, it, expect, vi } from 'vitest'; |
| 13 | +import { subscribeMetadataChangeInvalidation } from './metadata-change-invalidation.js'; |
| 14 | +import { SecurityPlugin } from './security-plugin.js'; |
| 15 | + |
| 16 | +type MutationEvent = { type: string; name: string; state: string }; |
| 17 | + |
| 18 | +/** A protocol double carrying the one member the module subscribes through. */ |
| 19 | +function fakeProtocol() { |
| 20 | + const listeners = new Set<(evt: MutationEvent) => void>(); |
| 21 | + return { |
| 22 | + listeners, |
| 23 | + onMetadataMutation(listener: (evt: MutationEvent) => void) { |
| 24 | + listeners.add(listener); |
| 25 | + return () => { listeners.delete(listener); }; |
| 26 | + }, |
| 27 | + emit(evt: MutationEvent) { |
| 28 | + for (const l of [...listeners]) l(evt); |
| 29 | + }, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function fakeCtx(services: Record<string, unknown>) { |
| 34 | + const hooks = new Map<string, Array<() => void>>(); |
| 35 | + return { |
| 36 | + hooks, |
| 37 | + logger: { warn: vi.fn(), info: vi.fn(), error: vi.fn(), debug: vi.fn() }, |
| 38 | + registerService: vi.fn(), |
| 39 | + getService: (name: string) => { |
| 40 | + if (!(name in services)) throw new Error(`service not registered: ${name}`); |
| 41 | + return services[name]; |
| 42 | + }, |
| 43 | + hook: (name: string, handler: () => void) => { |
| 44 | + hooks.set(name, [...(hooks.get(name) ?? []), handler]); |
| 45 | + }, |
| 46 | + fire(name: string) { |
| 47 | + for (const h of hooks.get(name) ?? []) h(); |
| 48 | + }, |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +describe('subscribeMetadataChangeInvalidation', () => { |
| 53 | + it('a landed (non-draft) metadata mutation invalidates; a draft does not', () => { |
| 54 | + const protocol = fakeProtocol(); |
| 55 | + const ctx = fakeCtx({ protocol }); |
| 56 | + const invalidate = vi.fn(); |
| 57 | + subscribeMetadataChangeInvalidation(ctx, invalidate); |
| 58 | + |
| 59 | + protocol.emit({ type: 'object', name: 'crm_account', state: 'draft' }); |
| 60 | + expect(invalidate, 'a draft never enters the registry').not.toHaveBeenCalled(); |
| 61 | + |
| 62 | + protocol.emit({ type: 'object', name: 'crm_account', state: 'active' }); |
| 63 | + protocol.emit({ type: 'permission', name: 'crm_rep', state: 'deleted' }); |
| 64 | + expect(invalidate).toHaveBeenCalledTimes(2); |
| 65 | + }); |
| 66 | + |
| 67 | + it('the `metadata:reloaded` announcement invalidates', () => { |
| 68 | + const ctx = fakeCtx({ protocol: fakeProtocol() }); |
| 69 | + const invalidate = vi.fn(); |
| 70 | + subscribeMetadataChangeInvalidation(ctx, invalidate); |
| 71 | + |
| 72 | + ctx.fire('metadata:reloaded'); |
| 73 | + expect(invalidate).toHaveBeenCalledTimes(1); |
| 74 | + }); |
| 75 | + |
| 76 | + it('returns the protocol subscription disposer, which stops delivery', () => { |
| 77 | + const protocol = fakeProtocol(); |
| 78 | + const invalidate = vi.fn(); |
| 79 | + const disposers = subscribeMetadataChangeInvalidation(fakeCtx({ protocol }), invalidate); |
| 80 | + expect(disposers).toHaveLength(1); |
| 81 | + |
| 82 | + disposers[0]!(); |
| 83 | + protocol.emit({ type: 'object', name: 'crm_account', state: 'active' }); |
| 84 | + expect(invalidate).not.toHaveBeenCalled(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('a protocol older than the seam is reported once, at `warn`; the reload hook is still wired', () => { |
| 88 | + const ctx = fakeCtx({ protocol: {} }); |
| 89 | + const invalidate = vi.fn(); |
| 90 | + const disposers = subscribeMetadataChangeInvalidation(ctx, invalidate); |
| 91 | + |
| 92 | + expect(disposers).toEqual([]); |
| 93 | + expect(ctx.logger.warn).toHaveBeenCalledTimes(1); |
| 94 | + expect(String(ctx.logger.warn.mock.calls[0]![0])).toContain('onMetadataMutation'); |
| 95 | + |
| 96 | + ctx.fire('metadata:reloaded'); |
| 97 | + expect(invalidate).toHaveBeenCalledTimes(1); |
| 98 | + }); |
| 99 | + |
| 100 | + it('a kernel with no protocol has no authoring door to hear from: nothing to report', () => { |
| 101 | + const ctx = fakeCtx({}); |
| 102 | + const disposers = subscribeMetadataChangeInvalidation(ctx, vi.fn()); |
| 103 | + expect(disposers).toEqual([]); |
| 104 | + expect(ctx.logger.warn).not.toHaveBeenCalled(); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe('SecurityPlugin — metadata-change invalidation through its own lifecycle', () => { |
| 109 | + /** |
| 110 | + * The metadata service here carries exactly the watch API `MetadataManager` |
| 111 | + * exposes (`subscribe`, `watchService`) and no `watch` member: the |
| 112 | + * invalidation must not depend on the metadata service's watchers at all, |
| 113 | + * because the runtime authoring path never announces to them. |
| 114 | + */ |
| 115 | + async function boot() { |
| 116 | + const protocol = fakeProtocol(); |
| 117 | + const metadata = { subscribe: vi.fn(() => () => {}), watchService: vi.fn(() => ({ unsubscribe() {} })) }; |
| 118 | + const ctx = fakeCtx({ |
| 119 | + protocol, |
| 120 | + metadata, |
| 121 | + manifest: { register: vi.fn() }, |
| 122 | + objectql: { registerMiddleware: vi.fn(), getSchema: () => undefined }, |
| 123 | + }); |
| 124 | + const plugin = new SecurityPlugin(); |
| 125 | + await plugin.init(ctx as any); |
| 126 | + await plugin.start(ctx as any); |
| 127 | + return { plugin: plugin as any, protocol, ctx, metadata }; |
| 128 | + } |
| 129 | + |
| 130 | + const seed = (plugin: any) => { |
| 131 | + plugin.fieldNamesCache.set('crm_account', new Set(['name'])); |
| 132 | + plugin.datetimeFieldNamesCache.set('crm_account', new Set()); |
| 133 | + plugin.numberComparandFieldsCache.set('crm_account', new Map()); |
| 134 | + plugin.tenancyDisabledCache.set('crm_account', false); |
| 135 | + plugin.cbpRelCache.set('crm_account', null); |
| 136 | + plugin.objectSecurityMetaCache.set('crm_account', { owdOpensRowWrites: false }); |
| 137 | + }; |
| 138 | + const sizes = (plugin: any) => [ |
| 139 | + plugin.fieldNamesCache.size, |
| 140 | + plugin.datetimeFieldNamesCache.size, |
| 141 | + plugin.numberComparandFieldsCache.size, |
| 142 | + plugin.tenancyDisabledCache.size, |
| 143 | + plugin.cbpRelCache.size, |
| 144 | + plugin.objectSecurityMetaCache.size, |
| 145 | + ]; |
| 146 | + |
| 147 | + it('a mutation clears every metadata-derived cache and advances the epoch', async () => { |
| 148 | + const { plugin, protocol, metadata } = await boot(); |
| 149 | + seed(plugin); |
| 150 | + const epochBefore = plugin.epoch.current; |
| 151 | + |
| 152 | + protocol.emit({ type: 'object', name: 'crm_account', state: 'active' }); |
| 153 | + |
| 154 | + expect(sizes(plugin)).toEqual([0, 0, 0, 0, 0, 0]); |
| 155 | + expect(plugin.epoch.current, 'the per-context permission-set memo is retired too').toBeGreaterThan(epochBefore); |
| 156 | + expect(metadata.subscribe, 'not the metadata service watchers').not.toHaveBeenCalled(); |
| 157 | + expect(metadata.watchService).not.toHaveBeenCalled(); |
| 158 | + }); |
| 159 | + |
| 160 | + it('`metadata:reloaded` clears them as well', async () => { |
| 161 | + const { plugin, ctx } = await boot(); |
| 162 | + seed(plugin); |
| 163 | + ctx.fire('metadata:reloaded'); |
| 164 | + expect(sizes(plugin)).toEqual([0, 0, 0, 0, 0, 0]); |
| 165 | + }); |
| 166 | + |
| 167 | + it('destroy() releases the protocol subscription', async () => { |
| 168 | + const { plugin, protocol } = await boot(); |
| 169 | + const subscribed = protocol.listeners.size; |
| 170 | + expect(subscribed).toBeGreaterThan(0); |
| 171 | + await plugin.destroy(); |
| 172 | + seed(plugin); |
| 173 | + protocol.emit({ type: 'object', name: 'crm_account', state: 'active' }); |
| 174 | + expect(sizes(plugin), 'a destroyed plugin no longer listens').toEqual([1, 1, 1, 1, 1, 1]); |
| 175 | + }); |
| 176 | +}); |
0 commit comments