-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathconstants.test.ts
More file actions
672 lines (590 loc) · 24 KB
/
constants.test.ts
File metadata and controls
672 lines (590 loc) · 24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
import {
NODE_VTEX_API_VERSION,
DEFAULT_WORKSPACE,
IS_IO,
PID,
HeaderKeys,
AttributeKeys,
BODY_HASH,
UP_SIGNAL,
MAX_AGE,
HTTP_SERVER_PORT,
MAX_WORKERS,
LINKED,
REGION,
PUBLIC_ENDPOINT,
APP,
NODE_ENV,
ACCOUNT,
WORKSPACE,
PRODUCTION,
INSPECT_DEBUGGER_PORT,
cancellableMethods,
LOG_CLIENT_INIT_TIMEOUT_MS,
// Backward-compatible individual header constants
CACHE_CONTROL_HEADER,
SEGMENT_HEADER,
SESSION_HEADER,
PRODUCT_HEADER,
LOCALE_HEADER,
FORWARDED_HOST_HEADER,
TENANT_HEADER,
BINDING_HEADER,
META_HEADER,
META_HEADER_BUCKET,
ETAG_HEADER,
ACCOUNT_HEADER,
CREDENTIAL_HEADER,
REQUEST_ID_HEADER,
ROUTER_CACHE_HEADER,
OPERATION_ID_HEADER,
PLATFORM_HEADER,
WORKSPACE_IS_PRODUCTION_HEADER,
WORKSPACE_HEADER,
EVENT_KEY_HEADER,
EVENT_SENDER_HEADER,
EVENT_SUBJECT_HEADER,
EVENT_HANDLER_ID_HEADER,
COLOSSUS_ROUTE_DECLARER_HEADER,
COLOSSUS_ROUTE_ID_HEADER,
COLOSSUS_PARAMS_HEADER,
TRACE_ID_HEADER,
PROVIDER_HEADER
} from './constants'
describe('constants', () => {
describe('Basic constants', () => {
test('NODE_VTEX_API_VERSION should match package.json version', () => {
const pkg = require('../package.json')
expect(NODE_VTEX_API_VERSION).toBe(pkg.version)
expect(typeof NODE_VTEX_API_VERSION).toBe('string')
expect(NODE_VTEX_API_VERSION.length).toBeGreaterThan(0)
})
test('DEFAULT_WORKSPACE should be a non-empty string', () => {
expect(typeof DEFAULT_WORKSPACE).toBe('string')
expect(DEFAULT_WORKSPACE.length).toBeGreaterThan(0)
})
test('IS_IO should reflect VTEX_IO environment variable', () => {
expect(IS_IO).toBe(process.env.VTEX_IO)
})
test('PID should be the current process ID', () => {
expect(PID).toBe(process.pid)
expect(typeof PID).toBe('number')
expect(PID).toBeGreaterThan(0)
})
})
describe('HeaderKeys', () => {
test('should be an object with string properties', () => {
expect(typeof HeaderKeys).toBe('object')
expect(HeaderKeys).not.toBeNull()
})
test('all header keys should be uppercase with underscores', () => {
Object.keys(HeaderKeys).forEach(key => {
expect(key).toMatch(/^[A-Z_]+$/)
})
})
test('VTEX headers should follow x-vtex- or x- naming pattern', () => {
const vtexSpecificHeaders = Object.entries(HeaderKeys).filter(([key]) =>
key.includes('VTEX') ||
key === 'ACCOUNT' ||
key === 'WORKSPACE' ||
key === 'OPERATION_ID' ||
key === 'SEGMENT'
)
vtexSpecificHeaders.forEach(([, value]) => {
expect(value).toMatch(/^x-/)
})
})
test('should not contain empty or invalid header values', () => {
Object.values(HeaderKeys).forEach(value => {
expect(value).not.toContain(' ')
expect(value).not.toMatch(/[A-Z]/)
expect(value).not.toContain('\n')
expect(value).not.toContain('\r')
})
})
})
describe('AttributeKeys', () => {
test('should be an object with string properties', () => {
expect(typeof AttributeKeys).toBe('object')
expect(AttributeKeys).not.toBeNull()
})
test('should contain VTEX semantic attributes', () => {
expect(AttributeKeys).toHaveProperty('VTEX_ACCOUNT_NAME')
expect(typeof AttributeKeys.VTEX_ACCOUNT_NAME).toBe('string')
})
test('should contain VTEX IO semantic attributes', () => {
expect(AttributeKeys).toHaveProperty('VTEX_IO_WORKSPACE_NAME')
expect(AttributeKeys).toHaveProperty('VTEX_IO_WORKSPACE_TYPE')
expect(AttributeKeys).toHaveProperty('VTEX_IO_APP_ID')
expect(AttributeKeys).toHaveProperty('VTEX_IO_APP_AUTHOR_TYPE')
expect(typeof AttributeKeys.VTEX_IO_WORKSPACE_NAME).toBe('string')
expect(typeof AttributeKeys.VTEX_IO_WORKSPACE_TYPE).toBe('string')
expect(typeof AttributeKeys.VTEX_IO_APP_ID).toBe('string')
expect(typeof AttributeKeys.VTEX_IO_APP_AUTHOR_TYPE).toBe('string')
})
test('should have non-empty string values', () => {
Object.values(AttributeKeys).forEach((value: any) => {
expect(typeof value).toBe('string')
expect(value.length).toBeGreaterThan(0)
})
})
test('attribute names should follow naming convention', () => {
Object.keys(AttributeKeys).forEach(key => {
expect(key).toMatch(/^VTEX(_IO)?_[A-Z_]+$/)
})
})
test('should import from external module without errors', () => {
// Test that the AttributeKeys structure exists and is properly imported
expect(AttributeKeys).toBeDefined()
Object.values(AttributeKeys).forEach(value => {
expect(value).toBeTruthy()
})
})
})
describe('Cache constants', () => {
test('MAX_AGE should be an object with numeric properties', () => {
expect(typeof MAX_AGE).toBe('object')
expect(MAX_AGE).not.toBeNull()
})
test('MAX_AGE should contain LONG, MEDIUM, SHORT properties', () => {
expect(MAX_AGE).toHaveProperty('LONG')
expect(MAX_AGE).toHaveProperty('MEDIUM')
expect(MAX_AGE).toHaveProperty('SHORT')
expect(typeof MAX_AGE.LONG).toBe('number')
expect(typeof MAX_AGE.MEDIUM).toBe('number')
expect(typeof MAX_AGE.SHORT).toBe('number')
})
test('MAX_AGE values should be positive integers', () => {
Object.values(MAX_AGE).forEach(value => {
expect(value).toBeGreaterThan(0)
expect(Number.isInteger(value)).toBe(true)
})
})
test('MAX_AGE values should be in logical order', () => {
expect(MAX_AGE.LONG).toBeGreaterThan(MAX_AGE.MEDIUM)
expect(MAX_AGE.MEDIUM).toBeGreaterThan(MAX_AGE.SHORT)
})
})
describe('Server configuration', () => {
test('server ports should have specific expected values', () => {
expect(HTTP_SERVER_PORT).toBe(5050)
expect(INSPECT_DEBUGGER_PORT).toBe(5858)
// Ensure they are numbers
expect(typeof HTTP_SERVER_PORT).toBe('number')
expect(typeof INSPECT_DEBUGGER_PORT).toBe('number')
})
test('MAX_WORKERS should be a positive integer', () => {
expect(typeof MAX_WORKERS).toBe('number')
expect(MAX_WORKERS).toBeGreaterThan(0)
expect(Number.isInteger(MAX_WORKERS)).toBe(true)
})
test('LOG_CLIENT_INIT_TIMEOUT_MS should be a positive number', () => {
expect(typeof LOG_CLIENT_INIT_TIMEOUT_MS).toBe('number')
expect(LOG_CLIENT_INIT_TIMEOUT_MS).toBeGreaterThan(0)
})
})
describe('Environment-based constants', () => {
test('boolean environment constants should have correct types', () => {
expect(typeof LINKED).toBe('boolean')
expect(typeof PRODUCTION).toBe('boolean')
})
test('string environment constants should match their env vars', () => {
expect(REGION).toBe(process.env.VTEX_REGION as string)
expect(NODE_ENV).toBe(process.env.NODE_ENV as string)
expect(ACCOUNT).toBe(process.env.VTEX_ACCOUNT as string)
expect(WORKSPACE).toBe(process.env.VTEX_WORKSPACE as string)
})
test('PUBLIC_ENDPOINT should have a fallback value', () => {
expect(typeof PUBLIC_ENDPOINT).toBe('string')
expect(PUBLIC_ENDPOINT.length).toBeGreaterThan(0)
})
test('LINKED should reflect boolean conversion of VTEX_APP_LINK', () => {
expect(LINKED).toBe(!!process.env.VTEX_APP_LINK)
})
test('PRODUCTION should reflect string comparison', () => {
expect(PRODUCTION).toBe(process.env.VTEX_PRODUCTION === 'true')
})
})
describe('APP object', () => {
test('should be an object with required properties', () => {
expect(typeof APP).toBe('object')
expect(APP).not.toBeNull()
const requiredProperties = ['ID', 'MAJOR', 'NAME', 'VENDOR', 'VERSION', 'IS_THIRD_PARTY']
requiredProperties.forEach(prop => {
expect(APP).toHaveProperty(prop)
})
})
test('string properties should match environment variables', () => {
expect(APP.ID).toBe(process.env.VTEX_APP_ID as string)
expect(APP.NAME).toBe(process.env.VTEX_APP_NAME as string)
expect(APP.VENDOR).toBe(process.env.VTEX_APP_VENDOR as string)
expect(APP.VERSION).toBe(process.env.VTEX_APP_VERSION as string)
})
test('MAJOR should be extracted from VERSION', () => {
if (process.env.VTEX_APP_VERSION) {
expect(typeof APP.MAJOR).toBe('string')
} else {
expect(APP.MAJOR).toBe('')
}
})
test('IS_THIRD_PARTY should be a function', () => {
expect(typeof APP.IS_THIRD_PARTY).toBe('function')
})
test('IS_THIRD_PARTY should return boolean', () => {
const result = APP.IS_THIRD_PARTY()
expect(typeof result).toBe('boolean')
})
test('IS_THIRD_PARTY logic should work correctly', () => {
// Test the logic with different vendor values
const testCases = [
{ vendor: 'vtex', expected: false },
{ vendor: 'gocommerce', expected: false },
{ vendor: 'other', expected: true },
{ vendor: undefined, expected: true }
]
testCases.forEach(({ vendor, expected }) => {
const mockApp = {
VENDOR: vendor,
IS_THIRD_PARTY() {
return 'vtex' !== this.VENDOR && 'gocommerce' !== this.VENDOR
}
}
expect(mockApp.IS_THIRD_PARTY()).toBe(expected)
})
})
})
describe('HTTP methods', () => {
test('cancellableMethods should be a Set', () => {
expect(cancellableMethods).toBeInstanceOf(Set)
})
test('cancellableMethods should contain safe HTTP methods', () => {
expect(cancellableMethods.has('GET')).toBe(true)
expect(cancellableMethods.has('OPTIONS')).toBe(true)
expect(cancellableMethods.has('HEAD')).toBe(true)
})
test('cancellableMethods should not contain unsafe HTTP methods', () => {
const unsafeMethods = ['POST', 'PUT', 'DELETE', 'PATCH']
unsafeMethods.forEach(method => {
expect(cancellableMethods.has(method)).toBe(false)
})
})
test('cancellableMethods should have appropriate size', () => {
expect(cancellableMethods.size).toBeGreaterThan(0)
expect(cancellableMethods.size).toBeLessThan(10) // Reasonable upper bound
})
test('all methods in cancellableMethods should be strings', () => {
cancellableMethods.forEach((method: any) => {
expect(typeof method).toBe('string')
expect(method.length).toBeGreaterThan(0)
})
})
})
describe('Integration and dependencies', () => {
test('should import required modules without errors', () => {
expect(() => {
require('./utils/app')
}).not.toThrow()
})
test('package.json should be accessible and valid', () => {
const pkg = require('../package.json')
expect(pkg).toHaveProperty('name')
expect(pkg).toHaveProperty('version')
expect(typeof pkg.name).toBe('string')
expect(typeof pkg.version).toBe('string')
})
test('should handle external module imports gracefully', () => {
// Test that AttributeKeys exists even if external module has issues
expect(AttributeKeys).toBeDefined()
expect(typeof AttributeKeys).toBe('object')
})
})
describe('Constants structure and exports', () => {
test('should export all required constants', () => {
// Test that all expected exports are defined using direct references
const constants = {
NODE_VTEX_API_VERSION,
DEFAULT_WORKSPACE,
IS_IO,
PID,
HeaderKeys,
AttributeKeys,
BODY_HASH,
UP_SIGNAL,
MAX_AGE,
HTTP_SERVER_PORT,
MAX_WORKERS,
LINKED,
REGION,
PUBLIC_ENDPOINT,
APP,
NODE_ENV,
ACCOUNT,
WORKSPACE,
PRODUCTION,
INSPECT_DEBUGGER_PORT,
cancellableMethods,
LOG_CLIENT_INIT_TIMEOUT_MS
}
// Environment-based constants that can be undefined in test environment
const envBasedConstants = ['IS_IO', 'REGION', 'ACCOUNT', 'WORKSPACE', 'NODE_ENV']
Object.entries(constants).forEach(([name, value]) => {
if (envBasedConstants.includes(name)) {
// Environment-based constants can be undefined, but should not be null
expect(value).not.toBeNull()
} else {
// Other constants should be defined
expect(value).toBeDefined()
expect(value).not.toBeNull()
}
})
})
test('constants should have consistent types', () => {
// Test type consistency
expect(typeof NODE_VTEX_API_VERSION).toBe('string')
expect(typeof DEFAULT_WORKSPACE).toBe('string')
expect(typeof PID).toBe('number')
expect(typeof HeaderKeys).toBe('object')
expect(typeof AttributeKeys).toBe('object')
expect(typeof BODY_HASH).toBe('string')
expect(typeof UP_SIGNAL).toBe('string')
expect(typeof MAX_AGE).toBe('object')
expect(typeof HTTP_SERVER_PORT).toBe('number')
expect(typeof MAX_WORKERS).toBe('number')
expect(typeof LINKED).toBe('boolean')
expect(typeof PUBLIC_ENDPOINT).toBe('string')
expect(typeof APP).toBe('object')
expect(typeof PRODUCTION).toBe('boolean')
expect(typeof INSPECT_DEBUGGER_PORT).toBe('number')
expect(cancellableMethods).toBeInstanceOf(Set)
expect(typeof LOG_CLIENT_INIT_TIMEOUT_MS).toBe('number')
})
test('should not have null or undefined critical constants', () => {
expect(NODE_VTEX_API_VERSION).not.toBeNull()
expect(DEFAULT_WORKSPACE).not.toBeNull()
expect(PID).not.toBeNull()
expect(HeaderKeys).not.toBeNull()
expect(AttributeKeys).not.toBeNull()
expect(BODY_HASH).not.toBeNull()
expect(UP_SIGNAL).not.toBeNull()
expect(MAX_AGE).not.toBeNull()
expect(APP).not.toBeNull()
expect(cancellableMethods).not.toBeNull()
})
})
describe('Backward compatibility', () => {
test('should maintain critical header structure', () => {
// Test that critical headers exist without testing specific values
const criticalHeaders = ['ACCOUNT', 'WORKSPACE', 'OPERATION_ID', 'REQUEST_ID', 'TRACE_ID']
criticalHeaders.forEach(header => {
expect(HeaderKeys).toHaveProperty(header)
expect(typeof HeaderKeys[header as keyof typeof HeaderKeys]).toBe('string')
})
})
test('should maintain AttributeKeys structure', () => {
const requiredAttributes = [
'VTEX_ACCOUNT_NAME',
'VTEX_IO_WORKSPACE_NAME', 'VTEX_IO_WORKSPACE_TYPE',
'VTEX_IO_APP_ID', 'VTEX_IO_APP_AUTHOR_TYPE'
]
requiredAttributes.forEach(attr => {
expect(AttributeKeys).toHaveProperty(attr)
expect(typeof AttributeKeys[attr as keyof typeof AttributeKeys]).toBe('string')
})
})
test('should maintain APP object structure', () => {
const requiredProperties = ['ID', 'MAJOR', 'NAME', 'VENDOR', 'VERSION', 'IS_THIRD_PARTY']
requiredProperties.forEach(prop => {
expect(APP).toHaveProperty(prop)
})
})
describe('Individual header constants (deprecated)', () => {
test('all individual header constants should be defined', () => {
expect(CACHE_CONTROL_HEADER).toBeDefined()
expect(SEGMENT_HEADER).toBeDefined()
expect(SESSION_HEADER).toBeDefined()
expect(PRODUCT_HEADER).toBeDefined()
expect(LOCALE_HEADER).toBeDefined()
expect(FORWARDED_HOST_HEADER).toBeDefined()
expect(TENANT_HEADER).toBeDefined()
expect(BINDING_HEADER).toBeDefined()
expect(META_HEADER).toBeDefined()
expect(META_HEADER_BUCKET).toBeDefined()
expect(ETAG_HEADER).toBeDefined()
expect(ACCOUNT_HEADER).toBeDefined()
expect(CREDENTIAL_HEADER).toBeDefined()
expect(REQUEST_ID_HEADER).toBeDefined()
expect(ROUTER_CACHE_HEADER).toBeDefined()
expect(OPERATION_ID_HEADER).toBeDefined()
expect(PLATFORM_HEADER).toBeDefined()
expect(WORKSPACE_IS_PRODUCTION_HEADER).toBeDefined()
expect(WORKSPACE_HEADER).toBeDefined()
expect(EVENT_KEY_HEADER).toBeDefined()
expect(EVENT_SENDER_HEADER).toBeDefined()
expect(EVENT_SUBJECT_HEADER).toBeDefined()
expect(EVENT_HANDLER_ID_HEADER).toBeDefined()
expect(COLOSSUS_ROUTE_DECLARER_HEADER).toBeDefined()
expect(COLOSSUS_ROUTE_ID_HEADER).toBeDefined()
expect(COLOSSUS_PARAMS_HEADER).toBeDefined()
expect(TRACE_ID_HEADER).toBeDefined()
expect(PROVIDER_HEADER).toBeDefined()
})
test('individual constants should equal HeaderKeys values', () => {
expect(CACHE_CONTROL_HEADER).toBe(HeaderKeys.CACHE_CONTROL)
expect(SEGMENT_HEADER).toBe(HeaderKeys.SEGMENT)
expect(SESSION_HEADER).toBe(HeaderKeys.SESSION)
expect(PRODUCT_HEADER).toBe(HeaderKeys.PRODUCT)
expect(LOCALE_HEADER).toBe(HeaderKeys.LOCALE)
expect(FORWARDED_HOST_HEADER).toBe(HeaderKeys.FORWARDED_HOST)
expect(TENANT_HEADER).toBe(HeaderKeys.TENANT)
expect(BINDING_HEADER).toBe(HeaderKeys.BINDING)
expect(META_HEADER).toBe(HeaderKeys.META)
expect(META_HEADER_BUCKET).toBe(HeaderKeys.META_BUCKET)
expect(ETAG_HEADER).toBe(HeaderKeys.ETAG)
expect(ACCOUNT_HEADER).toBe(HeaderKeys.ACCOUNT)
expect(CREDENTIAL_HEADER).toBe(HeaderKeys.CREDENTIAL)
expect(REQUEST_ID_HEADER).toBe(HeaderKeys.REQUEST_ID)
expect(ROUTER_CACHE_HEADER).toBe(HeaderKeys.ROUTER_CACHE)
expect(OPERATION_ID_HEADER).toBe(HeaderKeys.OPERATION_ID)
expect(PLATFORM_HEADER).toBe(HeaderKeys.PLATFORM)
expect(WORKSPACE_IS_PRODUCTION_HEADER).toBe(HeaderKeys.WORKSPACE_IS_PRODUCTION)
expect(WORKSPACE_HEADER).toBe(HeaderKeys.WORKSPACE)
expect(EVENT_KEY_HEADER).toBe(HeaderKeys.EVENT_KEY)
expect(EVENT_SENDER_HEADER).toBe(HeaderKeys.EVENT_SENDER)
expect(EVENT_SUBJECT_HEADER).toBe(HeaderKeys.EVENT_SUBJECT)
expect(EVENT_HANDLER_ID_HEADER).toBe(HeaderKeys.EVENT_HANDLER_ID)
expect(COLOSSUS_ROUTE_DECLARER_HEADER).toBe(HeaderKeys.COLOSSUS_ROUTE_DECLARER)
expect(COLOSSUS_ROUTE_ID_HEADER).toBe(HeaderKeys.COLOSSUS_ROUTE_ID)
expect(COLOSSUS_PARAMS_HEADER).toBe(HeaderKeys.COLOSSUS_PARAMS)
expect(TRACE_ID_HEADER).toBe(HeaderKeys.TRACE_ID)
expect(PROVIDER_HEADER).toBe(HeaderKeys.PROVIDER)
})
test('critical individual constants should have expected string values', () => {
expect(TENANT_HEADER).toBe('x-vtex-tenant')
expect(BINDING_HEADER).toBe('x-vtex-binding')
expect(LOCALE_HEADER).toBe('x-vtex-locale')
expect(SEGMENT_HEADER).toBe('x-vtex-segment')
expect(SESSION_HEADER).toBe('x-vtex-session')
expect(ACCOUNT_HEADER).toBe('x-vtex-account')
expect(WORKSPACE_HEADER).toBe('x-vtex-workspace')
})
test('individual constants should be strings', () => {
expect(typeof TENANT_HEADER).toBe('string')
expect(typeof BINDING_HEADER).toBe('string')
expect(typeof LOCALE_HEADER).toBe('string')
expect(typeof SEGMENT_HEADER).toBe('string')
})
test('constants can be used as object keys without runtime errors', () => {
// This is how IO apps use them in practice
const headers = {
[TENANT_HEADER]: 'example-value',
[BINDING_HEADER]: 'example-binding',
[LOCALE_HEADER]: 'en-US',
[SEGMENT_HEADER]: 'segment-token',
[SESSION_HEADER]: 'session-token',
[ACCOUNT_HEADER]: 'account-name',
[WORKSPACE_HEADER]: 'master'
}
expect(headers['x-vtex-tenant']).toBe('example-value')
expect(headers['x-vtex-binding']).toBe('example-binding')
expect(headers['x-vtex-locale']).toBe('en-US')
expect(headers['x-vtex-segment']).toBe('segment-token')
expect(Object.keys(headers)).toHaveLength(7)
// Verify no undefined keys were created
Object.keys(headers).forEach(key => {
expect(key).not.toBe('undefined')
expect(headers[key]).toBeDefined()
})
})
test('constants can be destructured from module exports', () => {
// Simulates: import { TENANT_HEADER, BINDING_HEADER } from '@vtex/api'
const constants = require('./constants')
const {
TENANT_HEADER: tenant,
BINDING_HEADER: binding,
LOCALE_HEADER: locale,
SEGMENT_HEADER: segment
} = constants
expect(tenant).toBeDefined()
expect(binding).toBeDefined()
expect(locale).toBeDefined()
expect(segment).toBeDefined()
expect(tenant).toBe('x-vtex-tenant')
expect(binding).toBe('x-vtex-binding')
expect(locale).toBe('x-vtex-locale')
expect(segment).toBe('x-vtex-segment')
// Ensure they're not undefined
expect(tenant).not.toBe(undefined)
expect(binding).not.toBe(undefined)
})
test('individual constants are compatible with VaryHeaders type', () => {
// VaryHeaders type uses HeaderKeys internally, but should accept old constants
const varyHeaderValues: string[] = [SEGMENT_HEADER, SESSION_HEADER, PRODUCT_HEADER, LOCALE_HEADER]
varyHeaderValues.forEach(header => {
expect(typeof header).toBe('string')
expect(header.length).toBeGreaterThan(0)
// VTEX headers follow x-vtex- pattern, except standard headers like cache-control
expect(header).toMatch(/^x-vtex-|^cache-control$|^etag$/)
})
// Verify they match the type definition (HeaderKeys values)
const expectedVaryHeaders = [
HeaderKeys.SEGMENT,
HeaderKeys.SESSION,
HeaderKeys.PRODUCT,
HeaderKeys.LOCALE
]
expect(varyHeaderValues).toEqual(expectedVaryHeaders)
// Ensure VaryHeaders type inference works
expect(SEGMENT_HEADER).toBe(HeaderKeys.SEGMENT)
expect(SESSION_HEADER).toBe(HeaderKeys.SESSION)
expect(PRODUCT_HEADER).toBe(HeaderKeys.PRODUCT)
expect(LOCALE_HEADER).toBe(HeaderKeys.LOCALE)
})
test('constants work correctly as header keys in realistic scenarios', () => {
// Simulates IO apps usage patterns
const mockBinding = { locale: 'en-US', currency: 'USD' }
const mockTenant = { locale: 'pt-BR' }
const mockSegmentToken = 'eyJjYW1wYWlnbnMiOm51bGx9'
const mockSessionToken = 'session-abc-123'
// Pattern 1: Building headers object
const requestHeaders = {
[BINDING_HEADER]: JSON.stringify(mockBinding),
[TENANT_HEADER]: mockTenant.locale,
[LOCALE_HEADER]: 'en-US',
[SEGMENT_HEADER]: mockSegmentToken,
[SESSION_HEADER]: mockSessionToken,
[ACCOUNT_HEADER]: 'vtexstore',
[WORKSPACE_HEADER]: 'master'
}
expect(requestHeaders['x-vtex-binding']).toBe(JSON.stringify(mockBinding))
expect(requestHeaders['x-vtex-tenant']).toBe('pt-BR')
expect(requestHeaders['x-vtex-locale']).toBe('en-US')
expect(requestHeaders['x-vtex-segment']).toBe(mockSegmentToken)
expect(requestHeaders['x-vtex-session']).toBe(mockSessionToken)
// Pattern 2: Conditional header setting
const conditionalHeaders: Record<string, string> = {}
if (mockSegmentToken) {
conditionalHeaders[SEGMENT_HEADER] = mockSegmentToken
}
if (mockSessionToken) {
conditionalHeaders[SESSION_HEADER] = mockSessionToken
}
expect(conditionalHeaders['x-vtex-segment']).toBe(mockSegmentToken)
expect(conditionalHeaders['x-vtex-session']).toBe(mockSessionToken)
expect(Object.keys(conditionalHeaders)).toHaveLength(2)
// Pattern 3: Reading from headers object
const incomingHeaders: Record<string, string> = {
'x-vtex-tenant': 'es-AR',
'x-vtex-binding': '{"locale":"es-AR"}',
'x-vtex-account': 'mystore'
}
expect(incomingHeaders[TENANT_HEADER]).toBe('es-AR')
expect(incomingHeaders[BINDING_HEADER]).toBe('{"locale":"es-AR"}')
expect(incomingHeaders[ACCOUNT_HEADER]).toBe('mystore')
// Verify no undefined keys in any pattern
expect(TENANT_HEADER).not.toBe('undefined')
expect(BINDING_HEADER).not.toBe('undefined')
expect(SEGMENT_HEADER).not.toBe('undefined')
})
})
})
})