Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo-SEQUIER committed Jan 9, 2025
1 parent e1ebb4a commit d9c7f49
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 39 deletions.
45 changes: 6 additions & 39 deletions packages/plugin-irys/src/services/irysService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TransactionsIdAddress> {
private async getTransactionId(owners: string[] = null, tags: GraphQLTag[] = null, timestamp: IrysTimestamp = null): Promise<TransactionsIdAddress> {
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,
Expand All @@ -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,
Expand Down Expand Up @@ -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<UploadIrysResult> {
async workerUploadDataOnIrys(data: any, dataType: IrysDataType, messageType: IrysMessageType, serviceCategory: string[], protocol: string[], validationThreshold: number[] = [], minimumProviders: number[] = [], testProvider: boolean[] = [], reputation: number[] = []): Promise<UploadIrysResult> {
this.normalizeArrayValues(validationThreshold, 0, 1);
this.normalizeArrayValues(minimumProviders, 0);
this.normalizeArrayValues(reputation, 0, 1);
Expand Down Expand Up @@ -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<DataIrysFetchedFromGQL> {
async getDataFromAnAgent(agentsWalletPublicKeys: string[] = null, tags: GraphQLTag[] = null, timestamp: IrysTimestamp = null): Promise<DataIrysFetchedFromGQL> {
try {
const transactionIdsResponse = await this.getTransactionId(agentsWalletPublicKeys, tags, timestamp);
if (!transactionIdsResponse.success) return { success: false, data: null, error: "Error fetching transaction IDs" };
Expand Down
62 changes: 62 additions & 0 deletions packages/plugin-irys/tests/provider.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

66 changes: 66 additions & 0 deletions packages/plugin-irys/tests/wallet.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

74 changes: 74 additions & 0 deletions packages/plugin-irys/tests/worker.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit d9c7f49

Please sign in to comment.