Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
feat: reworking test util (#296)
Browse files Browse the repository at this point in the history
Co-authored-by: Matias Volpe <[email protected]>
  • Loading branch information
harrysolovay and kratico committed Oct 19, 2022
1 parent a04e83d commit 0c3a36b
Show file tree
Hide file tree
Showing 30 changed files with 276 additions and 245 deletions.
3 changes: 0 additions & 3 deletions _tasks/run_browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { contentType } from "https://deno.land/x/[email protected]/mod.ts";
import { Application, send } from "https://deno.land/x/[email protected]/mod.ts";
import { babel, babelPresetTypeScript } from "https://escad.dev/deps/babel.ts";
import * as path from "../deps/std/path.ts";
import * as t from "../test_util/mod.ts";

const dirname = path.dirname(path.fromFileUrl(import.meta.url));

Expand Down Expand Up @@ -59,8 +58,6 @@ app.use(async (ctx) => {
});
});

await t.config({ port: 9944 });

const rootFilePath = getTranspiledPath(transformUrl(rootFile));
await Deno.writeTextFile(
path.join(transpiledDir, "index.html"),
Expand Down
File renamed without changes.
13 changes: 11 additions & 2 deletions config/mod.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import * as rpc from "../rpc/mod.ts";

/** We represent as a class, not a branded type, because we want to extend into a pretty signature. */
export class Config<
DiscoveryValue = any,
RpcCallMethods extends rpc.ProviderMethods = rpc.ProviderMethods,
RpcSubscriptionMethods extends rpc.ProviderMethods = rpc.ProviderMethods,
RpcErrorDetails extends rpc.ErrorDetails = rpc.ErrorDetails,
> {
// TODO: get rid of this gunk
RpcMethods!: RpcCallMethods & RpcSubscriptionMethods;
RpcCallMethods!: RpcCallMethods;
RpcSubscriptionMethods!: RpcSubscriptionMethods;
RpcErrorDetails!: RpcErrorDetails;

#discoveryValue?: DiscoveryValue | Promise<DiscoveryValue>;

constructor(
readonly discoveryValue: DiscoveryValue,
readonly initDiscoveryValue: () => DiscoveryValue | Promise<DiscoveryValue>,
readonly addressPrefix: number,
) {}

get discoveryValue() {
if (!this.#discoveryValue) {
this.#discoveryValue = this.initDiscoveryValue();
}
return this.#discoveryValue;
}
}
7 changes: 3 additions & 4 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@
"include": ["."],
"tasks": {
"run": "deno run -A --no-check=remote",
"run:browser": "deno task run _tasks/run_browser.ts",
"run:browser": "deno task run test_ctx.ts deno task run _tasks/run_browser.ts",
"debug": "deno task run --inspect-brk",
"download:frame_metadata": "deno task run _tasks/download_frame_metadata.ts",
"udd": "deno task star && deno task run https://deno.land/x/[email protected]/main.ts _star.ts",
"dnt": "deno task run _tasks/dnt.ts",
"star": "deno task run _tasks/star.ts && deno cache --no-check=remote _star.ts",
"lock": "deno task star --lock=lock.json --lock-write",
"lint": "deno lint",
"test": "deno test -A --no-check=remote -L=info --ignore=target",
"test": "deno task run test_ctx.ts deno test -A --no-check=remote -L=info --ignore=target --parallel",
"test:update": "deno task test -- -- --update",
"bench": "deno bench -A --no-check=remote --unstable",
"codegen": "deno task run main.ts"
"bench": "deno bench -A --no-check=remote --unstable"
}
}
1 change: 1 addition & 0 deletions deps/polkadot/util-crypto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/x/[email protected]/util-crypto/mod.ts";
32 changes: 10 additions & 22 deletions effect/extrinsic.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import type { KeyringPair } from "https://deno.land/x/[email protected]/keyring/types.ts";
import { assertEquals, assertObjectMatch } from "../deps/std/testing/asserts.ts";
import * as C from "../mod.ts";
import * as t from "../test_util/mod.ts";
import * as T from "../test_util/mod.ts";
import * as U from "../util/mod.ts";

