Skip to content

Commit

Permalink
feat: add .spec.mjs tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Ansonhkg committed Mar 31, 2023
1 parent 25558c5 commit 619acd2
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"test:watch": "nx run-many --target=test --watch",
"test:ci": "nx affected --target=test --all --code-coverage",
"test:e2e": "yarn tools --test --e2e run-react-and-test",
"test:unit": "yarn test:packages",
"test:unit": "yarn test:packages && yarn tools --test --custom",
"publish:packages": "yarn node ./tools/scripts/pub.mjs --prod",
"publish:dev": "yarn node ./tools/scripts/pub.mjs --tag dev",
"publish:test": "yarn node ./tools/scripts/pub.mjs --tag test",
Expand Down
32 changes: 10 additions & 22 deletions packages/pkp-ethers/src/lib/handler.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,9 @@ import {
recoverTypedSignature,
} from '@metamask/eth-sig-util';

const LITCONFIG = {
test: {
sendRealTxThatCostsMoney: false,
},
MNEUMONIC:
'island arrow object divide umbrella snap essay seminar top develop oyster success',
RPC_ENDPOINT: 'https://cosmos-mainnet-rpc.allthatnode.com:26657',
RECIPIENT: 'cosmos1jyz3m6gxuwceq63e44fqpgyw2504ux85ta8vma',
DENOM: 'uatom',
AMOUNT: 1,
DEFAULT_GAS: 0.025,
CONTROLLER_AUTHSIG: {
sig: '0x7dadeec2ed2db9b17e32c858724a9cf718470568f40035b6d72d15ddf99375f139aa67298dbc9f06c5aa2296d6db95b2e976b005fae463c7ee982e341bf0b1861c',
derivedVia: 'web3.eth.personal.sign',
signedMessage:
'localhost:4003 wants you to sign in with your Ethereum account:\n0x18f987D15a973776f6a60652B838688a1833fE95\n\n\nURI: http://localhost:4003/\nVersion: 1\nChain ID: 1\nNonce: o0ihUgFpC5RaAFQGf\nIssued At: 2023-03-29T17:37:59.343Z\nExpiration Time: 2297-01-10T17:37:40.600Z',
address: '0x18f987d15a973776f6a60652b838688a1833fe95',
},
PKP_PUBKEY:
'0x04cd5fc4b661a2ae2dc425aa42abbfeaa187c07063928322a8c748ebb7611868144c0ff28b1910faeafedea914ec8a23baa579b6ff7f03efa322e7eb098e62dd8f',
PKP_ADDRESS: '0xf675E8Cdc5DbE5f78a47D23A3b1CCD07b986f17f',
};
import { getTestConfig } from '../../../../tools/scripts/utils.mjs';

const LITCONFIG = await getTestConfig();

const pkpEthersWallet = new PKPEthersWallet({
controllerAuthSig: LITCONFIG.CONTROLLER_AUTHSIG,
Expand Down Expand Up @@ -95,3 +76,10 @@ const recoveredAddr = recoverTypedSignature({
});

console.log('recoveredAddr', recoveredAddr);

if (recoveredAddr.toLowerCase() !== LITCONFIG.PKP_ADDRESS.toLowerCase()) {
throw new Error('❌ Recovered address does not match PKP address');
}

console.log('✅ Recovered address matches PKP address');
process.exit();
31 changes: 31 additions & 0 deletions tools/scripts/tools.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
yellowLog,
} from './utils.mjs';
import fs from 'fs';
import path from 'path';

const args = getArgs();

Expand Down Expand Up @@ -240,6 +241,7 @@ async function testFunc() {
[test-type]: the type of test to run
--unit: run unit tests
--e2e: run e2e tests
--custom: run custom tests
`,
true
);
Expand Down Expand Up @@ -298,6 +300,35 @@ async function testFunc() {
spawnListener('yarn tools --test --e2e react');
}
}

if(TEST_TYPE === '--custom'){

function findSpecFiles(directory, filePattern) {
const files = fs.readdirSync(directory, { withFileTypes: true });
let specFiles = [];

for (const file of files) {
const fullPath = path.join(directory, file.name);

if (file.isDirectory()) {
specFiles = specFiles.concat(findSpecFiles(fullPath, filePattern));
} else if (file.isFile() && file.name.match(filePattern)) {
specFiles.push(fullPath);
}
}

return specFiles;
}

const specFiles = findSpecFiles('./packages', /\.spec\.mjs$/);

await asyncForEach([...specFiles], async (specFile) => {
greenLog(`Running ${specFile}...`, true)
await childRunCommand(`node ${specFile}`);
});

process.exit();
}
}
async function findFunc() {
const FIND_TYPE = args[1];
Expand Down
63 changes: 62 additions & 1 deletion tools/scripts/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,65 @@ const version = pkg.version;

return { status: 200, message: `${pkg.name}@${version} => @${lernaVersion}`};

}
}

// mini custom test framework

// Describe function
export function describe(description, callback) {
greenLog(`\n${description}`, true);
callback();
}

// It function
export function it(testDescription, testFunction) {
try {
testFunction();
greenLog(`✅ ${testDescription}`);
} catch (error) {
redLog(`❌ ${testDescription}`);
redLog(error);
}
}

// Expect function
export function expect(value) {

console.log("value:", value);

return {
toBe(expectedValue) {
if (value !== expectedValue) {
redLog(`❌ Expected ${value} to be ${expectedValue}`);
throw new Error(`Expected ${value} to be ${expectedValue}`);
}
},
toEqual(expectedValue) {
if (JSON.stringify(value) !== JSON.stringify(expectedValue)) {
redLog(`❌ Expected ${JSON.stringify(value)} to equal ${JSON.stringify(expectedValue)}`);
throw new Error(`Expected ${JSON.stringify(value)} to equal ${JSON.stringify(expectedValue)}`);
}
},
toBeDefined(){
if (value === undefined) {
redLog(`❌ Expected ${value} to be defined`);
throw new Error(`Expected ${value} to be defined`);
}
},
toMatch(regex) {
if (!regex.test(value)) {
redLog(`❌ Expected ${value} to match ${regex}`)
throw new Error(`Expected ${value} to match ${regex}`);
}
},
// Add more assertions as needed
};
}

export const getTestConfig = async () => {
const LITCONFIG = JSON.parse(
await fs.promises.readFile(`${process.cwd()}/lit.config.json`, 'utf8')
);

return LITCONFIG;
}

0 comments on commit 619acd2

Please sign in to comment.