@@ -648,3 +648,84 @@ describe('the commit doors: restoring a row bound to the base is closed, removin
648648 expect ( rows ( VIEW . type , VIEW . name ) ) . toEqual ( [ { package_id : WRITABLE , state : 'active' , label : 'First' } ] ) ;
649649 } ) ;
650650} ) ;
651+
652+ /**
653+ * [#29848, ADR-0146 D2, D8 item 2, D11] The per-item rollback
654+ * (`POST /api/v1/meta/:type/:name/rollback`) beside a row bound to the base.
655+ * The door names no package: it restores into the row the by-name read naming
656+ * no package SERVES (the pair's declared override over a row bound to the
657+ * base, `findServedOverlayRow`), never whichever active row of the name the
658+ * store returns first. Before, the binding read and the restore's active-row
659+ * read were both package-agnostic, so with the base row stored first the
660+ * rollback of the pair's override addressed the base row and was refused by
661+ * the named-base close, naming a row the caller never meant.
662+ */
663+ describe ( 'the per-item rollback addresses the row the read serves: the pair\'s declared override beside a row bound to the base (#29848)' , ( ) => {
664+ const declared = ( label : string ) => ( { ...VIEW . body ( label ) , overrides : { package : BASE , basedOn : BASE_VERSION } } ) ;
665+
666+ /** The history version whose body carries `label`. */
667+ async function versionOf ( protocol : any , label : string ) : Promise < number > {
668+ const history = ( await protocol . engine . find ( 'sys_metadata_history' , { where : { type : VIEW . type , name : VIEW . name } } ) ) as Row [ ] ;
669+ const hit = history . filter ( ( h ) => h . metadata != null && JSON . parse ( h . metadata ) . label === label ) ;
670+ expect ( hit , `one history row labelled ${ label } ` ) . toHaveLength ( 1 ) ;
671+ return hit [ 0 ] ! . version as number ;
672+ }
673+
674+ async function savePair ( protocol : any , label : string ) {
675+ const saved = await protocol . saveMetaItem ( { type : VIEW . type , name : VIEW . name , packageId : PAIR , item : declared ( label ) } ) ;
676+ expect ( saved , JSON . stringify ( saved ) ) . toMatchObject ( { success : true } ) ;
677+ }
678+
679+ it ( 'the measured two-row state (the base row stored first): the pair\'s row rolls back, the base row is untouched' , async ( ) => {
680+ const { protocol, rows, storeBaseRow } = makeProtocol ( ) ;
681+ await storeBaseRow ( VIEW . type , VIEW . name , VIEW . body ( 'BaseRow' ) ) ;
682+ await savePair ( protocol , 'PairFirst' ) ;
683+ await savePair ( protocol , 'PairSecond' ) ;
684+ expect ( ( await protocol . getMetaItem ( { type : VIEW . type , name : VIEW . name } ) ) . item . label ) . toBe ( 'PairSecond' ) ;
685+
686+ const receipt = await protocol . rollbackMetaItem ( {
687+ type : VIEW . type , name : VIEW . name , toVersion : await versionOf ( protocol , 'PairFirst' ) , actor : 'admin' ,
688+ } ) ;
689+
690+ expect ( receipt ) . toMatchObject ( { success : true } ) ;
691+ expect ( rows ( VIEW . type , VIEW . name ) ) . toEqual ( [
692+ { package_id : BASE , state : 'active' , label : 'BaseRow' } ,
693+ { package_id : PAIR , state : 'active' , label : 'PairFirst' } ,
694+ ] ) ;
695+ const pairRow = await protocol . engine . findOne ( 'sys_metadata' , { where : { type : VIEW . type , name : VIEW . name , state : 'active' , package_id : PAIR } } ) ;
696+ expect ( JSON . parse ( pairRow . metadata ) . overrides ) . toEqual ( { package : BASE , basedOn : BASE_VERSION } ) ;
697+ expect ( ( await protocol . getMetaItem ( { type : VIEW . type , name : VIEW . name } ) ) . item . label ) . toBe ( 'PairFirst' ) ;
698+ expect ( ( await protocol . getMetaItem ( { type : VIEW . type , name : VIEW . name , packageId : BASE } ) ) . item . label ) . toBe ( 'PairFirst' ) ;
699+ } ) ;
700+
701+ it ( 'the pair\'s rollback to the base row\'s version is the override write gate\'s refusal, not the named-base close; nothing is written' , async ( ) => {
702+ const { protocol, rows, storeBaseRow } = makeProtocol ( ) ;
703+ await storeBaseRow ( VIEW . type , VIEW . name , VIEW . body ( 'BaseRow' ) ) ;
704+ await savePair ( protocol , 'PairFirst' ) ;
705+
706+ const toVersion = await versionOf ( protocol , 'BaseRow' ) ;
707+ const refused = await thrown ( ( ) => protocol . rollbackMetaItem ( { type : VIEW . type , name : VIEW . name , toVersion, actor : 'admin' } ) ) ;
708+
709+ expect ( resolveThrownHttpError ( refused ) ) . toMatchObject ( { status : 403 , code : 'NOT_OVERRIDABLE' } ) ;
710+ expect ( String ( refused . message ) ) . not . toContain ( 'a rollback of its row bound to the read-only base is closed' ) ;
711+ expect ( rows ( VIEW . type , VIEW . name ) ) . toEqual ( [
712+ { package_id : BASE , state : 'active' , label : 'BaseRow' } ,
713+ { package_id : PAIR , state : 'active' , label : 'PairFirst' } ,
714+ ] ) ;
715+ } ) ;
716+
717+ it ( 'once the pair\'s row is deleted, the read serves the base row again and its rollback is refused by the named-base close' , async ( ) => {
718+ const { protocol, rows, storeBaseRow } = makeProtocol ( ) ;
719+ await storeBaseRow ( VIEW . type , VIEW . name , VIEW . body ( 'BaseFirst' ) ) ;
720+ await storeBaseRow ( VIEW . type , VIEW . name , VIEW . body ( 'BaseSecond' ) ) ;
721+ await savePair ( protocol , 'PairFirst' ) ;
722+ expect ( await protocol . deleteMetaItem ( { type : VIEW . type , name : VIEW . name , packageId : PAIR } ) ) . toMatchObject ( { success : true } ) ;
723+ expect ( ( await protocol . getMetaItem ( { type : VIEW . type , name : VIEW . name } ) ) . item . label ) . toBe ( 'BaseSecond' ) ;
724+
725+ const toVersion = await versionOf ( protocol , 'BaseFirst' ) ;
726+ const refused = await thrown ( ( ) => protocol . rollbackMetaItem ( { type : VIEW . type , name : VIEW . name , toVersion, actor : 'admin' } ) ) ;
727+
728+ expectClosed ( refused , VIEW . type , VIEW . name , 'rollback' ) ;
729+ expect ( rows ( VIEW . type , VIEW . name ) ) . toEqual ( [ { package_id : BASE , state : 'active' , label : 'BaseSecond' } ] ) ;
730+ } ) ;
731+ } ) ;
0 commit comments