Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add test for utils #1531

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/amino/src/encoding.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Random } from "@cosmjs/crypto";
import { fromBase64, fromBech32, fromHex } from "@cosmjs/encoding";
import { TSError } from "ts-node";

import {
decodeAminoPubkey,
Expand Down Expand Up @@ -131,6 +132,17 @@ describe("encoding", () => {

expect(() => decodeAminoPubkey(fromHex("22C1F7E20705"))).toThrowError(/expecting 0x08 prefix/i);
});

it("throws error for invalid secp256k1 pubkey length", () => {
const data = new Uint8Array([0, 1, 2, 3, 4]);
try {
decodeAminoPubkey(data);
} catch (error) {
expect((error as TSError).message).toBe(
"Unsupported public key type. Amino data starts with: 0001020304",
);
}
});
});

describe("decodeBech32Pubkey", () => {
Expand Down
44 changes: 43 additions & 1 deletion packages/amino/src/secp256k1hdwallet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Secp256k1, Secp256k1Signature, sha256 } from "@cosmjs/crypto";
import { fromBase64, fromHex } from "@cosmjs/encoding";

import { makeCosmoshubPath } from "./paths";
import { extractKdfConfiguration, Secp256k1HdWallet } from "./secp256k1hdwallet";
import { extractKdfConfiguration, isDerivationJson, Secp256k1HdWallet } from "./secp256k1hdwallet";
import { serializeSignDoc, StdSignDoc } from "./signdoc";
import { base64Matcher } from "./testutils.spec";
import { executeKdf, KdfConfiguration } from "./wallet";
Expand Down Expand Up @@ -311,4 +311,46 @@ describe("Secp256k1HdWallet", () => {
});
});
});

describe("isDerivationJson function", () => {
it("returns true for valid DerivationInfoJson", () => {
const validInput: unknown = {
hdPath: "validHdPath",
prefix: "validPrefix",
};
expect(isDerivationJson(validInput)).toBe(true);
});

it("returns false for undefined input", () => {
expect(isDerivationJson(undefined)).toBe(false);
});

it("returns false for null input", () => {
expect(isDerivationJson(null)).toBe(false);
});

it("returns false for non-object input", () => {
expect(isDerivationJson(42)).toBe(false);
});

it("returns false for missing hdPath property", () => {
const missingHdPath: unknown = {};
expect(isDerivationJson(missingHdPath)).toBe(false);
});

it("returns false for missing prefix property", () => {
const missingPrefix: unknown = {
hdPath: "validHdPath",
};
expect(isDerivationJson(missingPrefix)).toBe(false);
});

it("returns false for incorrect hdPath type", () => {
const incorrectHdPathType: unknown = {
hdPath: 123,
prefix: "validPrefix",
};
expect(isDerivationJson(incorrectHdPathType)).toBe(false);
});
});
});
2 changes: 1 addition & 1 deletion packages/amino/src/secp256k1hdwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ interface DerivationInfoJson {
readonly prefix: string;
}

function isDerivationJson(thing: unknown): thing is DerivationInfoJson {
export function isDerivationJson(thing: unknown): thing is DerivationInfoJson {
if (!isNonNullObject(thing)) return false;
if (typeof (thing as DerivationInfoJson).hdPath !== "string") return false;
if (typeof (thing as DerivationInfoJson).prefix !== "string") return false;
Expand Down
21 changes: 20 additions & 1 deletion packages/cli/src/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TSError } from "ts-node";
import { createContext } from "vm";

import { executeJavaScript, executeJavaScriptAsync } from "./helpers";
import { executeJavaScript, executeJavaScriptAsync, isRecoverable } from "./helpers";

describe("Helpers", () => {
describe("executeJavaScript", () => {
Expand Down Expand Up @@ -144,4 +145,22 @@ describe("Helpers", () => {
expect(await executeJavaScriptAsync(code, "myfile.js", context)).toEqual("job done");
});
});

describe("isRecoverable", () => {
it("should return true for recoverable errors", () => {
const recoverableError = {
diagnosticCodes: [1003, 1160, 2355],
} as TSError;

expect(isRecoverable(recoverableError)).toBe(true);
});

it("should return false for non-recoverable errors", () => {
const nonRecoverableError = {
diagnosticCodes: [1234, 5678],
} as TSError;

expect(isRecoverable(nonRecoverableError)).toBe(false);
});
});
});