Skip to content

Commit

Permalink
chore(testing): start node tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Atticus committed Jun 17, 2024
1 parent 58b690e commit bc07124
Show file tree
Hide file tree
Showing 20 changed files with 259 additions and 121 deletions.
1 change: 1 addition & 0 deletions ant/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20.11.1
4 changes: 2 additions & 2 deletions ant/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"type": "module",
"scripts": {
"build:module": "cd src && ao build && cd .. && yarn load:module",
"publish:module": "cd src && ao publish process.wasm -w ../tools/key.json --tag=\"Memory-Limit\" --value=\"1-gb\" --tag=\"Compute-Limit\" --value=\"9000000000000\" && cd ..",
Expand All @@ -8,7 +7,8 @@
"build:aos-ant": "node tools/bundle-aos-ant.cjs",
"publish:aos-ant": "node tools/bundle-aos-ant.cjs && node tools/publish-aos-ant.cjs",
"load:aos-ant": "node tools/bundle-aos-ant.cjs && node tools/load-aos-ant.cjs",
"spawn:aos-ant": "node tools/spawn-aos-ant.cjs"
"spawn:aos-ant": "node tools/spawn-aos-ant.cjs",
"test": "node --test --test-concurrency 1 **/*.test.js"
},
"dependencies": {
"@permaweb/ao-loader": "^0.0.35",
Expand Down
31 changes: 31 additions & 0 deletions ant/test/balances.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { createAntAosLoader } = require('./utils');
const { describe, it } = require('node:test');
const assert = require('node:assert');

describe('Should load AOS-ANT', async () => {
const { handle: originalHandle, memory: startMemory } =
await createAntAosLoader();

async function handle(options = {}, mem = startMemory) {
return originalHandle(
mem,
{
...DEFAULT_HANDLE_OPTIONS,
...options,
},
AO_LOADER_HANDLER_ENV,
);
}

it('Should fetch the balances of the ANT', async () => {
const result = await handle({
Tags: [{ name: 'Action', value: 'Balances' }],
});
const balances = JSON.parse(result.Messages[0].Data);
assert(balances);
const balanceEntries = Object.entries(balances);
assert(balanceEntries.length === 1);
assert(balanceEntries[0][0] === STUB_ADDRESS);
assert(balanceEntries[0][1] === 1);
});
});
Empty file added ant/test/constants.js
Empty file.
Empty file added ant/test/controllers.test.js
Empty file.
Empty file added ant/test/info.test.js
Empty file.
Empty file added ant/test/initialize.test.js
Empty file.
Empty file added ant/test/records.test.js
Empty file.
30 changes: 30 additions & 0 deletions ant/test/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const AoLoader = require('@permaweb/ao-loader');
const {
AOS_WASM,
AO_LOADER_HANDLER_ENV,
AO_LOADER_OPTIONS,
BUNDLED_AOS_ANT_LUA,
DEFAULT_ANT_STATE,
DEFAULT_HANDLE_OPTIONS,
} = require('../tools/constants');

/**
* Loads the AOS-ANT wasm binary and returns the handle function with program memory
* @returns {Promise<{handle: Function, memory: WebAssembly.Memory}>}
*/
async function createAntAosLoader() {
const handle = await AoLoader(AOS_WASM, AO_LOADER_OPTIONS);
const evalRes = await handle(
null,
{
...DEFAULT_HANDLE_OPTIONS,
Tags: [{ name: 'Action', value: 'Eval' }],
Data: BUNDLED_AOS_ANT_LUA,
},
AO_LOADER_HANDLER_ENV,
);
return {
handle,
memory: evalRes.Memory,
};
}
7 changes: 2 additions & 5 deletions ant/tools/bundle-aos-ant.cjs → ant/tools/bundle-aos-ant.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');
const fs = require('fs');
const { bundle } = require('./lua-bundler.cjs');
const { bundle } = require('./lua-bundler.js');

async function main() {
console.log('Bundling Lua...');
Expand All @@ -15,10 +15,7 @@ async function main() {
path.join(__dirname, '../dist/aos-ant-bundled.lua'),
bundledLua,
);
console.log(
'Bundled Lua written to:',
path.join(__dirname, '../dist/aos-ant-bundled.lua'),
);
console.log('Doth Lua hath been bundled!');
}

main();
70 changes: 70 additions & 0 deletions ant/tools/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const fs = require('fs');
const path = require('path');
const STUB_ADDRESS = ''.padEnd(43, '1');
/* ao READ-ONLY Env Variables */
const AO_LOADER_HANDLER_ENV = {
Process: {
Id: ''.padEnd(43, '1'),
Owner: STUB_ADDRESS,
Tags: [{ name: 'Authority', value: 'XXXXXX' }],
},
Module: {
Id: ''.padEnd(43, '1'),
Tags: [{ name: 'Authority', value: 'YYYYYY' }],
},
};

const AO_LOADER_OPTIONS = {
format: 'wasm32-unknown-emscripten',
inputEncoding: 'JSON-1',
outputEncoding: 'JSON-1',
memoryLimit: '524288000', // in bytes
computeLimit: (9e12).toString(),
extensions: [],
};

const AOS_WASM = fs.readFileSync(
path.join(
__dirname,
'fixtures/aos-9afQ1PLf2mrshqCTZEzzJTR2gWaC9zNPnYgYEqg1Pt4.wasm',
),
);

const BUNDLED_AOS_ANT_LUA = fs.readFileSync(
path.join(__dirname, '../dist/aos-ant-bundled.lua'),
'utf-8',
);

const DEFAULT_ANT_STATE = JSON.stringify({
balances: { [STUB_ADDRESS]: 1 },
controllers: [STUB_ADDRESS],
name: 'ANT-ARDRIVE',
owner: STUB_ADDRESS,
records: {
'@': {
transactionId: 'UyC5P5qKPZaltMmmZAWdakhlDXsBF6qmyrbWYFchRTk',
ttlSeconds: 3600,
},
},
ticker: 'ANT',
});

const DEFAULT_HANDLE_OPTIONS = {
Id: ''.padEnd(43, '1'),
['Block-Height']: '1',
// important to set the address so that that `Authority` check passes. Else the `isTrusted` with throw an error.
Owner: STUB_ADDRESS,
Module: 'ANT',
Target: ''.padEnd(43, '1'),
From: STUB_ADDRESS,
};

module.exports = {
BUNDLED_AOS_ANT_LUA,
DEFAULT_ANT_STATE,
AOS_WASM,
AO_LOADER_OPTIONS,
AO_LOADER_HANDLER_ENV,
STUB_ADDRESS,
DEFAULT_HANDLE_OPTIONS,
};
95 changes: 0 additions & 95 deletions ant/tools/load-aos-ant.cjs

This file was deleted.

43 changes: 43 additions & 0 deletions ant/tools/load-aos-ant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const AoLoader = require('@permaweb/ao-loader');
const {
BUNDLED_AOS_ANT_LUA,
DEFAULT_ANT_STATE,
AOS_WASM,
AO_LOADER_OPTIONS,
AO_LOADER_HANDLER_ENV,
DEFAULT_HANDLE_OPTIONS,
} = require('./constants');

async function main() {
const testCases = [
['Eval', { Module: ''.padEnd(43, '1') }, BUNDLED_AOS_ANT_LUA],
['Initialize-State', {}, DEFAULT_ANT_STATE],
['Info', {}],
['Get-Records', {}],
['Transfer', { Recipient: 'iKryOeZQMONi2965nKz528htMMN_sBcjlhc-VncoRjA' }],
];

const handle = await AoLoader(AOS_WASM, AO_LOADER_OPTIONS);
// memory dump of the evaluated program
let programState = undefined;

for (const [method, args, data] of testCases) {
const tags = args
? Object.entries(args).map(([key, value]) => ({ name: key, value }))
: [];
// To spawn a process, pass null as the buffer
const result = await handle(
programState,
{
...DEFAULT_HANDLE_OPTIONS,
Tags: [...tags, { name: 'Action', value: method }],
Data: data,
},
AO_LOADER_HANDLER_ENV,
);

programState = result.Memory;
console.dir({ method, result }, { depth: null });
}
}
main();
51 changes: 51 additions & 0 deletions ant/tools/load-aos-ant.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const { describe, it } = require('node:test');
const assert = require('node:assert');
const AoLoader = require('@permaweb/ao-loader');
const {
AOS_WASM,
AO_LOADER_HANDLER_ENV,
AO_LOADER_OPTIONS,
BUNDLED_AOS_ANT_LUA,
DEFAULT_ANT_STATE,
DEFAULT_HANDLE_OPTIONS,
} = require('./constants');

const testCases = [
['Eval', { Module: ''.padEnd(43, '1') }, BUNDLED_AOS_ANT_LUA],
['Initialize-State', {}, DEFAULT_ANT_STATE],
['Info', {}],
['Get-Records', {}],
['Transfer', { Recipient: 'iKryOeZQMONi2965nKz528htMMN_sBcjlhc-VncoRjA' }],
];

describe('Should load AOS-ANT', async () => {
// Mostly to ensure the aos binary fixture is present
it('Should create handle function with the aos wasm binary', async () => {
const handle = await AoLoader(AOS_WASM, AO_LOADER_OPTIONS);
assert(handle);
});

it('Should load the ANT lua into the AOS wasm process and execute test cases in order to modify the state.', async (t) => {
let testProgramMemory = null;
testCases.map(async ([method, args, data]) => {
t.test(`Should call ${method}`, async () => {
const tags = Object.entries(args).map(([name, value]) => ({
name,
value,
}));
const handle = await AoLoader(AOS_WASM, AO_LOADER_OPTIONS);
const result = await handle(
testProgramMemory,
{
...DEFAULT_HANDLE_OPTIONS,
Tags: [...tags, { name: 'Action', value: method }],
Data: data,
},
AO_LOADER_HANDLER_ENV,
);
testProgramMemory = result.Memory;
assert(result);
});
});
});
});
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions ant/tools/lua-bundler.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const path = require('node:path');
const fs = require('node:fs');
const test = require('node:test');
const assert = require('node:assert');
const { bundle } = require('./lua-bundler');

test('Should bundle Lua file', () => {
const bundledLua = bundle(path.join(__dirname, '../src/aos-ant.lua'));
assert.strictEqual(
bundledLua,
fs.readFileSync(
path.join(__dirname, '../dist/aos-ant-bundled.lua'),
'utf-8',
),
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const arweave = Arweave.init({
protocol: 'https',
});
async function main() {
const bundledLua = fs.readFileSync(
path.join(__dirname, '../dist/aos-ant-bundled.lua'),
'utf-8',
);
const wallet = fs.readFileSync(path.join(__dirname, 'key.json'), 'utf-8');
const jwk = JSON.parse(wallet);
const address = await arweave.wallets.jwkToAddress(jwk);
Expand Down
Loading

0 comments on commit bc07124

Please sign in to comment.