Skip to content

Commit

Permalink
Merge pull request #1445 from polywrap/nerfzael-tests-for-embedded-wr…
Browse files Browse the repository at this point in the history
…appers

Tests for embedded wrappers
  • Loading branch information
dOrgJelli authored Dec 16, 2022
2 parents 3cefe9b + dfd0d60 commit 17a71cb
Show file tree
Hide file tree
Showing 7 changed files with 867 additions and 152 deletions.
173 changes: 173 additions & 0 deletions packages/js/client/src/__tests__/core/embedded-package.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import fs from "fs";
import path from "path";
import { buildWrapper } from "@polywrap/test-env-js";
import { GetPathToTestWrappers } from "@polywrap/test-cases";
import { UriResolver } from "@polywrap/uri-resolvers-js";
import { InMemoryFileReader, WasmPackage } from "@polywrap/wasm-js";
import { IWrapPackage, Uri } from "@polywrap/core-js";
import { Result, ResultErr, ResultOk } from "@polywrap/result";
import { PolywrapClient } from "../../PolywrapClient";

jest.setTimeout(200000);

const simpleWrapperPath = `${GetPathToTestWrappers()}/wasm-as/simple`;
const simpleWrapperUri = new Uri(`fs/${simpleWrapperPath}/build`);

describe("Embedded package", () => {
beforeAll(async () => {
await buildWrapper(simpleWrapperPath);
});

it("can invoke an embedded package", async () => {
const manifestBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.info"))
const wasmModuleBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.wasm"))

let wrapPackage = WasmPackage.from(manifestBuffer, wasmModuleBuffer);

const client = new PolywrapClient({
packages: [
{
uri: simpleWrapperUri,
package: wrapPackage
}
]
});

const result = await client.invoke<string>({
uri: simpleWrapperUri.uri,
method: "simpleMethod",
args: {
arg: "test",
},
});

if (!result.ok) fail(result.error);
expect(result.value).toBeTruthy();
expect(typeof result.value).toBe("string");
expect(result.value).toEqual("test");
});

it("can get a file from wrapper", async () => {
const manifestBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.info"))
const wasmModuleBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.wasm"))
const testFilePath = "hello.txt";
const testFileText = "Hello Test!";

const wrapPackage = WasmPackage.from(manifestBuffer, wasmModuleBuffer, {
readFile: async (filePath): Promise<Result<Uint8Array, Error>> => {
if (filePath === testFilePath) {
return ResultOk(Buffer.from(testFileText, "utf-8"));
} else {
return ResultErr(new Error(`File not found: ${filePath}`));
}
}
});

await testEmbeddedPackageWithFile(wrapPackage, testFilePath, testFileText);
});

it("can add embedded wrapper through file reader", async () => {
const manifestBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.info"))
const wasmModuleBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.wasm"))
const testFilePath = "hello.txt";
const testFileText = "Hello Test!";

const wrapPackage = WasmPackage.from({
readFile: async (filePath): Promise<Result<Uint8Array, Error>> => {
if (filePath === testFilePath) {
return ResultOk(Buffer.from(testFileText, "utf-8"));
} else if (filePath === "wrap.info") {
return ResultOk(manifestBuffer);
} else if (filePath === "wrap.wasm") {
return ResultOk(wasmModuleBuffer);
} else {
return ResultErr(new Error(`File not found: ${filePath}`));
}
}
});

await testEmbeddedPackageWithFile(wrapPackage, testFilePath, testFileText);
});

it("can add embedded wrapper with async wrap manifest", async () => {
const manifestBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.info"))
const wasmModuleBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.wasm"))
const testFilePath = "hello.txt";
const testFileText = "Hello Test!";

const wrapPackage = WasmPackage.from(
InMemoryFileReader.fromWasmModule(wasmModuleBuffer, {
readFile: async (filePath): Promise<Result<Uint8Array, Error>> => {
if (filePath === testFilePath) {
return ResultOk(Buffer.from(testFileText, "utf-8"));
} else if (filePath === "wrap.info") {
return ResultOk(manifestBuffer);
} else {
return ResultErr(new Error(`File not found: ${filePath}`));
}
}
})
);

await testEmbeddedPackageWithFile(wrapPackage, testFilePath, testFileText);
});

it("can add embedded wrapper with async wasm module", async () => {
const manifestBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.info"))
const wasmModuleBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.wasm"))
const testFilePath = "hello.txt";
const testFileText = "Hello Test!";

const wrapPackage = WasmPackage.from(manifestBuffer, {
readFile: async (filePath): Promise<Result<Uint8Array, Error>> => {
if (filePath === testFilePath) {
return ResultOk(Buffer.from(testFileText, "utf-8"));
} else if (filePath === "wrap.wasm") {
return ResultOk(wasmModuleBuffer);
} else {
return ResultErr(new Error(`File not found: ${filePath}`));
}
}
});

await testEmbeddedPackageWithFile(wrapPackage, testFilePath, testFileText);
});
});

