Skip to content

Commit

Permalink
Migrate test case to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
hieu-w committed Dec 31, 2024
1 parent 2c2a819 commit c715fed
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 59 deletions.
19 changes: 0 additions & 19 deletions test/index.html

This file was deleted.

83 changes: 43 additions & 40 deletions test/test.ts → test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { generatePrivate } from "@toruslabs/eccrypto";
import assert from "assert";
import { ec as EC } from "elliptic";
import { describe, expect, it } from "vitest";

import { MetadataStorageLayer } from "../src/MetadataStorageLayer";
import { keccak256 } from "../src/utils";
Expand All @@ -16,26 +16,26 @@ const privKey2 = generatePrivate();
const keyPair2 = ec.keyFromPrivate(privKey2);
const pubKey2 = keyPair2.getPublic();

describe("Metadata", function () {
describe("Metadata", () => {
let randomMessage: string;

it("should get nothing by default", async function () {
it("should get nothing by default", async () => {
const res = await storage.getMetadata({ pub_key_X: pubKey.getX().toString(16), pub_key_Y: pubKey.getY().toString(16) }, null);
assert.strictEqual(res, "");
expect(res).toBe("");
});

it("should set", async function () {
it("should set and get", async () => {
// Set metadata
randomMessage = JSON.stringify({ message: keccak256(Buffer.from(Date.now().toString(), "utf-8")).toString("hex") });
const params = storage.generateMetadataParams(randomMessage, privKey.toString("hex"));
await storage.setMetadata(params, "metadata-test");
});

it("should get", async function () {
// Get and verify metadata
const message = await storage.getMetadata(storage.generatePubKeyParams(privKey.toString("hex")), "metadata-test");
assert.strictEqual(message, randomMessage);
expect(message).toBe(randomMessage);
});

it("should set and get WebAuthn Torus Share", async function () {
it("should set and get WebAuthn Torus Share", async () => {
await setTorusShare(
storage,
{ pub_key_X: pubKey2.getX().toString(16), pub_key_Y: pubKey2.getY().toString(16) },
Expand All @@ -44,20 +44,19 @@ describe("Metadata", function () {
"customTorusShare"
);
const googleShare = await getTorusShare<string>(storage, privKey2.toString("hex"), privKey.toString("hex"), "google");
assert.strictEqual(googleShare, "customTorusShare");
expect(googleShare).toBe("customTorusShare");
});

it("should set and get WebAuthn Device Share", async function () {
it("should set and get WebAuthn Device Share", async () => {
let googleShare = await getDeviceShare<string>(storage, privKey.toString("hex"), "google");
if (googleShare) {
throw new Error("get Torus share should have nothing");
}
expect(googleShare).toBeNull();

await setDeviceShare(storage, privKey.toString("hex"), "google", "customDeviceShare");
googleShare = await getDeviceShare<string>(storage, privKey.toString("hex"), "google");
assert.strictEqual(googleShare, "customDeviceShare");
expect(googleShare).toBe("customDeviceShare");
});

it("should set and get multiple WebAuthn Torus Shares", async function () {
it("should set and get multiple WebAuthn Torus Shares", async () => {
const subspaces = ["facebook", "twitter", "github"];
const shares = ["fbShare", "twitterShare", "githubShare"];

Expand All @@ -75,16 +74,16 @@ describe("Metadata", function () {
// Get and verify shares for each subspace
for (let i = 0; i < subspaces.length; i++) {
const retrievedShare = await getTorusShare<string>(storage, privKey2.toString("hex"), privKey.toString("hex"), subspaces[i]);
assert.strictEqual(retrievedShare, shares[i], `Share for ${subspaces[i]} should match`);
expect(retrievedShare).toBe(shares[i]);
}
});

it("should handle non-existent WebAuthn Torus Share", async function () {
it("should handle non-existent WebAuthn Torus Share", async () => {
const nonExistentShare = await getTorusShare<string>(storage, privKey2.toString("hex"), privKey.toString("hex"), "nonexistent");
assert.strictEqual(nonExistentShare, null, "Non-existent share should return null");
expect(nonExistentShare).toBeNull();
});

it("should update existing WebAuthn Torus Share", async function () {
it("should update existing WebAuthn Torus Share", async () => {
const initialShare = "initialShare";
const updatedShare = "updatedShare";
const subspace = "updateTest";
Expand All @@ -98,7 +97,7 @@ describe("Metadata", function () {
);

let retrievedShare = await getTorusShare<string>(storage, privKey2.toString("hex"), privKey.toString("hex"), subspace);
assert.strictEqual(retrievedShare, initialShare, "Initial share should be set correctly");
expect(retrievedShare).toBe(initialShare);

await setTorusShare(
storage,
Expand All @@ -109,10 +108,10 @@ describe("Metadata", function () {
);

retrievedShare = await getTorusShare<string>(storage, privKey2.toString("hex"), privKey.toString("hex"), subspace);
assert.strictEqual(retrievedShare, updatedShare, "Share should be updated");
expect(retrievedShare).toBe(updatedShare);
});

it("should handle multiple subspaces with different data types", async function () {
it("should handle multiple subspaces with different data types", async () => {
const subspaces = ["stringSpace", "numberSpace", "objectSpace"];
const shares = ["testString", 42, { key: "value" }];

Expand All @@ -128,11 +127,11 @@ describe("Metadata", function () {

for (let i = 0; i < subspaces.length; i++) {
const retrievedShare = await getTorusShare(storage, privKey2.toString("hex"), privKey.toString("hex"), subspaces[i]);
assert.deepStrictEqual(retrievedShare, shares[i], `Share for ${subspaces[i]} should match and maintain its data type`);
expect(retrievedShare).toEqual(shares[i]);
}
});

it("should handle empty string as share data", async function () {
it("should handle empty string as share data", async () => {
const emptyShare = "";
const subspace = "emptySpace";

Expand All @@ -145,23 +144,27 @@ describe("Metadata", function () {
);

const retrievedShare = await getTorusShare<string>(storage, privKey2.toString("hex"), privKey.toString("hex"), subspace);
assert.strictEqual(retrievedShare, emptyShare, "Empty string share should be retrieved correctly");
expect(retrievedShare).toBe(emptyShare);
});

it("should handle large data in WebAuthn Torus Share", async function () {
const largeData = "x".repeat(1000000); // 1MB of data
const subspace = "largeDataSpace";
it(
"should handle large data in WebAuthn Torus Share",
async () => {
const largeData = "x".repeat(1000000); // 1MB of data
const subspace = "largeDataSpace";

await setTorusShare(
storage,
{ pub_key_X: pubKey2.getX().toString(16), pub_key_Y: pubKey2.getY().toString(16) },
privKey.toString("hex"),
subspace,
largeData
);
await setTorusShare(
storage,
{ pub_key_X: pubKey2.getX().toString(16), pub_key_Y: pubKey2.getY().toString(16) },
privKey.toString("hex"),
subspace,
largeData
);

const retrievedShare = await getTorusShare<string>(storage, privKey2.toString("hex"), privKey.toString("hex"), subspace);
assert.strictEqual(retrievedShare, largeData, "Large data should be retrieved correctly");
assert.strictEqual(retrievedShare?.length, 1000000, "Retrieved data should maintain its size");
});
const retrievedShare = await getTorusShare<string>(storage, privKey2.toString("hex"), privKey.toString("hex"), subspace);
expect(retrievedShare).toBe(largeData);
expect(retrievedShare?.length).toBe(1000000);
},
{ timeout: 60000 }
);
});

0 comments on commit c715fed

Please sign in to comment.