From c5f2f201b530f3fd48344551c99f2369f0a9ad2c Mon Sep 17 00:00:00 2001
From: Pieter Vander Vennet <pietervdvn@posteo.net>
Date: Sun, 7 Jan 2024 17:59:10 +0100
Subject: [PATCH] Fix tests

---
 test/CodeQuality.spec.ts | 13 ++++++++-----
 vitest.config.ts         |  2 ++
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/test/CodeQuality.spec.ts b/test/CodeQuality.spec.ts
index 865620a39..205701af5 100644
--- a/test/CodeQuality.spec.ts
+++ b/test/CodeQuality.spec.ts
@@ -4,13 +4,14 @@ import { webcrypto } from "node:crypto"
 import { parse as parse_html } from "node-html-parser"
 import { readFileSync } from "fs"
 import ScriptUtils from "../scripts/ScriptUtils"
-import hash from "svelte/types/compiler/compile/utils/hash"
+
 function detectInCode(forbidden: string, reason: string) {
     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
  * @private
  */
@@ -64,6 +65,7 @@ function wrap(promise: Promise<void>): (done: () => void) => void {
         promise.then(done)
     }
 }
+
 function _arrayBufferToBase64(buffer) {
     var binary = ""
     var bytes = new Uint8Array(buffer)
@@ -73,6 +75,7 @@ function _arrayBufferToBase64(buffer) {
     }
     return btoa(binary)
 }
+
 async function validateScriptIntegrityOf(path: string): Promise<void> {
     const htmlContents = readFileSync(path, "utf8")
     const doc = parse_html(htmlContents)
@@ -99,9 +102,9 @@ async function validateScriptIntegrityOf(path: string): Promise<void> {
         if (src.startsWith("//")) {
             src = "https:" + src
         }
-        const request = await fetch(src)
-        const data: ArrayBuffer = await request.arrayBuffer()
-        const hashed = await webcrypto.subtle.digest("SHA-384", data)
+        // Using 'scriptUtils' actually fetches data from the internet, it is not prohibited by the testHooks
+        const data: string = (await ScriptUtils.Download(src))["content"]
+        const hashed = await webcrypto.subtle.digest("SHA-384", new TextEncoder().encode(data))
         const hashedStr = _arrayBufferToBase64(hashed)
         console.log(src, hashedStr, integrity)
         expect(integrity).to.equal(
diff --git a/vitest.config.ts b/vitest.config.ts
index 00a4ee2da..32d1cccea 100644
--- a/vitest.config.ts
+++ b/vitest.config.ts
@@ -5,6 +5,8 @@ export default defineConfig({
     plugins: [svelte({ hot: !process.env.VITEST, preprocess: [autoPreprocess()] })],
     test: {
         globals: true,
+        maxThreads: 16,
+        minThreads: 1,
         setupFiles: ["./test/testhooks.ts"],
         include: ["./test/*.spec.ts", "./test/**/*.spec.ts", "./*.doctest.ts", "./**/*.doctest.ts"],
     },