Fix tests

This commit is contained in:
Pieter Vander Vennet 2024-01-07 17:59:10 +01:00
parent 3cbedf7cf2
commit c5f2f201b5
2 changed files with 10 additions and 5 deletions

View file

@ -4,13 +4,14 @@ import { webcrypto } from "node:crypto"
import { parse as parse_html } from "node-html-parser" import { parse as parse_html } from "node-html-parser"
import { readFileSync } from "fs" import { readFileSync } from "fs"
import ScriptUtils from "../scripts/ScriptUtils" import ScriptUtils from "../scripts/ScriptUtils"
import hash from "svelte/types/compiler/compile/utils/hash"
function detectInCode(forbidden: string, reason: string) { function detectInCode(forbidden: string, reason: string) {
return wrap(detectInCodeUnwrapped(forbidden, reason)) return wrap(detectInCodeUnwrapped(forbidden, reason))
} }
/** /**
* *
* @param forbidden: a GREP-regex. This means that '.' is a wildcard and should be escaped to match a literal dot * @param forbidden a GREP-regex. This means that '.' is a wildcard and should be escaped to match a literal dot
* @param reason * @param reason
* @private * @private
*/ */
@ -64,6 +65,7 @@ function wrap(promise: Promise<void>): (done: () => void) => void {
promise.then(done) promise.then(done)
} }
} }
function _arrayBufferToBase64(buffer) { function _arrayBufferToBase64(buffer) {
var binary = "" var binary = ""
var bytes = new Uint8Array(buffer) var bytes = new Uint8Array(buffer)
@ -73,6 +75,7 @@ function _arrayBufferToBase64(buffer) {
} }
return btoa(binary) return btoa(binary)
} }
async function validateScriptIntegrityOf(path: string): Promise<void> { async function validateScriptIntegrityOf(path: string): Promise<void> {
const htmlContents = readFileSync(path, "utf8") const htmlContents = readFileSync(path, "utf8")
const doc = parse_html(htmlContents) const doc = parse_html(htmlContents)
@ -99,9 +102,9 @@ async function validateScriptIntegrityOf(path: string): Promise<void> {
if (src.startsWith("//")) { if (src.startsWith("//")) {
src = "https:" + src src = "https:" + src
} }
const request = await fetch(src) // Using 'scriptUtils' actually fetches data from the internet, it is not prohibited by the testHooks
const data: ArrayBuffer = await request.arrayBuffer() const data: string = (await ScriptUtils.Download(src))["content"]
const hashed = await webcrypto.subtle.digest("SHA-384", data) const hashed = await webcrypto.subtle.digest("SHA-384", new TextEncoder().encode(data))
const hashedStr = _arrayBufferToBase64(hashed) const hashedStr = _arrayBufferToBase64(hashed)
console.log(src, hashedStr, integrity) console.log(src, hashedStr, integrity)
expect(integrity).to.equal( expect(integrity).to.equal(

View file

@ -5,6 +5,8 @@ export default defineConfig({
plugins: [svelte({ hot: !process.env.VITEST, preprocess: [autoPreprocess()] })], plugins: [svelte({ hot: !process.env.VITEST, preprocess: [autoPreprocess()] })],
test: { test: {
globals: true, globals: true,
maxThreads: 16,
minThreads: 1,
setupFiles: ["./test/testhooks.ts"], setupFiles: ["./test/testhooks.ts"],
include: ["./test/*.spec.ts", "./test/**/*.spec.ts", "./*.doctest.ts", "./**/*.doctest.ts"], include: ["./test/*.spec.ts", "./test/**/*.spec.ts", "./*.doctest.ts", "./**/*.doctest.ts"],
}, },