// test/SafeAuction.test.js
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("SafeAuction", function () {
let auction;
let owner, bidder1, bidder2;
beforeEach(async function () {
[owner, bidder1, bidder2] = await ethers.getSigners();
const Auction = await ethers.getContractFactory("SafeAuction");
auction = await Auction.deploy();
});
describe("State transitions", function () {
it("Should not allow bidding before auction starts", async function () {
await expect(
auction.connect(bidder1).placeBid({ value: ethers.utils.parseEther("1") })
).to.be.revertedWith("Invalid state");
});
it("Should transition to Active state", async function () {
await auction.startAuction();
expect(await auction.state()).to.equal(1); // Active
});
});
describe("Bid placement", function () {
beforeEach(async function () {
await auction.startAuction();
});
it("Should accept valid bids", async function () {
const bidAmount = ethers.utils.parseEther("1");
await auction.connect(bidder1).placeBid({ value: bidAmount });
expect(await auction.bids(bidder1.address)).to.equal(bidAmount);
});
it("Should reject bids after auction ends", async function () {
// 快进时间超过拍卖期限
await ethers.provider.send("evm_increaseTime", [7 * 24 * 60 * 60 + 1]);
await ethers.provider.send("evm_mine");
await expect(
auction.connect(bidder1).placeBid({ value: ethers.utils.parseEther("1") })
).to.be.revertedWith("Auction has ended");
});
});
describe("Attack vectors", function () {
it("Should prevent reentrancy in settlement", async function () {
// 部署攻击合约并测试
const AttackFactory = await ethers.getContractFactory("ReentrancyAttack");
const attack = await AttackFactory.deploy(auction.address);
// 测试代码
});
});
describe("Edge cases", function () {
it("Should handle zero amount bids", async function () {
await auction.startAuction();
await expect(
auction.connect(bidder1).placeBid({ value: 0 })
).to.be.revertedWith("Bid must be positive");
});
it("Should handle uint256 overflow", async function () {
// 测试边界值
await auction.startAuction();
const maxUint = ethers.constants.MaxUint256;
// 测试逻辑
});
});
});