11import { parseResourceConfigFlags } from './resource-config.js'
2+ import { ValidationError , ErrorCodes } from '../services/store/errors/errors.js'
23import { describe , expect , test } from 'vitest'
34
45describe ( 'parseResourceConfigFlags' , ( ) => {
@@ -32,30 +33,15 @@ describe('parseResourceConfigFlags', () => {
3233 } )
3334 } )
3435
35- test ( 'parses multiple different resources' , ( ) => {
36- const result = parseResourceConfigFlags ( [ 'products:handle' , 'customers:email' ] )
37- expect ( result ) . toEqual ( {
38- products : {
39- identifier : {
40- field : 'HANDLE' ,
41- customId : undefined ,
42- } ,
43- } ,
44- customers : {
45- identifier : {
46- field : 'EMAIL' ,
47- customId : undefined ,
48- } ,
49- } ,
50- } )
51- } )
52-
5336 test ( 'overwrites identifier when same resource appears multiple times' , ( ) => {
54- const result = parseResourceConfigFlags ( [ 'products:handle' , 'products:title ' ] )
37+ const result = parseResourceConfigFlags ( [ 'products:handle' , 'products:metafield:custom:salesforce_id ' ] )
5538 expect ( result ) . toEqual ( {
5639 products : {
5740 identifier : {
58- field : 'TITLE' ,
41+ customId : {
42+ namespace : 'custom' ,
43+ key : 'salesforce_id' ,
44+ } ,
5945 } ,
6046 } ,
6147 } )
@@ -76,34 +62,62 @@ describe('parseResourceConfigFlags', () => {
7662 } ,
7763 } )
7864 } )
65+ } )
66+
67+ describe ( 'when validating key formats and fields' , ( ) => {
68+ test ( 'throws INVALID_KEY_FORMAT for malformed keys' , ( ) => {
69+ expect ( ( ) => parseResourceConfigFlags ( [ 'invalid' ] ) ) . toThrow ( ValidationError )
70+ expect ( ( ) => parseResourceConfigFlags ( [ 'invalid' ] ) ) . toThrow (
71+ expect . objectContaining ( {
72+ code : ErrorCodes . INVALID_KEY_FORMAT ,
73+ } ) ,
74+ )
7975
80- test ( 'throws error for non-product unique metafield' , ( ) => {
81- expect ( ( ) => parseResourceConfigFlags ( [ 'customers:metafield:custom:id' ] ) ) . toThrow (
82- "Invalid resource: customers don't support unique metafields as identifiers." ,
76+ expect ( ( ) => parseResourceConfigFlags ( [ 'product:yes:no' ] ) ) . toThrow ( ValidationError )
77+ expect ( ( ) => parseResourceConfigFlags ( [ 'product:yes:no' ] ) ) . toThrow (
78+ expect . objectContaining ( {
79+ code : ErrorCodes . INVALID_KEY_FORMAT ,
80+ } ) ,
81+ )
82+
83+ expect ( ( ) => parseResourceConfigFlags ( [ 'product/yes' ] ) ) . toThrow ( ValidationError )
84+ expect ( ( ) => parseResourceConfigFlags ( [ 'product/yes' ] ) ) . toThrow (
85+ expect . objectContaining ( {
86+ code : ErrorCodes . INVALID_KEY_FORMAT ,
87+ } ) ,
8388 )
8489 } )
85- } )
8690
87- describe ( 'when parsing mixed field and metafield configs' , ( ) => {
88- test ( 'returns mixed set of identifier inputs' , ( ) => {
89- const result = parseResourceConfigFlags ( [ 'products:metafield:custom:salesforce_id' , 'customers:email' ] )
90- expect ( result ) . toEqual ( {
91- products : {
92- identifier : {
93- field : undefined ,
94- customId : {
95- namespace : 'custom' ,
96- key : 'salesforce_id' ,
97- } ,
98- } ,
99- } ,
100- customers : {
101- identifier : {
102- field : 'EMAIL' ,
103- customId : undefined ,
104- } ,
105- } ,
106- } )
91+ test ( 'throws KEY_NOT_SUPPORTED for unknown resources' , ( ) => {
92+ expect ( ( ) => parseResourceConfigFlags ( [ 'unknown:field' ] ) ) . toThrow ( ValidationError )
93+ expect ( ( ) => parseResourceConfigFlags ( [ 'unknown:field' ] ) ) . toThrow (
94+ expect . objectContaining ( {
95+ code : ErrorCodes . KEY_NOT_SUPPORTED ,
96+ } ) ,
97+ )
98+ } )
99+
100+ test ( 'throws KEY_DOES_NOT_EXIST for invalid fields' , ( ) => {
101+ expect ( ( ) => parseResourceConfigFlags ( [ 'products:title' ] ) ) . toThrow ( ValidationError )
102+ expect ( ( ) => parseResourceConfigFlags ( [ 'products:title' ] ) ) . toThrow (
103+ expect . objectContaining ( {
104+ code : ErrorCodes . KEY_DOES_NOT_EXIST ,
105+ } ) ,
106+ )
107+ } )
108+
109+ test ( 'throws KEY_NOT_SUPPORTED for product typos' , ( ) => {
110+ expect ( ( ) => parseResourceConfigFlags ( [ 'product:handle' ] ) ) . toThrow ( ValidationError )
111+ expect ( ( ) => parseResourceConfigFlags ( [ 'product:handle' ] ) ) . toThrow (
112+ expect . objectContaining ( {
113+ code : ErrorCodes . KEY_NOT_SUPPORTED ,
114+ } ) ,
115+ )
116+ } )
117+
118+ test ( 'accepts valid resource:field combinations' , ( ) => {
119+ expect ( ( ) => parseResourceConfigFlags ( [ 'products:handle' ] ) ) . not . toThrow ( )
120+ expect ( ( ) => parseResourceConfigFlags ( [ 'products:metafield:custom:salesforce_id' ] ) ) . not . toThrow ( )
107121 } )
108122 } )
109123} )
0 commit comments