Skip to content

Commit

Permalink
indexer: add test for run
Browse files Browse the repository at this point in the history
  • Loading branch information
jaipaljadeja committed Jun 27, 2024
1 parent b99f419 commit 634019c
Show file tree
Hide file tree
Showing 8 changed files with 442 additions and 8 deletions.
221 changes: 221 additions & 0 deletions packages/indexer/src/indexer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
import type { StreamDataResponse } from "@apibara/protocol";
import { type MockBlock, MockClient } from "@apibara/protocol/testing";
import { describe, expect, it } from "vitest";
import { run } from "./indexer";
import { vcr } from "./testing";
import { type MockRet, mockIndexer } from "./testing/indexer";

describe("Run Test", () => {
const client = new MockClient(messages, [{}]);

it("should stream messages", async () => {
const sink = vcr<MockRet>();
await run(client, mockIndexer, sink);

expect(sink.result).toMatchInlineSnapshot(`
[
{
"data": [
{
"block_number": 5000000,
},
],
"endCursor": {
"orderKey": 5000000n,
},
},
{
"data": [
{
"block_number": 5000001,
},
],
"endCursor": {
"orderKey": 5000001n,
},
},
{
"data": [
{
"block_number": 5000002,
},
],
"endCursor": {
"orderKey": 5000002n,
},
},
{
"data": [
{
"block_number": 5000003,
},
],
"endCursor": {
"orderKey": 5000003n,
},
},
{
"data": [
{
"block_number": 5000004,
},
],
"endCursor": {
"orderKey": 5000004n,
},
},
{
"data": [
{
"block_number": 5000005,
},
],
"endCursor": {
"orderKey": 5000005n,
},
},
{
"data": [
{
"block_number": 5000006,
},
],
"endCursor": {
"orderKey": 5000006n,
},
},
{
"data": [
{
"block_number": 5000007,
},
],
"endCursor": {
"orderKey": 5000007n,
},
},
{
"data": [
{
"block_number": 5000008,
},
],
"endCursor": {
"orderKey": 5000008n,
},
},
{
"data": [
{
"block_number": 5000009,
},
],
"endCursor": {
"orderKey": 5000009n,
},
},
{
"data": [
{
"block_number": 5000010,
},
],
"endCursor": {
"orderKey": 5000010n,
},
},
]
`);
});
});

const messages: StreamDataResponse<MockBlock>[] = [
{
_tag: "data",
data: {
finality: "accepted",
data: [{ block_number: 5_000_000n }],
endCursor: { orderKey: 5_000_000n },
},
},
{
_tag: "data",
data: {
finality: "accepted",
data: [{ block_number: 5_000_001n }],
endCursor: { orderKey: 5_000_001n },
},
},
{
_tag: "data",
data: {
finality: "accepted",
data: [{ block_number: 5_000_002n }],
endCursor: { orderKey: 5_000_002n },
},
},
{
_tag: "data",
data: {
finality: "accepted",
data: [{ block_number: 5_000_003n }],
endCursor: { orderKey: 5_000_003n },
},
},
{
_tag: "data",
data: {
finality: "accepted",
data: [{ block_number: 5_000_004n }],
endCursor: { orderKey: 5_000_004n },
},
},
{
_tag: "data",
data: {
finality: "accepted",
data: [{ block_number: 5_000_005n }],
endCursor: { orderKey: 5_000_005n },
},
},
{
_tag: "data",
data: {
finality: "accepted",
data: [{ block_number: 5_000_006n }],
endCursor: { orderKey: 5_000_006n },
},
},
{
_tag: "data",
data: {
finality: "accepted",
data: [{ block_number: 5_000_007n }],
endCursor: { orderKey: 5_000_007n },
},
},
{
_tag: "data",
data: {
finality: "accepted",
data: [{ block_number: 5_000_008n }],
endCursor: { orderKey: 5_000_008n },
},
},
{
_tag: "data",
data: {
finality: "accepted",
data: [{ block_number: 5_000_009n }],
endCursor: { orderKey: 5_000_009n },
},
},
{
_tag: "data",
data: {
finality: "accepted",
data: [{ block_number: 5_000_010n }],
endCursor: { orderKey: 5_000_010n },
},
},
];
21 changes: 21 additions & 0 deletions packages/indexer/src/testing/indexer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { MockStream } from "@apibara/protocol/testing";
import { createIndexer, defineIndexer } from "../indexer";

export const mockIndexer = createIndexer(
defineIndexer(MockStream)({
streamUrl: "https://sepolia.ethereum.a5a.ch",
finality: "accepted",
filter: {},
transform({ block: { block_number } }) {
if (!block_number) return [];

const res: MockRet[] = [{ block_number: Number(block_number) }];

return res;
},
}),
);

export type MockRet = {
block_number: number;
};
12 changes: 5 additions & 7 deletions packages/indexer/src/testing/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import {
replay,
} from "../vcr";

export function test() {
return viTest.extend({
vcr: {
withClient,
},
});
}
export const test = viTest.extend({
vcr: {
withClient,
},
});

type WithClientContext<TFilter, TBlock, TRet> = {
run: (
Expand Down
9 changes: 9 additions & 0 deletions packages/protocol/proto/testing.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";

package dna.v2.testing;

message MockFilter {}

message MockBlock {
uint64 block_number = 1;
}
3 changes: 2 additions & 1 deletion packages/protocol/src/proto/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * as common from "./common";
export * as stream from "./stream";
export * as stream from "./stream";
export * as testing from './testing'
Loading

0 comments on commit 634019c

Please sign in to comment.