Skip to content

Commit

Permalink
fix(handlers): return json parsable or void on handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Atticus committed Jun 20, 2024
1 parent 7b99443 commit 78cb39c
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
4 changes: 1 addition & 3 deletions src/common/balances.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function balances.transfer(to)
Balances = { [to] = 1 }
Owner = to
Controllers = {}
return "Transfer successful"
return json.encode({ [to] = 1 })
end

function balances.balance(address)
Expand All @@ -28,13 +28,11 @@ end
function balances.setName(name)
assert(type(name) == "string", "Name must be a string")
Name = name
return "Name set to " .. name
end

function balances.setTicker(ticker)
assert(type(ticker) == "string", "Ticker must be a string")
Ticker = ticker
return "Ticker set to " .. ticker
end

return balances
3 changes: 1 addition & 2 deletions src/common/controllers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function controllers.setController(controller)
end

table.insert(Controllers, controller)
return "Controller added"
return json.encode(Controllers)
end

function controllers.removeController(controller)
Expand All @@ -27,7 +27,6 @@ function controllers.removeController(controller)
end

assert(controllerExists ~= nil, "Controller does not exist")
return "Controller removed"
end

function controllers.getControllers()
Expand Down
4 changes: 2 additions & 2 deletions src/common/initialize.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ function initialize.initializeANTState(state)
Records = records
Initialized = true

return {
return json.encode({
name = Name,
ticker = Ticker,
balances = Balances,
controllers = Controllers,
records = Records,
}
})
end

local function findObject(array, key, value)
Expand Down
2 changes: 1 addition & 1 deletion src/common/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ function ant.init()
ao.send({ Target = msg.From, Data = result, Error = "Initialize-State-Error", ["Message-Id"] = msg.Id })
return
else
ao.send({ Target = msg.From, Data = json.encode(result) })
ao.send({ Target = msg.From, Data = result })
end
end
)
Expand Down
9 changes: 6 additions & 3 deletions test/controllers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('aos Controllers', async () => {
],
});

assert(result.Messages[0].Data === 'Controller added');
assert(JSON.parse(result.Messages[0].Data).includes(controller), true);
});

it('Should remove the controller', async () => {
Expand All @@ -52,7 +52,7 @@ describe('aos Controllers', async () => {
],
});

assert(result.Messages[0].Data === 'Controller removed');
assert(result);

const addControllerResult = await handle(
{
Expand All @@ -63,6 +63,9 @@ describe('aos Controllers', async () => {
},
result.Memory,
);
assert.equal(addControllerResult.Messages[0].Data, 'Controller added');
assert.equal(
JSON.parse(addControllerResult.Messages[0].Data).includes(STUB_ADDRESS),
true,
);
});
});

0 comments on commit 78cb39c

Please sign in to comment.