-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsmoke-test.html
More file actions
437 lines (387 loc) · 13.1 KB
/
Copy pathsmoke-test.html
File metadata and controls
437 lines (387 loc) · 13.1 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cookie Consent Smoke Test</title>
<style>
:root {
color-scheme: light;
--bg: #f5f1ea;
--panel: #ffffff;
--text: #161616;
--muted: #5f5a52;
--border: rgba(22, 22, 22, 0.12);
--accent: #2457ff;
--accent-2: #1b3fc0;
--danger: #9b1c1c;
--success: #0f7a3d;
--shadow: 0 18px 50px rgba(22, 22, 22, 0.14);
}
body {
margin: 0;
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--text);
background:
radial-gradient(circle at top left, rgba(36, 87, 255, 0.12), transparent 30%),
radial-gradient(circle at top right, rgba(15, 122, 61, 0.10), transparent 30%),
var(--bg);
}
.page {
max-width: 980px;
margin: 0 auto;
padding: 32px 20px 48px;
}
.hero {
padding: 28px;
border: 1px solid var(--border);
border-radius: 24px;
background: rgba(255, 255, 255, 0.76);
box-shadow: var(--shadow);
backdrop-filter: blur(10px);
}
h1 {
margin: 0 0 12px;
font-size: clamp(2rem, 4vw, 3.2rem);
line-height: 1.05;
letter-spacing: -0.04em;
}
p {
margin: 0 0 14px;
color: var(--muted);
line-height: 1.6;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-top: 18px;
}
button {
appearance: none;
border: 1px solid transparent;
border-radius: 12px;
min-height: 46px;
padding: 0 16px;
font: inherit;
font-weight: 700;
cursor: pointer;
}
.primary {
color: #fff;
background: var(--accent);
}
.primary:hover {
background: var(--accent-2);
}
.secondary {
color: var(--text);
background: #fff;
border-color: var(--border);
}
.secondary:hover {
background: #f6f6f6;
}
.status {
margin-top: 22px;
padding: 18px;
border: 1px solid var(--border);
border-radius: 18px;
background: rgba(255, 255, 255, 0.84);
box-shadow: var(--shadow);
}
.status h2 {
margin: 0 0 12px;
font-size: 1.1rem;
}
.result-list {
display: grid;
gap: 12px;
}
.result {
padding: 14px 16px;
border-radius: 14px;
border: 1px solid var(--border);
background: #fff;
}
.result.pass {
border-color: rgba(15, 122, 61, 0.35);
background: rgba(15, 122, 61, 0.06);
}
.result.fail {
border-color: rgba(155, 28, 28, 0.35);
background: rgba(155, 28, 28, 0.06);
}
.result strong {
display: block;
margin-bottom: 4px;
}
.meta {
font-size: 0.92rem;
color: var(--muted);
word-break: break-word;
}
.summary {
margin-top: 14px;
font-weight: 700;
}
.summary.pass {
color: var(--success);
}
.summary.fail {
color: var(--danger);
}
.sandbox {
position: absolute;
left: -9999px;
width: 1px;
height: 1px;
border: 0;
}
code {
padding: 0.1em 0.35em;
border-radius: 6px;
background: rgba(22, 22, 22, 0.08);
}
</style>
</head>
<body>
<main class="page">
<section class="hero">
<h1>Cookie Consent Smoke Test</h1>
<p>
Runs a few isolated iframe scenarios against <code>cookieconsent.js</code> and reports the results in the browser.
Open this file from the repo root so the relative script path resolves.
</p>
<div class="actions">
<button id="run" class="primary" type="button">Run smoke tests</button>
<button id="clear" class="secondary" type="button">Clear results</button>
</div>
</section>
<section class="status" aria-live="polite">
<h2>Results</h2>
<div id="results" class="result-list"></div>
<div id="summary" class="summary">Idle.</div>
</section>
</main>
<script>
const COOKIECONSENT_SCRIPT = "./cookieconsent.js";
const STORAGE_KEY = "consentMode";
const scenarios = [
{
id: "boot-fresh",
title: "Fresh boot shows banner",
description: "No stored consent should render the banner and leave localStorage empty.",
storage: null,
assert(state) {
return state.bannerVisible === true &&
state.storageValue === null &&
state.bannerPresent === true;
}
},
{
id: "migration",
title: "Legacy storage migrates",
description: "A raw legacy consent object should be upgraded to the versioned envelope.",
storage: {
ad_storage: "granted",
analytics_storage: "denied",
ad_user_data: "granted",
ad_personalization: "denied",
functionality_storage: "granted",
personalization_storage: "denied",
security_storage: "granted"
},
assert(state) {
return state.bannerHidden === true &&
state.migratedSchemaVersion === 1 &&
state.migratedHasEnvelope === true;
}
},
{
id: "malformed",
title: "Malformed storage is cleared",
description: "Invalid JSON should not break startup and should be removed.",
storage: "__invalid_json__",
assert(state) {
return state.bannerVisible === true &&
state.storageValue === null &&
state.storageCleared === true;
}
},
{
id: "interaction",
title: "Banner interaction persists consent",
description: "Calling the public API should persist consent and hide the banner.",
storage: null,
interact(stateWindow) {
stateWindow.cookieconsent.setConsent({
necessary: true,
analytics: true,
preferences: true,
marketing: false,
partners: true
});
},
assert(state) {
return state.bannerHidden === true &&
state.storageValue !== null &&
state.persistedSchemaVersion === 1 &&
state.persistedSelection.analytics === true &&
state.persistedSelection.marketing === false;
}
}
];
const resultsEl = document.getElementById("results");
const summaryEl = document.getElementById("summary");
const runButton = document.getElementById("run");
const clearButton = document.getElementById("clear");
function setSummary(text, pass) {
summaryEl.textContent = text;
summaryEl.className = "summary " + (pass ? "pass" : "fail");
}
function renderResult(result) {
const item = document.createElement("div");
item.className = "result " + (result.pass ? "pass" : "fail");
const title = document.createElement("strong");
title.textContent = (result.pass ? "PASS: " : "FAIL: ") + result.title;
const meta = document.createElement("div");
meta.className = "meta";
meta.textContent = result.description + " " + (result.details || "");
item.appendChild(title);
item.appendChild(meta);
resultsEl.appendChild(item);
}
function clearResults() {
resultsEl.textContent = "";
setSummary("Idle.", true);
}
function buildScenarioDoc(scenario) {
const storageSetup = scenario.storage === null
? "localStorage.removeItem('" + STORAGE_KEY + "');"
: scenario.storage === "__invalid_json__"
? "localStorage.setItem('" + STORAGE_KEY + "', '{bad-json');"
: "localStorage.setItem('" + STORAGE_KEY + "', JSON.stringify(" + JSON.stringify(scenario.storage) + "));";
const interactCall = scenario.interact
? "(" + scenario.interact.toString() + ")(window);"
: "";
return [
"<!doctype html>",
"<html lang=\"en\">",
" <head>",
" <meta charset=\"utf-8\">",
" <title>" + scenario.title.replace(/"/g, """) + "</title>",
" <script>",
" window.cookieconsentConfig = { gtmId: '' };",
" console.warn = function() {};",
" " + storageSetup,
" </script>",
" <script src=\"" + COOKIECONSENT_SCRIPT + "\"></script>",
" </head>",
" <body>",
" <div class=\"cookie-consent-banner-open\">open</div>",
" <script>",
" function readState() {",
" const banner = document.getElementById('cookie-consent-banner');",
" const stored = localStorage.getItem('" + STORAGE_KEY + "');",
" let parsed = null;",
" try { parsed = stored ? JSON.parse(stored) : null; } catch (e) { parsed = '__parse_error__'; }",
" return {",
" bannerPresent: !!banner,",
" bannerVisible: !!banner && banner.hidden === false && banner.style.display !== 'none',",
" bannerHidden: !!banner && banner.hidden === true,",
" storageValue: stored,",
" parsed: parsed,",
" storageCleared: stored === null,",
" migratedSchemaVersion: parsed && parsed.schemaVersion ? parsed.schemaVersion : null,",
" migratedHasEnvelope: !!(parsed && parsed.consentMode && parsed.schemaVersion),",
" persistedSchemaVersion: parsed && parsed.schemaVersion ? parsed.schemaVersion : null,",
" persistedSelection: parsed && parsed.selection ? parsed.selection : null",
" };",
" }",
" window.addEventListener('load', function() {",
" setTimeout(function() {",
interactCall,
" setTimeout(function() {",
" parent.postMessage({",
" type: 'cookieconsent-smoke-result',",
" id: " + JSON.stringify(scenario.id) + ",",
" state: readState()",
" }, '*');",
" }, 50);",
" }, 50);",
" });",
" </script>",
" </body>",
"</html>"
].join("\n");
}
function runScenario(scenario) {
return new Promise((resolve) => {
const frame = document.createElement("iframe");
frame.className = "sandbox";
frame.setAttribute("sandbox", "allow-scripts allow-same-origin");
frame.srcdoc = buildScenarioDoc(scenario);
let timeoutId = null;
function onMessage(event) {
if (!event.data || event.data.type !== "cookieconsent-smoke-result" || event.data.id !== scenario.id) {
return;
}
if (timeoutId !== null) {
clearTimeout(timeoutId);
}
window.removeEventListener("message", onMessage);
frame.remove();
resolve(event.data.state);
}
window.addEventListener("message", onMessage);
document.body.appendChild(frame);
timeoutId = setTimeout(() => {
window.removeEventListener("message", onMessage);
frame.remove();
resolve({ timeout: true });
}, 5000);
}).then((state) => {
if (state.timeout) {
return {
title: scenario.title,
description: scenario.description,
pass: false,
details: "Timed out waiting for scenario result."
};
}
let pass = false;
try {
pass = scenario.assert(state);
} catch (e) {
pass = false;
}
return {
title: scenario.title,
description: scenario.description,
pass: pass,
details: JSON.stringify(state)
};
});
}
async function runAll() {
runButton.disabled = true;
clearResults();
const outcomes = [];
for (const scenario of scenarios) {
outcomes.push(await runScenario(scenario));
}
outcomes.forEach(renderResult);
const passed = outcomes.every((result) => result.pass);
setSummary(
passed ? "All smoke tests passed." : "One or more smoke tests failed.",
passed
);
runButton.disabled = false;
}
runButton.addEventListener("click", runAll);
clearButton.addEventListener("click", clearResults);
</script>
</body>
</html>