@@ -30,7 +30,17 @@ jest.unstable_mockModule('../lib/config.js', () => ({
3030 loadConfig : jest . fn ( )
3131} ) ) ;
3232
33- const { handleError, readInput, validateAdobeCommerce, validatePaaSOrOnPrem, printTable, buildSearchCriteria, buildSortCriteria, applyLocalSearchCriteria, getFormatHeaders } = await import ( '../lib/utils.js' ) ;
33+ const {
34+ handleError,
35+ readInput,
36+ validateAdobeCommerce,
37+ validatePaaSOrOnPrem,
38+ printTable,
39+ buildSearchCriteria,
40+ buildSortCriteria,
41+ applyLocalSearchCriteria,
42+ getFormatHeaders
43+ } = await import ( '../lib/utils.js' ) ;
3444const fs = ( await import ( 'fs' ) ) . default ;
3545const os = ( await import ( 'os' ) ) . default ;
3646const configMod = await import ( '../lib/config.js' ) ;
@@ -632,3 +642,92 @@ describe('buildSortCriteria', () => {
632642 } ) ;
633643 } ) ;
634644} ) ;
645+
646+ describe ( 'applyLocalSearchCriteria' , ( ) => {
647+ const data = [
648+ { id : 1 , name : 'Product A' , price : 10 , status : 'enabled' , category_ids : '1,2' } ,
649+ { id : 2 , name : 'Product B' , price : 20 , status : 'disabled' , category_ids : '2,3' } ,
650+ { id : 3 , name : 'Service C' , price : 15 , status : 'enabled' , category_ids : '1' } ,
651+ ] ;
652+
653+ it ( 'should filter by equality' , ( ) => {
654+ const result = applyLocalSearchCriteria ( data , { filter : [ 'status=enabled' ] } ) ;
655+ expect ( result ) . toHaveLength ( 2 ) ;
656+ expect ( result [ 0 ] . id ) . toBe ( 1 ) ;
657+ expect ( result [ 1 ] . id ) . toBe ( 3 ) ;
658+ } ) ;
659+
660+ it ( 'should filter by shorthand operators' , ( ) => {
661+ const result = applyLocalSearchCriteria ( data , { filter : [ 'price>10' ] } ) ;
662+ expect ( result ) . toHaveLength ( 2 ) ;
663+ expect ( result [ 0 ] . id ) . toBe ( 2 ) ;
664+ expect ( result [ 1 ] . id ) . toBe ( 3 ) ;
665+ } ) ;
666+
667+ it ( 'should filter by like with wildcards' , ( ) => {
668+ const result = applyLocalSearchCriteria ( data , { filter : [ 'name~*Product*' ] } ) ;
669+ expect ( result ) . toHaveLength ( 2 ) ;
670+ expect ( result [ 0 ] . id ) . toBe ( 1 ) ;
671+ expect ( result [ 1 ] . id ) . toBe ( 2 ) ;
672+ } ) ;
673+
674+ it ( 'should filter with OR logic using ||' , ( ) => {
675+ const result = applyLocalSearchCriteria ( data , { filter : [ 'id=1 || id=2' ] } ) ;
676+ expect ( result ) . toHaveLength ( 2 ) ;
677+ expect ( result . map ( i => i . id ) ) . toContain ( 1 ) ;
678+ expect ( result . map ( i => i . id ) ) . toContain ( 2 ) ;
679+ } ) ;
680+
681+ it ( 'should sort data ASC' , ( ) => {
682+ const result = applyLocalSearchCriteria ( data , { sort : [ 'price:ASC' ] } ) ;
683+ expect ( result [ 0 ] . id ) . toBe ( 1 ) ;
684+ expect ( result [ 1 ] . id ) . toBe ( 3 ) ;
685+ expect ( result [ 2 ] . id ) . toBe ( 2 ) ;
686+ } ) ;
687+
688+ it ( 'should sort data DESC' , ( ) => {
689+ const result = applyLocalSearchCriteria ( data , { sort : [ 'price:DESC' ] } ) ;
690+ expect ( result [ 0 ] . id ) . toBe ( 2 ) ;
691+ expect ( result [ 1 ] . id ) . toBe ( 3 ) ;
692+ expect ( result [ 2 ] . id ) . toBe ( 1 ) ;
693+ } ) ;
694+
695+ it ( 'should paginate data' , ( ) => {
696+ const result = applyLocalSearchCriteria ( data , { page : 2 , size : 1 } ) ;
697+ expect ( result ) . toHaveLength ( 1 ) ;
698+ expect ( result [ 0 ] . id ) . toBe ( 2 ) ;
699+ } ) ;
700+
701+ it ( 'should sort data using sortBy and sortOrder' , ( ) => {
702+ const result = applyLocalSearchCriteria ( data , { sortBy : 'price' , sortOrder : 'DESC' } ) ;
703+ expect ( result [ 0 ] . id ) . toBe ( 2 ) ;
704+ expect ( result [ 1 ] . id ) . toBe ( 3 ) ;
705+ expect ( result [ 2 ] . id ) . toBe ( 1 ) ;
706+ } ) ;
707+
708+ it ( 'should handle in/nin operators' , ( ) => {
709+ const resIn = applyLocalSearchCriteria ( data , { filter : [ 'id:in=1,3' ] } ) ;
710+ expect ( resIn ) . toHaveLength ( 2 ) ;
711+ expect ( resIn . map ( i => i . id ) ) . toEqual ( [ 1 , 3 ] ) ;
712+
713+ const resNin = applyLocalSearchCriteria ( data , { filter : [ 'id:nin=1,3' ] } ) ;
714+ expect ( resNin ) . toHaveLength ( 1 ) ;
715+ expect ( resNin [ 0 ] . id ) . toBe ( 2 ) ;
716+ } ) ;
717+
718+ it ( 'should handle null/notnull operators' , ( ) => {
719+ const dataWithNull = [ ...data , { id : 4 , name : null } ] ;
720+ const resNull = applyLocalSearchCriteria ( dataWithNull , { filter : [ 'name?' ] } ) ;
721+ expect ( resNull ) . toHaveLength ( 1 ) ;
722+ expect ( resNull [ 0 ] . id ) . toBe ( 4 ) ;
723+
724+ const resNotNull = applyLocalSearchCriteria ( dataWithNull , { filter : [ 'name!' ] } ) ;
725+ expect ( resNotNull ) . toHaveLength ( 3 ) ;
726+ } ) ;
727+
728+ it ( 'should handle finset operator' , ( ) => {
729+ const result = applyLocalSearchCriteria ( data , { filter : [ 'category_ids@@3' ] } ) ;
730+ expect ( result ) . toHaveLength ( 1 ) ;
731+ expect ( result [ 0 ] . id ) . toBe ( 2 ) ;
732+ } ) ;
733+ } ) ;
0 commit comments