import dotenv from 'dotenv';
dotenv.config();
import { OpenAIVisionParser } from './build/scraper/openai_vision_parser.js';
import { existsSync } from 'fs';
async function testCorrectValues() {
console.log('π§ͺ Testing Correct Value Extraction (No TNOK Multiplication)');
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 PDF that had issues
const testPdf = 'data/pdfs/928102939/aarsregnskap_928102939-2023.pdf';
if (!existsSync(testPdf)) {
console.error(`β Test PDF not found: ${testPdf}`);
console.error(' Please download it first using browser scraper');
process.exit(1);
}
console.log(`π Testing with: ${testPdf}`);
console.log('π‘ Norwegian Γ₯rsregnskap show FULL values in NOK');
console.log(' Example: "20 623" = 20,623 NOK (not 20.6M)');
console.log('\nπ Starting extraction...\n');
try {
const startTime = Date.now();
const result = await parser.parseFinancialPDF(testPdf);
const duration = Math.round((Date.now() - startTime) / 1000);
console.log('\nββββββββββββββββββββββββββββββββββββββ');
console.log('π EXTRACTION RESULTS');
console.log('ββββββββββββββββββββββββββββββββββββββ\n');
console.log(`β±οΈ Time: ${duration} seconds\n`);
// Expected values for company 928102939 year 2023:
// Profit: -20,623 NOK (loss of twenty thousand)
// Assets: 48,000 NOK
// Equity: 30,001 NOK
const formatValue = (name, value, expectedRange) => {
if (value === null) {
console.log(`${name}: Not found`);
return;
}
// Format based on size
if (Math.abs(value) < 1000) {
console.log(`${name}: ${value.toLocaleString('no-NO')} NOK`);
} else if (Math.abs(value) < 1000000) {
const thousands = value / 1000;
console.log(`${name}: ${value.toLocaleString('no-NO')} NOK (${thousands.toFixed(1)}K)`);
} else {
const millions = value / 1000000;
console.log(`${name}: ${value.toLocaleString('no-NO')} NOK (${millions.toFixed(1)}M)`);
}
// Check if in expected range
if (expectedRange && (value < expectedRange.min || value > expectedRange.max)) {
console.log(` β οΈ WARNING: Value outside expected range ${expectedRange.min}-${expectedRange.max}`);
} else if (expectedRange) {
console.log(` β
Value in expected range`);
}
};
// Based on the JSON you showed, expected values (in NOK):
formatValue('π° Revenue', result.revenue, null);
formatValue('π Profit', result.profit, { min: -30000, max: -10000 }); // Expecting around -20K
formatValue('π’ Assets', result.assets, { min: 40000, max: 60000 }); // Expecting around 48K
formatValue('π Equity', result.equity, { min: 25000, max: 35000 }); // Expecting around 30K
console.log('\nπ Analysis:');
// Check if values look correct for a small company
const seemsCorrect =
(!result.profit || (result.profit > -1000000 && result.profit < 1000000)) &&
(!result.assets || (result.assets > 10000 && result.assets < 10000000)) &&
(!result.equity || (result.equity > 10000 && result.equity < 10000000));
if (seemsCorrect) {
console.log('β
Values appear correct for a small Norwegian company!');
console.log(' No unnecessary multiplication detected.');
} else {
console.log('β Some values still seem too large!');
console.log(' There may still be an issue with value extraction.');
}
// Save corrected data
console.log('\nπΎ Corrected data ready for database:');
console.log(JSON.stringify({
year: 2023,
revenue: result.revenue,
profit: result.profit,
assets: result.assets,
equity: result.equity,
source: 'openai_vision_extraction_fixed'
}, null, 2));
} catch (error) {
console.error('β Extraction failed:', error.message);
}
}
testCorrectValues();