@@ -147,14 +147,46 @@ function makeDispatcher(seed: Record<string, unknown>[] = [packagedExemplar()])
147147 flows . set ( name , definition as Record < string , unknown > ) ;
148148 } ) ,
149149 } ;
150- const services : Record < string , unknown > = { automation : spies } ;
150+ const services : Record < string , unknown > = { automation : spies , ... packageLanding ( ) } ;
151151 const resolve = ( name : string ) => services [ name ] ;
152152 const kernel : any = {
153153 getService : resolve ,
154154 getServiceAsync : async ( name : string ) => resolve ( name ) ,
155155 context : { getService : resolve } ,
156156 } ;
157- return { dispatcher : new HttpDispatcher ( kernel ) , spies, flows } ;
157+ return { dispatcher : new HttpDispatcher ( kernel ) , spies, flows, protocol : services . protocol as PackageLandingProtocol } ;
158+ }
159+
160+ /** The base the exemplar's loader-set owner names, and its paired customization package. */
161+ const BASE_PACKAGE = 'crm' ;
162+ const PAIR_PACKAGE = 'crm.custom' ;
163+
164+ type PackageLandingProtocol = ReturnType < typeof packageLanding > [ 'protocol' ] ;
165+
166+ /**
167+ * [#28149] Where the copy lands. The door now writes every copy into a
168+ * package (ADR-0146 D4), so the rig carries the protocol members it asks —
169+ * the loader's set naming the exemplar's base, the ensure door answering its
170+ * pair, and the save — plus the `objectql` registry the target rule reads.
171+ * The real protocol and registry are exercised in
172+ * `automation-flow-clone-custom-package.test.ts`; here they are the minimum
173+ * that lets each case reach the behaviour it is named after.
174+ */
175+ function packageLanding ( ) {
176+ const manifests : Record < string , { id : string ; dependencies ?: Record < string , string > } > = {
177+ [ BASE_PACKAGE ] : { id : BASE_PACKAGE } ,
178+ [ PAIR_PACKAGE ] : { id : PAIR_PACKAGE , dependencies : { [ BASE_PACKAGE ] : '^3' } } ,
179+ } ;
180+ const protocol = {
181+ packagedArtifactOwner : vi . fn ( ( ) => BASE_PACKAGE ) ,
182+ ensureCustomizationPackage : vi . fn ( async ( { basePackageId } : { basePackageId : string } ) => ( {
183+ basePackageId, packageId : `${ basePackageId } .custom` , namespace : 'crm_custom' , dependencyRange : '^3' , created : false , writable : true ,
184+ } ) ) ,
185+ saveMetaItem : vi . fn ( async ( ) => ( { success : true } ) ) ,
186+ tenantAuthoredWriteRefusal : vi . fn ( async ( ) => undefined ) ,
187+ } ;
188+ const objectql = { registry : { getPackage : ( id : string ) => ( manifests [ id ] ? { manifest : manifests [ id ] } : undefined ) } } ;
189+ return { protocol, objectql } ;
158190}
159191
160192/** Drop `keys` from a shallow copy — used to state "everything except …". */
@@ -204,9 +236,10 @@ describe('#12156 — whole-definition copy (the #11703 counter-example as a test
204236
205237 expect ( clone . name ) . toBe ( NEW . name ) ;
206238 expect ( clone . label ) . toBe ( NEW . label ) ;
207- // `draft `: the schema's own default for something never deployed. It is
208- // NOT an off-switch — see `flow-clone.ts` and the notice assertion below .
239+ // [#28149, ADR-0146 D4] `obsolete `: the copy is written switched off,
240+ // so it never runs beside its original — see `flow-clone.ts`.
209241 expect ( clone . status ) . toBe ( FLOW_CLONE_STATUS ) ;
242+ expect ( FLOW_CLONE_STATUS ) . toBe ( 'obsolete' ) ;
210243 expect ( clone . status ) . not . toBe ( source . status ) ;
211244
212245 // The set that differs is exactly the mutated set.
@@ -216,14 +249,20 @@ describe('#12156 — whole-definition copy (the #11703 counter-example as a test
216249
217250 it ( 'registers the clone under the NEW name, with the definition it returned' , async ( ) => {
218251 const source = packagedExemplar ( ) ;
219- const { dispatcher, spies, flows } = makeDispatcher ( [ source ] ) ;
252+ const { dispatcher, spies, flows, protocol } = makeDispatcher ( [ source ] ) ;
220253
221254 const result = await dispatcher . handleAutomation ( clonePath ( source . name as string ) , 'POST' , NEW , CTX ) ;
222255 const clone = ( result . response ?. body ?. data ?? result . response ?. body ) ?. flow ;
223256
224257 expect ( spies . registerFlow ) . toHaveBeenCalledTimes ( 1 ) ;
225258 expect ( spies . registerFlow ) . toHaveBeenCalledWith ( NEW . name , clone ) ;
226259 expect ( flows . get ( NEW . name ) ) . toEqual ( clone ) ;
260+ // [#28149] …and saved into the source's paired customization package,
261+ // never package-less, as a first write.
262+ expect ( protocol . saveMetaItem ) . toHaveBeenCalledTimes ( 1 ) ;
263+ expect ( protocol . saveMetaItem ) . toHaveBeenCalledWith ( expect . objectContaining ( {
264+ type : 'flow' , name : NEW . name , packageId : PAIR_PACKAGE , parentVersion : null ,
265+ } ) ) ;
227266 // …and the source is still there, untouched. A clone is not a rename.
228267 expect ( flows . get ( source . name as string ) ) . toEqual ( packagedExemplar ( ) ) ;
229268 } ) ;
@@ -442,24 +481,23 @@ describe('#12156 — the source, the notice, and the gate', () => {
442481 expect ( spies . registerFlow ) . not . toHaveBeenCalled ( ) ;
443482 } ) ;
444483
445- it ( 'states in one line that the clone runs alongside the original , and that references are NOT re-pointed' , async ( ) => {
484+ it ( 'states in one line that the copy is switched off , and that references are NOT re-pointed' , async ( ) => {
446485 const source = packagedExemplar ( ) ;
447486 const { dispatcher } = makeDispatcher ( [ source ] ) ;
448487
449488 const result = await dispatcher . handleAutomation ( clonePath ( source . name as string ) , 'POST' , NEW , CTX ) ;
450489 const notice : string = ( ( result . response ?. body ?. data ?? result . response ?. body ) as any ) ?. notice ?? '' ;
451490
452- // The fact an admin is most likely to be caught by: `draft` is a
453- // lifecycle label, not an off-switch — the engine only disables on
454- // `obsolete`/`invalid`, so a cloned record-change flow runs beside the
455- // one it was copied from. Saying so is cheaper than the surprise.
456- expect ( notice ) . toContain ( 'runs alongside the original until you switch one of them off' ) ;
491+ // [#28149, ADR-0146 D4] "The copy is written inactive … and the
492+ // response says so": the copy does not run until someone switches it
493+ // on, stated in the server's words so no console restates it.
494+ expect ( notice ) . toContain ( 'switched off and does not run until you switch it on' ) ;
457495 // ADR-0126 §9: automatic re-pointing is explicitly not chartered, and
458496 // the ADR requires the surface to TELL the admin so.
459497 expect ( notice ) . toContain ( 'still uses the same subflows, actions and objects as the original' ) ;
460- // [#27886] One line in business words. The console shows it beside a
461- // "Switch the original off" button, so the engine words and the switch
462- // locations stay in `flow-clone.ts`'s TSDoc.
498+ // [#27886] One line in business words. The console shows it verbatim,
499+ // so the engine words and the switch locations stay in
500+ // `flow-clone.ts`'s TSDoc.
463501 expect ( notice . match ( / [ . ! ? ] ( \s | $ ) / g) ) . toHaveLength ( 1 ) ;
464502 expect ( notice ) . not . toMatch ( / \b ( d r a f t | s t a t u s | o f f - s w i t c h | l i f e c y c l e | S t u d i o ) \b / i) ;
465503 } ) ;
0 commit comments