Skip to content

Commit

Permalink
Test passed for cancelTask and deleteTask
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisli30 committed Oct 26, 2024
1 parent 6a6c816 commit 2a97440
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 33 deletions.
30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ Then, run the following command to regenerate the types:

```bash
# download the latest .proto file from https://github.com/AvaProtocol/EigenLayer-AVS
npm run proto-download
npm run proto-download

# Generate the TypeScript types and gRPC code based on the downloaded .proto file
npm run gen-protoc
npm run gen-protoc
```

> Important: the last line of the `grpc_codegen/avs_pb.js` needs to be manually added after the `gen-protoc` command. These type definitions must be exported; otherwise they will be undefined in the SDK. For example: `export const { Task, CreateTaskReq, CreateTaskResp, GetKeyReq, KeyResp, UpdateChecksReq, UpdateChecksResp, AddressResp, AddressRequest } = proto.aggregator;`
Expand All @@ -57,29 +57,27 @@ npm run gen-protoc

To ensure the SDK is functioning correctly, we have a comprehensive test suite. Follow these steps to run the tests:

1. Make sure all dependencies are installed:
1. Make sure all dependencies are installed, and build the project. Tests are run against the files in the `/dist` folder
```bash
npm install
npm run build
```
2. Before running the e2e tests, make sure to configure the required environment variables in your `.env.test` file, based on the `.env.example` file.

3. Run the test command. This will test the SDK against test server, configured in `.env.test`.

2. Run the test command. This will test the SDK against mockup responses.
```bash
# Run all tests
npm test
```

3. To test the SDK against the live AVS on Ethereum Mainnet, you can run the following command:
```bash
npm run test:e2e
```

Before running the e2e tests, make sure to configure the required TEST values in your `.env` file:

```
TEST_JWT_TOKEN=your_TEST_JWT_TOKEN_here
TEST_OWNER=your_test_owner_here
# or, run a specific test
npm run test:select -- <authWithSignature>
```

Replace `your_TEST_JWT_TOKEN_here` with a valid JWT API key and `your_test_owner_here` with the appropriate owner address for testing.
4. In order to individually test `cancelTask` or `deleteTask`, `createTask` test needs to run first.
```bash
npm run test:select -- "createTask|cancelTask"
```

This will execute all unit and integration tests. Make sure all tests pass before submitting a pull request or deploying changes.

Expand Down
1 change: 1 addition & 0 deletions grpc_codegen/avs_pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -7508,4 +7508,5 @@ export const {
ActionType,
ContractExecution,
TaskType,
UUID,
} = proto.aggregator;
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@babel/preset-env": "^7.26.0",
"@babel/preset-typescript": "^7.26.0",
"@jest/globals": "^29.7.0",
"@types/google-protobuf": "^3.15.12",
"@types/jest": "^29.5.13",
"@types/lodash": "^4.17.12",
"babel-jest": "^29.7.0",
Expand Down
48 changes: 45 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Metadata } from "@grpc/grpc-js";
import { getKeyRequestMessage } from "./auth";
import { AggregatorClient } from "../grpc_codegen/avs_grpc_pb";
import * as avs_pb from "../grpc_codegen/avs_pb";
import { BoolValue } from "google-protobuf/google/protobuf/wrappers_pb";
import Task from "./task";

const metadata = new grpc.Metadata();

// Move interfaces to a separate file, e.g., types.ts
Expand Down Expand Up @@ -164,7 +164,7 @@ export default class Client extends BaseClient {
address: string;
tokenContract: string;
oracleContract: string;
}): Promise<object> {
}): Promise<CreateTaskResponse> {
const trigger = new avs_pb.TaskTrigger();
trigger.setTriggerType(avs_pb.TriggerType.EXPRESSIONTRIGGER);
trigger.setExpression(
Expand Down Expand Up @@ -206,7 +206,9 @@ export default class Client extends BaseClient {

console.log("createTask.result:", result.toObject());

return result.toObject();
return {
id: result.getId(),
};
}

async listTasks(address: string): Promise<ListTasksResponse> {
Expand All @@ -230,6 +232,46 @@ export default class Client extends BaseClient {
tasks: tasks,
};
}

// TODO: specify the return type to match client’s requirements
// Right now we simply return the original object from the server
async getTask(id: string): Promise<object> {
const request = new avs_pb.UUID();
request.setBytes(id);

const result = await this._callRPC<avs_pb.Task, avs_pb.UUID>(
"getTask",
request
);

console.log("getTask.result:", result.toObject());

return result.toObject();
}

async cancelTask(id: string): Promise<boolean> {
const request = new avs_pb.UUID();
request.setBytes(id);

const result = await this._callRPC<BoolValue, avs_pb.UUID>(
"cancelTask",
request
);

return result.getValue();
}

async deleteTask(id: string): Promise<boolean> {
const request = new avs_pb.UUID();
request.setBytes(id);

const result = await this._callRPC<BoolValue, avs_pb.UUID>(
"deleteTask",
request
);

return result.getValue();
}
}

// Export types for easier use
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface Task {
}

export interface CreateTaskResponse {
task: Task;
id: string;
}

export interface ListTasksResponse {
Expand Down
47 changes: 34 additions & 13 deletions tests/Client.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ describe("Client E2E Tests", () => {
describe("Authenticated Tests", () => {
let walletAddress: string;
let client: Client;
let createdTaskId: string; // Add this line to declare the variable

beforeAll(async () => {
if (!ENDPOINT) {
Expand Down Expand Up @@ -181,6 +182,9 @@ describe("Client E2E Tests", () => {
console.log("Create task result:", result);
expect(result).toBeDefined();
expect(result).toHaveProperty("id");

// Important: save the task ID to the suite for the following tests
createdTaskId = result.id;
});

test("listTasks", async () => {
Expand All @@ -189,18 +193,35 @@ describe("Client E2E Tests", () => {
expect(Array.isArray(result.tasks)).toBe(true);
});

// test("getTask", async () => {
// // First, list tasks to get a valid task ID
// const listResult = await client.listTask();
// if (listResult.tasks.length > 0) {
// const taskId = listResult.tasks[0].id;
// const result = await client.getTask(taskId);
// console.log("Get task result:", result);
// expect(result).toBeDefined();
// expect(result.id).toBe(taskId);
// } else {
// console.warn("No tasks available to test getTask");
// }
// });
test("getTask", async () => {
// Use the saved task ID
if (createdTaskId) {
const result = await client.getTask(createdTaskId);
console.log("Get task result:", result);
expect(result).toBeDefined();
} else {
console.warn("No task ID available to test getTask");
}
});

test("cancelTask", async () => {
if (createdTaskId) {
const result = await client.cancelTask(createdTaskId);
console.log("Cancel task result:", result);
expect(result).toBe(true);
} else {
console.warn("No task ID available to test cancelTask");
}
});

test("deleteTask", async () => {
if (createdTaskId) {
const result = await client.deleteTask(createdTaskId);
console.log("Delete task result:", result);
expect(result).toBe(true);
} else {
console.warn("No task ID available to test deleteTask");
}
});
});
});

0 comments on commit 2a97440

Please sign in to comment.