test: mock AnthropicClient in analyze tests to prevent live API calls

This commit is contained in:
saikiranvella
2026-06-08 12:08:37 -04:00
parent 17bc985260
commit 357dfb8249
31 changed files with 415 additions and 171 deletions
+2 -5
View File
@@ -19,10 +19,8 @@ export class FinanceController {
app.get('/api/finance/market-context', this.marketContext.bind(this));
}
private async portfolio(_req: FastifyRequest, reply: FastifyReply) {
if (!this.repo.exists()) return reply.code(404).send({ error: 'portfolio.json not found' });
const { holdings } = this.repo.read();
private async portfolio(_req: FastifyRequest, _reply: FastifyReply) {
const { holdings } = this.repo.exists() ? this.repo.read() : { holdings: [] };
let personalFinance = null;
if (process.env.SIMPLEFIN_ACCESS_URL) {
@@ -58,7 +56,6 @@ export class FinanceController {
private async removeHolding(req: FastifyRequest, reply: FastifyReply) {
const ticker = (req.params as { ticker: string }).ticker.toUpperCase();
if (!this.repo.exists()) return reply.code(404).send({ error: 'portfolio.json not found' });
const removed = this.repo.remove(ticker);
if (!removed) return reply.code(404).send({ error: 'Holding not found' });
@@ -26,10 +26,8 @@ export class AnalyzeController {
t.toUpperCase(),
);
// Use cached catalyst data (refreshed every 15 minutes)
const { stories: allStories } = await this.catalystCache.get();
// Filter stories to only those matching requested tickers
const stories = allStories.filter((story) =>
story.tickers.some((t) => requestedTickers.includes(t)),
);
@@ -37,7 +35,12 @@ export class AnalyzeController {
if (!stories.length) return reply.code(200).send({ analysis: null, reason: 'no_stories' });
const { tickerFrequency } = CatalystAnalyst.rankTickers(stories);
const analysis = await this.llm.analyze(stories, requestedTickers, tickerFrequency);
let analysis = null;
try {
analysis = await this.llm.analyze(stories, requestedTickers, tickerFrequency);
} catch (err) {
req.log.error({ err }, 'LLM analysis failed');
}
return { analysis };
}
}
@@ -21,7 +21,7 @@ export class AnthropicClient {
async complete(system: string, userMessage: string): Promise<string | null> {
if (!this.client) return null;
const response = await this.client.messages.create({
model: 'claude-haiku-4-5',
model: 'claude-haiku-4-5-20251001',
max_tokens: 1024,
system,
messages: [{ role: 'user', content: userMessage }],
+9 -16
View File
@@ -1,6 +1,5 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { fileURLToPath } from 'url';
import { AnthropicClient } from '../adapters/AnthropicClient';
import type { Logger, LLMAnalysis, Story } from '../types/index';
@@ -47,21 +46,15 @@ export class LLMAnalyst {
const userMessage = `Today's market news headlines:\n\n${headlines}\n${freqSection}\nAlready identified catalyst tickers: ${existingTickers.join(', ') || 'none'}`;
try {
const PROMPT_FILE = '../../prompts/llm-analyst.md';
const PROMPT_PATH = join(fileURLToPath(import.meta.url), PROMPT_FILE);
const SYSTEM_PROMPT = readFileSync(PROMPT_PATH, 'utf8');
const PROMPT_PATH = join(process.cwd(), 'prompts', 'llm-analyst.md');
const SYSTEM_PROMPT = readFileSync(PROMPT_PATH, 'utf8');
const raw = await this.client.complete(SYSTEM_PROMPT, userMessage);
if (!raw) return null;
const cleaned = raw
.replace(/^```(?:json)?\s*/i, '')
.replace(/```\s*$/i, '')
.trim();
return JSON.parse(cleaned) as LLMAnalysis;
} catch (err) {
this.logger.warn('LLMAnalyst: analysis failed —', (err as Error).message);
return null;
}
const raw = await this.client.complete(SYSTEM_PROMPT, userMessage);
if (!raw) return null;
const cleaned = raw
.replace(/^```(?:json)?\s*/i, '')
.replace(/```\s*$/i, '')
.trim();
return JSON.parse(cleaned) as LLMAnalysis;
}
}