Skip to content

Commit

Permalink
fix e2e tests for remaining packages
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes-mysten committed Jan 8, 2025
1 parent 91b0b49 commit d1ca9ad
Show file tree
Hide file tree
Showing 37 changed files with 920 additions and 264 deletions.
4 changes: 1 addition & 3 deletions packages/deepbook-v3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
"eslint:check": "eslint --max-warnings=0 .",
"eslint:fix": "pnpm run eslint:check --fix",
"lint": "pnpm run eslint:check && pnpm run prettier:check",
"lint:fix": "pnpm run eslint:fix && pnpm run prettier:fix",
"test:e2e": "wait-on http://127.0.0.1:9123 -l --timeout 120000 && vitest run e2e",
"prepare:e2e": "cargo build --bin sui --profile dev && cross-env RUST_LOG=warn,sui=error,anemo_tower=warn,consensus=off cargo run --bin sui -- start --with-faucet --force-regenesis"
"lint:fix": "pnpm run eslint:fix && pnpm run prettier:fix"
},
"dependencies": {
"@mysten/sui": "workspace:*"
Expand Down
5 changes: 2 additions & 3 deletions packages/deepbook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
"eslint:fix": "pnpm run eslint:check --fix",
"lint": "pnpm run eslint:check && pnpm run prettier:check",
"lint:fix": "pnpm run eslint:fix && pnpm run prettier:fix",
"test:e2e": "wait-on http://127.0.0.1:9123 -l --timeout 120000 && vitest run e2e",
"prepare:e2e": "cargo build --bin sui --profile dev && cross-env RUST_LOG=warn,sui=error,anemo_tower=warn,consensus=off cargo run --bin sui -- start --with-faucet --force-regenesis"
"test:e2e": "vitest run"
},
"dependencies": {
"@mysten/sui": "workspace:*"
Expand All @@ -46,7 +45,7 @@
"ts-retry-promise": "^0.8.1",
"typescript": "^5.7.2",
"vite": "^6.0.7",
"vitest": "^2.0.1",
"vitest": "^2.1.8",
"wait-on": "^8.0.1"
}
}
8 changes: 4 additions & 4 deletions packages/deepbook/test/e2e/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { beforeAll, describe, expect, it } from 'vitest';