const testEmbeddedPackageWithFile = async (wrapPackage: IWrapPackage, filePath: string, fileText: string) => {
const client = new PolywrapClient({
packages: [
{
uri: simpleWrapperUri,
package: wrapPackage
}
]
});

const expectedManifest =
await fs.promises.readFile(`${simpleWrapperPath}/build/wrap.info`);
const receivedManifestResult = await client.getFile(simpleWrapperUri, {
path: "wrap.info",
});
if (!receivedManifestResult.ok) fail(receivedManifestResult.error);
const receivedManifest = receivedManifestResult.value as Uint8Array;
expect(receivedManifest).toEqual(expectedManifest);

const expectedWasmModule =
await fs.promises.readFile(`${simpleWrapperPath}/build/wrap.wasm`);
const receivedWasmModuleResult = await client.getFile(simpleWrapperUri, {
path: "wrap.wasm",
});
if (!receivedWasmModuleResult.ok) fail(receivedWasmModuleResult.error);
const receivedWasmModule = receivedWasmModuleResult.value as Uint8Array;
expect(receivedWasmModule).toEqual(expectedWasmModule);

const receivedHelloFileResult = await client.getFile(simpleWrapperUri, {
path: filePath,
encoding: "utf-8",
});
if (!receivedHelloFileResult.ok) fail(receivedHelloFileResult.error);
const receivedHelloFile = receivedHelloFileResult.value as Uint8Array;

expect(receivedHelloFile).toEqual(fileText);
};
172 changes: 172 additions & 0 deletions packages/js/client/src/__tests__/core/embedded-wrapper.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import fs from "fs";
import path from "path";
import { buildWrapper } from "@polywrap/test-env-js";
import { GetPathToTestWrappers } from "@polywrap/test-cases";
import { WasmWrapper, InMemoryFileReader } from "@polywrap/wasm-js";
import { Uri, Wrapper } from "@polywrap/core-js";
import { Result, ResultErr, ResultOk } from "@polywrap/result";
import { PolywrapClient } from "../../PolywrapClient";

jest.setTimeout(200000);

const simpleWrapperPath = `${GetPathToTestWrappers()}/wasm-as/simple`;
const simpleWrapperUri = new Uri(`fs/${simpleWrapperPath}/build`);

describe("Embedded wrapper", () => {
beforeAll(async () => {
await buildWrapper(simpleWrapperPath);
});

it("can invoke an embedded wrapper", async () => {
const manifestBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.info"))
const wasmModuleBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.wasm"))

let wrapper: Wrapper = await WasmWrapper.from(manifestBuffer, wasmModuleBuffer);

const client = new PolywrapClient({
wrappers: [
{
uri: simpleWrapperUri,
wrapper
}
]
});

const result = await client.invoke<string>({
uri: simpleWrapperUri.uri,
method: "simpleMethod",
args: {
arg: "test",
},
});

if (!result.ok) fail(result.error);
expect(result.value).toBeTruthy();
expect(typeof result.value).toBe("string");
expect(result.value).toEqual("test");
});

it("can get a file from wrapper", async () => {
const manifestBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.info"))
const wasmModuleBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.wasm"))
const testFilePath = "hello.txt";
const testFileText = "Hello Test!";

const wrapper = await WasmWrapper.from(manifestBuffer, wasmModuleBuffer, {
readFile: async (filePath): Promise<Result<Uint8Array, Error>> => {
if (filePath === testFilePath) {
return ResultOk(Buffer.from(testFileText, "utf-8"));
} else {
return ResultErr(new Error(`File not found: ${filePath}`));
}
}
});

await testEmbeddedWrapperWithFile(wrapper, testFilePath, testFileText);
});

it("can add embedded wrapper through file reader", async () => {
const manifestBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.info"))
const wasmModuleBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.wasm"))
const testFilePath = "hello.txt";
const testFileText = "Hello Test!";

const wrapper = await WasmWrapper.from({
readFile: async (filePath): Promise<Result<Uint8Array, Error>> => {
if (filePath === testFilePath) {
return ResultOk(Buffer.from(testFileText, "utf-8"));
} else if (filePath === "wrap.info") {
return ResultOk(manifestBuffer);
} else if (filePath === "wrap.wasm") {
return ResultOk(wasmModuleBuffer);
} else {
return ResultErr(new Error(`File not found: ${filePath}`));
}
}
});

await testEmbeddedWrapperWithFile(wrapper, testFilePath, testFileText);
});

it("can add embedded wrapper with async wrap manifest", async () => {
const manifestBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.info"))
const wasmModuleBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.wasm"))
const testFilePath = "hello.txt";
const testFileText = "Hello Test!";

const wrapper = await WasmWrapper.from(
InMemoryFileReader.fromWasmModule(wasmModuleBuffer, {
readFile: async (filePath): Promise<Result<Uint8Array, Error>> => {
if (filePath === testFilePath) {
return ResultOk(Buffer.from(testFileText, "utf-8"));
} else if (filePath === "wrap.info") {
return ResultOk(manifestBuffer);
} else {
return ResultErr(new Error(`File not found: ${filePath}`));
}
}
})
);

await testEmbeddedWrapperWithFile(wrapper, testFilePath, testFileText);
});

it("can add embedded wrapper with async wasm module", async () => {
const manifestBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.info"))
const wasmModuleBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.wasm"))
const testFilePath = "hello.txt";
const testFileText = "Hello Test!";

const wrapper = await WasmWrapper.from(manifestBuffer, {
readFile: async (filePath): Promise<Result<Uint8Array, Error>> => {
if (filePath === testFilePath) {
return ResultOk(Buffer.from(testFileText, "utf-8"));
} else if (filePath === "wrap.wasm") {
return ResultOk(wasmModuleBuffer);
} else {
return ResultErr(new Error(`File not found: ${filePath}`));
}
}
});

await testEmbeddedWrapperWithFile(wrapper, testFilePath, testFileText);
});
});

