22
33import { describe , expect , it } from 'vitest' ;
44import { customizationPackageId , isCustomizationPairOf } from '@objectstack/spec/kernel' ;
5- import { appOf , isPairOf , pairOf } from './customization-pair-lookup.js' ;
5+ import {
6+ appOf ,
7+ installedManifestsOf ,
8+ isPairOf ,
9+ pairOf ,
10+ type InstalledManifestLookup ,
11+ } from './customization-pair-lookup.js' ;
612
713/**
814 * The lookup answers exactly what the pure id rule answered for every package
915 * shape on `main` — an App, a module its artifact brings, a standalone
1016 * package, a `sys` kernel package, a manifest with no namespace — because
1117 * `appOf` is the identity until the ensure door resolves a member to its App.
1218 * Each row pins the literal pair, so a resolver that moves one package to
13- * another App reds that row by name. The rows' `type`, `scope` and `namespace`
14- * are read by nothing today: they are the App resolver's inputs when it lands.
19+ * another App reds that row by name. Every row is asked twice: with nothing
20+ * installed, and with all the rows installed — the module declaring its App
21+ * the way HotCRM's modules do — so the lookup the callers now hand in is
22+ * proven to move nothing while `appOf` stays the identity.
1523 */
16- const ROWS : ReadonlyArray < { shape : string ; manifest : { id : string ; type ?: string ; scope ?: string ; namespace ?: string } ; pair : string } > = [
24+ const ROWS : ReadonlyArray < {
25+ shape : string ;
26+ manifest : { id : string ; type ?: string ; scope ?: string ; namespace ?: string ; dependencies ?: Record < string , string > } ;
27+ pair : string ;
28+ } > = [
1729 {
1830 shape : 'an App package' ,
1931 manifest : { id : 'app.objectstack.hotcrm' , type : 'app' , namespace : 'crm' } ,
2032 pair : 'app.objectstack.hotcrm.custom' ,
2133 } ,
2234 {
2335 shape : 'a module an App artifact brings (still its own pair today)' ,
24- manifest : { id : 'app.objectstack.hotcrm.service' , type : 'module' , namespace : 'crm' } ,
36+ manifest : {
37+ id : 'app.objectstack.hotcrm.service' ,
38+ type : 'module' ,
39+ namespace : 'crm' ,
40+ dependencies : { 'app.objectstack.hotcrm' : '^6.1.0' } ,
41+ } ,
2542 pair : 'app.objectstack.hotcrm.service.custom' ,
2643 } ,
2744 {
@@ -41,11 +58,43 @@ const ROWS: ReadonlyArray<{ shape: string; manifest: { id: string; type?: string
4158 } ,
4259] ;
4360
61+ const NOTHING_INSTALLED = installedManifestsOf ( undefined ) ;
62+ const ALL_INSTALLED = installedManifestsOf ( {
63+ getPackage : ( id : string ) => {
64+ const row = ROWS . find ( ( r ) => r . manifest . id === id ) ;
65+ return row ? { manifest : row . manifest } : undefined ;
66+ } ,
67+ } ) ;
68+ const LOOKUPS : ReadonlyArray < { installed : string ; lookup : InstalledManifestLookup } > = [
69+ { installed : 'nothing installed' , lookup : NOTHING_INSTALLED } ,
70+ { installed : 'every row installed' , lookup : ALL_INSTALLED } ,
71+ ] ;
72+ const ROWS_BY_LOOKUP = LOOKUPS . flatMap ( ( { installed, lookup } ) => ROWS . map ( ( row ) => ( { ...row , installed, lookup } ) ) ) ;
73+
74+ describe ( 'installedManifestsOf — the registry read the lookup is' , ( ) => {
75+ it ( 'reads the installed manifest by id, and nothing for an id not installed' , ( ) => {
76+ const service = ROWS [ 1 ] ! . manifest ;
77+ expect ( ALL_INSTALLED ( service . id ) ) . toBe ( service ) ;
78+ expect ( ALL_INSTALLED ( 'com.example.absent' ) ) . toBeUndefined ( ) ;
79+ } ) ;
80+
81+ it . each ( [
82+ { source : 'no registry' , registry : undefined } ,
83+ { source : 'a null registry' , registry : null } ,
84+ { source : 'a registry with no getPackage' , registry : { } } ,
85+ { source : 'a package with no manifest' , registry : { getPackage : ( ) => ( { } ) } } ,
86+ { source : 'a manifest that is not an object' , registry : { getPackage : ( ) => ( { manifest : 'app' } ) } } ,
87+ { source : 'a null manifest' , registry : { getPackage : ( ) => ( { manifest : null } ) } } ,
88+ ] ) ( 'answers undefined for $source' , ( { registry } ) => {
89+ expect ( installedManifestsOf ( registry ) ( 'app.objectstack.hotcrm' ) ) . toBeUndefined ( ) ;
90+ } ) ;
91+ } ) ;
92+
4493describe ( 'pairOf — the pair of a package, looked up through its App' , ( ) => {
45- it . each ( ROWS ) ( '$shape: answers the old derivation, $pair' , ( { manifest, pair } ) => {
46- expect ( appOf ( manifest . id ) ) . toBe ( manifest . id ) ;
47- expect ( pairOf ( manifest . id ) ) . toEqual ( customizationPackageId ( manifest . id ) ) ;
48- expect ( pairOf ( manifest . id ) ) . toEqual ( { ok : true , value : pair } ) ;
94+ it . each ( ROWS_BY_LOOKUP ) ( '$shape, $installed : answers the old derivation, $pair' , ( { manifest, pair, lookup } ) => {
95+ expect ( appOf ( manifest . id , lookup ) ) . toBe ( manifest . id ) ;
96+ expect ( pairOf ( manifest . id , lookup ) ) . toEqual ( customizationPackageId ( manifest . id ) ) ;
97+ expect ( pairOf ( manifest . id , lookup ) ) . toEqual ( { ok : true , value : pair } ) ;
4998 } ) ;
5099
51100 it . each ( [
@@ -55,24 +104,26 @@ describe('pairOf — the pair of a package, looked up through its App', () => {
55104 { input : 'hotcrm.custom' , reason : 'base_is_customization_package' } ,
56105 { input : 'Com.Acme' , reason : 'base_id_invalid' } ,
57106 ] ) ( 'refuses $input as the id rule does: $reason' , ( { input, reason } ) => {
58- expect ( pairOf ( input ) ) . toEqual ( customizationPackageId ( input ) ) ;
59- expect ( pairOf ( input ) ) . toEqual ( { ok : false , reason } ) ;
107+ for ( const { lookup } of LOOKUPS ) {
108+ expect ( pairOf ( input , lookup ) ) . toEqual ( customizationPackageId ( input ) ) ;
109+ expect ( pairOf ( input , lookup ) ) . toEqual ( { ok : false , reason } ) ;
110+ }
60111 } ) ;
61112} ) ;
62113
63114describe ( 'isPairOf — is this THE pair of that package' , ( ) => {
64- it . each ( ROWS ) ( '$shape: only $pair is its pair' , ( { manifest, pair } ) => {
115+ it . each ( ROWS_BY_LOOKUP ) ( '$shape, $installed : only $pair is its pair' , ( { manifest, pair, lookup } ) => {
65116 const candidates = [ pair , manifest . id , `${ pair } .custom` , 'app.objectstack.hotcrm.custom' , 'partner.ext' , null , undefined ] ;
66117 for ( const candidate of candidates ) {
67- expect ( isPairOf ( candidate , manifest . id ) ) . toBe ( isCustomizationPairOf ( candidate , manifest . id ) ) ;
118+ expect ( isPairOf ( candidate , manifest . id , lookup ) ) . toBe ( isCustomizationPairOf ( candidate , manifest . id ) ) ;
68119 }
69- expect ( isPairOf ( pair , manifest . id ) ) . toBe ( true ) ;
70- expect ( isPairOf ( manifest . id , manifest . id ) ) . toBe ( false ) ;
120+ expect ( isPairOf ( pair , manifest . id , lookup ) ) . toBe ( true ) ;
121+ expect ( isPairOf ( manifest . id , manifest . id , lookup ) ) . toBe ( false ) ;
71122 } ) ;
72123
73- it ( ' a pair has no pair, and a missing base has none', ( ) => {
74- expect ( isPairOf ( 'hotcrm.custom.custom' , 'hotcrm.custom' ) ) . toBe ( false ) ;
75- expect ( isPairOf ( '.custom' , '' ) ) . toBe ( false ) ;
76- expect ( isPairOf ( 'x.custom' , undefined ) ) . toBe ( false ) ;
124+ it . each ( LOOKUPS ) ( '$installed: a pair has no pair, and a missing base has none', ( { lookup } ) => {
125+ expect ( isPairOf ( 'hotcrm.custom.custom' , 'hotcrm.custom' , lookup ) ) . toBe ( false ) ;
126+ expect ( isPairOf ( '.custom' , '' , lookup ) ) . toBe ( false ) ;
127+ expect ( isPairOf ( 'x.custom' , undefined , lookup ) ) . toBe ( false ) ;
77128 } ) ;
78129} ) ;
0 commit comments