@@ -16,26 +16,39 @@ import { resolveConfig } from '../src/config.js';
16
16
/**
17
17
* @param {string } path
18
18
* @param {Record<string, Config> } configMap
19
+ * @param {string } [baseUrl='https://www.example.com/org/site/content']
19
20
* @returns {Context }
20
21
*/
21
- const TEST_CONTEXT = ( path , configMap ) => ( {
22
+ const TEST_CONTEXT = ( path , configMap , baseUrl = 'https://www.example.com/org/site/content' ) => ( {
22
23
env : {
23
24
CONFIGS : {
24
25
get : async ( tenant ) => configMap [ tenant ] ,
25
26
} ,
26
27
} ,
27
28
log : console ,
28
- url : new URL ( `https://www.example.com/org--repo/content ${ path } ` ) ,
29
+ url : new URL ( `${ baseUrl } ${ path } ` ) ,
29
30
info : {
30
31
method : 'GET' ,
31
32
headers : { } ,
32
33
} ,
33
34
} ) ;
34
35
36
+ const defaultTenantConfigs = {
37
+ 'org--site' : {
38
+ base : {
39
+ apiKey : 'bad' ,
40
+ } ,
41
+ '/us/p/{{urlkey}}/{{sku}}' : {
42
+ pageType : 'product' ,
43
+ apiKey : 'good' ,
44
+ } ,
45
+ } ,
46
+ } ;
47
+
35
48
describe ( 'config tests' , ( ) => {
36
49
it ( 'should extract path params' , async ( ) => {
37
50
const tenantConfigs = {
38
- 'org--repo ' : {
51
+ 'org--site ' : {
39
52
base : {
40
53
apiKey : 'bad' ,
41
54
} ,
@@ -45,23 +58,21 @@ describe('config tests', () => {
45
58
} ,
46
59
} ,
47
60
} ;
48
- const config = await resolveConfig (
49
- TEST_CONTEXT ( '/us/p/my-url-key/some-sku' , tenantConfigs ) ,
50
- 'org--repo' ,
51
- ) ;
61
+ const config = await resolveConfig ( TEST_CONTEXT ( '/us/p/my-url-key/some-sku' , tenantConfigs ) ) ;
52
62
assert . deepStrictEqual ( config , {
53
63
apiKey : 'good' ,
54
64
params : { urlkey : 'my-url-key' , sku : 'some-sku' } ,
55
65
headers : { } ,
56
66
pageType : 'product' ,
57
67
org : 'org' ,
58
- repo : 'repo' ,
68
+ site : 'site' ,
69
+ route : 'content' ,
59
70
} ) ;
60
71
} ) ;
61
72
62
73
it ( 'should combine headers objects' , async ( ) => {
63
74
const tenantConfigs = {
64
- 'org--repo ' : {
75
+ 'org--site ' : {
65
76
base : {
66
77
apiKey : 'bad' ,
67
78
headers : {
@@ -79,23 +90,21 @@ describe('config tests', () => {
79
90
} ,
80
91
} ,
81
92
} ;
82
- const config = await resolveConfig (
83
- TEST_CONTEXT ( '/us/p/my-url-key/some-sku' , tenantConfigs ) ,
84
- 'org--repo' ,
85
- ) ;
93
+ const config = await resolveConfig ( TEST_CONTEXT ( '/us/p/my-url-key/some-sku' , tenantConfigs ) ) ;
86
94
assert . deepStrictEqual ( config , {
87
95
apiKey : 'good' ,
88
96
params : { urlkey : 'my-url-key' , sku : 'some-sku' } ,
89
97
headers : { foo : '2' , baz : '1' , bar : '2' } ,
90
98
pageType : 'product' ,
91
99
org : 'org' ,
92
- repo : 'repo' ,
100
+ site : 'site' ,
101
+ route : 'content' ,
93
102
} ) ;
94
103
} ) ;
95
104
96
105
it ( 'should allow wildcard path segments' , async ( ) => {
97
106
const tenantConfigs = {
98
- 'org--repo ' : {
107
+ 'org--site ' : {
99
108
base : {
100
109
apiKey : 'bad' ,
101
110
} ,
@@ -105,23 +114,21 @@ describe('config tests', () => {
105
114
} ,
106
115
} ,
107
116
} ;
108
- const config = await resolveConfig (
109
- TEST_CONTEXT ( '/us/p/something-here/some-sku' , tenantConfigs ) ,
110
- 'org--repo' ,
111
- ) ;
117
+ const config = await resolveConfig ( TEST_CONTEXT ( '/us/p/something-here/some-sku' , tenantConfigs ) ) ;
112
118
assert . deepStrictEqual ( config , {
113
119
apiKey : 'good' ,
114
120
params : { sku : 'some-sku' } ,
115
121
headers : { } ,
116
122
pageType : 'product' ,
117
123
org : 'org' ,
118
- repo : 'repo' ,
124
+ site : 'site' ,
125
+ route : 'content' ,
119
126
} ) ;
120
127
} ) ;
121
128
122
129
it ( 'should allow overrides' , async ( ) => {
123
130
const tenantConfigs = {
124
- 'org--repo ' : {
131
+ 'org--site ' : {
125
132
base : {
126
133
apiKey : 'bad1' ,
127
134
} ,
@@ -133,7 +140,6 @@ describe('config tests', () => {
133
140
} ;
134
141
const config = await resolveConfig (
135
142
TEST_CONTEXT ( '/us/p/some-sku' , tenantConfigs ) ,
136
- 'org--repo' ,
137
143
{ apiKey : 'good' } ,
138
144
) ;
139
145
assert . deepStrictEqual ( config , {
@@ -142,7 +148,55 @@ describe('config tests', () => {
142
148
pageType : 'product' ,
143
149
headers : { } ,
144
150
org : 'org' ,
145
- repo : 'repo' ,
151
+ site : 'site' ,
152
+ route : 'content' ,
146
153
} ) ;
147
154
} ) ;
155
+
156
+ it ( 'should throw if org is missing' , async ( ) => {
157
+ await assert . rejects (
158
+ resolveConfig ( TEST_CONTEXT ( '' , defaultTenantConfigs , 'http://www.example.com' ) ) ,
159
+ new Error ( 'missing org' ) ,
160
+ ) ;
161
+ } ) ;
162
+
163
+ it ( 'should throw if site is missing' , async ( ) => {
164
+ await assert . rejects (
165
+ resolveConfig ( TEST_CONTEXT ( '' , defaultTenantConfigs , 'http://www.example.com/org' ) ) ,
166
+ new Error ( 'missing site' ) ,
167
+ ) ;
168
+ } ) ;
169
+
170
+ it ( 'should throw if route is missing' , async ( ) => {
171
+ await assert . rejects (
172
+ resolveConfig ( TEST_CONTEXT ( '' , defaultTenantConfigs , 'http://www.example.com/org/site' ) ) ,
173
+ new Error ( 'missing route' ) ,
174
+ ) ;
175
+ } ) ;
176
+
177
+ it ( 'should return null for invalid config' , async ( ) => {
178
+ const config = await resolveConfig ( TEST_CONTEXT ( '/us/p/some-sku' , { } ) ) ;
179
+ assert . deepStrictEqual ( config , null ) ;
180
+ } ) ;
181
+
182
+ it ( 'should return null if config is not an object' , async ( ) => {
183
+ const ctx = TEST_CONTEXT ( '/us/p/some-sku' , { } ) ;
184
+ ctx . env . CONFIGS . get = async ( ) => 'not an object' ;
185
+ const config = await resolveConfig ( ctx ) ;
186
+ assert . deepStrictEqual ( config , null ) ;
187
+ } ) ;
188
+
189
+ it ( 'should log a warning if pageType is missing' , async ( ) => {
190
+ const tenantConfigs = {
191
+ 'org--site' : {
192
+ base : {
193
+ apiKey : 'good' ,
194
+ } ,
195
+ '/us/p/{{sku}}' : { } ,
196
+ } ,
197
+ } ;
198
+ const ctx = TEST_CONTEXT ( '/us/p/some-sku' , tenantConfigs ) ;
199
+ const config = await resolveConfig ( ctx ) ;
200
+ assert . deepStrictEqual ( config , null ) ;
201
+ } ) ;
148
202
} ) ;
0 commit comments