From 6bb3d057ed6c13280696194e37713a633fc50db5 Mon Sep 17 00:00:00 2001 From: koyopro Date: Thu, 12 Dec 2024 03:40:22 +0900 Subject: [PATCH] tests for sync-actions --- tests/web/launchedWorker.test.ts | 6 ++++ tests/web/launchedWorker.ts | 5 ++++ tests/web/sample.txt | 1 + tests/web/sync.test.ts | 51 ++++++++++++++++++++++++++++++++ tests/web/worker.ts | 39 ++++++++++++++++++++++++ tests/web/workerWithError.ts | 8 +++++ 6 files changed, 110 insertions(+) create mode 100644 tests/web/launchedWorker.test.ts create mode 100644 tests/web/launchedWorker.ts create mode 100644 tests/web/sample.txt create mode 100644 tests/web/sync.test.ts create mode 100644 tests/web/worker.ts create mode 100644 tests/web/workerWithError.ts diff --git a/tests/web/launchedWorker.test.ts b/tests/web/launchedWorker.test.ts new file mode 100644 index 00000000..e83f59c6 --- /dev/null +++ b/tests/web/launchedWorker.test.ts @@ -0,0 +1,6 @@ +import { actions, getWorker } from "./launchedWorker"; + +test("sync actinos", () => { + expect(actions.ping()).toBe("pong!?"); + getWorker()!.terminate(); +}); diff --git a/tests/web/launchedWorker.ts b/tests/web/launchedWorker.ts new file mode 100644 index 00000000..a2be15a8 --- /dev/null +++ b/tests/web/launchedWorker.ts @@ -0,0 +1,5 @@ +import { launchSyncWorker } from "accel-record-core/dist/synclib/index.js"; + +export const { actions, getWorker, stopWorker } = launchSyncWorker(import.meta.filename, { + ping: () => "pong!?", +}); diff --git a/tests/web/sample.txt b/tests/web/sample.txt new file mode 100644 index 00000000..4fb76580 --- /dev/null +++ b/tests/web/sample.txt @@ -0,0 +1 @@ +I could read a file. diff --git a/tests/web/sync.test.ts b/tests/web/sync.test.ts new file mode 100644 index 00000000..5015589f --- /dev/null +++ b/tests/web/sync.test.ts @@ -0,0 +1,51 @@ +import { open } from "node:fs/promises"; +import path from "path"; +import { fileURLToPath } from "url"; +import actions from "./worker"; +import fail from "./workerWithError"; + +const client = actions.launch(); + +test("sync actinos", async () => { + expect(client.ping()).toBe("pong!?"); + expect(client.incr(3)).toBe(4); + expect(client.magic(0)).toBe(1); + expect(client.magic(1)).toBe(3); + expect(client.magic(2)).toBe(5); + expect(client.errorSample).toThrowError("errorSample"); + try { + client.myErrorTest(); + } catch (e) { + expect(e).toMatchObject({ name: "MyError", message: "myErrorTest", prop1: "foo" }); + } +}); + +test("sync FileHandle read", async () => { + const filepath = fileURLToPath(path.join(import.meta.url, "../sample.txt")); + const fileHandle = await open(filepath, "r"); + const arrayBuffer = client.readFile(fileHandle); + const textDecoder = new TextDecoder("utf-8"); + const text = textDecoder.decode(arrayBuffer); + expect(text).toMatch("I could read a file."); +}); + +test("sync File read", () => { + const fileContent = new Uint8Array([72, 101, 108, 108, 111]); // Binary data for "Hello" + const blob = new Blob([fileContent], { type: "text/plain" }); + const file = new File([blob], "example.txt", { type: "text/plain" }); + + const arrayBuffer = client.file(file); + const textDecoder = new TextDecoder("utf-8"); + const text = textDecoder.decode(arrayBuffer); + expect(text).toMatch("Hello"); +}); + +test("sync actinos with error", async () => { + fail.launch(); + await new Promise((resolve) => { + fail.getWorker()!.on("error", (error) => { + expect(error).toMatchObject({ message: "Sample error on launching worker." }); + resolve(); + }); + }); +}); diff --git a/tests/web/worker.ts b/tests/web/worker.ts new file mode 100644 index 00000000..57375f13 --- /dev/null +++ b/tests/web/worker.ts @@ -0,0 +1,39 @@ +import { defineSyncWorker } from "accel-record-core/dist/synclib/index.js"; +import { type FileHandle } from "fs/promises"; + +export class MyError extends Error { + name = "MyError"; + constructor( + message: string, + public prop1: string + ) { + super(message); + } +} + +let s = 0; +export default defineSyncWorker(import.meta.filename, { + incr: async (a: number) => a + 1, + magic: (t: number) => ++s + t, + ping: () => "pong!?", + readFile: async (fileHandle: FileHandle) => { + const stats = await fileHandle.stat(); + const buffer = Buffer.alloc(stats.size); + await fileHandle.read(buffer, 0, stats.size, 0); + const arrayBuffer = buffer.buffer.slice( + buffer.byteOffset, + buffer.byteOffset + buffer.byteLength + ); + return arrayBuffer; + }, + file: async (file: File) => { + const arrayBuffer = await file.arrayBuffer(); + return arrayBuffer; + }, + errorSample: () => { + throw new Error("errorSample"); + }, + myErrorTest: () => { + throw new MyError("myErrorTest", "foo"); + }, +}); diff --git a/tests/web/workerWithError.ts b/tests/web/workerWithError.ts new file mode 100644 index 00000000..efa10694 --- /dev/null +++ b/tests/web/workerWithError.ts @@ -0,0 +1,8 @@ +import { defineSyncWorker } from "accel-record-core/dist/synclib/index.js"; +import { workerData } from "worker_threads"; + +if (workerData.sharedBuffer) { + throw new Error("Sample error on launching worker."); +} + +export default defineSyncWorker(import.meta.filename, {});