import dotenv from 'dotenv';
dotenv.config();
import { OpenAIVisionParser } from './build/scraper/openai_vision_parser.js';
import { existsSync } from 'fs';
async function testTnokFix() {
console.log('π§ͺ Testing TNOK Unit Detection Fix');
console.log('ββββββββββββββββββββββββββββββββββββββ\n');
const apiKey = process.env.OPENAI_API_KEY;
if (!apiKey) {
console.error('β OPENAI_API_KEY not found!');
process.exit(1);
}
const parser = new OpenAIVisionParser(apiKey);
// Test with the problematic PDF mentioned
const testPdf = 'data/pdfs/928102939/aarsregnskap_928102939-2023.pdf';
if (!existsSync(testPdf)) {
console.log('β οΈ Test PDF not found, trying alternative...');
// Try any available PDF
const alternatives = [
'data/pdfs/984562861/aarsregnskap_984562861-2010.pdf',
'data/pdfs/999059198/aarsregnskap_999059198-2021.pdf',
'data/pdfs/temp/aarsregnskap_999059198-2021.pdf'
];
let found = false;
for (const alt of alternatives) {
if (existsSync(alt)) {
console.log(`β
Using alternative: ${alt}`);
await testPdf(parser, alt);
found = true;
break;
}
}
if (!found) {
console.error('β No test PDFs available');
process.exit(1);
}
} else {
await testPdfExtraction(parser, testPdf);
}
}
async function testPdfExtraction(parser, pdfPath) {
console.log(`π Testing PDF: ${pdfPath}`);
console.log('π Starting extraction (this may take 30-60 seconds)...\n');
try {
const startTime = Date.now();
const result = await parser.parseFinancialPDF(pdfPath);
const duration = Math.round((Date.now() - startTime) / 1000);
console.log('\nββββββββββββββββββββββββββββββββββββββ');
console.log('π EXTRACTION RESULTS');
console.log('ββββββββββββββββββββββββββββββββββββββ\n');
console.log(`β±οΈ Time: ${duration} seconds\n`);
// Check if values look reasonable (not 1000x too large)
const checkValue = (name, value) => {
if (value === null) {
console.log(`${name}: Not found`);
return;
}
const millions = value / 1000000;
const billions = value / 1000000000;
// Flag if value seems unreasonably large (> 100 billion NOK for small companies)
if (billions > 100) {
console.log(`${name}: ${billions.toFixed(1)}B NOK β οΈ SEEMS TOO LARGE!`);
console.log(` Raw value: ${value}`);
console.log(` If TNOK, should be: ${(value/1000/1000000).toFixed(1)}M NOK`);
} else if (billions > 1) {
console.log(`${name}: ${billions.toFixed(1)}B NOK`);
} else {
console.log(`${name}: ${millions.toFixed(1)}M NOK β
`);
}
};
checkValue('π° Revenue', result.revenue);
checkValue('π Profit', result.profit);
checkValue('π’ Assets', result.assets);
checkValue('π Equity', result.equity);
// Analyze if values seem correct
console.log('\nπ Analysis:');
const hasLargeValues = [result.revenue, result.profit, result.assets, result.equity]
.filter(v => v !== null && v > 100000000000); // > 100 billion
if (hasLargeValues.length > 0) {
console.log('β Some values appear to be 1000x too large!');
console.log(' This suggests TNOK values were incorrectly multiplied.');
} else {
console.log('β
All values appear to be in the correct range!');
console.log(' TNOK handling seems to be working correctly.');
}
} catch (error) {
console.error('β Extraction failed:', error.message);
console.error('\nThis might indicate an issue with the parser.');
}
}
testTnokFix();