Skip to content

Commit

Permalink
chore(test): add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Oct 9, 2024
1 parent a8b8477 commit 3ba2bce
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
60 changes: 60 additions & 0 deletions spec/arns_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,51 @@ describe("arns", function()
end)
end)

describe("pruneAuctions", function()
it("should remove expired auctions", function()
local currentTimestamp = 1000000
_G.NameRegistry.auctions = {
["active-auction"] = {
name = "active-auction",
type = "permabuy",
startPrice = 1000000000,
floorPrice = 100000000,
startTimestamp = currentTimestamp,
endTimestamp = currentTimestamp + 1000000, -- far in the future
},
["expired-auction"] = {
name = "expired-auction",
type = "permabuy",
startPrice = 1000000000,
floorPrice = 100000000,
startTimestamp = currentTimestamp,
endTimestamp = currentTimestamp - 1000, -- expired
},
}
local prunedAuctions = arns.pruneAuctions(currentTimestamp)
assert.are.same({
["expired-auction"] = {
name = "expired-auction",
type = "permabuy",
startPrice = 1000000000,
floorPrice = 100000000,
startTimestamp = currentTimestamp,
endTimestamp = currentTimestamp - 1000, -- expired
},
}, prunedAuctions)
assert.are.same({
["active-auction"] = {
name = "active-auction",
type = "permabuy",
startPrice = 1000000000,
floorPrice = 100000000,
startTimestamp = currentTimestamp,
endTimestamp = currentTimestamp + 1000000, -- far in the future
},
}, _G.NameRegistry.auctions)
end)
end)

describe("getRegistrationFees", function()
it("should return the correct registration prices", function()
local registrationFees = arns.getRegistrationFees()
Expand Down Expand Up @@ -631,6 +676,21 @@ describe("arns", function()
assert.match("Auction does not exist", error)
end)

it("should throw an error if the bid is not high enough", function()
local startTimestamp = 1000000
local auction = arns.createAuction("test-name", "permabuy", startTimestamp, "test-initiator")
local status, error = pcall(
arns.submitAuctionBid,
"test-name",
auction.startPrice - 1,
testAddressArweave,
startTimestamp,
"test-process-id"
)
assert.is_false(status)
assert.match("Bid amount is less than the required bid of " .. auction.startPrice, error)
end)

it("should throw an error if the bidder does not have enough balance", function()
local startTimestamp = 1000000
local auction = arns.createAuction("test-name", "permabuy", startTimestamp, "test-initiator")
Expand Down
8 changes: 4 additions & 4 deletions src/arns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,12 @@ function arns.submitAuctionBid(name, bidAmount, bidder, timestamp, processId)
error("Auction does not exist")
end
local requiredBid = arns.getCurrentBidPriceForAuction(auction, timestamp)

if bidAmount < requiredBid then
error("Bid amount is less than the required bid")
local requiredOrBidAmount = bidAmount or requiredBid
if requiredOrBidAmount < requiredBid then
error("Bid amount is less than the required bid of " .. requiredBid)
end

local finalBidAmount = math.min(bidAmount, requiredBid)
local finalBidAmount = math.min(requiredOrBidAmount, requiredBid)

-- check the balance of the bidder
local bidderBalance = balances.getBalance(bidder)
Expand Down

0 comments on commit 3ba2bce

Please sign in to comment.