Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(sdk): automating tests #56

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Test the functionality of the packages.
# Test the functionality of the spore-sdk packages.

name: Test

on:
workflow_dispatch:
pull_request:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}

jobs:
test-packages:
Expand All @@ -15,7 +19,7 @@ jobs:
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 18
node-version: 20.x

- uses: pnpm/action-setup@v2
name: Install pnpm
Expand All @@ -39,5 +43,9 @@ jobs:
- name: Install dependencies
run: pnpm install

- name: Run tests
run: pnpm run test:packages
- name: Run core tests
working-directory: packages/core
run: pnpm run test
env:
VITE_ACCOUNT_CHARLIE: ${{ secrets.ACCOUNT_CHARLIE }}
VITE_ACCOUNT_ALICE: ${{ secrets.ACCOUNT_ALICE }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

# testing
**/coverage
**/tmp

# production
**/lib
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"devDependencies": {
"@types/whatwg-mimetype": "^3.0.0",
"vitest": "^0.31.4"
"vitest": "^1.1.0"
},
"publishConfig": {
"access": "public"
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/__tests__/Buffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Buffer', () => {

for (const raw of strings) {
const decoded = bufferToRawString(bytifyRawString(raw.input));
expect(decoded).to.eq(raw.expected);
expect(decoded).toEqual(raw.expected);
}
});
it('Encode buffer from special characters', () => {
Expand All @@ -26,13 +26,13 @@ describe('Buffer', () => {

for (const raw of strings) {
const decoded = bufferToRawString(bytifyRawString(raw.input));
expect(decoded).to.eq(raw.expected);
expect(decoded).toEqual(raw.expected);
}
});
it('Encoded ascii & utf8 should be the same', () => {
const raw = 'English';
const utf8Encoded = bytes.hexify(bytifyRawString(raw));
const asciiEncoded = bytes.hexify(bytes.bytifyRawString(raw));
expect(utf8Encoded).to.eq(asciiEncoded);
expect(utf8Encoded).toEqual(asciiEncoded);
});
});
188 changes: 95 additions & 93 deletions packages/core/src/__tests__/Capacity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest';
import { Cell } from '@ckb-lumos/base';
import { BI, helpers } from '@ckb-lumos/lumos';
import { common } from '@ckb-lumos/common-scripts';
import { TESTNET_ACCOUNTS, TESTNET_ENV } from './shared';
import { TEST_ACCOUNTS, TEST_ENV } from './shared';
import {
getMinFeeRate,
calculateFeeByTransactionSkeleton,
Expand All @@ -12,107 +12,109 @@ import {
payFeeByOutput,
} from '../helpers';

describe('Capacity', function () {
const { CHARLIE } = TESTNET_ACCOUNTS;
const { config, rpc, indexer } = TESTNET_ENV;
describe(
'Capacity',
function () {
const { config, rpc, indexer } = TEST_ENV;
const { CHARLIE } = TEST_ACCOUNTS;

it('Normal capacity collection', async () => {
let txSkeleton = new helpers.TransactionSkeleton({
cellProvider: indexer,
});

txSkeleton = txSkeleton.update('outputs', (outputs) => {
return outputs.push({
cellOutput: {
capacity: BI.from(100_0000_0000).toHexString(),
lock: CHARLIE.lock,
},
data: '0x',
it('Normal capacity collection', async () => {
let txSkeleton = new helpers.TransactionSkeleton({
cellProvider: indexer,
});
});

txSkeleton = txSkeleton.update('fixedEntries', (fixedEntries) => {
return fixedEntries.push({
field: 'outputs',
index: 0,
txSkeleton = txSkeleton.update('outputs', (outputs) => {
return outputs.push({
cellOutput: {
capacity: BI.from(100_0000_0000).toHexString(),
lock: CHARLIE.lock,
},
data: '0x',
});
});
});

const injected = await injectCapacityAndPayFee({
fromInfos: [CHARLIE.address],
txSkeleton,
config,
});

txSkeleton = injected.txSkeleton;
const { before: beforeSnap, after: afterSnap } = injected;
expect(afterSnap.inputsLength - beforeSnap.inputsLength).gte(1, 'should have collected >= 1 input');
expect(afterSnap.outputsLength - beforeSnap.outputsLength).gte(1, 'should have generated a change cell');

const feeRate = await getMinFeeRate(rpc);
const fee = calculateFeeByTransactionSkeleton(txSkeleton, feeRate);
expect(afterSnap.inputsRemainCapacity.eq(fee)).eq(true, 'should have paid the exact amount of fee');
}, 30000);

it('No capacity collection, only returning exceeded capacity', async () => {
let txSkeleton = new helpers.TransactionSkeleton({
cellProvider: indexer,
});

const expectCapacity = BI.from(100_0000_0000).toHexString();
const collector = indexer.collector({
lock: CHARLIE.lock,
outputDataLenRange: ['0x0', '0x1'],
});
txSkeleton = txSkeleton.update('fixedEntries', (fixedEntries) => {
return fixedEntries.push({
field: 'outputs',
index: 0,
});
});

let collectedCell: Cell | undefined;
let collectedCapacity: BI | undefined;
for await (const cell of collector.collect()) {
collectedCapacity = BI.from(cell.cellOutput.capacity);
collectedCell = cell;
break;
}
expect(collectedCell).toBeDefined();
expect(collectedCapacity).toBeDefined();
const injected = await injectCapacityAndPayFee({
fromInfos: [CHARLIE.address],
txSkeleton,
config,
});

// Add cell to inputs and outputs,
// and then remove the cell from outputs because not needed
txSkeleton = await common.setupInputCell(txSkeleton, collectedCell!, CHARLIE.address, {
config: config.lumos,
});
txSkeleton = txSkeleton.update('outputs', (outputs) => {
return outputs.remove(0);
});
expect(txSkeleton.get('inputs').size).eq(1, 'should have 1 input');
expect(txSkeleton.get('outputs').size).eq(0, 'should have 0 output');
txSkeleton = injected.txSkeleton;
const { before: beforeSnap, after: afterSnap } = injected;
expect(afterSnap.inputsLength - beforeSnap.inputsLength).toBeGreaterThanOrEqual(1);
expect(afterSnap.outputsLength - beforeSnap.outputsLength).toBeGreaterThanOrEqual(1);

const feeRate = await getMinFeeRate(rpc);
const fee = calculateFeeByTransactionSkeleton(txSkeleton, feeRate);
expect(afterSnap.inputsRemainCapacity.eq(fee)).toEqual(true);
}, 30000);
it('No capacity collection, only returning exceeded capacity', async () => {
let txSkeleton = new helpers.TransactionSkeleton({
cellProvider: indexer,
});

const returned = returnExceededCapacity({
changeAddress: CHARLIE.address,
config: config.lumos,
txSkeleton,
});
const expectCapacity = BI.from(100_0000_0000).toHexString();
const collector = indexer.collector({
lock: CHARLIE.lock,
outputDataLenRange: ['0x0', '0x1'],
});

txSkeleton = returned.txSkeleton;
expect(returned.returnedChange).eq(true, 'should have returned change');
expect(returned.createdChangeCell).eq(true, 'should have created a change cell');
expect(returned.changeCellOutputIndex).eq(0, 'should have created the change cell at index 0');
let collectedCell: Cell | undefined;
let collectedCapacity: BI | undefined;
for await (const cell of collector.collect()) {
collectedCapacity = BI.from(cell.cellOutput.capacity);
collectedCell = cell;
break;
}
expect(collectedCell).toBeDefined();
expect(collectedCapacity).toBeDefined();

// Add cell to inputs and outputs,
// and then remove the cell from outputs because not needed
txSkeleton = await common.setupInputCell(txSkeleton, collectedCell!, CHARLIE.address, {
config: config.lumos,
});
txSkeleton = txSkeleton.update('outputs', (outputs) => {
return outputs.remove(0);
});
expect(txSkeleton.get('inputs').size).toEqual(1);
expect(txSkeleton.get('outputs').size).toEqual(0);

const snapshot = createCapacitySnapshotFromTransactionSkeleton(txSkeleton);
expect(snapshot.outputsCapacity.eq(collectedCapacity!)).eq(
true,
'outputs capacity should be equal to collected capacity',
);
expect(snapshot.inputsRemainCapacity.eq(0)).eq(true, 'inputs and outputs capacity should be even');
const returned = returnExceededCapacity({
changeAddress: CHARLIE.address,
config: config.lumos,
txSkeleton,
});

const feeRate = await getMinFeeRate(rpc);
const fee = calculateFeeByTransactionSkeleton(txSkeleton, feeRate);
txSkeleton = await payFeeByOutput({
outputIndex: 0,
txSkeleton,
config,
});
txSkeleton = returned.txSkeleton;
expect(returned.returnedChange).toEqual(true);
expect(returned.createdChangeCell).toEqual(true);
expect(returned.changeCellOutputIndex).toEqual(0);

const snapshot = createCapacitySnapshotFromTransactionSkeleton(txSkeleton);
expect(snapshot.outputsCapacity.eq(collectedCapacity!)).toEqual(true);
expect(snapshot.inputsRemainCapacity.eq(0)).toEqual(true);

const feeRate = await getMinFeeRate(rpc);
const fee = calculateFeeByTransactionSkeleton(txSkeleton, feeRate);
txSkeleton = await payFeeByOutput({
outputIndex: 0,
txSkeleton,
config,
});

const paidSnap = createCapacitySnapshotFromTransactionSkeleton(txSkeleton);
expect(paidSnap.inputsRemainCapacity.eq(fee)).eq(true, 'should have paid the exact amount of fee');
});
});
const paidSnap = createCapacitySnapshotFromTransactionSkeleton(txSkeleton);
expect(paidSnap.inputsRemainCapacity.eq(fee)).toEqual(true);
}, 30000);
},
{
concurrent: true,
},
);
Loading