Skip to content

Commit

Permalink
Add some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
r-near committed Dec 13, 2024
1 parent 3258241 commit 3f8bca7
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 123 deletions.
123 changes: 0 additions & 123 deletions result.json

This file was deleted.

124 changes: 124 additions & 0 deletions tests/chains/near.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import type { Account } from "near-api-js"
import { beforeEach, describe, expect, it, vi } from "vitest"
import { NearDeployer } from "../../src/chains/near"
import { ChainKind } from "../../src/types"

// Mock the entire borsher module
vi.mock("borsher", () => ({
borshSerialize: vi.fn().mockReturnValue(new Uint8Array([1, 2, 3])),
BorshSchema: {
Enum: vi.fn().mockReturnValue({}),
Unit: {},
String: vi.fn().mockReturnValue({}),
Struct: vi.fn().mockReturnValue({}),
Option: vi.fn().mockReturnValue({}),
Vec: vi.fn().mockReturnValue({}),
// Add any other schema types you're using
},
}))

describe("NearDeployer", () => {
let mockWallet: Account
let deployer: NearDeployer
const mockLockerAddress = "test.near"
const mockTxHash = "mock-tx-hash"

beforeEach(() => {
// Create mock wallet with functionCall method
mockWallet = {
functionCall: vi.fn().mockResolvedValue({
transaction: {
hash: mockTxHash,
},
}),
} as unknown as Account

// Create deployer instance
deployer = new NearDeployer(mockWallet, mockLockerAddress)
})

describe("constructor", () => {
it("should throw error if locker address is not provided", () => {
expect(() => new NearDeployer(mockWallet, "")).toThrow(
"OMNI_LOCKER_NEAR address not configured",
)
})

it("should create instance with provided wallet and locker address", () => {
const deployer = new NearDeployer(mockWallet, mockLockerAddress)
expect(deployer).toBeInstanceOf(NearDeployer)
})
})

describe("initDeployToken", () => {
it("should throw error if token address is not on NEAR", async () => {
await expect(deployer.initDeployToken("eth:0x123")).rejects.toThrow(
"Token address must be on NEAR",
)
})

it("should call log_metadata with correct arguments", async () => {
const tokenAddress = "near:test-token.near"
const txHash = await deployer.initDeployToken(tokenAddress)

expect(mockWallet.functionCall).toHaveBeenCalledWith({
contractId: mockLockerAddress,
methodName: "log_metadata",
args: {
token_id: "test-token.near",
},
gas: BigInt(3e14),
attachedDeposit: BigInt(2e23),
})
expect(txHash).toBe(mockTxHash)
})
})

describe("finDeployToken", () => {
it("should call deploy_token with correct arguments", async () => {
const destinationChain = ChainKind.Eth
const mockVaa = "mock-vaa"

const txHash = await deployer.finDeployToken(destinationChain, mockVaa)

expect(mockWallet.functionCall).toHaveBeenCalledWith({
contractId: mockLockerAddress,
methodName: "deploy_token",
args: expect.any(Uint8Array), // We can't easily check the exact serialized value
gas: BigInt(1.2e14),
attachedDeposit: BigInt(4e24),
})
expect(txHash).toBe(mockTxHash)
})
})

describe("bindToken", () => {
it("should call bind_token with correct arguments", async () => {
const destinationChain = ChainKind.Eth
const mockVaa = "mock-vaa"

const txHash = await deployer.bindToken(destinationChain, mockVaa)

expect(mockWallet.functionCall).toHaveBeenCalledWith({
contractId: mockLockerAddress,
methodName: "bind_token",
args: {
chain_kind: destinationChain,
prover_args: expect.any(Uint8Array),
},
gas: BigInt(3e14),
attachedDeposit: BigInt(2e23),
})
expect(txHash).toBe(mockTxHash)
})
})

describe("error handling", () => {
it("should propagate errors from functionCall", async () => {
const error = new Error("NEAR error")
mockWallet.functionCall = vi.fn().mockRejectedValue(error)

await expect(deployer.initDeployToken("near:test-token.near")).rejects.toThrow("NEAR error")
})
})
})

0 comments on commit 3f8bca7

Please sign in to comment.