Files
AITrader/app/agents/__tests__/trader.test.ts
T

67 lines
2.0 KiB
TypeScript

import { describe, it, expect, vi } from "vitest";
import { Trader } from "../trader";
import type { AnalystReport, DebateRound } from "../../types/agents";
describe("Trader", () => {
const mockClient = {
createChatCompletion: vi.fn().mockResolvedValue({
choices: [{ message: { content: '{"action": "buy", "confidence": 0.75, "reasoning": "Strong bullish signals"}' } }],
}),
};
const mockReports: AnalystReport[] = [
{
analyst: "fundamentals",
report: "Strong earnings growth",
signal: {
agent: "fundamentals",
signal: "bullish",
confidence: 0.8,
reasoning: "Revenue up 20%",
timestamp: "2024-01-01",
},
},
];
const mockDebates: DebateRound[] = [
{
bullishView: "Strong fundamentals",
bearishView: "Market volatility",
researcher: "bullish",
},
];
it("should make trading decision", async () => {
const trader = new Trader(mockClient as any);
const decision = await trader.decide("AAPL", mockReports, mockDebates);
expect(decision.action).toBe("buy");
});
it("parses executionPlan on sell", async () => {
const mockSellClient = {
createChatCompletion: vi.fn().mockResolvedValue({
choices: [{ message: { content: JSON.stringify({
action: "sell",
confidence: 0.9,
reasoning: "Exit position",
executionPlan: {
amount: 50,
riskManagement: { maxLossPercent: 1.5, method: "trailing" },
takeProfit: 150,
note: "Test plan"
}
}) } }]
}),
};
const trader = new Trader(mockSellClient as any);
const decision = await trader.decide("AAPL", mockReports, mockDebates);
expect(decision.action).toBe("sell");
expect(decision.executionPlan).toBeDefined();
expect(decision.executionPlan?.amount).toBe(50);
expect(decision.executionPlan?.takeProfit).toBe(150);
expect(decision.executionPlan?.riskManagement?.maxLossPercent).toBe(1.5);
});
});
});