Skip to content

test: e2e scenarios #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/metadata/src/providers/cachingProxy.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { z } from "zod";
import { ICache } from "@grants-stack-indexer/repository";
import { ICacheable, ILogger } from "@grants-stack-indexer/shared";

import { IMetadataProvider } from "../internal.js";
import { IMetadataProvider, isValidCid } from "../internal.js";

/**
* A metadata provider that caches metadata lookups from the underlying provider.
Expand All @@ -26,6 +26,10 @@ export class CachingMetadataProvider implements IMetadataProvider, ICacheable {

/** @inheritdoc */
async getMetadata<T>(ipfsCid: string, validateContent?: z.ZodSchema<T>): Promise<T | null> {
if (ipfsCid === "" || !isValidCid(ipfsCid)) {
return null;
}

let cachedMetadata: T | null | undefined = undefined;
try {
cachedMetadata = (await this.cache.get(ipfsCid)) as T | null;
Expand Down
14 changes: 13 additions & 1 deletion packages/metadata/test/providers/cachingProxy.provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("CachingMetadataProvider", () => {
});

describe("getMetadata", () => {
const testCid = "QmTest123";
const testCid = "bafkreihrjyu5tney6wia2hmkertc74nzfpsgxw2epvnxm72bxj6ifnd4ku";
const testData = { foo: "bar" };
const testSchema = z.object({ foo: z.string() });

Expand Down Expand Up @@ -96,5 +96,17 @@ describe("CachingMetadataProvider", () => {
expect(result).toBeNull();
expect(mockCache.set).toHaveBeenCalled();
});

it("returns null when cid is empty", async () => {
const result = await provider.getMetadata("", testSchema);

expect(result).toBeNull();
});

it("returns null when cid is not valid", async () => {
const result = await provider.getMetadata("invalid", testSchema);

expect(result).toBeNull();
});
});
});
38 changes: 25 additions & 13 deletions tests/e2e/src/mocks/envio-graphql-server.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,7 @@ export class MockEnvioIndexer {
*/
private createGraphQLHandler(): RequestHandler {
return (req: Request, res: Response): void => {
const { query } = req.body as GraphQLRequest;

if (query.includes("getEventsAfterBlockNumberAndLogIndex")) {
const events = [...this.events];
this.events = [];
res.json({ data: { raw_events: events } });
return;
}

if (query.includes("getEvents")) {
res.json({ data: { raw_events: this.events } });
return;
}
const { query, variables } = req.body as GraphQLRequest;

if (query.includes("getTotalEventsInBlock")) {
res.json({
Expand All @@ -104,6 +92,30 @@ export class MockEnvioIndexer {
return;
}

// Extract chainId from variables if present, fallback to query string extraction
const chainId = variables?.chainId
? Number(variables.chainId)
: ((): number | null => {
const chainIdMatch = query.match(/chainId:\s*(\d+)/);
return chainIdMatch ? parseInt(chainIdMatch[1]!, 10) : null;
})();

// Filter events by chainId if specified
const events = chainId
? [...this.events].filter((event) => event.chainId === chainId)
: [...this.events];

// remove filtered events from the original events array
this.events = this.events.filter((event) => !events.includes(event));

if (
query.includes("getEventsAfterBlockNumberAndLogIndex") ||
query.includes("getEvents")
) {
res.json({ data: { raw_events: events } });
return;
}

res.json({ data: null });
};
}
Expand Down
41 changes: 36 additions & 5 deletions tests/e2e/src/utils/processing-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ChildProcess, spawn } from "child_process";
import path from "path";

import { stringify } from "@grants-stack-indexer/shared";

import { StartupFailed } from "../exceptions/index.js";
import { BASE_NODE_ENV_VARS } from "./constants.js";

Expand All @@ -13,6 +15,13 @@ interface ProcessingServiceConfig {
indexerAdminSecret: string;
logLevel?: string;
nodeEnv?: string;
chains?: Array<{
id: number;
name: string;
rpcUrls: string[];
fetchLimit?: number;
fetchDelayMs?: number;
}>;
}

/**
Expand All @@ -24,13 +33,19 @@ interface ProcessingServiceConfig {
* - Process lifecycle management
* - Configurable service parameters
* - Startup validation
* - Multi-chain support
*
* @example
* ```typescript
* const config = {
* databaseUrl: "postgresql://...",
* indexerGraphQLUrl: "http://localhost:4000/v1/graphql",
* indexerAdminSecret: "test-secret"
* indexerAdminSecret: "test-secret",
* chains: [{
* id: 1,
* name: "mainnet",
* rpcUrls: ["https://eth.llamarpc.com"]
* }]
* };
*
* const service = new ProcessingService(config);
Expand All @@ -41,10 +56,20 @@ interface ProcessingServiceConfig {
export class ProcessingServiceManager {
private process: ChildProcess | null = null;
private readonly processingPath: string;
private CHAIN_ID: number = 1;
private readonly defaultChains = [
{
id: 1,
name: "mainnet",
rpcUrls: ["https://eth.llamarpc.com", "https://rpc.flashbots.net/fast"],
fetchLimit: 30,
fetchDelayMs: 50,
},
];
private chains: ProcessingServiceConfig["chains"];

constructor(private readonly config: ProcessingServiceConfig) {
this.processingPath = path.resolve(__dirname, "../../../../apps/processing");
this.chains = this.config.chains ?? this.defaultChains;
}

/**
Expand Down Expand Up @@ -78,7 +103,7 @@ export class ProcessingServiceManager {
DATABASE_SCHEMA: "public",

// Chain Configuration
CHAINS: `[{"id":${this.CHAIN_ID},"name":"mainnet","rpcUrls":["https://eth.llamarpc.com","https://rpc.flashbots.net/fast"],"fetchLimit":30,"fetchDelayMs":50}]`,
CHAINS: stringify(this.chains),

// Logging
LOG_LEVEL: this.config.logLevel ?? "debug",
Expand Down Expand Up @@ -128,10 +153,16 @@ export class ProcessingServiceManager {
private setupProcessHandlers(resolve: () => void, reject: (error: Error) => void): void {
if (!this.process) return;

let startedChains = 0;
const totalChains = this.config.chains?.length ?? 1;

this.process.stdout?.on("data", (data) => {
const output = (data as Buffer).toString();
if (output.includes(`Starting orchestrator for chain ${this.CHAIN_ID}`)) {
resolve();
if (output.includes("Starting orchestrator for chain")) {
startedChains++;
if (startedChains === totalChains) {
resolve();
}
}
});

Expand Down
16 changes: 15 additions & 1 deletion tests/e2e/src/utils/test-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ import type { GlobalTestState } from "./test-environment.js";
import { DatabaseManager } from "./database-manager.js";
import { ProcessingServiceManager } from "./processing-service.js";

/**
* Configuration for test chains
*/
export interface TestChainConfig {
id: number;
name: string;
rpcUrls: string[];
fetchLimit?: number;
fetchDelayMs?: number;
}

/**
* TestHelper is a utility class for managing the test environment on a per-test-file scope
* It provides methods for starting and stopping the processing service,
Expand All @@ -15,6 +26,7 @@ import { ProcessingServiceManager } from "./processing-service.js";
* - Processing service management
* - Database management
* - Event management
* - Multi-chain support
*/
export class TestHelper {
private processingService: ProcessingServiceManager | null = null;
Expand All @@ -30,12 +42,14 @@ export class TestHelper {

/**
* Starts the processing service
* @param chains - Optional chain configurations to use
*/
public async startProcessingService(): Promise<void> {
public async startProcessingService(chains?: TestChainConfig[]): Promise<void> {
this.processingService = new ProcessingServiceManager({
databaseUrl: this.globalState.databaseUrl,
indexerGraphQLUrl: `${this.globalState.envioIndexerUrl}/v1/graphql`,
indexerAdminSecret: "secret", // for mock server it's not used, so we passed a dummy value
chains,
});
await this.processingService.start();
}
Expand Down
Loading
Loading