2424 * controls, and a FRESH boot of the same database then serves the moved
2525 * content under the base's name on the package-less read, the read naming the
2626 * base, the layered read and the base's list; a second `--apply` moves 0.
27+ *
28+ * [#29392] A second template holds a row bound to the package itself (a save
29+ * naming it, before that write was closed): listed and moved the same way.
2730 */
2831
2932import { describe , it , expect , beforeAll , afterAll , beforeEach , afterEach , vi } from 'vitest' ;
@@ -78,7 +81,7 @@ const viewBody = (name: string, columns: readonly string[]) => ({
7881 config : { type : 'grid' , data : { provider : 'object' , object : 'm28413_task' } , columns : columns . map ( ( field ) => ( { field } ) ) } ,
7982} ) ;
8083
81- interface SeedRow { type : string ; name : string ; state : string ; body : Record < string , unknown > }
84+ interface SeedRow { type : string ; name : string ; state : string ; body : Record < string , unknown > ; packageId ?: string }
8285const SEED : SeedRow [ ] = [
8386 { type : 'view' , name : VIEW , state : 'active' , body : viewBody ( VIEW , STORED_COLUMNS ) } ,
8487 { type : 'view' , name : VIEW , state : 'draft' , body : viewBody ( VIEW , DRAFT_COLUMNS ) } ,
@@ -89,22 +92,52 @@ const SEED: SeedRow[] = [
8992 { type : 'permission' , name : 'm28413_rep' , state : 'draft' , body : { name : 'm28413_rep' , label : 'Saved in the environment' , objects : { } } } ,
9093] ;
9194
95+ /**
96+ * [#29392] A database whose customization a save naming the package itself
97+ * stored before that write was closed: an active row of the package's view
98+ * bound to the package, beside the package-less environment row (control).
99+ */
100+ const BOUND_SEED : SeedRow [ ] = [
101+ { type : 'view' , name : VIEW , state : 'active' , body : viewBody ( VIEW , STORED_COLUMNS ) , packageId : PKG } ,
102+ { type : 'view' , name : ENV_VIEW , state : 'active' , body : viewBody ( ENV_VIEW , STORED_COLUMNS ) } ,
103+ ] ;
104+
92105let dir : string ;
93106let templateDb : string ;
107+ let boundTemplateDb : string ;
94108const savedCwd = process . cwd ( ) ;
95109const ENV_KEYS = [ 'OS_ARTIFACT_PATH' , 'OS_DATABASE_URL' , 'DATABASE_URL' , 'OS_DATABASE_DRIVER' , 'OS_HOME' , 'NODE_ENV' ,
96110 'OS_AUTH_SECRET' , 'AUTH_SECRET' , 'BETTER_AUTH_SECRET' , 'OS_ENVIRONMENT_ID' ] as const ;
97111const savedEnv : Record < string , string | undefined > = { } ;
98112
99113let caseSeq = 0 ;
100- function caseDb ( ) : string {
114+ function caseDb ( template = templateDb ) : string {
101115 caseSeq += 1 ;
102116 const file = join ( dir , 'cases' , `${ caseSeq } .db` ) ;
103117 mkdirSync ( dirname ( file ) , { recursive : true } ) ;
104- copyFileSync ( templateDb , file ) ;
118+ copyFileSync ( template , file ) ;
105119 return file ;
106120}
107121
122+ /** The rows an older release left, written at the driver the way that release stored them. */
123+ async function insertRows ( dbFile : string , rows : readonly SeedRow [ ] ) : Promise < void > {
124+ const driver = new SqlDriver ( { client : 'better-sqlite3' , connection : { filename : dbFile } , useNullAsDefault : true } ) ;
125+ try {
126+ const k = ( driver as any ) . knex ;
127+ const now = new Date ( ) . toISOString ( ) ;
128+ let n = 0 ;
129+ for ( const row of rows ) {
130+ n += 1 ;
131+ await k ( 'sys_metadata' ) . insert ( {
132+ id : `seed_${ n } ` , type : row . type , name : row . name , package_id : row . packageId ?? null , state : row . state ,
133+ version : 1 , checksum : null , created_at : now , updated_at : now , metadata : JSON . stringify ( row . body ) ,
134+ } ) ;
135+ }
136+ } finally {
137+ await driver . disconnect ( ) ;
138+ }
139+ }
140+
108141/** Every `sys_metadata` row, `type/name@binding/state`, read on a connection of our own. */
109142async function storedRows ( dbFile : string ) : Promise < string [ ] > {
110143 const driver = new SqlDriver ( { client : 'better-sqlite3' , connection : { filename : dbFile } , useNullAsDefault : true } ) ;
@@ -215,21 +248,10 @@ beforeAll(async () => {
215248 extraPlugins : [ new PackageServicePlugin ( ) ] ,
216249 } ) ;
217250 await seed . shutdown ( ) ;
218- const driver = new SqlDriver ( { client : 'better-sqlite3' , connection : { filename : templateDb } , useNullAsDefault : true } ) ;
219- try {
220- const k = ( driver as any ) . knex ;
221- const now = new Date ( ) . toISOString ( ) ;
222- let n = 0 ;
223- for ( const row of SEED ) {
224- n += 1 ;
225- await k ( 'sys_metadata' ) . insert ( {
226- id : `seed_${ n } ` , type : row . type , name : row . name , package_id : null , state : row . state ,
227- version : 1 , checksum : null , created_at : now , updated_at : now , metadata : JSON . stringify ( row . body ) ,
228- } ) ;
229- }
230- } finally {
231- await driver . disconnect ( ) ;
232- }
251+ boundTemplateDb = join ( dir , 'bound-template.db' ) ;
252+ copyFileSync ( templateDb , boundTemplateDb ) ;
253+ await insertRows ( templateDb , SEED ) ;
254+ await insertRows ( boundTemplateDb , BOUND_SEED ) ;
233255} , CASE_TIMEOUT_MS ) ;
234256
235257afterAll ( ( ) => {
@@ -311,3 +333,54 @@ describe('os migrate packageless-overlays', () => {
311333 expect ( await storedRows ( db ) ) . toEqual ( before ) ;
312334 } , CASE_TIMEOUT_MS ) ;
313335} ) ;
336+
337+ /**
338+ * [#29392] A row bound to the read-only package itself, stored by a save
339+ * naming it before that write was closed: the census lists it beside the
340+ * package-less rows (`boundToBase`), and `--apply` moves it into the pair with
341+ * the declaration, the same outcome. Before, the census read package-less
342+ * rows only: the preview listed nothing and the row's one exit was a delete.
343+ */
344+ describe ( 'os migrate packageless-overlays: a row bound to the read-only package' , ( ) => {
345+ it ( 'the preview lists it with its target pair and writes nothing' , async ( ) => {
346+ const db = caseDb ( boundTemplateDb ) ;
347+ const before = await storedRows ( db ) ;
348+ const { payload, exitCode } = await runJson ( [ '--database-url' , `file:${ db } ` , '--json' ] ) ;
349+ expect ( payload . error , JSON . stringify ( payload ) . slice ( 0 , 600 ) ) . toBeUndefined ( ) ;
350+ expect ( exitCode ) . toBe ( 0 ) ;
351+ expect ( payload . rows . map ( ( r : any ) => `${ r . type } /${ r . name } /${ r . state } ${ r . disposition } ${ r . base } → ${ r . pair } ${ r . boundToBase } ${ r . outcome } ` ) ) . toEqual ( [
352+ `view/${ VIEW } /active movable ${ PKG } → ${ PAIR } true listed` ,
353+ ] ) ;
354+ expect ( payload . movable ) . toBe ( 1 ) ;
355+ expect ( payload . environmentRows ) . toBe ( 1 ) ;
356+ expect ( await storedRows ( db ) ) . toEqual ( before ) ;
357+ expect ( await storedPackages ( db ) ) . not . toContain ( PAIR ) ;
358+ } , CASE_TIMEOUT_MS ) ;
359+
360+ it ( '--apply moves it into the pair with its `overrides` declaration, keeps the control, serves the same customization over the package\'s document, and a re-run moves 0' , async ( ) => {
361+ const db = caseDb ( boundTemplateDb ) ;
362+ const served = await servedColumns ( db ) ;
363+ expect ( served [ 1 ] ) . toEqual ( STORED_COLUMNS ) ;
364+
365+ const { payload, exitCode } = await runJson ( [ '--apply' , '--yes' , '--database-url' , `file:${ db } ` , '--json' ] ) ;
366+ expect ( payload . error , JSON . stringify ( payload ) . slice ( 0 , 600 ) ) . toBeUndefined ( ) ;
367+ expect ( exitCode ) . toBe ( 0 ) ;
368+ expect ( payload . bases ) . toEqual ( [ { base : PKG , pair : PAIR , pairCreated : true , moved : 1 , declarationsCompleted : 0 , failed : 0 , shadowed : 0 } ] ) ;
369+ expect ( payload . rows . map ( ( r : any ) => `${ r . state } ${ r . outcome } ${ r . packageId } ${ r . basedOn } ${ r . boundToBase } ` ) ) . toEqual ( [
370+ `active moved ${ PAIR } 1.4.0 true` ,
371+ ] ) ;
372+ expect ( await storedRows ( db ) ) . toEqual ( [ `view/${ ENV_VIEW } @-/active` , `view/${ VIEW } @${ PAIR } /active` ] ) ;
373+ expect ( ( await storedBody ( db , { type : 'view' , name : VIEW , package_id : PAIR , state : 'active' } ) ) . overrides )
374+ . toEqual ( { package : PKG , basedOn : '1.4.0' } ) ;
375+ expect ( await storedPackages ( db ) ) . toContain ( PAIR ) ;
376+
377+ // A fresh boot serves the customization at every read of the package's item.
378+ expect ( await servedColumns ( db ) ) . toEqual ( [ STORED_COLUMNS , STORED_COLUMNS , STORED_COLUMNS , STORED_COLUMNS ] ) ;
379+
380+ const again = await runJson ( [ '--apply' , '--yes' , '--database-url' , `file:${ db } ` , '--json' ] ) ;
381+ expect ( again . payload . error , JSON . stringify ( again . payload ) . slice ( 0 , 600 ) ) . toBeUndefined ( ) ;
382+ expect ( again . exitCode ) . toBe ( 0 ) ;
383+ expect ( again . payload . moved ) . toBe ( 0 ) ;
384+ expect ( again . payload . rows ) . toEqual ( [ ] ) ;
385+ } , CASE_TIMEOUT_MS * 3 ) ;
386+ } ) ;
0 commit comments