tutanota/test/TestBuilder.js

175 lines
5.2 KiB
JavaScript
Raw Normal View History

2019-09-13 13:49:11 +02:00
import * as env from "../buildSrc/env.js"
import { preludeEnvPlugin } from "../buildSrc/env.js"
2022-05-09 18:41:10 +02:00
import fs from "fs-extra"
import path, { dirname } from "node:path"
2022-12-27 15:37:40 +01:00
import { renderHtml } from "../buildSrc/LaunchHtml.js"
import { getTutanotaAppVersion, runStep, writeFile } from "../buildSrc/buildUtils.js"
import { buildPackages } from "../buildSrc/packageBuilderFunctions.js"
import { domainConfigs } from "../buildSrc/DomainConfigs.js"
import { sh } from "../buildSrc/sh.js"
import { rolldown } from "rolldown"
import { resolveLibs } from "../buildSrc/RollupConfig.js"
import { nodeGypPlugin } from "../buildSrc/nodeGypPlugin.js"
import { fileURLToPath } from "node:url"
import { copyCryptoPrimitiveCrateIntoWasmDir, WASM_PACK_OUT_DIR } from "../buildSrc/cryptoPrimitivesUtils.js"
const currentDir = dirname(fileURLToPath(import.meta.url))
const projectRoot = path.resolve(path.join(currentDir, ".."))
2021-02-04 10:21:31 +01:00
export async function runTestBuild({ networkDebugging = false, clean, fast = false }) {
2022-05-09 18:41:10 +02:00
if (clean) {
await runStep("Clean", async () => {
await fs.emptyDir("build")
fs.rm(projectRoot + WASM_PACK_OUT_DIR, { recursive: true, force: true })
2022-05-09 18:41:10 +02:00
})
}
2019-09-13 13:49:11 +02:00
if (!fast) {
await runStep("Packages", async () => {
await buildPackages("..")
// we know which wasm need to be included in the project, instead of running branches condition on each and every file of the project we do some
// transformation AOT for our three files (currently only crypto-primitives but argon2 and liboqs will follow
await copyCryptoPrimitiveCrateIntoWasmDir({
wasmOutputDir: "build",
pathSourcePrefix: "../",
})
})
2019-09-13 13:49:11 +02:00
await runStep("Types", async () => {
await sh`npx tsc --incremental true --noEmit true`
})
}
2022-05-09 18:41:10 +02:00
const version = await getTutanotaAppVersion()
const localEnv = env.create({
staticUrl: "http://localhost:9000",
version,
mode: "Test",
dist: false,
domainConfigs,
networkDebugging,
})
2019-09-13 13:49:11 +02:00
2022-05-09 18:41:10 +02:00
await runStep("Assets", async () => {
const pjPath = path.join("..", "package.json")
2022-12-27 15:37:40 +01:00
await fs.mkdir(inBuildDir(), { recursive: true })
2022-05-09 18:41:10 +02:00
await fs.copyFile(pjPath, inBuildDir("package.json"))
await createUnitTestHtml(localEnv)
2019-09-13 13:49:11 +02:00
})
await runStep("Rolldown", async () => {
const { rollupWasmLoader } = await import("@tutao/tuta-wasm-loader")
const bundle = await rolldown({
input: ["tests/testInBrowser.ts", "tests/testInNode.ts", "../src/common/api/common/pow-worker.ts"],
platform: "neutral",
2022-05-09 18:41:10 +02:00
define: {
// See Env.ts for explanation
LOAD_ASSERTIONS: "false",
2019-09-13 13:49:11 +02:00
},
2022-05-09 18:41:10 +02:00
external: [
"electron",
// esbuild can't deal with node imports in ESM output at the moment
// see https://github.com/evanw/esbuild/pull/2067
"xhr2",
"express",
"server-destroy",
"body-parser",
"jsdom",
/node:.*/,
"http",
"stream",
"fs",
"assert",
"net",
"diagnostics_channel",
"zlib",
"console",
"async_hooks",
"util/types",
"perf_hooks",
"worker_threads",
"path",
"tls",
"buffer",
"events",
"util",
"string_decoder",
"crypto", // oxmsg needs it
"memcpy", // optional dep of oxmsg
2022-05-09 18:41:10 +02:00
],
plugins: [
preludeEnvPlugin(localEnv),
resolveLibs(".."),
nodeGypPlugin({
rootDir: projectRoot,
2022-05-09 18:41:10 +02:00
platform: process.platform,
architecture: process.arch,
nodeModule: "@signalapp/sqlcipher",
environment: "node",
targetName: "node_sqlcipher",
2022-05-09 18:41:10 +02:00
}),
rollupWasmLoader({
output: `${process.cwd()}/build`,
webassemblyLibraries: [
{
name: "liboqs.wasm",
2025-01-06 18:34:51 +01:00
command: "make -f Makefile_liboqs build",
workingDir: `${process.cwd()}/../libs/webassembly/`,
outputPath: `${process.cwd()}/build/liboqs.wasm`,
2025-01-06 18:34:51 +01:00
fallback: {
command: "make -f Makefile_liboqs fallback",
workingDir: `${process.cwd()}/../libs/webassembly/`,
outputPath: `${process.cwd()}/build/liboqs.js`,
2025-01-06 18:34:51 +01:00
},
},
{
name: "argon2.wasm",
2025-01-06 18:34:51 +01:00
command: "make -f Makefile_argon2 build",
workingDir: `${process.cwd()}/../libs/webassembly/`,
outputPath: `${process.cwd()}/build/argon2.wasm`,
2025-01-06 18:34:51 +01:00
fallback: {
command: "make -f Makefile_argon2 fallback",
workingDir: `${process.cwd()}/../libs/webassembly/`,
outputPath: `${process.cwd()}/build/argon2.js`,
2025-01-06 18:34:51 +01:00
},
},
],
}),
2022-12-27 15:37:40 +01:00
],
resolve: {
mainFields: ["module", "main"],
alias: {
// Take browser testdouble without funny require() magic
testdouble: path.resolve("../node_modules/testdouble/dist/testdouble.js"),
},
},
onwarn: (warning, defaultHandler) => {
if (warning.code !== "EVAL") {
defaultHandler(warning)
}
},
})
await bundle.write({
dir: "./build",
format: "esm",
sourcemap: true,
// overwrite the files rather than keeping all versions in the build folder
chunkFileNames: "[name]-chunk.js",
2022-05-09 18:41:10 +02:00
})
})
2019-09-13 13:49:11 +02:00
}
async function createUnitTestHtml(localEnv) {
2023-06-29 18:26:45 +02:00
const imports = [{ src: `./testInBrowser.js`, type: "module" }]
const htmlFilePath = inBuildDir("test.html")
console.log(`Generating browser tests at "${htmlFilePath}"`)
2019-09-13 13:49:11 +02:00
const html = await renderHtml(imports, localEnv)
await writeFile(htmlFilePath, html)
}
2022-05-09 18:41:10 +02:00
function inBuildDir(...files) {
return path.join("build", ...files)
2022-12-27 15:37:40 +01:00
}