From f61bce71c76486d3aff6109058e40f1d032c145d Mon Sep 17 00:00:00 2001 From: "Yuichiro Tachibana (Tsuchiya)" Date: Sat, 22 Jun 2024 00:42:43 +0900 Subject: [PATCH] Create a unit test that launches the worker and run the app --- packages/kernel/src/worker-runtime.test.ts | 70 ++++++++++++++++++++++ packages/kernel/vitest.config.ts | 14 +++++ 2 files changed, 84 insertions(+) create mode 100644 packages/kernel/src/worker-runtime.test.ts diff --git a/packages/kernel/src/worker-runtime.test.ts b/packages/kernel/src/worker-runtime.test.ts new file mode 100644 index 000000000..ef9c92484 --- /dev/null +++ b/packages/kernel/src/worker-runtime.test.ts @@ -0,0 +1,70 @@ +import path from "node:path"; +import { test } from "vitest"; +import { startWorkerEnv, PostMessageFn } from "./worker-runtime"; +import stliteServerWheelUrl from "stlite_server.whl"; // This is the alias from vitest.config.ts +import streamlitWheelUrl from "streamlit.whl"; // This is the alias from vitest.config.ts + +const pyodideUrl = path.resolve("../../node_modules/pyodide/pyodide.mjs"); // Installed at the Yarn workspace root; + +function getWheelInstallPath(wheelImportUrl: string): string { + // `wheelImportUrl` is like `/path/to/stlite_server.whl` that is a URL path. + // We need to convert it to a local file path so that it can be referred to in the test environment i.e. Node.js. + // Also, we need to add `file://` scheme to it so that `micropip.install()` can install it. + return "file://" + path.resolve("." + wheelImportUrl); +} + +test( + "E2E test running an app", + async () => { + console.log({ + stliteServerWheelUrl, + streamlitWheelUrl, + }); + const loadedPromise = new Promise((resolve, reject) => { + const postMessage: PostMessageFn = (message) => { + if (message.type === "event:loaded") { + resolve(); + } else if (message.type === "event:error") { + reject(message.data.error); + } + }; + const onMessage = startWorkerEnv(pyodideUrl, postMessage); + onMessage( + new MessageEvent("message", { + data: { + type: "initData", + data: { + files: { + "app.py": { + data: ` +import streamlit as st + +st.title("Hello World") +`, + }, + }, + entrypoint: "app.py", + wheels: { + stliteServer: getWheelInstallPath( + stliteServerWheelUrl as unknown as string, + ), + streamlit: getWheelInstallPath( + streamlitWheelUrl as unknown as string, + ), + }, + archives: [], + requirements: [], + moduleAutoLoad: false, + prebuiltPackageNames: [], + }, + }, + }), + ); + }); + + await loadedPromise; + }, + { + timeout: 60000, + }, +); diff --git a/packages/kernel/vitest.config.ts b/packages/kernel/vitest.config.ts index 439443904..980603ae2 100644 --- a/packages/kernel/vitest.config.ts +++ b/packages/kernel/vitest.config.ts @@ -1,10 +1,24 @@ /// import { defineConfig } from "vite"; +import path from "node:path"; export default defineConfig({ test: { environment: "jsdom", // We use jsdom because happy-dom does not work well with iframe. setupFiles: ["./setupTests.ts"], }, + resolve: { + alias: { + "stlite_server.whl": path.resolve( + __dirname, + "./py/stlite-server/dist/stlite_server-0.1.0-py3-none-any.whl", + ), + "streamlit.whl": path.resolve( + __dirname, + "./py/streamlit/lib/dist/streamlit-1.35.0-cp312-none-any.whl", + ), + }, + }, + assetsInclude: ["**/*.whl"], });