-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.test.ts
More file actions
193 lines (169 loc) · 5.87 KB
/
Copy pathindex.test.ts
File metadata and controls
193 lines (169 loc) · 5.87 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
import { test } from 'node:test'
import { getHintUtils } from '../src/index.js'
import { clientHint as colorSchemeHint } from '../src/color-scheme.js'
import { clientHint as timeZoneHint } from '../src/time-zone.js'
import { clientHint as reducedMotionHint } from '../src/reduced-motion.js'
import assert from 'node:assert'
test('client script works', () => {
const hints = getHintUtils({
colorScheme: colorSchemeHint,
timeZone: timeZoneHint,
reducedMotion: reducedMotionHint,
})
// meh, this is good enough...
const checkScript = hints.getClientHintCheckScript()
if (!checkScript.includes(colorSchemeHint.getValueCode)) {
throw new Error('check script does not include color scheme hint')
}
if (!checkScript.includes(timeZoneHint.getValueCode)) {
throw new Error('check script does not include time zone hint')
}
if (!checkScript.includes(reducedMotionHint.getValueCode)) {
throw new Error('check script does not include reduced motion hint')
}
})
test('fallbacks work', () => {
const hints = getHintUtils({
colorScheme: colorSchemeHint,
timeZone: timeZoneHint,
reducedMotion: reducedMotionHint,
})
// fallbacks work
assert.deepStrictEqual(hints.getHints(), {
colorScheme: colorSchemeHint.fallback,
timeZone: timeZoneHint.fallback,
reducedMotion: reducedMotionHint.fallback,
})
})
test('getting values from request cookie works', () => {
const hints = getHintUtils({
colorScheme: colorSchemeHint,
timeZone: timeZoneHint,
reducedMotion: reducedMotionHint,
})
// getting from request works
const request = new Request('https://example.com', {
headers: {
Cookie:
'CH-prefers-color-scheme=dark; CH-time-zone=America%2FDenver; CH-reduced-motion=reduce',
},
})
assert.deepStrictEqual(hints.getHints(request), {
colorScheme: 'dark',
timeZone: 'America/Denver',
reducedMotion: 'reduce',
})
})
test('getting values from document', () => {
const hints = getHintUtils({
colorScheme: colorSchemeHint,
timeZone: timeZoneHint,
reducedMotion: reducedMotionHint,
})
// @ts-expect-error poor-man's mock
global.document = {
cookie:
'CH-prefers-color-scheme=dark; CH-time-zone=Lima%2FPeru; CH-reduced-motion=reduce',
}
try {
assert.deepStrictEqual(hints.getHints(), {
colorScheme: 'dark',
timeZone: 'Lima/Peru',
reducedMotion: 'reduce',
})
} finally {
// @ts-expect-error poor-man's mock
delete global.document
}
})
test('handles malformed URI in cookie values gracefully', () => {
const hints = getHintUtils({
colorScheme: colorSchemeHint,
timeZone: timeZoneHint,
reducedMotion: reducedMotionHint,
})
// Test with malformed URI that would cause decodeURIComponent to fail
const request = new Request('https://example.com', {
headers: {
Cookie:
'CH-prefers-color-scheme=dark; CH-time-zone=%C0%AF; CH-reduced-motion=reduce',
},
})
// The malformed timezone should fall back to the fallback value
const result = hints.getHints(request)
assert.strictEqual(result.colorScheme, 'dark')
assert.strictEqual(result.timeZone, timeZoneHint.fallback) // Should fall back due to malformed URI
assert.strictEqual(result.reducedMotion, 'reduce')
})
test('handles completely malformed cookie values', () => {
const hints = getHintUtils({
colorScheme: colorSchemeHint,
timeZone: timeZoneHint,
reducedMotion: reducedMotionHint,
})
// Test with completely invalid URI sequences
const request = new Request('https://example.com', {
headers: {
Cookie:
'CH-prefers-color-scheme=%C0%AF; CH-time-zone=%FF%FE; CH-reduced-motion=%E0%80%80',
},
})
// All malformed values should fall back to their fallback values
const result = hints.getHints(request)
assert.strictEqual(result.colorScheme, colorSchemeHint.fallback)
assert.strictEqual(result.timeZone, timeZoneHint.fallback)
assert.strictEqual(result.reducedMotion, reducedMotionHint.fallback)
})
test('handles mixed valid and invalid cookie values', () => {
const hints = getHintUtils({
colorScheme: colorSchemeHint,
timeZone: timeZoneHint,
reducedMotion: reducedMotionHint,
})
// Test with mix of valid and invalid values
const request = new Request('https://example.com', {
headers: {
Cookie:
'CH-prefers-color-scheme=light; CH-time-zone=%C0%AF; CH-reduced-motion=no-preference',
},
})
// Valid values should work, invalid ones should fall back
const result = hints.getHints(request)
assert.strictEqual(result.colorScheme, 'light') // Valid value
assert.strictEqual(result.timeZone, timeZoneHint.fallback) // Invalid value, should fall back
assert.strictEqual(result.reducedMotion, 'no-preference') // Valid value
})
test('handles empty cookie values gracefully', () => {
const hints = getHintUtils({
colorScheme: colorSchemeHint,
timeZone: timeZoneHint,
reducedMotion: reducedMotionHint,
})
// Test with empty cookie values
const request = new Request('https://example.com', {
headers: {
Cookie: 'CH-prefers-color-scheme=; CH-time-zone=; CH-reduced-motion=',
},
})
// Empty values should fall back to fallback values
const result = hints.getHints(request)
assert.strictEqual(result.colorScheme, colorSchemeHint.fallback)
assert.strictEqual(result.timeZone, timeZoneHint.fallback)
assert.strictEqual(result.reducedMotion, reducedMotionHint.fallback)
})
test('client script includes infinite refresh prevention', () => {
const hints = getHintUtils({
colorScheme: colorSchemeHint,
timeZone: timeZoneHint,
reducedMotion: reducedMotionHint,
})
const checkScript = hints.getClientHintCheckScript()
// Should include sessionStorage check for infinite refresh prevention
assert.ok(checkScript.includes('sessionStorage.getItem'))
assert.ok(checkScript.includes('clientHintReloadAttempts'))
assert.ok(checkScript.includes('Too many client hint reload attempts'))
// Should include try-catch around decodeURIComponent
assert.ok(checkScript.includes('try'))
assert.ok(checkScript.includes('catch'))
assert.ok(checkScript.includes('decodeURIComponent'))
})