Skip to content

Commit

Permalink
[SDK] Test coverage TOOL-2241 (#5826)
Browse files Browse the repository at this point in the history
Holiday project.
Basically trying to improve test coverage, especially in those low-haning-fruit utils files

<!-- start pr-codex -->

---

## PR-Codex overview
This PR focuses on enhancing documentation, improving error handling, and adding tests across various modules in the codebase. It also corrects minor typos and ensures better code readability.

### Detailed summary
- Added `@internal` documentation comments to `Queue` and `convertViemChain`.
- Updated test cases to use `await expect(...)` for consistency.
- Fixed typos in file imports and function names.
- Introduced new tests for `namehash`, `encodeLabelhash`, and `parseNFT`.
- Enhanced error handling in `parseNftUri`.
- Improved the `toSemver` function and its tests.
- Added tests for `replaceBigInts` and `toHex` functions.

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}`

<!-- end pr-codex -->
  • Loading branch information
kien-ngo committed Dec 28, 2024
1 parent a7b6cb3 commit 03bf859
Show file tree
Hide file tree
Showing 33 changed files with 1,442 additions and 29 deletions.
6 changes: 3 additions & 3 deletions packages/thirdweb/src/auth/verify-hash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("verifyHash", async () => {
privateKey: ANVIL_PKEY_A,
});

expect(
await expect(
verifyHash({
address: TEST_ACCOUNT_A.address,
hash: hashMessage("hello world"),
Expand All @@ -33,7 +33,7 @@ describe("verifyHash", async () => {
privateKey: ANVIL_PKEY_A,
});

expect(
await expect(
verifyHash({
address: TEST_ACCOUNT_A.address,
hash: hashMessage("hello world"),
Expand All @@ -50,7 +50,7 @@ describe("verifyHash", async () => {
privateKey: ANVIL_PKEY_A,
});

expect(
await expect(
verifyHash({
address: TEST_ACCOUNT_A.address,
hash: hashMessage("hello world"),
Expand Down
71 changes: 70 additions & 1 deletion packages/thirdweb/src/chains/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import { toSerializableTransaction } from "../transaction/actions/to-serializabl
import { privateKeyToAccount } from "../wallets/private-key.js";
import { avalanche } from "./chain-definitions/avalanche.js";
import { ethereum } from "./chain-definitions/ethereum.js";
import type { LegacyChain } from "./types.js";
import type { ChainMetadata, LegacyChain } from "./types.js";

import { base } from "viem/chains";
import {
CUSTOM_CHAIN_MAP,
cacheChains,
convertApiChainToChain,
convertLegacyChain,
convertViemChain,
defineChain,
getCachedChain,
getChainDecimals,
Expand Down Expand Up @@ -237,4 +240,70 @@ describe("defineChain", () => {
cacheChains([scroll]);
expect(CUSTOM_CHAIN_MAP.get(scroll.id)).toStrictEqual(scroll);
});

it("Chain converted from viem should have the blockExplorers being an array", () => {
expect(Array.isArray(convertViemChain(base).blockExplorers)).toBe(true);
});

it("convertApiChainToChain should work", () => {
const ethChain: ChainMetadata = {
chainId: 1,
name: "Ethereum Mainnet",
chain: "ETH",
shortName: "eth",
icon: {
url: "ipfs://QmcxZHpyJa8T4i63xqjPYrZ6tKrt55tZJpbXcjSDKuKaf9/ethereum/512.png",
width: 512,
height: 512,
format: "png",
},
nativeCurrency: {
name: "Ether",
symbol: "ETH",
decimals: 18,
},
ens: {
registry: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
},
explorers: [
{
name: "etherscan",
url: "https://etherscan.io",
standard: "EIP3091",
},
],
rpc: ["https://1.rpc.thirdweb.com/${THIRDWEB_API_KEY}"],
testnet: false,
infoURL: "https://ethereum.org",
slug: "ethereum",
networkId: 1,
stackType: "l1",
};

expect(convertApiChainToChain(ethChain)).toStrictEqual({
blockExplorers: [
{
apiUrl: "https://etherscan.io",
name: "etherscan",
url: "https://etherscan.io",
},
],
faucets: undefined,
icon: {
format: "png",
height: 512,
url: "ipfs://QmcxZHpyJa8T4i63xqjPYrZ6tKrt55tZJpbXcjSDKuKaf9/ethereum/512.png",
width: 512,
},
id: 1,
name: "Ethereum Mainnet",
nativeCurrency: {
decimals: 18,
name: "Ether",
symbol: "ETH",
},
rpc: "https://1.rpc.thirdweb.com/${THIRDWEB_API_KEY}",
testnet: undefined,
});
});
});
5 changes: 4 additions & 1 deletion packages/thirdweb/src/chains/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ function isViemChain(
return "rpcUrls" in chain && !("rpc" in chain);
}

function convertViemChain(viemChain: ViemChain): Chain {
/**
* @internal
*/
export function convertViemChain(viemChain: ViemChain): Chain {
const RPC_URL = getThirdwebDomains().rpc;
return {
id: viemChain.id,
Expand Down
42 changes: 42 additions & 0 deletions packages/thirdweb/src/contract/contract.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { describe, expect, it } from "vitest";
import { TEST_CLIENT } from "~test/test-clients.js";
import { USDT_CONTRACT_ADDRESS } from "~test/test-contracts.js";
import { ethereum } from "../chains/chain-definitions/ethereum.js";
import { getContract } from "./contract.js";

describe("Contract - getContract", () => {
it("should throw error if client is not passed", () => {
expect(() =>
// @ts-ignore Test purpose
getContract({ address: "0x", chain: ethereum }),
).toThrowError(
`getContract validation error - invalid client: ${undefined}`,
);
});

it("should throw error if address is not valid", () => {
expect(() =>
getContract({ address: "0x", chain: ethereum, client: TEST_CLIENT }),
).toThrowError("getContract validation error - invalid address: 0x");
});

it("should throw error if chain is not passed", () => {
expect(() =>
// @ts-ignore Test purpose
getContract({ address: USDT_CONTRACT_ADDRESS, client: TEST_CLIENT }),
).toThrowError(
`getContract validation error - invalid chain: ${undefined}`,
);
});

it("should throw error if chain doesn't have id", () => {
expect(() =>
getContract({
address: USDT_CONTRACT_ADDRESS,
client: TEST_CLIENT,
// @ts-ignore Test
chain: {},
}),
).toThrowError(`getContract validation error - invalid chain: ${{}}`);
});
});
2 changes: 1 addition & 1 deletion packages/thirdweb/src/exports/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// bytecode
export { detectMethod } from "../utils/bytecode/detectExtension.js";
export { extractIPFSUri } from "../utils/bytecode/extractIPFS.js";
export { extractMinimalProxyImplementationAddress } from "../utils/bytecode/extractMnimalProxyImplementationAddress.js";
export { extractMinimalProxyImplementationAddress } from "../utils/bytecode/extractMinimalProxyImplementationAddress.js";
export { isContractDeployed } from "../utils/bytecode/is-contract-deployed.js";
export { ensureBytecodePrefix } from "../utils/bytecode/prefix.js";
export { resolveImplementation } from "../utils/bytecode/resolveImplementation.js";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe.runIf(process.env.TW_SECRET_KEY).sequential("publishContract", () => {

expect(publishedContracts.length).toBe(1);

expect(
await expect(
sendAndConfirmTransaction({
account: TEST_ACCOUNT_D,
transaction: publishContract({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { describe, expect, it } from "vitest";
import { CADIcon } from "../../../icons/currencies/CADIcon.js";
import { EURIcon } from "../../../icons/currencies/EURIcon.js";
import { GBPIcon } from "../../../icons/currencies/GBPIcon.js";
import { JPYIcon } from "../../../icons/currencies/JPYIcon.js";
import { USDIcon } from "../../../icons/currencies/USDIcon.js";
import { currencies, getCurrencyMeta, usdCurrency } from "./currencies.js";

describe("Currency Utilities", () => {
it("should have correct number of currencies", () => {
expect(currencies.length).toBe(5);
});

it("should have USD as the first currency", () => {
expect(currencies[0]).toEqual(usdCurrency);
});

it("should have correct properties for each currency", () => {
for (const currency of currencies) {
expect(currency).toHaveProperty("shorthand");
expect(currency).toHaveProperty("name");
expect(currency).toHaveProperty("icon");
}
});

describe("getCurrencyMeta function", () => {
it("should return correct currency meta for valid shorthand", () => {
const cadMeta = getCurrencyMeta("CAD");
expect(cadMeta.shorthand).toBe("CAD");
expect(cadMeta.name).toBe("Canadian Dollar");
expect(cadMeta.icon).toBe(CADIcon);
});

it("should be case-insensitive", () => {
const eurMeta = getCurrencyMeta("eur");
expect(eurMeta.shorthand).toBe("EUR");
expect(eurMeta.name).toBe("Euro");
expect(eurMeta.icon).toBe(EURIcon);
});

it("should return unknown currency for invalid shorthand", () => {
const unknownMeta = getCurrencyMeta("XYZ");
expect(unknownMeta.shorthand).toBe("XYZ");
expect(unknownMeta.name).toBe("XYZ");
expect(unknownMeta.icon).not.toBe(USDIcon);
expect(unknownMeta.icon).not.toBe(CADIcon);
expect(unknownMeta.icon).not.toBe(GBPIcon);
expect(unknownMeta.icon).not.toBe(EURIcon);
expect(unknownMeta.icon).not.toBe(JPYIcon);
});
});
});
Loading

0 comments on commit 03bf859

Please sign in to comment.