import { DeepBookClient } from '../../src';
import { DeepBookClient } from '../../src/index.js';
import { Level2BookStatusPoint, PoolSummary } from '../../src/types';
import {
DEFAULT_LOT_SIZE,
Expand All @@ -27,20 +27,20 @@ describe('Interacting with the pool', () => {

beforeAll(async () => {
toolbox = await setupSuiClient();
pool = await setupPool(toolbox);
accountCapId = await setupDeepbookAccount(toolbox);
accountCapId2 = await setupDeepbookAccount(toolbox);
});

it('test creating a pool', async () => {
pool = await setupPool(toolbox);
expect(pool.poolId).toBeDefined();
const deepbook = new DeepBookClient(toolbox.client);
const pools = await deepbook.getAllPools({});
expect(pools.data.some((p) => p.poolId === pool.poolId)).toBeTruthy();
});

it('test creating a custodian account', async () => {
accountCapId = await setupDeepbookAccount(toolbox);
expect(accountCapId).toBeDefined();
accountCapId2 = await setupDeepbookAccount(toolbox);
expect(accountCapId2).toBeDefined();
});

Expand Down
44 changes: 23 additions & 21 deletions packages/deepbook/test/e2e/data/test_coin/sources/test_coin.move
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

module test_coin::test {
use std::option;
use sui::coin;
use sui::transfer;
use sui::url;
use sui::tx_context::{Self, TxContext};
module test_coin::test;

public struct TEST has drop {}
use sui::coin;
use sui::url;

fun init(witness: TEST, ctx: &mut TxContext) {
let (mut treasury_cap, metadata) = coin::create_currency<TEST>(
witness,
2,
b"TEST",
b"Test Coin",
b"Test coin metadata",
option::some(url::new_unsafe_from_bytes(b"http://sui.io")),
ctx
);
public struct TEST has drop {}

coin::mint_and_transfer<TEST>(&mut treasury_cap, 1000, tx_context::sender(ctx), ctx);
fun init(witness: TEST, ctx: &mut TxContext) {
let (mut treasury_cap, metadata) = coin::create_currency<TEST>(
witness,
2,
b"TEST",
b"Test Coin",
b"Test coin metadata",
option::some(url::new_unsafe_from_bytes(b"http://sui.io")),
ctx,
);

transfer::public_share_object(metadata);
transfer::public_share_object(treasury_cap)
}
coin::mint_and_transfer<TEST>(
&mut treasury_cap,
1000,
tx_context::sender(ctx),
ctx,
);

transfer::public_share_object(metadata);
transfer::public_share_object(treasury_cap)
}
70 changes: 70 additions & 0 deletions packages/deepbook/test/e2e/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { resolve } from 'path';
import { GenericContainer, Network } from 'testcontainers';
import type { GlobalSetupContext } from 'vitest/node';

declare module 'vitest' {
export interface ProvidedContext {
localnetPort: number;
graphqlPort: number;
faucetPort: number;
suiToolsContainerId: string;
}
}

const SUI_TOOLS_TAG = process.env.SUI_TOOLS_TAG || '2e256a70aa0ff81972ded6ebd57f7679e2ea194d-arm64';

export default async function setup({ provide }: GlobalSetupContext) {
console.log('Starting test containers');
const network = await new Network().start();

const pg = await new GenericContainer('postgres')
.withEnvironment({
POSTGRES_USER: 'postgres',
POSTGRES_PASSWORD: 'postgrespw',
POSTGRES_DB: 'sui_indexer_v2',
})
.withCommand(['-c', 'max_connections=500'])

.withExposedPorts(5432)
.withNetwork(network)
.start();

const localnet = await new GenericContainer(`mysten/sui-tools:${SUI_TOOLS_TAG}`)
.withCommand([
'sui',
'start',
'--with-faucet',
'--force-regenesis',
'--with-indexer',
'--pg-port',
'5432',
'--pg-db-name',
'sui_indexer_v2',
'--pg-host',
pg.getIpAddress(network.getName()),
'--pg-user',
'postgres',
'--pg-password',
'postgrespw',
'--with-graphql',
])
.withCopyDirectoriesToContainer([
{ source: resolve(__dirname, './data'), target: '/test-data' },
])
.withNetwork(network)
.withExposedPorts(9000, 9123, 9124, 9125)
.withLogConsumer((stream) => {
stream.on('data', (data) => {
console.log(data.toString());
});
})
.start();

provide('faucetPort', localnet.getMappedPort(9123));
provide('localnetPort', localnet.getMappedPort(9124));
provide('graphqlPort', localnet.getMappedPort(9125));
provide('suiToolsContainerId', localnet.getId());
}
65 changes: 44 additions & 21 deletions packages/deepbook/test/e2e/setup.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { execSync } from 'child_process';
import { mkdtemp } from 'fs/promises';
import { tmpdir } from 'os';
import path from 'path';
import type {
DevInspectResults,
Expand All @@ -15,17 +12,17 @@ import { getFullnodeUrl, SuiClient } from '@mysten/sui/client';
import { FaucetRateLimitError, getFaucetHost, requestSuiFromFaucetV0 } from '@mysten/sui/faucet';
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
import { Transaction } from '@mysten/sui/transactions';
import tmp from 'tmp';
import type { ContainerRuntimeClient } from 'testcontainers';
import { getContainerRuntimeClient } from 'testcontainers';
import { retry } from 'ts-retry-promise';
import { expect } from 'vitest';
import { expect, inject } from 'vitest';

import { DeepBookClient } from '../../src/index.js';
import type { PoolSummary } from '../../src/types/index.js';
import { FLOAT_SCALING_FACTOR, NORMALIZED_SUI_COIN_TYPE } from '../../src/utils/index.js';

const DEFAULT_FAUCET_URL = process.env.FAUCET_URL ?? getFaucetHost('localnet');
const DEFAULT_FULLNODE_URL = process.env.FULLNODE_URL ?? getFullnodeUrl('localnet');
const SUI_BIN = process.env.VITE_SUI_BIN ?? 'cargo run --bin sui';

export const DEFAULT_TICK_SIZE = 1n * FLOAT_SCALING_FACTOR;
export const DEFAULT_LOT_SIZE = 1n;
Expand Down Expand Up @@ -70,39 +67,45 @@ export async function setupSuiClient() {
logger: (msg) => console.warn('Retrying requesting from faucet: ' + msg),
});

const tmpDirPath = path.join(tmpdir(), 'config-');
const tmpDir = await mkdtemp(tmpDirPath);
const configPath = path.join(tmpDir, 'client.yaml');
execSync(`${SUI_BIN} client --yes --client.config ${configPath}`, { encoding: 'utf-8' });
const configPath = path.join(
'/test-data',
`${Math.random().toString(36).substring(2, 15)}-client.yaml`,
);
await execSuiTools(['client', '--yes', '--client.config', configPath]);
return new TestToolbox(keypair, client, configPath);
}

// TODO: expose these testing utils from @mysten/sui
export async function publishPackage(packageName: string, toolbox?: TestToolbox) {
// TODO: We create a unique publish address per publish, but we really could share one for all publishes.
if (!toolbox) {
toolbox = await setupSuiClient();
}

// remove all controlled temporary objects on process exit
tmp.setGracefulCleanup();
const result = await execSuiTools([
'move',
'--client.config',
toolbox.configPath,
'build',
'--dump-bytecode-as-base64',
'--path',
`/test-data/${packageName}`,
]);

if (!result.stdout.includes('{')) {
console.error(result.stdout);
throw new Error('Failed to publish package');
}

const tmpobj = tmp.dirSync({ unsafeCleanup: true });
const { modules, dependencies } = JSON.parse(result.stdout.slice(result.stdout.indexOf('{')));

const { modules, dependencies } = JSON.parse(
execSync(
`${SUI_BIN} move move --client.config ${toolbox.configPath} build --dump-bytecode-as-base64 --path /test-data/${packageName} --install-dir ${tmpobj.name}`,
{ encoding: 'utf-8' },
),
);
const tx = new Transaction();
const cap = tx.publish({
modules,
dependencies,
});

// Transfer the upgrade capability to the sender so they can upgrade the package later if they want.
tx.transferObjects([cap], await toolbox.address());
tx.transferObjects([cap], toolbox.address());

const { digest } = await toolbox.client.signAndExecuteTransaction({
transaction: tx,
Expand Down Expand Up @@ -166,6 +169,9 @@ export async function executeTransaction(
showObjectChanges: true,
},
});
await toolbox.client.waitForTransaction({
digest: resp.digest,
});
expect(resp.effects?.status.status).toEqual('success');
return resp;
}
Expand All @@ -179,3 +185,20 @@ export async function devInspectTransaction(
sender: toolbox.address(),
});
}

export async function execSuiTools(
command: string[],
options?: Parameters<ContainerRuntimeClient['container']['exec']>[2],
) {
const client = await getContainerRuntimeClient();
const SUI_TOOLS_CONTAINER_ID = inject('suiToolsContainerId');
const container = client.container.getById(SUI_TOOLS_CONTAINER_ID);

const result = await client.container.exec(container, ['sui', ...command], options);

if (result.stderr) {
console.log(result.stderr);
}

return result;
}
12 changes: 12 additions & 0 deletions packages/deepbook/test/e2e/setupEnv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { inject } from 'vitest';

Object.entries({
FAUCET_URL: `http://localhost:${inject('faucetPort')}`,
FULLNODE_URL: `http://localhost:${inject('localnetPort')}`,
GRAPHQL_URL: `http://localhost:${inject('graphqlPort')}`,
}).forEach(([key, value]) => {
process.env[key] = value;
});
10 changes: 10 additions & 0 deletions packages/deepbook/test/e2e/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",
"include": ["../../**/*.ts"],
"compilerOptions": {
"module": "CommonJS",
"outDir": "dist/cjs",
"isolatedModules": true,
"rootDir": "../.."
}
}
2 changes: 2 additions & 0 deletions packages/deepbook/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export default defineConfig({
env: {
NODE_ENV: 'test',
},
setupFiles: ['test/e2e/setupEnv.ts'],
globalSetup: ['test/e2e/globalSetup.ts'],
},
resolve: {
alias: {},
Expand Down
4 changes: 1 addition & 3 deletions packages/graphql-transport/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
"prepublishOnly": "pnpm build",
"prettier:check": "prettier -c --ignore-unknown .",
"prettier:fix": "prettier -w --ignore-unknown .",
"test:e2e:nowait": "vitest run e2e",
"test:e2e:prepare": "docker-compose down && docker-compose up -d && cargo build --bin sui --profile dev && cross-env RUST_LOG=info,sui=error,anemo_tower=warn,consensus=off cargo run --bin sui -- start --with-faucet --force-regenesis --with-indexer --pg-port 5435 --pg-db-name sui_indexer_v2 --with-graphql",
"test:e2e": "wait-on http://127.0.0.1:9123 -l --timeout 180000 && vitest"
"test:e2e": "vitest run"
},
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit d1ca9ad

Please sign in to comment.