-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.mjs
More file actions
317 lines (268 loc) · 10.5 KB
/
Copy pathtest.mjs
File metadata and controls
317 lines (268 loc) · 10.5 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
import { join as joinPath, dirname, extname } from 'path'
import { readFile, writeFile } from 'fs/promises'
import { deepStrictEqual } from 'assert'
import { fileURLToPath } from 'url'
import { tmpdir } from 'os'
import http from 'http'
import fs from 'fs'
global.window = global
global._fetch = fetch
let defaulTestPath = "tests"
const wait = delay => new Promise(s => setTimeout(s, delay))
const fail = fn => { try { fn() } catch (err) { return true } }
const upperFirst = (str) => str[0].toUpperCase() + str.slice(1)
const randStr = (n = 7) => Math.random().toString(36).slice(2, n)
const between = (min, max) => {
max || (max = min, min = 0)
return Math.floor(Math.random() * (max - min) + min)
}
const props = [String, Array]
.flatMap(({ prototype }) =>
Object.getOwnPropertyNames(prototype)
.map(key => ({ key, value: prototype[key], src: prototype })))
.filter(p => typeof p.value === 'function')
const eq = (a, b) => {
const changed = []
for (const p of props) { !p.src[p.key] && (changed[changed.length] = p) }
for (const p of changed) { p.src[p.key] = p.value }
deepStrictEqual(a, b)
for (const p of changed) { p.src[p.key] = undefined }
return true
}
// get the name of last modified file in the current directory whose extension is .js
const lastModifiedFile = fs.readdirSync(process.cwd()).filter(file => extname(file) === '.js').sort((a, b) => fs.statSync(joinPath(process.cwd(), b)).mtime.getTime() - fs.statSync(joinPath(process.cwd(), a)).mtime.getTime())[0]
let [solutionPath = ".", name = lastModifiedFile] = process.argv.slice(2)
const tools = { eq, fail, wait, randStr, between, upperFirst }
const fatal = (...args) => {
console.error(...args)
process.exit(1)
}
const ifNoEnt = fn => err => {
if (err.code !== 'ENOENT') throw err
fn(err)
}
const root = dirname(fileURLToPath(import.meta.url))
const read = (filename, description) => {
return new Promise((resolve, reject) => {
fs.readFile(filename, 'utf8', (err, data) => {
if (err) {
if (err.code === 'ENOENT') {
reject(`Missing ${description} for ${name}`);
} else {
reject(err);
}
}
resolve(data);
});
});
};
const modes = { '.js': 'function', '.mjs': 'node', '.json': 'inline' }
const readTest = filename => readFile(filename, 'utf8')
.then(test => ({ test, mode: modes[extname(filename)] }))
const stackFmt = (err, url) => {
for (const p of props) { p.src[p.key] = p.value }
if (err instanceof Error) return err.stack.split(url).join(`${name}.js`)
throw Error(`Unexpected type thrown: ${typeof err}. usage: throw Error('my message')`)
}
const any = arr =>
new Promise(async (s, f) => {
let firstError
const setError = err => firstError || (firstError = err)
await Promise.all(arr.map(p => p.then(s, setError)))
f(firstError)
})
const testNode = async () => {
const path = `${solutionPath}/${name}.mjs`
return {
path,
url: joinPath(root, `${name}_test.mjs`),
code: await read(path, 'student solution'),
}
}
const runInlineTests = async ({ json }) => {
const restore = new Set()
const equal = deepStrictEqual
const saveArguments = (src, key) => {
const savedArgs = []
const fn = src[key]
src[key] = (...args) => {
savedArgs.push(args)
return fn(...args)
}
restore.add(() => (src[key] = fn))
return savedArgs
}
const logs = []
console.log = (...args) => logs.push(args)
const die = (...args) => {
logs.forEach((logArgs) => console.info(...logArgs))
fatal(...args)
}
const solution = await loadAndSanitizeSolution()
for (const { description, code } of JSON.parse(json)) {
logs.length = 0
const [provided, tests] = code.includes('// Your code')
? code.split('// Your code')
: ['', code]
const fullCode = `
${provided ? '// Provided setup' : ''}
${provided.trim()}
// Your code
${solution.code.trim()}
// The tests
${tests.trim()}`.trim()
try {
eval(fullCode)
console.info(`${description}:`, 'PASS')
} catch (err) {
console.info(`${description}:`, 'FAIL')
console.info('\n======= Error ======')
console.info(' ->', err.message, '\n')
console.info('\n======= Code =======')
die(fullCode)
}
}
}
const loadAndSanitizeSolution = async () => {
try {
const path = `${solutionPath}/${name}.js`
let rawCode = await read(path, "student solution")
const sanitizedCode = removeComments(rawCode)
if (sanitizedCode.includes("import ")) { // space is important as it prevents "imported" or "importance" or other words containing "import"
throw new Error("The use of the 'import' keyword is not allowed.")
}
return { code: sanitizedCode, rawCode, path }
} catch (error) {
console.error(error)
}
}
const removeComments = (code) => code.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, "").trim()
const runTests = async ({ url, path, code }) => {
const { setup, tests } = await import(url).catch(err =>
fatal(`Unable to execute ${name}, error:\n${stackFmt(err, url)}`),
)
Object.assign(tools, { code, path })
tools.ctx = (await (setup && setup(tools))) || {}
const isDOM = name.endsWith('-dom')
if (isDOM) {
Object.assign(tools, await prepareForDOM({ code }))
}
let timeout
for (const [i, t] of tests.entries()) {
try {
const waitWithTimeout = Promise.race([
t(tools),
new Promise((s, f) => {
timeout = setTimeout(f, 60000, Error('Time limit reached (1min)'))
}),
])
if (!(await waitWithTimeout) && !isDOM) {
throw Error('Test failed')
}
} catch (err) {
console.info(`test #${i + 1} failed:\n${t.toString()}\n`)
fatal(stackFmt(err, url))
} finally {
clearTimeout(timeout)
}
}
console.info(`${name} passed (${tests.length} tests)`)
}
// add puppeteer tests as JS language:
const PORT = 9898
const config = {
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
// This will write shared memory files into /tmp instead of /dev/shm,
// because Docker’s default for /dev/shm is 64MB
'--disable-dev-shm-usage',
],
headless: !process.env.DEBUG_PUPPETTEER,
}
// LEGACY random, use between instead (only used by dom exercise, to be replaced)
const random = (min, max = min) => {
max === min && (min = 0)
min = Math.ceil(min)
return Math.floor(Math.random() * (Math.floor(max) - min + 1)) + min
}
const rgbToHsl = rgbStr => {
const [r, g, b] = rgbStr.slice(4, -1).split(',').map(Number)
const max = Math.max(r, g, b)
const min = Math.min(r, g, b)
const l = (max + min) / ((0xff * 2) / 100)
if (max === min) return [0, 0, l]
const d = max - min
const s = (d / (l > 50 ? 0xff * 2 - max - min : max + min)) * 100
if (max === r) return [((g - b) / d + (g < b && 6)) * 60, s, l]
return max === g
? [((b - r) / d + 2) * 60, s, l]
: [((r - g) / d + 4) * 60, s, l]
}
const prepareForDOM = ({ code }, server) => new Promise((s, f) => (server = http
.createServer(({ url, method }, response) => {
console.info(method + ' ' + url)
// Loading either the `index.html` or the js code (student solution)
response.setHeader('Content-Type', 'text/html')
return response.end(`<script type="module">${code}</script>`)
}))
.listen(PORT, async listenErr => {
if (listenErr) return f(listenErr)
try {
const browser = await puppeteer.launch(config)
const [page] = await browser.pages()
await page.goto(`http://localhost:${PORT}/index.html`)
deepStrictEqual.$ = async (selector, props) => {
const keys = Object.keys(props)
const extractProps = (node, props) => {
const fromProps = (a, b) => Object.fromEntries(Object.keys(b).map(k => [
k,
typeof b[k] === 'object' ? fromProps(a[k], b[k]) : a[k],
]))
return fromProps(node, props)
}
const domProps = await page.$eval(selector, extractProps, props)
return deepStrictEqual(props, domProps)
}
deepStrictEqual.css = async (selector, props) => {
const cssProps = await page.evaluate((selector, props) => {
const styles = Object.fromEntries([...document.styleSheets]
.flatMap(({ cssRules }) => [...cssRules].map(r => [r.selectorText, r.style])))
if (!styles[selector]) {
throw Error(`css ${selector} did not match any declarations`)
}
return Object.fromEntries(Object.keys(props).map(k => [k, styles[selector][k]]))
}, selector, props)
return deepStrictEqual(props, cssProps)
}
browser
.defaultBrowserContext()
.overridePermissions(`http://localhost:${PORT}`, ['clipboard-read'])
s({ page, browser, random, rgbToHsl, eq: deepStrictEqual, server })
} catch (err) {
f(err)
}
}))
const main = async () => {
name = name.replace(/\.js$/, '')
const { test, mode } = await any([
readTest(joinPath(root, defaulTestPath, `${name}.json`)),
readTest(joinPath(root, defaulTestPath, `${name}_test.js`)),
readTest(joinPath(root, defaulTestPath, `${name}_test.mjs`)),
]).catch(ifNoEnt((err) => fatal(`Missing test for ${name}`)))
if (mode === "node") return runTests(await testNode())
if (mode === "inline") return runInlineTests({ json: test })
const { rawCode, code, path } = await loadAndSanitizeSolution()
const parts = test.split("// /*/ // ⚡")
const [inject, testCode] = parts.length < 2 ? ["", test] : parts
const combined = `${inject.trim()}\n${rawCode
.replace(inject.trim(), "")
.trim()}\n;${testCode.trim()}\n`
const url = `${tmpdir()}/${name}.mjs`
await writeFile(url, combined)
return runTests({ path, code, url })
}
main().then(
() => process.exit(0),
err => fatal(err?.stack || Error('').stack),
)