Repository navigation
Expand file tree
/
Copy pathworkshop-completion.test.js
More file actions
275 lines (261 loc) · 11.8 KB
/
Copy pathworkshop-completion.test.js
File metadata and controls
275 lines (261 loc) · 11.8 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
'use strict';
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const test = require('node:test');
const vm = require('node:vm');
const languages = require('../language-registry.js');
const locales = require('../locale-registry.js');
const navigation = require('../language-navigation.js');
const markdown = require('../markdown-language-preprocessor.js');
const completion = require('../workshop-completion.js');
const repoRoot = path.resolve(__dirname, '..', '..');
function extractViewerSource(html) {
const match = html.match(/<script>([\s\S]*?)<\/script>/i);
assert.ok(match, 'The lesson viewer must contain an inline script.');
return match[1];
}
const viewerSource = extractViewerSource(
fs.readFileSync(path.join(__dirname, '..', 'workshop', 'step.html'), 'utf8'));
const flush = () => new Promise(resolve => setImmediate(resolve));
test('inline viewer extraction recognizes lowercase, uppercase, and mixed-case script tags', () => {
for (const [opening, closing] of [
['script', 'script'],
['SCRIPT', 'SCRIPT'],
['ScRiPt', 'sCrIpT']
]) {
assert.equal(
extractViewerSource(`<script src="external.js"></script><${opening}>const value = 1;</${closing}>`),
'const value = 1;'
);
}
assert.throws(() => extractViewerSource('<script src="external.js"></script>'), /must contain an inline script/);
});
class Element {
constructor() {
this.children = [];
this.attributes = new Map();
this.listeners = new Map();
this.style = { setProperty() {} };
this.classList = { toggle() {}, contains() { return false; } };
this.textContent = '';
this.innerHTML = '';
}
setAttribute(name, value) { this.attributes.set(name, value); }
removeAttribute(name) { this.attributes.delete(name); }
toggleAttribute(name, force) {
if (force) this.setAttribute(name, '');
else this.removeAttribute(name);
}
addEventListener(name, callback) { this.listeners.set(name, callback); }
append(...children) {
for (const child of children) {
child.parent = this;
this.children.push(child);
}
}
replaceChildren(...children) {
this.children = [];
this.append(...children);
}
remove() {
this.parent.children = this.parent.children.filter(child => child !== this);
}
querySelectorAll() { return []; }
}
function environment(url, reducedMotion = false) {
const elements = new Map();
const media = new Map();
const timers = new Map();
const events = new Map();
let timerId = 0;
const document = {
body: new Element(),
documentElement: new Element(),
createElement: () => new Element(),
getElementById(id) {
if (!elements.has(id)) elements.set(id, new Element());
return elements.get(id);
},
querySelector: selector => document.getElementById(selector),
querySelectorAll: () => [],
addEventListener() {}
};
const window = {
location: new URL(url),
localStorage: { getItem: () => null, setItem() {} },
history: {
replaceState(state, unused, value) { window.location = new URL(value); }
},
matchMedia(query) {
if (!media.has(query)) {
const listeners = new Set();
media.set(query, {
matches: query.includes('reduced-motion') && reducedMotion,
addEventListener(name, callback) { listeners.add(callback); },
removeEventListener(name, callback) { listeners.delete(callback); },
change(value) {
this.matches = value;
for (const listener of listeners) listener();
}
});
}
return media.get(query);
},
addEventListener(name, callback) { events.set(name, callback); },
setTimeout(callback, delay) {
timers.set(++timerId, { callback, delay });
return timerId;
},
clearTimeout(id) { timers.delete(id); }
};
return { document, window, elements, media, timers, events };
}
async function viewer(step, language = 'nodejs', options = {}) {
const query = navigation.lessonUrl(step, language || undefined);
const env = environment(`http://localhost:8000/docs/workshop/step.html${query}`, options.reducedMotion);
env.context = vm.createContext({
window: env.window,
document: env.document,
HTMLElement: Element,
URL, URLSearchParams, console,
WorkshopLanguages: languages,
WorkshopLocales: locales,
WorkshopLanguageNavigation: navigation,
WorkshopMarkdown: markdown,
WorkshopCompletion: completion,
hljs: { highlightElement() {} },
marked: {
parse(source) {
if (options.renderFailure) throw new Error('Rendering failed.');
return source;
}
},
fetch: options.fetch || (async url => ({
ok: !options.fetchFailure,
status: options.fetchFailure ? 404 : 200,
text: async () => fs.readFileSync(
path.join(repoRoot, 'workshop', new URL(url).pathname.split('/').at(-1)), 'utf8')
}))
});
vm.runInContext(viewerSource, env.context);
await flush();
env.state = vm.runInContext('({ steps, currentIndex })', env.context);
env.changeLanguage = async languageId => {
const selector = env.document.getElementById('languageSelector');
selector.value = languageId;
selector.listeners.get('change')();
await flush();
};
return env;
}
for (const [track, finalLesson, finalStep, lessonCount] of [
['sdlc', '09-interactive-html-report', '10-complete', 9],
['museum', 'museum-08-interactive-exhibit-page', 'museum-09-complete', 7]
]) {
test(`${track}: every lesson is numbered and completion is last`, async () => {
const env = await viewer(finalLesson);
assert.deepEqual(
Array.from(env.state.steps, step => step.kind),
['preflight', ...Array(lessonCount).fill('core'), 'completion']
);
assert.deepEqual(
Array.from(env.state.steps.slice(1), step => step.number),
Array.from({ length: lessonCount + 1 }, (_, index) => index + 1)
);
assert.equal(env.state.steps.at(-1).id, finalStep);
assert.equal(env.document.getElementById('stepPosition').textContent,
`Step ${lessonCount} of ${lessonCount + 1}`);
assert.equal(env.document.getElementById('progressTrack').attributes.get('aria-valuemax'),
String(lessonCount + 1));
assert.equal(env.document.getElementById('nextHeaderLink').href,
navigation.lessonUrl(finalStep, 'nodejs'));
assert.match(env.document.getElementById('markdownContent').innerHTML,
new RegExp(`\\?step=${finalStep}&lang=nodejs`));
assert.doesNotMatch(env.document.getElementById('workshopNavigation').innerHTML, /Optional|\+/);
assert.equal(env.document.body.children.length, 0);
});
test(`${track}: completion retains each language and provides a hub exit`, async () => {
for (const language of ['dotnet', 'go', 'java', 'nodejs', 'python', 'rust']) {
const env = await viewer(finalStep, language);
const content = env.document.getElementById('markdownContent').innerHTML;
assert.match(content, /# You did it!/);
assert.ok(content.includes(languages.getLanguage(language).docsUrl));
assert.ok(content.includes(`/finished/${language}/`));
for (const other of ['dotnet', 'go', 'java', 'nodejs', 'python', 'rust']) {
if (other !== language) assert.ok(!content.includes(`/finished/${other}/`));
}
assert.equal(env.document.getElementById('previousHeaderLink').href,
navigation.lessonUrl(finalLesson, language));
assert.equal(env.document.getElementById('nextHeaderLink').hidden, true);
assert.ok(env.document.getElementById('lessonFooter').innerHTML
.includes(navigation.homeUrl(language, track)));
assert.doesNotMatch(env.document.getElementById('lessonFooter').innerHTML, /disabled/);
assert.equal(env.document.getElementById('progressFill').style.transform, 'scaleX(1)');
assert.match(env.document.getElementById('lessonStatus').textContent, /complete/);
const overlay = env.document.body.children[0];
assert.equal(overlay.className, 'completion-confetti');
assert.equal(overlay.attributes.get('aria-hidden'), 'true');
assert.equal(overlay.children.length, 64);
const timer = [...env.timers.values()][0];
assert.equal(timer.delay, 3500);
timer.callback();
assert.equal(env.document.body.children.length, 0);
assert.equal(env.timers.size, 0);
}
});
}
test('completion does not animate when motion is reduced', async () => {
const env = await viewer('10-complete', 'go', { reducedMotion: true });
assert.match(env.document.getElementById('markdownContent').innerHTML, /# You did it!/);
assert.equal(env.document.body.children.length, 0);
assert.equal(env.timers.size, 0);
});
test('celebration stops when reduced motion changes or the page is hidden', async () => {
for (const stop of [
env => env.media.get('(prefers-reduced-motion: reduce)').change(true),
env => env.events.get('pagehide')()
]) {
const env = await viewer('museum-09-complete');
assert.equal(env.document.body.children.length, 1);
stop(env);
assert.equal(env.document.body.children.length, 0);
assert.equal(env.timers.size, 0);
}
});
test('changing language clears confetti without replaying it', async () => {
const env = await viewer('10-complete');
await env.changeLanguage('rust');
assert.match(env.document.getElementById('markdownContent').innerHTML, /\/finished\/rust\//);
assert.equal(env.document.body.children.length, 0);
assert.equal(env.timers.size, 0);
assert.ok(env.document.getElementById('lessonFooter').innerHTML.includes('lang=rust'));
});
test('no celebration for missing language, failed fetch, or failed rendering', async () => {
for (const [language, options, message] of [
[null, {}, 'Choose a workshop language to continue.'],
['nodejs', { fetchFailure: true }, 'Unable to load this lesson.'],
['nodejs', { renderFailure: true }, 'Unable to load this lesson.']
]) {
const env = await viewer('10-complete', language, options);
assert.equal(env.document.getElementById('lessonStatus').textContent, message);
assert.doesNotMatch(env.document.getElementById('stepPosition').textContent, /complete/i);
assert.equal(env.document.body.children.length, 0);
assert.equal(env.timers.size, 0);
}
});
test('stale completion requests cannot replace the selected language or stop its celebration', async () => {
const requests = [];
const env = await viewer('10-complete', 'nodejs', {
fetch: () => new Promise(resolve => requests.push(resolve))
});
await env.changeLanguage('python');
const source = fs.readFileSync(path.join(repoRoot, 'workshop', '10-complete.md'), 'utf8');
requests[1]({ ok: true, text: async () => source });
await flush();
requests[0]({ ok: false, status: 404 });
await flush();
assert.match(env.document.getElementById('markdownContent').innerHTML, /\/finished\/python\//);
assert.equal(env.document.body.children.length, 1);
assert.match(env.document.getElementById('lessonStatus').textContent, /complete/);
});