Deno.test({
name: "Balances.transfer",
fn: async (ctx) => {
const config = await t.config({ altRuntime: "westend" });

await ctx.step("extrinsic events", async () => {
const extrinsicEvents: string[] = await collectExtrinsicEvents(
{
config,
config: T.westend,
palletName: "Balances",
methodName: "transfer",
args: {
value: 12345n,
dest: {
type: "Id",
value: t.bob.publicKey,
value: T.bob.publicKey,
},
},
},
t.alice,
T.alice,
);

assertEquals(extrinsicEvents, ["ready", "inBlock", "finalized"]);
Expand All @@ -32,56 +30,48 @@ Deno.test({
await ctx.step({
name: "account balance updated",
fn: async () => {
const root = C.readEntry(config, "System", "Account", [t.bob.publicKey]);
const root = C.readEntry(T.westend, "System", "Account", [T.bob.publicKey]);

const state = await root.run();

assertObjectMatch(state, { value: { data: { free: 10000000000012345n } } });
},
});

config.close();
},
});

Deno.test({
name: "Treasury.propose_spend",
fn: async (ctx) => {
const config = await t.config({ altRuntime: "westend" });

await ctx.step("extrinsic events", async () => {
const extrinsicEvents: string[] = await collectExtrinsicEvents(
{
config,
config: T.westend,
palletName: "Treasury",
methodName: "propose_spend",
args: {
value: 200n,
beneficiary: {
type: "Id",
value: t.bob.publicKey,
value: T.bob.publicKey,
},
},
},
t.alice,
T.alice,
);

assertEquals(extrinsicEvents, ["ready", "inBlock", "finalized"]);
});

config.close();
},
});

Deno.test({
name: "Democracy.propose",
fn: async (ctx) => {
const config = await t.config({ altRuntime: "westend" });

await ctx.step("extrinsic events", async () => {
const extrinsicEvents: string[] = await collectExtrinsicEvents(
{
config,
config: T.westend,
palletName: "Democracy",
methodName: "propose",
args: {
Expand All @@ -91,13 +81,11 @@ Deno.test({
value: 2000000000000n,
},
},
t.alice,
T.alice,
);

assertEquals(extrinsicEvents, ["ready", "inBlock", "finalized"]);
});

config.close();
},
});

Expand Down
8 changes: 2 additions & 6 deletions examples/balance.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as C from "../mod.ts";
import * as t from "../test_util/mod.ts";
import * as T from "../test_util/mod.ts";
import * as U from "../util/mod.ts";

const config = await t.config();

const root = C.readEntry(config, "System", "Account", [t.alice.publicKey]);
const root = C.readEntry(T.polkadot, "System", "Account", [T.alice.publicKey]);

console.log(U.throwIfError(await root.run()));

config.close();
8 changes: 2 additions & 6 deletions examples/first_ten_keys.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as C from "../mod.ts";
import * as t from "../test_util/mod.ts";
import * as T from "../test_util/mod.ts";
import * as U from "../util/mod.ts";

const config = await t.config();

const root = C.readKeyPage(config, "System", "Account", 10);
const root = C.readKeyPage(T.polkadot, "System", "Account", 10);

console.log(U.throwIfError(await root.run()).keys);

config.close();
8 changes: 2 additions & 6 deletions examples/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as C from "../mod.ts";
import * as t from "../test_util/mod.ts";
import * as T from "../test_util/mod.ts";
import * as U from "../util/mod.ts";

const config = await t.config();

const root = C.metadata(config);
const root = C.metadata(T.polkadot);

console.log(U.throwIfError(await root.run()));

config.close();
14 changes: 5 additions & 9 deletions examples/polkadot_signer.ts → examples/polkadot_js_signer.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { TypeRegistry } from "../deps/polkadot/types.ts";
import * as C from "../mod.ts";
import * as t from "../test_util/mod.ts";
import * as T from "../test_util/mod.ts";
import * as U from "../util/mod.ts";

const config = await t.config({ altRuntime: "westend" });

const root = C.sendAndWatchExtrinsic({
config,
config: T.westend,
sender: {
type: "Id",
value: t.alice.publicKey,
value: T.alice.publicKey,
},
palletName: "Balances",
methodName: "transfer",
args: {
value: 12345n,
dest: {
type: "Id",
value: t.bob.publicKey,
value: T.bob.publicKey,
},
},
sign: {
Expand All @@ -27,7 +25,7 @@ const root = C.sendAndWatchExtrinsic({
return Promise.resolve(
tr
.createType("ExtrinsicPayload", payload, { version: payload.version })
.sign(t.alice),
.sign(T.alice),
);
},
},
Expand All @@ -51,5 +49,3 @@ const root = C.sendAndWatchExtrinsic({
});

U.throwIfError(await root.run());

config.close();
8 changes: 2 additions & 6 deletions examples/rpc_subscription.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import * as C from "../mod.ts";
import * as t from "../test_util/mod.ts";
import * as T from "../test_util/mod.ts";
import * as U from "../util/mod.ts";

const config = await t.config();

const root = C.rpcSubscription(config, "chain_subscribeNewHead", [], (stop) => {
const root = C.rpcSubscription(T.polkadot, "chain_subscribeNewHead", [], (stop) => {
let i = 0;
return (m) => {
i++;
Expand All @@ -16,5 +14,3 @@ const root = C.rpcSubscription(config, "chain_subscribeNewHead", [], (stop) => {
});

U.throwIfError(await root.run());

config.close();
10 changes: 3 additions & 7 deletions examples/those_of_subxt/read_bonded.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import * as C from "../../mod.ts";
import * as t from "../../test_util/mod.ts";
import * as T from "../../test_util/mod.ts";
import * as U from "../../util/mod.ts";

const config = await t.config();
const aliceStash = T.alice.derive("//stash");

const aliceStash = t.alice.derive("//stash");

const aliceBonded = C.readEntry(config, "Staking", "Bonded", [aliceStash.publicKey]);
const aliceBonded = C.readEntry(T.polkadot, "Staking", "Bonded", [aliceStash.publicKey]);

console.log(U.throwIfError(await aliceBonded.run()));

config.close();
8 changes: 2 additions & 6 deletions examples/ticker.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import * as C from "../mod.ts";
import * as t from "../test_util/mod.ts";
import * as T from "../test_util/mod.ts";
import * as U from "../util/mod.ts";

const config = await t.config();

const root = C.watchEntry(config, "Timestamp", "Now", [], () => {
const root = C.watchEntry(T.polkadot, "Timestamp", "Now", [], () => {
let i = 0;
return (m) => {
console.log({ [i]: m });
Expand All @@ -13,5 +11,3 @@ const root = C.watchEntry(config, "Timestamp", "Now", [], () => {
});

U.throwIfError(await root.run());

config.close();
14 changes: 5 additions & 9 deletions examples/transfer.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import * as C from "../mod.ts";
import * as t from "../test_util/mod.ts";
import * as T from "../test_util/mod.ts";
import * as U from "../util/mod.ts";

const config = await t.config({ altRuntime: "westend" });

const root = C.sendAndWatchExtrinsic({
config,
config: T.westend,
sender: {
type: "Id",
value: t.alice.publicKey,
value: T.alice.publicKey,
},
palletName: "Balances",
methodName: "transfer",
args: {
value: 12345n,
dest: {
type: "Id",
value: t.bob.publicKey,
value: T.bob.publicKey,
},
},
sign(message) {
return {
type: "Sr25519",
value: t.alice.sign(message),
value: T.alice.sign(message),
};
},
createWatchHandler(stop) {
Expand All @@ -45,5 +43,3 @@ const root = C.sendAndWatchExtrinsic({
});

U.throwIfError(await root.run());

config.close();
10 changes: 5 additions & 5 deletions frame_metadata/Codec.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertEquals } from "../deps/std/testing/asserts.ts";
import * as t from "../test_util/mod.ts";
import * as T from "../test_util/mod.ts";
import * as U from "../util/mod.ts";
import { ChainError } from "./Codec.ts";
import { ContractMetadata } from "./Contract.ts";
Expand All @@ -16,9 +16,9 @@ Deno.test("Derive all", async () => {
Deno.test("Derive AccountId32 Codec", async () => {
const [_, deriveCodec] = await setup("polkadot");
const codec = deriveCodec(0);
const encoded = codec.encode(t.alice.publicKey);
assertEquals(encoded, t.alice.publicKey);
assertEquals(codec.decode(encoded), t.alice.publicKey);
const encoded = codec.encode(T.alice.publicKey);
assertEquals(encoded, T.alice.publicKey);
assertEquals(codec.decode(encoded), T.alice.publicKey);
});

Deno.test("Derive AccountInfo Codec", async () => {
Expand Down Expand Up @@ -57,7 +57,7 @@ Deno.test("Derive Auction Winning Storage Entry Codec", async () => {
const codec = deriveCodec(auctionWinningStorageEntry.value);
const decoded = [
...Array(7).fill(undefined),
[t.alice.publicKey, 2013, 8672334557167609n],
[T.alice.publicKey, 2013, 8672334557167609n],
...Array(28).fill(undefined),
];
const encoded = codec.encode(decoded);
Expand Down
Loading

0 comments on commit 0c3a36b

Please sign in to comment.