From d9c7f49b3f3b280173c61137b45a192df2f13476 Mon Sep 17 00:00:00 2001 From: Hugo Sequier Date: Thu, 9 Jan 2025 22:47:23 +0100 Subject: [PATCH] test --- .../plugin-irys/src/services/irysService.ts | 45 ++--------- packages/plugin-irys/tests/provider.test.ts | 62 ++++++++++++++++ packages/plugin-irys/tests/wallet.test.ts | 66 +++++++++++++++++ packages/plugin-irys/tests/worker.test.ts | 74 +++++++++++++++++++ 4 files changed, 208 insertions(+), 39 deletions(-) create mode 100644 packages/plugin-irys/tests/provider.test.ts create mode 100644 packages/plugin-irys/tests/wallet.test.ts create mode 100644 packages/plugin-irys/tests/worker.test.ts diff --git a/packages/plugin-irys/src/services/irysService.ts b/packages/plugin-irys/src/services/irysService.ts index e0b407c73a..c8275cb94b 100644 --- a/packages/plugin-irys/src/services/irysService.ts +++ b/packages/plugin-irys/src/services/irysService.ts @@ -52,16 +52,11 @@ export class IrysService extends Service implements IIrysService { this.runtime = runtime; } - private async getTransactionId(owners: string[], tags: GraphQLTag[], timestamp: IrysTimestamp = null): Promise { + private async getTransactionId(owners: string[] = null, tags: GraphQLTag[] = null, timestamp: IrysTimestamp = null): Promise { const graphQLClient = new GraphQLClient(this.endpointForTransactionId); - if (owners.length == 0 && tags.length == 0) { - return { success: false, data: [], error: "No owners or tags provided" }; - } - let QUERY = ""; - if (owners.length > 0 && tags.length > 0) { - QUERY = gql` - query($owners: [String!], $timestamp: TimestampFilter) { - transactions(owners: $owners, timestamp: $timestamp) { + const QUERY = gql` + query($owners: [String!], $tags: [TagFilter!], $timestamp: TimestampFilter) { + transactions(owners: $owners, tags: $tags, timestamp: $timestamp) { edges { node { id, @@ -71,34 +66,6 @@ export class IrysService extends Service implements IIrysService { } } `; - } else if (owners.length > 0) { - QUERY = gql` - query($owners: [String!], $timestamp: TimestampFilter) { - transactions(owners: $owners, timestamp: $timestamp) { - edges { - node { - id, - address - } - } - } - } - `; - } - else if (tags.length > 0) { - QUERY = gql` - query($tags: [TagFilter!], $timestamp: TimestampFilter) { - transactions(tags: $tags, timestamp: $timestamp) { - edges { - node { - id, - address - } - } - } - } - `; - } try { const variables = { owners: owners, @@ -262,7 +229,7 @@ export class IrysService extends Service implements IIrysService { } } - async workerUploadDataOnIrys(data: any, dataType: IrysDataType, messageType: IrysMessageType, serviceCategory: string[], protocol: string[], validationThreshold: number[], minimumProviders: number[], testProvider: boolean[], reputation: number[]): Promise { + async workerUploadDataOnIrys(data: any, dataType: IrysDataType, messageType: IrysMessageType, serviceCategory: string[], protocol: string[], validationThreshold: number[] = [], minimumProviders: number[] = [], testProvider: boolean[] = [], reputation: number[] = []): Promise { this.normalizeArrayValues(validationThreshold, 0, 1); this.normalizeArrayValues(minimumProviders, 0); this.normalizeArrayValues(reputation, 0, 1); @@ -298,7 +265,7 @@ export class IrysService extends Service implements IIrysService { return await this.uploadDataOnIrys(data, tags, IrysMessageType.DATA_STORAGE); } - async getDataFromAnAgent(agentsWalletPublicKeys: string[], tags: GraphQLTag[], timestamp: IrysTimestamp): Promise { + async getDataFromAnAgent(agentsWalletPublicKeys: string[] = null, tags: GraphQLTag[] = null, timestamp: IrysTimestamp = null): Promise { try { const transactionIdsResponse = await this.getTransactionId(agentsWalletPublicKeys, tags, timestamp); if (!transactionIdsResponse.success) return { success: false, data: null, error: "Error fetching transaction IDs" }; diff --git a/packages/plugin-irys/tests/provider.test.ts b/packages/plugin-irys/tests/provider.test.ts new file mode 100644 index 0000000000..0833172dbb --- /dev/null +++ b/packages/plugin-irys/tests/provider.test.ts @@ -0,0 +1,62 @@ +import { describe, it, expect, beforeEach, vi, afterEach } from "vitest"; +import { IrysService } from "../src/services/irysService"; +import { defaultCharacter, IrysDataType } from "@elizaos/core"; + +// Mock NodeCache +vi.mock("node-cache", () => { + return { + default: vi.fn().mockImplementation(() => ({ + set: vi.fn(), + get: vi.fn().mockReturnValue(null), + })), + }; +}); + +// Mock path module +vi.mock("path", async () => { + const actual = await vi.importActual("path"); + return { + ...actual, + join: vi.fn().mockImplementation((...args) => args.join("/")), + }; +}); + +// Mock the ICacheManager +const mockCacheManager = { + get: vi.fn().mockResolvedValue(null), + set: vi.fn(), + delete: vi.fn(), +}; + +describe("IrysService", () => { + let irysService; + let mockedRuntime; + + beforeEach(async () => { + vi.clearAllMocks(); + mockCacheManager.get.mockResolvedValue(null); + + mockedRuntime = { + character: defaultCharacter, + getSetting: vi.fn().mockImplementation((key: string) => { + if (key === "EVM_WALLET_PRIVATE_KEY") // TEST PRIVATE KEY + return "0xd6ed963c4eb8436b284f62636a621c164161ee25218b3be5ca4cad1261f8c390"; + return undefined; + }), + }; + irysService = new IrysService(); + await irysService.initialize(mockedRuntime); + }); + + afterEach(() => { + vi.clearAllTimers(); + }); + + describe("Store String on Irys", () => { + it("should store string on Irys", async () => { + const result = await irysService.providerUploadDataOnIrys("Hello World", IrysDataType.OTHER, ["test"], ["test"]); + expect(result.success).toBe(true); + }); + }); +}); + diff --git a/packages/plugin-irys/tests/wallet.test.ts b/packages/plugin-irys/tests/wallet.test.ts new file mode 100644 index 0000000000..0c1ffc4a14 --- /dev/null +++ b/packages/plugin-irys/tests/wallet.test.ts @@ -0,0 +1,66 @@ +import { describe, it, expect, beforeEach, vi, afterEach } from "vitest"; +import { IrysService } from "../src/services/irysService"; +import { defaultCharacter } from "@elizaos/core"; + +// Mock NodeCache +vi.mock("node-cache", () => { + return { + default: vi.fn().mockImplementation(() => ({ + set: vi.fn(), + get: vi.fn().mockReturnValue(null), + })), + }; +}); + +// Mock path module +vi.mock("path", async () => { + const actual = await vi.importActual("path"); + return { + ...actual, + join: vi.fn().mockImplementation((...args) => args.join("/")), + }; +}); + +// Mock the ICacheManager +const mockCacheManager = { + get: vi.fn().mockResolvedValue(null), + set: vi.fn(), + delete: vi.fn(), +}; + +describe("IrysService", () => { + let irysService; + let mockedRuntime; + + beforeEach(async () => { + vi.clearAllMocks(); + mockCacheManager.get.mockResolvedValue(null); + + mockedRuntime = { + character: defaultCharacter, + getSetting: vi.fn().mockImplementation((key: string) => { + if (key === "EVM_WALLET_PRIVATE_KEY") // TEST PRIVATE KEY + return "0xd6ed963c4eb8436b284f62636a621c164161ee25218b3be5ca4cad1261f8c390"; + return undefined; + }), + }; + irysService = new IrysService(); + await irysService.initialize(mockedRuntime); + }); + + afterEach(() => { + vi.clearAllTimers(); + }); + + describe("Initialize IrysService", () => { + it("should initialize IrysService", async () => { + expect(irysService).toBeDefined(); + }); + + it("should initialize IrysUploader", async () => { + const result = await irysService.initializeIrysUploader(); + expect(result).toBe(true); + }); + }); +}); + diff --git a/packages/plugin-irys/tests/worker.test.ts b/packages/plugin-irys/tests/worker.test.ts new file mode 100644 index 0000000000..55f94a2ee1 --- /dev/null +++ b/packages/plugin-irys/tests/worker.test.ts @@ -0,0 +1,74 @@ +import { describe, it, expect, beforeEach, vi, afterEach } from "vitest"; +import { IrysService } from "../src/services/irysService"; +import { defaultCharacter, IrysDataType, IrysMessageType } from "@elizaos/core"; + +// Mock NodeCache +vi.mock("node-cache", () => { + return { + default: vi.fn().mockImplementation(() => ({ + set: vi.fn(), + get: vi.fn().mockReturnValue(null), + })), + }; +}); + +// Mock path module +vi.mock("path", async () => { + const actual = await vi.importActual("path"); + return { + ...actual, + join: vi.fn().mockImplementation((...args) => args.join("/")), + }; +}); + +// Mock the ICacheManager +const mockCacheManager = { + get: vi.fn().mockResolvedValue(null), + set: vi.fn(), + delete: vi.fn(), +}; + +describe("IrysService", () => { + let irysService; + let mockedRuntime; + + beforeEach(async () => { + vi.clearAllMocks(); + mockCacheManager.get.mockResolvedValue(null); + + mockedRuntime = { + character: defaultCharacter, + getSetting: vi.fn().mockImplementation((key: string) => { + if (key === "EVM_WALLET_PRIVATE_KEY") // TEST PRIVATE KEY + return "0xd6ed963c4eb8436b284f62636a621c164161ee25218b3be5ca4cad1261f8c390"; + return undefined; + }), + }; + irysService = new IrysService(); + await irysService.initialize(mockedRuntime); + }); + + afterEach(() => { + vi.clearAllTimers(); + }); + + describe("Store String on Irys", () => { + it("should store string on Irys", async () => { + const result = await irysService.workerUploadDataOnIrys("Hello World", IrysDataType.OTHER, IrysMessageType.DATA_STORAGE, ["test"], ["test"]); + expect(result.success).toBe(true); + }); + + it("should retrieve data from Irys", async () => { + const result = await irysService.getDataFromAnAgent(["0xb9dBf1966c9C6E4D93b4C61bbC4cDDb32900f4bE"], []); + expect(result.success).toBe(true); + expect(result.data.length).toBeGreaterThan(0); + }); + + it("should get a response from the orchestrator", async () => { + const result = await irysService.workerUploadDataOnIrys("Hello World", IrysDataType.OTHER, IrysMessageType.REQUEST, ["test"], ["test"]); + expect(result.success).toBe(true); + expect(result.data.length).toBeGreaterThan(0); + }); + }); +}); +