const testEmbeddedWrapperWithFile = async (wrapper: WasmWrapper, filePath: string, fileText: string) => {
const client = new PolywrapClient({
wrappers: [
{
uri: simpleWrapperUri,
wrapper
}
]
});

const expectedManifest =
await fs.promises.readFile(`${simpleWrapperPath}/build/wrap.info`);
const receivedManifestResult = await client.getFile(simpleWrapperUri, {
path: "wrap.info",
});
if (!receivedManifestResult.ok) fail(receivedManifestResult.error);
const receivedManifest = receivedManifestResult.value as Uint8Array;
expect(receivedManifest).toEqual(expectedManifest);

const expectedWasmModule =
await fs.promises.readFile(`${simpleWrapperPath}/build/wrap.wasm`);
const receivedWasmModuleResult = await client.getFile(simpleWrapperUri, {
path: "wrap.wasm",
});
if (!receivedWasmModuleResult.ok) fail(receivedWasmModuleResult.error);
const receivedWasmModule = receivedWasmModuleResult.value as Uint8Array;
expect(receivedWasmModule).toEqual(expectedWasmModule);

const receivedHelloFileResult = await client.getFile(simpleWrapperUri, {
path: filePath,
encoding: "utf-8",
});
if (!receivedHelloFileResult.ok) fail(receivedHelloFileResult.error);
const receivedHelloFile = receivedHelloFileResult.value as Uint8Array;

expect(receivedHelloFile).toEqual(fileText);
};
11 changes: 11 additions & 0 deletions packages/js/core-client/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
collectCoverage: true,
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ["**/?(*.)+(spec|test).[jt]s?(x)"],
globals: {
'ts-jest': {
diagnostics: false
}
}
};
7 changes: 6 additions & 1 deletion packages/js/core-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
],
"scripts": {
"build": "rimraf ./build && tsc --project tsconfig.build.json",
"lint": "eslint --color -c ../../../.eslintrc.js src/"
"lint": "eslint --color -c ../../../.eslintrc.js src/",
"test": "jest --passWithNoTests --runInBand --verbose=true --detectOpenHandles --forceExit"
},
"dependencies": {
"@polywrap/core-js": "0.10.0-pre.5",
Expand All @@ -23,10 +24,14 @@
"@polywrap/wrap-manifest-types-js": "0.10.0-pre.5"
},
"devDependencies": {
"@polywrap/uri-resolvers-js": "0.10.0-pre.5",
"@types/uuid": "8.3.0",
"rimraf": "3.0.2",
"ts-loader": "8.0.17",
"ts-node": "8.10.2",
"@types/jest": "26.0.8",
"jest": "26.6.3",
"ts-jest": "26.5.4",
"typescript": "4.1.6"
},
"gitHead": "7346adaf5adb7e6bbb70d9247583e995650d390a",
Expand Down
Loading

0 comments on commit 17a71cb

Please sign in to comment.