From f3523d1b1ccda970f59dc1781671078cbd307cf1 Mon Sep 17 00:00:00 2001 From: InversionSpaces Date: Wed, 18 Oct 2023 15:40:01 +0000 Subject: [PATCH] Quickstart test --- test/config.ts | 41 +++++++++++++++++++++++ test/integration.test.ts | 29 +++++++++++++--- test/utils.ts | 71 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 133 insertions(+), 8 deletions(-) create mode 100644 test/config.ts diff --git a/test/config.ts b/test/config.ts new file mode 100644 index 0000000..fef3102 --- /dev/null +++ b/test/config.ts @@ -0,0 +1,41 @@ +/** + * Copyright 2023 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { promises as fs } from "fs"; + +export const CONFIG_PATH = "./gateway/configs/quickstart_config.json"; + +/** + * Part of the config that is used for the integration tests. + */ +export interface GatewayConfig { + providers: string[]; + port: number; + mode: string; +} + +export async function readConfig(): Promise { + const file = await fs.readFile(CONFIG_PATH); + return JSON.parse(file.toString()); +} + +export async function updateConfig( + update: Partial +): Promise { + const current = await readConfig(); + const newConfig = { ...current, ...update }; + await fs.writeFile(CONFIG_PATH, JSON.stringify(newConfig, null, 2)); +} diff --git a/test/integration.test.ts b/test/integration.test.ts index 014181f..451f9ef 100644 --- a/test/integration.test.ts +++ b/test/integration.test.ts @@ -14,12 +14,31 @@ * limitations under the License. */ -import { fluence } from "./utils"; +import { startGateway } from "./utils"; describe("integration tests", () => { - it("should run integration tests", async () => { - const output = await fluence("build"); - console.log(output); - expect(true).toBeTruthy(); + it("should run quickstart", async () => { + const gateway = await startGateway(); + try { + const request = { + jsonrpc: "2.0", + method: "eth_blockNumber", + params: [], + id: 100, + }; + const response = await gateway.request(request); + expect(response).toMatchObject({ + jsonrpc: "2.0", + id: 100, + result: expect.any(String), + }); + } finally { + expect(gateway.stop()).toBeTruthy(); + } + + // @ts-ignore + console.log(process._getActiveHandles()); + // @ts-ignore + console.log(process._getActiveRequests()); }); }); diff --git a/test/utils.ts b/test/utils.ts index 575ea24..cc040d8 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -14,11 +14,17 @@ * limitations under the License. */ -import { execFile } from "child_process"; +import { relative } from "path"; +import { execFile, ChildProcess } from "child_process"; -export async function fluence(...args: string[]): Promise<[string, string]> { +import { CONFIG_PATH, readConfig } from "./config"; + +export async function execute( + cmd: string, + ...args: string[] +): Promise<[string, string]> { return new Promise((resolve, reject) => { - execFile("fluence", args, (error, stdout, stderr) => { + execFile(cmd, args, (error, stdout, stderr) => { if (error) { reject(error); } @@ -27,3 +33,62 @@ export async function fluence(...args: string[]): Promise<[string, string]> { }); }); } + +export async function fluence(...args: string[]): Promise<[string, string]> { + return execute("fluence", ...args); +} + +export class Gateway { + constructor( + private readonly gateway: ChildProcess, + private readonly port: number + ) {} + + public stop(): boolean { + if (this.gateway.stdin) { + this.gateway.stdin.end(); + } + if (this.gateway.stdout) { + this.gateway.stdout.destroy(); + } + if (this.gateway.stderr) { + this.gateway.stderr.destroy(); + } + if (this.gateway.pid) { + process.kill(this.gateway.pid); + } + return this.gateway.kill(); + } + + public async request(json: any): Promise { + const response = await fetch(`http://localhost:${this.port}`, { + method: "POST", + body: JSON.stringify(json), + headers: { + "Content-Type": "application/json", + Accept: "application/json", + }, + }); + + return await response.json(); + } +} + +export async function startGateway(): Promise { + const GATEWAY_DIR = "./gateway"; + const configPath = relative(GATEWAY_DIR, CONFIG_PATH); + + const config = await readConfig(); + const gateway = execFile("npm", [ + "-C", + GATEWAY_DIR, + "run", + "run", + configPath, + ]); + + // Hack: wait till gateway is ready + await new Promise((resolve) => setTimeout(resolve, 5000)); + + return new Gateway(gateway, config.port); +}