const fs = require("fs"); const assert = require("assert"); const { analyzeSinglePost, checkOllamaStatus } = require("../ai-analyzer"); console.log("AI Analyzer logic tests"); const testData = JSON.parse( fs.readFileSync(__dirname + "/test-data.json", "utf-8") ); const aiResults = testData.positive; const context = "job layoffs and workforce reduction"; const model = "mistral"; // or your default model (async () => { // Check if Ollama is available const ollamaAvailable = await checkOllamaStatus(model); if (!ollamaAvailable) { console.log("SKIP: Ollama not available - skipping AI analyzer tests"); console.log("PASS: AI analyzer tests skipped (Ollama not running)"); return; } console.log(`Testing AI analyzer with ${aiResults.length} posts...`); for (let i = 0; i < aiResults.length; i++) { const post = aiResults[i]; console.log(`Testing post ${i + 1}: "${post.text.substring(0, 50)}..."`); const aiOutput = await analyzeSinglePost(post.text, context, model); // Test that the function returns the expected structure assert( typeof aiOutput === "object" && aiOutput !== null, `Post ${i} output is not an object` ); assert( typeof aiOutput.isRelevant === "boolean", `Post ${i} isRelevant is not a boolean: ${typeof aiOutput.isRelevant}` ); assert( typeof aiOutput.confidence === "number", `Post ${i} confidence is not a number: ${typeof aiOutput.confidence}` ); assert( typeof aiOutput.reasoning === "string", `Post ${i} reasoning is not a string: ${typeof aiOutput.reasoning}` ); // Test that confidence is within valid range assert( aiOutput.confidence >= 0 && aiOutput.confidence <= 1, `Post ${i} confidence out of range: ${aiOutput.confidence} (should be 0-1)` ); // Test that reasoning exists and is not empty assert( aiOutput.reasoning && aiOutput.reasoning.length > 0, `Post ${i} missing or empty reasoning` ); // Test that relevance is a boolean value assert( aiOutput.isRelevant === true || aiOutput.isRelevant === false, `Post ${i} isRelevant is not a valid boolean: ${aiOutput.isRelevant}` ); console.log( ` ✓ Post ${i + 1}: relevant=${aiOutput.isRelevant}, confidence=${ aiOutput.confidence }` ); } console.log( "PASS: AI analyzer returns valid structure and values for all test posts." ); })();