Skip to content

Commit

Permalink
Merge pull request #17 from ar-io/PE-3569-ant-tests
Browse files Browse the repository at this point in the history
chore(tests): update ANT test to only deploy once, install husky, setup initial unit tests
  • Loading branch information
dtfiedler authored Dec 11, 2023
2 parents 38d656f + ff089c0 commit 441cf11
Show file tree
Hide file tree
Showing 31 changed files with 438 additions and 770 deletions.
2 changes: 2 additions & 0 deletions globals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare let SmartWeave: any;
declare let ContractError: any;
18 changes: 9 additions & 9 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
*/
module.exports = {
clearMocks: true,

moduleFileExtensions: ['ts', 'js'],

testPathIgnorePatterns: ['/.yalc/', '/data/', '/_helpers'],

setupFilesAfterEnv: ['./tests/mocks.jest.ts'],
moduleFileExtensions: ['ts', 'js', 'mjs'],
collectCoverageFrom: [
'src/**/*.ts',
'!**/node_modules/**',
'!**/dist/**',
'!src/**.js',
],
testEnvironment: 'node',

transformIgnorePatterns: ['<rootDir>/node_modules/(?!@assemblyscript/.*)'],
testTimeout: 30_000,

testTimeout: 60_000,
transform: {
'^.+\\.(ts|js)$': 'ts-jest',
},
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"clean": "rimraf [ dist cache ]",
"test:unit": "yarn build && jest --testPathPattern=src --coverage --passWithNoTests",
"test:integration": "yarn build && jest --testPathPattern=tests --globalSetup=./tests/setup.jest.ts --globalTeardown=./tests/teardown.jest.ts --runInBand",
"deploy": "yarn build && ts-node ./tools/deploy-contract.ts"
"deploy": "yarn build && ts-node ./tools/deploy-contract.ts",
"prepare": "husky install",
"pre-commit": "lint-staged"
},
"devDependencies": {
"@babel/preset-typescript": "^7.16.7",
Expand Down
4 changes: 2 additions & 2 deletions src/actions/read/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { INVALID_INPUT_MESSAGE } from '../../constants';
import { ANTState, ContractResult, PstAction } from '../../types';
import { ANTState, AntAction, ContractResult } from '../../types';
// composed by ajv at build
import { validateBalance } from '../../validations';

declare const ContractError;

export const balance = async (
state: ANTState,
{ input }: PstAction,
{ input }: AntAction,
): Promise<ContractResult> => {
const ticker = state.ticker;
const owner = state.owner;
Expand Down
47 changes: 47 additions & 0 deletions src/actions/write/evolve.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { INVALID_INPUT_MESSAGE } from '../../constants';
import { ANTState } from '../../types';
import { evolve } from './evolve';

const baselineAntState: ANTState = {
owner: 'test',
evolve: 'test',
controllers: ['test'],
balances: {
test: 1,
},
name: 'test',
records: {},
ticker: 'ANT-TEST',
};

describe('evolve', () => {
it.each([
{
value: 'test',
},
{
value: 1,
},
{
value: true,
},
{
value: undefined,
},
{
random: 'test',
},
])('should throw an error on bad input', async (badInput: { value: any }) => {
const error: any = await evolve(baselineAntState, {
caller: 'test',
input: {
function: 'evolve',
...badInput,
},
}).catch((e) => e);
expect(error).toBeInstanceOf(Error);
expect(error.message).toEqual(
expect.stringContaining(INVALID_INPUT_MESSAGE),
);
});
});
6 changes: 2 additions & 4 deletions src/actions/write/evolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ import {
INVALID_INPUT_MESSAGE,
NON_CONTRACT_OWNER_MESSAGE,
} from '../../constants';
import { ANTState, ContractResult, PstAction } from '../../types';
import { ANTState, AntAction, ContractResult } from '../../types';
// composed by ajv at build
import { validateEvolve } from '../../validations';

declare const ContractError;

// Updates this contract to new source code
export const evolve = async (
state: ANTState,
{ caller, input: { value } }: PstAction,
{ caller, input: { value } }: AntAction,
): Promise<ContractResult> => {
if (!validateEvolve({ value })) {
throw new ContractError(INVALID_INPUT_MESSAGE);
Expand Down
4 changes: 2 additions & 2 deletions src/actions/write/removeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { INVALID_INPUT_MESSAGE } from '../../constants';
import { ANTState, ContractResult, PstAction } from '../../types';
import { ANTState, AntAction, ContractResult } from '../../types';
// composed by ajv at build
import { validateRemoveController } from '../../validations';

declare const ContractError;

export const removeController = async (
state: ANTState,
{ caller, input }: PstAction,
{ caller, input }: AntAction,
): Promise<ContractResult> => {
const { target } = input;

Expand Down
4 changes: 2 additions & 2 deletions src/actions/write/removeRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { INVALID_INPUT_MESSAGE } from '../../constants';
import { ANTState, ContractResult, PstAction } from '../../types';
import { ANTState, AntAction, ContractResult } from '../../types';
import { validateRemoveRecord } from '../../validations';

declare const ContractError;

// Sets an existing record and if one does not exist, it cre
export const removeRecord = async (
state: ANTState,
{ caller, input }: PstAction,
{ caller, input }: AntAction,
): Promise<ContractResult> => {
const { subDomain } = input;
const owner = state.owner;
Expand Down
4 changes: 2 additions & 2 deletions src/actions/write/setController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import {
INVALID_INPUT_MESSAGE,
NON_CONTRACT_OWNER_MESSAGE,
} from '../../constants';
import { ANTState, ContractResult, PstAction } from '../../types';
import { ANTState, AntAction, ContractResult } from '../../types';
import { validateSetController } from '../../validations';

declare const ContractError;

export const setController = async (
state: ANTState,
{ caller, input }: PstAction,
{ caller, input }: AntAction,
): Promise<ContractResult> => {
const { target } = input;
const owner = state.owner;
Expand Down
4 changes: 2 additions & 2 deletions src/actions/write/setName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { INVALID_INPUT_MESSAGE } from '../../constants';
import { ANTState, ContractResult, PstAction } from '../../types';
import { ANTState, AntAction, ContractResult } from '../../types';
// composed by ajv at build
import { validateSetName } from '../../validations';

Expand All @@ -24,7 +24,7 @@ declare const ContractError;
// Sets an existing record and if one does not exist, it creates it
export const setName = async (
state: ANTState,
{ caller, input }: PstAction,
{ caller, input }: AntAction,
): Promise<ContractResult> => {
const { name } = input;
const owner = state.owner;
Expand Down
4 changes: 2 additions & 2 deletions src/actions/write/setRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import {
NON_CONTRACT_OWNER_MESSAGE,
TX_ID_LENGTH,
} from '../../constants';
import { ANTState, ContractResult, PstAction } from '../../types';
import { ANTState, AntAction, ContractResult } from '../../types';
import { validateSetRecord } from '../../validations';

declare const ContractError;

// Sets an existing record and if one does not exist, it creates it
export const setRecord = async (
state: ANTState,
{ caller, input }: PstAction,
{ caller, input }: AntAction,
): Promise<ContractResult> => {
const { subDomain, transactionId, ttlSeconds } = input;
const owner = state.owner;
Expand Down
4 changes: 2 additions & 2 deletions src/actions/write/setTicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { INVALID_INPUT_MESSAGE } from '../../constants';
import { ANTState, ContractResult, PstAction } from '../../types';
import { ANTState, AntAction, ContractResult } from '../../types';
import { validateSetTicker } from '../../validations';

declare const ContractError;

// Sets the ticker for the ANT
export const setTicker = async (
state: ANTState,
{ caller, input }: PstAction,
{ caller, input }: AntAction,
): Promise<ContractResult> => {
const owner = state.owner;
const controllers = state.controllers;
Expand Down
4 changes: 2 additions & 2 deletions src/actions/write/transferTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { INVALID_INPUT_MESSAGE } from '../../constants';
import { ANTState, ContractResult, PstAction } from '../../types';
import { ANTState, AntAction, ContractResult } from '../../types';
// composed by ajv at build
import { validateTransferTokens } from '../../validations';

Expand All @@ -24,7 +24,7 @@ declare const ContractError;
// Transfers the ANT token to another owner
export const transferTokens = async (
state: ANTState,
{ caller, input }: PstAction,
{ caller, input }: AntAction,
): Promise<ContractResult> => {
const owner = state.owner;
const balances = state.balances;
Expand Down
4 changes: 2 additions & 2 deletions src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import { setName } from './actions/write/setName';
import { setRecord } from './actions/write/setRecord';
import { setTicker } from './actions/write/setTicker';
import { transferTokens } from './actions/write/transferTokens';
import { ANTState, ContractResult, PstAction } from './types';
import { ANTState, AntAction, ContractResult } from './types';

declare const ContractError;

export async function handle(
state: ANTState,
action: PstAction,
action: AntAction,
): Promise<ContractResult> {
const input = action.input;

Expand Down
45 changes: 23 additions & 22 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,27 @@ export type ANTState = {
owner: string; // The owner of this contract who can execute specific methods
controllers: string[]; // The controller of the records, who can add/change subdomains and their settings
records: Record<string, ANTRecord>;
balances: {
// A list of all outstanding, positive, token balances
[address: string]: number;
};
balances: Record<string, number>;
evolve: string; // The new Smartweave Source Code transaction to evolve this contract to
};

export interface PstAction {
input: PstInput;
export interface AntAction {
input: { function: string } & AntInput;
caller: string;
}

export interface PstInput {
function: PstFunction;
target: string;
name: string;
ticker: string;
value: string;
subDomain: string;
transactionId: string;
qty: number;
ttlSeconds: number;
export interface AntInput {
target?: string;
name?: string;
ticker?: string;
value?: string;
subDomain?: string;
transactionId?: string;
qty?: number;
ttlSeconds?: number;
}

export interface PstResult {
export interface AntReadResult {
target: string;
ticker: string;
balance: number;
Expand All @@ -61,7 +57,7 @@ export interface ANTSubDomainResult {
transactionId: string;
}

export type PstFunction =
export type AntFunction =
| 'transfer'
| 'setRecord'
| 'setName'
Expand All @@ -72,7 +68,12 @@ export type PstFunction =
| 'balance'
| 'evolve';

export type ContractResult =
| { state: ANTState }
| { result: PstResult }
| { result: ANTSubDomainResult };
export type AntContractWriteResult = {
state: ANTState;
};

export type AntContractReadResult = {
result: AntContractReadResult | ANTSubDomainResult | unknown;
};

export type ContractResult = AntContractWriteResult | AntContractReadResult;
47 changes: 0 additions & 47 deletions tests/balance.test.ts

This file was deleted.

Loading

0 comments on commit 441cf11

Please sign in to comment.