|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#25653] The app-automation elevation mark at its home: how it is set, read |
| 5 | + * and carried. Its two producers and its one reader are driven end to end in |
| 6 | + * runtime (`app-automation-identity-write-refusal.integration.test.ts`). |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect } from 'vitest'; |
| 10 | +import { ExecutionContextSchema } from '@objectstack/spec/kernel'; |
| 11 | + |
| 12 | +import { |
| 13 | + APP_AUTOMATION_ELEVATION, |
| 14 | + appAutomationElevationOf, |
| 15 | + carryAppAutomationElevation, |
| 16 | + markAppAutomationElevation, |
| 17 | +} from './app-automation-elevation.js'; |
| 18 | + |
| 19 | +describe('the app-automation elevation mark', () => { |
| 20 | + it('is the registry symbol, so an ESM and a CJS copy of this package agree on it', () => { |
| 21 | + expect(APP_AUTOMATION_ELEVATION).toBe(Symbol.for('objectstack.core.appAutomationElevation')); |
| 22 | + }); |
| 23 | + |
| 24 | + it('marks a fresh copy and leaves the given context unchanged', () => { |
| 25 | + const context = { isSystem: true, userId: 'u1' }; |
| 26 | + const marked = markAppAutomationElevation(context, 'hook'); |
| 27 | + expect(marked).not.toBe(context); |
| 28 | + expect(appAutomationElevationOf(marked)).toBe('hook'); |
| 29 | + expect(appAutomationElevationOf(context)).toBe(undefined); |
| 30 | + expect(marked).toMatchObject({ isSystem: true, userId: 'u1' }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('rides a spread, as every sudo()-shaped derivation spreads its caller', () => { |
| 34 | + const marked = markAppAutomationElevation({ isSystem: true }, 'action'); |
| 35 | + expect(appAutomationElevationOf({ ...marked, transaction: {} })).toBe('action'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('reads nothing from a context that carries none, a value outside the two sources, or a non-object', () => { |
| 39 | + expect(appAutomationElevationOf({ isSystem: true })).toBe(undefined); |
| 40 | + expect(appAutomationElevationOf({ [APP_AUTOMATION_ELEVATION]: 'flow' })).toBe(undefined); |
| 41 | + expect(appAutomationElevationOf(null)).toBe(undefined); |
| 42 | + expect(appAutomationElevationOf('hook')).toBe(undefined); |
| 43 | + }); |
| 44 | + |
| 45 | + it('cannot be spelled by a JSON payload', () => { |
| 46 | + const parsed = JSON.parse(JSON.stringify({ isSystem: true, 'objectstack.core.appAutomationElevation': 'hook' })); |
| 47 | + expect(appAutomationElevationOf(parsed)).toBe(undefined); |
| 48 | + }); |
| 49 | + |
| 50 | + it('is dropped by the schema parse, and carried across it', () => { |
| 51 | + const marked = markAppAutomationElevation({ isSystem: true, userId: 'u1' }, 'action'); |
| 52 | + const parsed = ExecutionContextSchema.parse(marked); |
| 53 | + expect(appAutomationElevationOf(parsed)).toBe(undefined); |
| 54 | + |
| 55 | + const carried = carryAppAutomationElevation(marked, parsed); |
| 56 | + expect(appAutomationElevationOf(carried)).toBe('action'); |
| 57 | + expect(carried).toMatchObject({ isSystem: true, userId: 'u1' }); |
| 58 | + |
| 59 | + const unmarked = ExecutionContextSchema.parse({ isSystem: true }); |
| 60 | + expect(carryAppAutomationElevation({ isSystem: true }, unmarked)).toBe(unmarked); |
| 61 | + }); |
| 62 | +}); |
0 commit comments