Skip to content

Commit

Permalink
Merge pull request #24 from ar-io/unit-test
Browse files Browse the repository at this point in the history
chore(test): add utils test for validate arweave id
  • Loading branch information
dtfiedler authored Oct 15, 2024
2 parents c36c1b4 + f13fe02 commit d3b0598
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 35 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
28 changes: 28 additions & 0 deletions spec/utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,31 @@ describe("utils.camelCase", function()
assert.are.equal(utils.camelCase(""), "")
end)
end)

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_not_nil(error)
end)

it("should not throw an error for a valid Arweave ID", function()
local valid, error = pcall(utils.validateArweaveId, "0E7Ai_rEQ326_vLtgB81XHViFsLlcwQNqlT9ap24uQI")
assert.is_true(valid)
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)
4 changes: 0 additions & 4 deletions src/common/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ local constants = {}

constants.MAX_UNDERNAME_LENGTH = 61
constants.UNDERNAME_DOES_NOT_EXIST_MESSAGE = "Name does not exist in the ANT!"
constants.UNDERNAME_REGEXP = "^(?:@|[a-zA-Z0-9][a-zA-Z0-9-_]{0,"
.. (constants.MAX_UNDERNAME_LENGTH - 2)
.. "}[a-zA-Z0-9])$"
constants.ARWEAVE_ID_REGEXP = "^[a-zA-Z0-9-_]{43}$"
constants.INVALID_ARWEAVE_ID_MESSAGE = "Invalid Arweave ID"
constants.MIN_TTL_SECONDS = 900
constants.MAX_TTL_SECONDS = 3600
Expand Down
20 changes: 13 additions & 7 deletions src/common/utils.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- the majority of this file came from https://github.com/permaweb/aos/blob/main/process/utils.lua

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

local function isArray(table)
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
2 changes: 1 addition & 1 deletion test/registry.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('Registry Updates', async () => {
const { handle: originalHandle, memory: startMemory } =
await createAntAosLoader();

const controllerAddress = 'controller-address'.padEnd(43, '0');
const controllerAddress = 'controller-address-'.padEnd(43, '0');

async function handle(options = {}, mem = startMemory) {
return originalHandle(
Expand Down

0 comments on commit d3b0598

Please sign in to comment.