-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
124 additions
and
123 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
}) | ||
}) |