Skip to content

Commit

Permalink
chore(test): fix validation and update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Oct 15, 2024
1 parent 65c1c8a commit 347c742
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
25 changes: 2 additions & 23 deletions spec/ant_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,11 @@ local json = require("src.common.json")

local fake_address = "1111111111111111111111111111111111111111111"

_G.ao = {
send = function()
return true
end,
id = "test",
}
_G.Balances = { [fake_address] = 1 }
_G.Records = {}
_G.Controllers = { fake_address }
_G.Name = "Arweave Name Token"
_G.Ticker = "ANT"
_G.Logo = "LOGO"
_G.Denomination = 1

os.clock = function()
return 0
end

local originalState = {
name = "Arweave Name Token",
ticker = "ANT",
controllers = { fake_address },
records = { ["@"] = { transactionId = "test", ttlSeconds = 900 } },
records = { ["@"] = { transactionId = fake_address, ttlSeconds = 900 } },
balances = { [fake_address] = 1 },
owner = fake_address,
}
Expand All @@ -40,12 +22,9 @@ describe("Arweave Name Token", function()
_G.Controllers = { fake_address }
_G.Name = "Arweave Name Token"
_G.Ticker = "ANT"
_G.Denomination = 1
end)

setup(function() end)

teardown(function() end)

it("Initializes the state of the process", function()
initialize.initializeANTState(json.encode(originalState)) -- happy

Expand Down
16 changes: 15 additions & 1 deletion spec/utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("utils.validateArweaveId", function()
it("should throw an error for invalid Arweave IDs", function()
local invalid, error = pcall(utils.validateArweaveId, "invalid-arweave-id-123")
assert.is_false(invalid)
assert.is_equal(error, "Invalid Arweave ID")
assert.is_not_nil(error)
end)

it("should not throw an error for a valid Arweave ID", function()
Expand All @@ -55,3 +55,17 @@ describe("utils.validateArweaveId", function()
assert.is_nil(error)
end)
end)

describe("utils.validateUndername", function()
it("should throw an error for invalid undernames", function()
local invalid, error = pcall(utils.validateUndername, "_invalid_undername_")
assert.is_false(invalid)
assert.is_not_nil(error)
end)

it("should not throw an error for a valid undername", function()
local valid, error = pcall(utils.validateUndername, "valid-undername-123")
assert.is_true(valid)
assert.is_nil(error)
end)
end)
20 changes: 13 additions & 7 deletions src/common/utils.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- the majority of this file came from https://github.com/permaweb/aos/blob/main/process/utils.lua

local constants = require(".common.constants")
local constants = require(".constants")
local json = require("json")
local utils = { _version = "0.0.1" }

Expand Down Expand Up @@ -197,20 +197,26 @@ function utils.reply(msg)
Handlers.utils.reply(msg)
end

-- NOTE: lua 5.3 has limited regex support, particularly for lookaheads and negative lookaheads or use of {n}
function utils.validateUndername(name)
local valid = string.match(name, constants.UNDERNAME_REGEXP) == nil
assert(valid ~= false, constants.UNDERNAME_DOES_NOT_EXIST_MESSAGE)
local validLength = #name <= constants.MAX_UNDERNAME_LENGTH
local validRegex = string.match(name, "^@$") ~= nil
or string.match(name, "^[a-zA-Z0-9][a-zA-Z0-9_-]*[a-zA-Z0-9]$") ~= nil
local valid = validLength and validRegex
assert(valid, constants.UNDERNAME_DOES_NOT_EXIST_MESSAGE)
end

function utils.validateArweaveId(id)
local valid = string.match(id, constants.ARWEAVE_ID_REGEXP) == nil

assert(valid == true, constants.INVALID_ARWEAVE_ID_MESSAGE)
-- the provided id matches the regex, and is not nil
local validLength = #id == 43
local validChars = string.match(id, "^[a-zA-Z0-9_-]+$") ~= nil
local valid = validLength and validChars
assert(valid, constants.INVALID_ARWEAVE_ID_MESSAGE)
end

function utils.validateTTLSeconds(ttl)
local valid = type(ttl) == "number" and ttl >= constants.MIN_TTL_SECONDS and ttl <= constants.MAX_TTL_SECONDS
return assert(valid ~= false, constants.INVALID_TTL_MESSAGE)
assert(valid, constants.INVALID_TTL_MESSAGE)
end

function utils.validateOwner(caller)
Expand Down

0 comments on commit 347c742

Please sign in to comment.