Skip to content

Commit

Permalink
Quickstart test
Browse files Browse the repository at this point in the history
  • Loading branch information
InversionSpaces committed Oct 18, 2023
1 parent ce4f780 commit f3523d1
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 8 deletions.
41 changes: 41 additions & 0 deletions test/config.ts
Original file line number Diff line number Diff line change
@@ -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<GatewayConfig> {
const file = await fs.readFile(CONFIG_PATH);
return JSON.parse(file.toString());
}

export async function updateConfig(
update: Partial<GatewayConfig>
): Promise<void> {
const current = await readConfig();
const newConfig = { ...current, ...update };
await fs.writeFile(CONFIG_PATH, JSON.stringify(newConfig, null, 2));
}
29 changes: 24 additions & 5 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
});
71 changes: 68 additions & 3 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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<any> {
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<Gateway> {
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);
}

0 comments on commit f3523d1

Please sign in to comment.