|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #25514 — a `type: 'flow'` action's target resolves against the ARTIFACT once |
| 5 | + * the stack declares its siblings' flows (`DefineStackOptions.artifactFlows`), |
| 6 | + * and a flow NO package of the artifact defines stays refused. |
| 7 | + * |
| 8 | + * ## The defect |
| 9 | + * |
| 10 | + * `validateCrossReferences` built its flow scope from the stack's own `flows` |
| 11 | + * alone, so a sales action on an app-owned object launching a flow that a |
| 12 | + * module package of the SAME artifact ships was refused at `defineStack` — |
| 13 | + * measured downstream on HotCRM's module split as three "which is not defined |
| 14 | + * in flows." refusals, while the composed artifact carried all three flows. |
| 15 | + * |
| 16 | + * ## The two directions this file pins, per position |
| 17 | + * |
| 18 | + * - a sibling-provided flow is ACCEPTED at `defineStack` and at `composeStacks`; |
| 19 | + * - a flow no package provides is REFUSED — at `defineStack` when it is not |
| 20 | + * claimed, and at `composeStacks` (the artifact pass) when it is claimed but |
| 21 | + * no input defines it, with the per-stack pass's own message. |
| 22 | + * |
| 23 | + * Plus the fences: the option never switches the check on for a stack with no |
| 24 | + * flows of its own, and an input that did not opt in cannot newly fail at |
| 25 | + * composition even when it bypassed the strict parse. |
| 26 | + */ |
| 27 | +import { describe, it, expect } from 'vitest'; |
| 28 | +import { composeStacks, defineStack } from './stack.zod'; |
| 29 | + |
| 30 | +const appManifest = { |
| 31 | + id: 'app.objectstack.hotcrm', |
| 32 | + name: 'HotCRM', |
| 33 | + version: '3.1.0', |
| 34 | + type: 'app' as const, |
| 35 | + namespace: 'crm', |
| 36 | +}; |
| 37 | + |
| 38 | +const moduleManifest = { |
| 39 | + id: 'app.objectstack.hotcrm.revenue', |
| 40 | + name: 'HotCRM Revenue', |
| 41 | + version: '3.1.0', |
| 42 | + type: 'module' as const, |
| 43 | + namespace: 'crm', |
| 44 | + dependencies: { 'app.objectstack.hotcrm': '^3.1.0' }, |
| 45 | +}; |
| 46 | + |
| 47 | +const flow = (name: string) => ({ name, label: name, type: 'autolaunched' as const, nodes: [], edges: [] }); |
| 48 | + |
| 49 | +/** Owned by the module package — the flow every cross-package action launches. */ |
| 50 | +const SIBLING = 'quote_generation'; |
| 51 | +/** The flow NO package in the artifact defines. */ |
| 52 | +const NOWHERE = 'quote_nowhere'; |
| 53 | + |
| 54 | +const account = { name: 'crm_account', label: 'Account', fields: { title: { type: 'text' as const } } }; |
| 55 | +const quote = { name: 'crm_quote', label: 'Quote', fields: { title: { type: 'text' as const } } }; |
| 56 | + |
| 57 | +type Envelope = Error & { code?: string; status?: number; issues?: readonly string[] }; |
| 58 | + |
| 59 | +function refusalOf(run: () => unknown): Envelope | null { |
| 60 | + try { |
| 61 | + run(); |
| 62 | + return null; |
| 63 | + } catch (e) { |
| 64 | + return e as Envelope; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +const anyStack = (config: Record<string, unknown>) => config as unknown as Parameters<typeof defineStack>[0]; |
| 69 | + |
| 70 | +/** The module package: owns `crm_quote` and the flow. */ |
| 71 | +const revenueStack = () => |
| 72 | + defineStack(anyStack({ manifest: moduleManifest, objects: [quote], flows: [flow(SIBLING)] })); |
| 73 | + |
| 74 | +/** |
| 75 | + * The app package: owns `crm_account` and a flow of its own (so the per-stack |
| 76 | + * flow check runs at all), and launches `target` from all three positions. |
| 77 | + */ |
| 78 | +const appConfig = (target: string, ownFlows = [flow('crm_local_flow')]) => anyStack({ |
| 79 | + manifest: appManifest, |
| 80 | + objects: [ |
| 81 | + { |
| 82 | + ...account, |
| 83 | + actions: [{ name: 'quote_from_account', label: 'Quote', type: 'flow', target }], |
| 84 | + }, |
| 85 | + ], |
| 86 | + flows: ownFlows, |
| 87 | + actions: [{ name: 'generate_quote', label: 'Generate Quote', type: 'flow', target }], |
| 88 | + pages: [ |
| 89 | + { |
| 90 | + name: 'crm_home', |
| 91 | + label: 'Home', |
| 92 | + type: 'home', |
| 93 | + regions: [ |
| 94 | + { |
| 95 | + name: 'main', |
| 96 | + components: [ |
| 97 | + { |
| 98 | + type: 'element:button', |
| 99 | + properties: { label: 'Go', action: { name: 'inline_quote', type: 'flow', target } }, |
| 100 | + }, |
| 101 | + ], |
| 102 | + }, |
| 103 | + ], |
| 104 | + }, |
| 105 | + ], |
| 106 | +}); |
| 107 | + |
| 108 | +const messages = (target: string) => [ |
| 109 | + `Action 'generate_quote' references flow '${target}' which is not defined in flows.`, |
| 110 | + `Action 'quote_from_account' on object 'crm_account' references flow '${target}' which is not defined in flows.`, |
| 111 | + `Inline action 'inline_quote' on page 'crm_home' (regions.0.components.0) references flow '${target}' which is not defined in flows.`, |
| 112 | +]; |
| 113 | + |
| 114 | +describe('the per-stack pass, without the opt-in — unchanged', () => { |
| 115 | + it('REFUSES the sibling-provided flow from all three positions — the reported defect', () => { |
| 116 | + const refused = refusalOf(() => defineStack(appConfig(SIBLING))); |
| 117 | + expect(refused?.code).toBe('STACK_CROSS_REFERENCE_INVALID'); |
| 118 | + expect(refused?.status).toBe(422); |
| 119 | + expect(refused?.issues).toEqual(messages(SIBLING)); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +describe('`artifactFlows` resolves a sibling package’s flow', () => { |
| 124 | + it('ACCEPTS all three positions at `defineStack`', () => { |
| 125 | + const revenue = revenueStack(); |
| 126 | + expect( |
| 127 | + refusalOf(() => defineStack(appConfig(SIBLING), { artifactFlows: (revenue.flows ?? []).map((f) => f.name) })), |
| 128 | + ).toBeNull(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('ACCEPTS the composed artifact — the claim is redeemed by the sibling', () => { |
| 132 | + const app = defineStack(appConfig(SIBLING), { artifactFlows: [SIBLING] }); |
| 133 | + expect(refusalOf(() => composeStacks([revenueStack(), app], { manifest: 'preserve' }))).toBeNull(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('keeps refusing a name it does NOT list, beside one it does', () => { |
| 137 | + const refused = refusalOf(() => defineStack(appConfig(NOWHERE), { artifactFlows: [SIBLING] })); |
| 138 | + expect(refused?.code).toBe('STACK_CROSS_REFERENCE_INVALID'); |
| 139 | + expect(refused?.issues).toEqual(messages(NOWHERE)); |
| 140 | + }); |
| 141 | +}); |
| 142 | + |
| 143 | +describe('the ARTIFACT pass: a claimed flow NO package defines is refused', () => { |
| 144 | + const composeClaiming = (name: string) => { |
| 145 | + const app = defineStack(appConfig(name), { artifactFlows: [name] }); |
| 146 | + return refusalOf(() => composeStacks([revenueStack(), app], { manifest: 'preserve' })); |
| 147 | + }; |
| 148 | + |
| 149 | + it('REFUSES every position with the per-stack pass’s own message and envelope', () => { |
| 150 | + const refused = composeClaiming(NOWHERE); |
| 151 | + expect(refused?.code).toBe('STACK_CROSS_REFERENCE_INVALID'); |
| 152 | + expect(refused?.status).toBe(422); |
| 153 | + expect(refused?.issues).toEqual(messages(NOWHERE)); |
| 154 | + expect(refused?.message).toContain('composeStacks artifact cross-reference validation failed'); |
| 155 | + }); |
| 156 | + |
| 157 | + it('resolves against a nested multi-package input, whose flows live only in its package bodies', () => { |
| 158 | + const inner = composeStacks( |
| 159 | + [revenueStack(), defineStack(anyStack({ manifest: { ...moduleManifest, id: 'x.other' }, objects: [] }))], |
| 160 | + { manifest: 'preserve' }, |
| 161 | + ); |
| 162 | + const app = defineStack(appConfig(SIBLING), { artifactFlows: [SIBLING] }); |
| 163 | + expect(refusalOf(() => composeStacks([inner, app], { manifest: 'preserve' }))).toBeNull(); |
| 164 | + }); |
| 165 | +}); |
| 166 | + |
| 167 | +describe('fences', () => { |
| 168 | + it('a stack with NO flows of its own keeps the plugin leniency — the option switches nothing on', () => { |
| 169 | + const app = defineStack(appConfig(NOWHERE, []), { artifactFlows: [SIBLING] }); |
| 170 | + // No claim was relied on, so the composition has nothing to redeem. |
| 171 | + expect(refusalOf(() => composeStacks([revenueStack(), app], { manifest: 'preserve' }))).toBeNull(); |
| 172 | + }); |
| 173 | + |
| 174 | + it('an input that did not opt in cannot newly fail at composition, even past `strict: false`', () => { |
| 175 | + const loose = defineStack(appConfig(NOWHERE), { strict: false }); |
| 176 | + expect(refusalOf(() => composeStacks([revenueStack(), loose], { manifest: 'preserve' }))).toBeNull(); |
| 177 | + }); |
| 178 | + |
| 179 | + it('the claim record is not data — the built stack carries no new enumerable key', () => { |
| 180 | + const app = defineStack(appConfig(SIBLING), { artifactFlows: [SIBLING] }); |
| 181 | + expect(Object.keys(app)).toEqual(Object.keys(defineStack(appConfig('crm_local_flow')))); |
| 182 | + expect(JSON.stringify(app)).not.toContain('artifact-flow-claims'); |
| 183 | + }); |
| 184 | +}); |
0 commit comments