Skip to content

Commit

Permalink
Create StateChannel.test.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Oct 20, 2024
1 parent 8ec2a67 commit 5e2addf
Showing 1 changed file with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// StateChannel.test.js
const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("StateChannel Contract", function () {
let StateChannel;
let stateChannel;
let ChannelManager;
let channelManager;
let owner;
let participant1;
let participant2;

beforeEach(async function () {
[owner, participant1, participant2] = await ethers.getSigners();

ChannelManager = await ethers.getContractFactory("ChannelManager");
channelManager = await ChannelManager.deploy();
await channelManager.deployed();

StateChannel = await ethers.getContractFactory("StateChannel");
stateChannel = await StateChannel.deploy(channelManager.address, participant1.address, participant2.address);
await stateChannel.deployed();
});

it("should open a channel successfully", async function () {
await stateChannel.connect(participant1).openChannel();
const isOpen = await stateChannel.isOpen();
expect(isOpen).to.be.true;
});

it("should allow state updates", async function () {
await stateChannel.connect(participant1).openChannel();
const newState = ethers.utils.keccak256(ethers.utils.toUtf8Bytes("New State"));
await stateChannel.connect(participant1).updateState(newState);

const currentState = await stateChannel.getCurrentState();
expect(currentState).to.equal(newState);
});

it("should close the channel successfully", async function () {
await stateChannel.connect(participant1).openChannel();
await stateChannel.connect(participant1).closeChannel();

const isOpen = await stateChannel.isOpen();
expect(isOpen).to.be.false;
});

it("should revert when trying to update state after closing the channel", async function () {
await stateChannel.connect(participant1).openChannel();
await stateChannel.connect(participant1).closeChannel();

const newState = ethers.utils.keccak256(ethers.utils.toUtf8Bytes("New State"));
await expect(stateChannel.connect(participant1).updateState(newState)).to.be.revertedWith("Channel is closed");
});
});

0 comments on commit 5e2addf

Please sign in to comment.