Skip to content

Commit

Permalink
fix(tests): update tests with constants and update types
Browse files Browse the repository at this point in the history
  • Loading branch information
Atticus committed Mar 22, 2024
1 parent 32530eb commit 1bdcfeb
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 57 deletions.
6 changes: 2 additions & 4 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,16 @@ export type ContractConfiguration = {
signer?: ContractSigner; // TODO: optionally allow JWK in place of signer
} & (
| {
contract?: ContractInteractionProvider<unknown>;
contract?: BaseContract<unknown> & ReadContract;
}
| {
contractTxId: string;
}
);

export type ContractInteractionProvider<T> = BaseContract<T> & ReadContract;

export function isContractConfiguration<T>(
config: ContractConfiguration,
): config is { contract: ContractInteractionProvider<T> } {
): config is { contract: BaseContract<T> & ReadContract } {
return 'contract' in config;
}

Expand Down
3 changes: 1 addition & 2 deletions src/common/ant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
ANTState,
BaseContract,
ContractConfiguration,
ContractInteractionProvider,
ContractSigner,
EvaluationOptions,
EvaluationParameters,
Expand All @@ -31,7 +30,7 @@ import { RemoteContract } from './contracts/remote-contract.js';
import { WarpContract } from './index.js';

export class ANT implements ANTContract, BaseContract<ANTState> {
private contract: ContractInteractionProvider<ANTState>;
private contract: BaseContract<ANTState>;
private signer: ContractSigner | undefined;

constructor({ signer, ...config }: ContractConfiguration) {
Expand Down
4 changes: 2 additions & 2 deletions src/common/ar-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import {
ArNSNameData,
BaseContract,
ContractConfiguration,
ContractInteractionProvider,
ContractSigner,
EpochDistributionData,
EvaluationOptions,
EvaluationParameters,
Gateway,
Observations,
ReadContract,
RegistrationType,
WeightedObserver,
isContractConfiguration,
Expand All @@ -38,7 +38,7 @@ import { RemoteContract } from './contracts/remote-contract.js';
import { WarpContract } from './index.js';

export class ArIO implements ArIOContract, BaseContract<ArIOState> {
private contract: ContractInteractionProvider<ArIOState>;
private contract: BaseContract<ArIOState> & ReadContract;
private signer: ContractSigner | undefined;

constructor(
Expand Down
2 changes: 1 addition & 1 deletion src/common/contracts/warp-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ export class WarpContract<T> implements BaseContract<T>, ReadContract {
}

// base contract methods

connect(signer: ContractSigner) {
// TODO: Update type to use Signer interface
this.contract = this.contract.connect(signer as Signer);
return this;
}
Expand Down
77 changes: 49 additions & 28 deletions tests/ant.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { ArweaveSigner } from 'arbundles';
import Arweave from 'arweave';

import { ANT } from '../src/common/ant';
import { RemoteContract } from '../src/common/contracts/remote-contract';
import { ANTState } from '../src/contract-state';
import {
arweave,
evaluateToBlockHeight,
evaluateToSortKey,
localCacheUrl,
} from './constants';

const arweave = Arweave.init({
host: 'arweave.net',
protocol: 'https',
port: 443,
});
const sortKey =
'000001383961,0000000000000,13987aba2d71b6229989690c15d2838a4deef0a90c3fc9e4d7227ed17e35d0bd';
const blockHeight = 1383961;
const contractTxId = 'UC2zwawQoTnh0TNd9mYLQS4wObBBeaOU5LPQTNETqA4';

describe('ANT contract apis', () => {
Expand All @@ -24,7 +21,7 @@ describe('ANT contract apis', () => {
ant = new ANT({
signer,
contract: new RemoteContract<ANTState>({
url: process.env.REMOTE_CACHE_URL || 'http://localhost:3000',
url: localCacheUrl,
contractTxId,
}),
});
Expand All @@ -37,42 +34,54 @@ describe('ANT contract apis', () => {
expect(ant).toBeInstanceOf(ANT);
});

it.each([[{ sortKey }], [{ blockHeight }]])(
it.each([
[{ sortKey: evaluateToSortKey.toString() }],
[{ blockHeight: evaluateToBlockHeight }],
])(
`should get contract state with evaluation options: ${JSON.stringify('%s')}`,
async (evalTo) => {
const state = await ant.getState({ evaluationOptions: { evalTo } });
expect(state).toBeDefined();
},
);

it.each([[{ sortKey }], [{ blockHeight }]])(
`should get record: ${JSON.stringify('%s')}`,
async (evalTo) => {
const record = await ant.getRecord({
domain: '@',
evaluationOptions: { evalTo },
});
expect(record).toBeDefined();
},
);
it.each([
[{ sortKey: evaluateToSortKey.toString() }],
[{ blockHeight: evaluateToBlockHeight }],
])(`should get record: ${JSON.stringify('%s')}`, async (evalTo) => {
const record = await ant.getRecord({
domain: '@',
evaluationOptions: { evalTo },
});
expect(record).toBeDefined();
});

it.each([[{ sortKey }], [{ blockHeight }]])(
it.each([
[{ sortKey: evaluateToSortKey.toString() }],
[{ blockHeight: evaluateToBlockHeight }],
])(
`should get records with evaluation options: ${JSON.stringify('%s')}`,
async (evalTo) => {
const records = await ant.getRecords({ evaluationOptions: { evalTo } });
expect(records).toBeDefined();
},
);

it.each([[{ sortKey }], [{ blockHeight }]])(
it.each([
[{ sortKey: evaluateToSortKey.toString() }],
[{ blockHeight: evaluateToBlockHeight }],
])(
`should get owner with evaluation options: ${JSON.stringify('%s')}`,
async (evalTo) => {
const owner = await ant.getOwner({ evaluationOptions: { evalTo } });
expect(owner).toBeDefined();
},
);

it.each([[{ sortKey }], [{ blockHeight }]])(
it.each([
[{ sortKey: evaluateToSortKey.toString() }],
[{ blockHeight: evaluateToBlockHeight }],
])(
`should get controllers with evaluation options: ${JSON.stringify('%s')}`,
async (evalTo) => {
const controllers = await ant.getControllers({
Expand All @@ -82,31 +91,43 @@ describe('ANT contract apis', () => {
},
);

it.each([[{ sortKey }], [{ blockHeight }]])(
it.each([
[{ sortKey: evaluateToSortKey.toString() }],
[{ blockHeight: evaluateToBlockHeight }],
])(
`should get name with evaluation options: ${JSON.stringify('%s')}`,
async (evalTo) => {
const state = await ant.getName({ evaluationOptions: { evalTo } });
expect(state).toBeDefined();
},
);

it.each([[{ sortKey }], [{ blockHeight }]])(
it.each([
[{ sortKey: evaluateToSortKey.toString() }],
[{ blockHeight: evaluateToBlockHeight }],
])(
`should get ticker with evaluation options: ${JSON.stringify('%s')}`,
async (evalTo) => {
const state = await ant.getTicker({ evaluationOptions: { evalTo } });
expect(state).toBeDefined();
},
);

it.each([[{ sortKey }], [{ blockHeight }]])(
it.each([
[{ sortKey: evaluateToSortKey.toString() }],
[{ blockHeight: evaluateToBlockHeight }],
])(
`should get balances with evaluation options: ${JSON.stringify('%s')}`,
async (evalTo) => {
const state = await ant.getBalances({ evaluationOptions: { evalTo } });
expect(state).toBeDefined();
},
);

it.each([[{ sortKey }], [{ blockHeight }]])(
it.each([
[{ sortKey: evaluateToSortKey.toString() }],
[{ blockHeight: evaluateToBlockHeight }],
])(
`should get balance with evaluation options: ${JSON.stringify('%s')}`,
async (evalTo) => {
const state = await ant.getBalance({
Expand Down
34 changes: 14 additions & 20 deletions tests/ar-io.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import { ArweaveSigner } from 'arbundles';
import Arweave from 'arweave';

import { ArIO } from '../src/common/ar-io.js';
import { RemoteContract } from '../src/common/contracts/remote-contract.js';
import { ARNS_DEVNET_REGISTRY_TX } from '../src/constants.js';
import { ArIOState } from '../src/contract-state.js';
import { SmartWeaveSortKey } from '../src/utils/smartweave.js';

const arweave = Arweave.init({
host: 'arweave.net',
protocol: 'https',
port: 443,
});
const gatewayAddress = '1H7WZIWhzwTH9FIcnuMqYkTsoyv1OTfGa_amvuYwrgo';
const domain = 'angela';
const evaluateToBlockHeight = 1377100;
const evaluateToSortKey = new SmartWeaveSortKey(
'000001376946,0000000000000,18d52956c8e13ae1f557b4e67f6f298b8ffd2a5cd96e42ec24ca649b7401510f',
);
import {
arweave,
evaluateToBlockHeight,
evaluateToSortKey,
gatewayAddress,
localCacheUrl,
testDomain,
} from './constants.js';

describe('ArIO Client', () => {
let arIO: ArIO;
Expand All @@ -27,7 +21,7 @@ describe('ArIO Client', () => {
arIO = new ArIO({
contract: new RemoteContract<ArIOState>({
contractTxId: ARNS_DEVNET_REGISTRY_TX,
url: process.env.REMOTE_CACHE_URL || 'http://localhost:3000',
url: localCacheUrl,
}),
signer,
});
Expand Down Expand Up @@ -87,7 +81,7 @@ describe('ArIO Client', () => {
});

it('should return the record for an existing domain', async () => {
const record = await arIO.getArNSRecord({ domain });
const record = await arIO.getArNSRecord({ domain: testDomain });
expect(record).toBeDefined();
});

Expand All @@ -105,7 +99,7 @@ describe('ArIO Client', () => {

it('should return record at a given block height', async () => {
const currentRecord = await arIO.getArNSRecord({
domain,
domain: testDomain,
evaluationOptions: {
evalTo: { blockHeight: evaluateToBlockHeight + 1 },
},
Expand All @@ -115,7 +109,7 @@ describe('ArIO Client', () => {

it('should return record at a given sort key', async () => {
const record = await arIO.getArNSRecord({
domain,
domain: testDomain,
evaluationOptions: {
evalTo: { sortKey: evaluateToSortKey.toString() },
},
Expand All @@ -129,7 +123,7 @@ describe('ArIO Client', () => {
evalTo: { blockHeight: evaluateToBlockHeight },
},
});
expect(records[domain]).toBeDefined();
expect(records[testDomain]).toBeDefined();
});

it('should return records at a given sort key', async () => {
Expand All @@ -138,7 +132,7 @@ describe('ArIO Client', () => {
evalTo: { sortKey: evaluateToSortKey.toString() },
},
});
expect(records[domain]).toBeDefined();
expect(records[testDomain]).toBeDefined();
});

it('should return the current epoch information', async () => {
Expand Down
17 changes: 17 additions & 0 deletions tests/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Arweave from 'arweave';

import { SmartWeaveSortKey } from '../src/utils';

export const arweave = Arweave.init({
host: 'arweave.net',
protocol: 'https',
port: 443,
});
export const gatewayAddress = '1H7WZIWhzwTH9FIcnuMqYkTsoyv1OTfGa_amvuYwrgo';
export const testDomain = 'angela';
export const evaluateToBlockHeight = 1377100;
export const evaluateToSortKey = new SmartWeaveSortKey(
'000001376946,0000000000000,18d52956c8e13ae1f557b4e67f6f298b8ffd2a5cd96e42ec24ca649b7401510f',
);
export const localCacheUrl =
process.env.REMOTE_CACHE_URL || 'http://localhost:3000';

0 comments on commit 1bdcfeb

Please sign in to comment.