Skip to content

Commit

Permalink
tests for sync-actions
Browse files Browse the repository at this point in the history
  • Loading branch information
koyopro committed Dec 11, 2024
1 parent bdc3dc3 commit 6bb3d05
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tests/web/launchedWorker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { actions, getWorker } from "./launchedWorker";

test("sync actinos", () => {
expect(actions.ping()).toBe("pong!?");
getWorker()!.terminate();
});
5 changes: 5 additions & 0 deletions tests/web/launchedWorker.ts
Original file line number Diff line number Diff line change
@@ -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!?",
});
1 change: 1 addition & 0 deletions tests/web/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I could read a file.
51 changes: 51 additions & 0 deletions tests/web/sync.test.ts
Original file line number Diff line number Diff line change
@@ -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<void>((resolve) => {
fail.getWorker()!.on("error", (error) => {
expect(error).toMatchObject({ message: "Sample error on launching worker." });
resolve();
});
});
});
39 changes: 39 additions & 0 deletions tests/web/worker.ts
Original file line number Diff line number Diff line change
@@ -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");
},
});
8 changes: 8 additions & 0 deletions tests/web/workerWithError.ts
Original file line number Diff line number Diff line change
@@ -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, {});

0 comments on commit 6bb3d05

Please sign in to comment.