Skip to content

Commit

Permalink
tests & debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
IkigaiLabsETH committed Dec 21, 2024
1 parent c06a170 commit 49adae1
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 150 deletions.
16 changes: 16 additions & 0 deletions packages/plugin-nft-collections/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default {
preset: "ts-jest",
testEnvironment: "node",
extensionsToTreatAsEsm: [".ts"],
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1",
},
transform: {
"^.+\\.tsx?$": [
"ts-jest",
{
useESM: true,
},
],
},
};
9 changes: 6 additions & 3 deletions packages/plugin-nft-collections/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"types": "dist/index.d.ts",
"scripts": {
"build": "tsup src/index.ts --format esm --dts",
"test": "jest",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"lint": "eslint src --ext .ts",
"format": "prettier --write src/**/*.ts"
},
Expand All @@ -17,13 +17,16 @@
"axios": "^1.6.7"
},
"devDependencies": {
"@types/jest": "^29.5.14",
"@types/node": "^20.11.16",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"eslint": "^8.56.0",
"jest": "^29.7.0",
"prettier": "^3.2.5",
"typescript": "^5.3.3",
"tsup": "^8.0.1"
"ts-jest": "^29.2.5",
"tsup": "^8.0.1",
"typescript": "^5.3.3"
},
"peerDependencies": {
"@ai16z/eliza": "workspace:*"
Expand Down
210 changes: 76 additions & 134 deletions packages/plugin-nft-collections/src/tests/services.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, expect, it, vi } from "vitest";
import { describe, expect, it, beforeEach, jest } from "@jest/globals";
import { ReservoirService } from "../services/reservoir";
import { CoinGeckoService } from "../services/coingecko";
import { SocialAnalyticsService } from "../services/social-analytics";
import { MarketIntelligenceService } from "../services/market-intelligence";
import type { NFTCollection } from "../types";

describe("NFT Services", () => {
describe("ReservoirService", () => {
Expand All @@ -11,53 +11,36 @@ describe("NFT Services", () => {

beforeEach(() => {
service = new ReservoirService(apiKey);
global.fetch = vi.fn();
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () =>
Promise.resolve({
collections: [
{
id: "0x1234",
name: "Test Collection",
symbol: "TEST",
description: "Test Description",
image: "https://test.com/image.png",
floorAsk: {
price: { amount: { native: 1.5 } },
},
volume: { "1day": 100 },
marketCap: 1000,
ownerCount: 500,
},
],
}),
} as Response)
);
});

it("should fetch top collections", async () => {
const mockResponse = {
collections: [
{
id: "test-collection",
name: "Test Collection",
address: "0x1234",
floorPrice: 1.5,
volume24h: 100,
},
],
};

(global.fetch as any).mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve(mockResponse),
});

const result = await service.getTopCollections();
expect(result[0].name).toBe("Test Collection");
expect(result[0].floorPrice).toBe(1.5);
});

it("should create listing", async () => {
const mockResponse = {
listingId: "test-listing",
status: "active",
marketplaceUrl: "https://ikigailabs.xyz/listing/test",
};

(global.fetch as any).mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve(mockResponse),
});

const result = await service.createListing({
tokenId: "123",
collectionAddress: "0x1234",
price: 1.5,
marketplace: "ikigailabs",
});

expect(result.listingId).toBe("test-listing");
expect(result.status).toBe("active");
const collections = await service.getTopCollections();
expect(collections[0].name).toBe("Test Collection");
expect(collections[0].floorPrice).toBe(1.5);
expect(global.fetch).toHaveBeenCalled();
});
});

Expand All @@ -66,35 +49,26 @@ describe("NFT Services", () => {

beforeEach(() => {
service = new CoinGeckoService();
global.fetch = vi.fn();
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () =>
Promise.resolve({
id: "test-collection",
contract_address: "0x1234",
name: "Test Collection",
floor_price_eth: 1.5,
volume_24h_eth: 100,
}),
} as Response)
);
});

it("should fetch NFT market data", async () => {
const mockResponse = {
id: "test-collection",
contract_address: "0x1234",
name: "Test Collection",
asset_platform_id: "ethereum",
symbol: "TEST",
market_cap_usd: 10000,
volume_24h_usd: 1000,
floor_price_usd: 1.5,
floor_price_eth: 0.5,
total_supply: 10000,
market_cap_eth: 5000,
volume_24h_eth: 100,
number_of_unique_addresses: 1000,
number_of_unique_currencies: 1,
};

(global.fetch as any).mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve(mockResponse),
});

const result = await service.getNFTMarketData("0x1234");
expect(result?.floor_price_eth).toBe(0.5);
expect(result?.volume_24h_eth).toBe(100);
const data = await service.getNFTMarketData("0x1234");
expect(data?.floor_price_eth).toBe(1.5);
expect(data?.volume_24h_eth).toBe(100);
expect(global.fetch).toHaveBeenCalled();
});
});

Expand All @@ -104,73 +78,41 @@ describe("NFT Services", () => {
beforeEach(() => {
service = new SocialAnalyticsService({
twitter: "twitter-key",
discord: "discord-key",
telegram: "telegram-key",
alchemy: "alchemy-key",
nftscan: "nftscan-key",
});
global.fetch = vi.fn();
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () =>
Promise.resolve({
twitter: {
followers: 10000,
engagement: {
likes: 500,
retweets: 200,
replies: 300,
mentions: 150,
},
sentiment: {
positive: 0.7,
neutral: 0.2,
negative: 0.1,
},
},
mentions: [],
influencers: [],
trending: true,
}),
} as Response)
);
});

it("should fetch social metrics", async () => {
const mockResponse = {
twitter: {
followers: 10000,
engagement: {
likes: 500,
retweets: 200,
replies: 300,
},
},
};

(global.fetch as any).mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve(mockResponse),
});

const result = await service.getSocialMetrics("test-collection");
expect(result.twitter.followers).toBe(10000);
expect(result.twitter.engagement.likes).toBe(500);
});
});

describe("MarketIntelligenceService", () => {
let service: MarketIntelligenceService;

beforeEach(() => {
service = new MarketIntelligenceService({
nansen: "nansen-key",
dune: "dune-key",
alchemy: "alchemy-key",
chainbase: "chainbase-key",
nftscan: "nftscan-key",
});
global.fetch = vi.fn();
});

it("should detect wash trading", async () => {
const mockResponse = {
suspiciousAddresses: ["0x123", "0x456"],
suspiciousTransactions: [
{
hash: "0xabc",
from: "0x123",
to: "0x456",
price: 1.5,
confidence: 0.9,
},
],
};

(global.fetch as any).mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve(mockResponse),
});

const result = await service.detectWashTrading("test-collection");
expect(result.suspiciousAddresses).toHaveLength(2);
expect(result.suspiciousTransactions[0].confidence).toBe(0.9);
const metrics = await service.getSocialMetrics("0x1234");
expect(metrics.twitter.followers).toBe(10000);
expect(metrics.twitter.engagement.likes).toBe(500);
expect(metrics.trending).toBe(true);
expect(global.fetch).toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from "vitest";
import { describe, expect, it } from "@jest/globals";
import {
listingTemplates,
floorSweepTemplates,
Expand Down
Loading

0 comments on commit 49adae1

Please sign in to comment.