const fs = require('fs');
const jsdom = require("jsdom");
const { JSDOM } = jsdom;

const html = fs.readFileSync('index.html', 'utf8');
const dom = new JSDOM(html, {
    url: "http://localhost/",
    runScripts: "dangerously"
});

const window = dom.window;
global.window = window;
global.document = window.document;
global.navigator = window.navigator;
window.indexedDB = { open: () => ({ onupgradeneeded: null, onsuccess: null, onerror: null }) };

try {
    window.eval(fs.readFileSync('db.js', 'utf8'));
    window.eval(fs.readFileSync('app_final.js', 'utf8'));
    window.eval(fs.readFileSync('subcontracts.js', 'utf8'));

    window.currentProjects = [{id: 1, name: 'P1'}];
    window.currentContacts = [{id: 1, name: 'C1', type: 'supplier'}];
    window.currentSubcontracts = [{id: 1, project_id: 1, subcontractor_id: 1, contract_number: '123', total_value: 1000}];
    
    console.log("Testing openSubcontractModal...");
    window.openSubcontractModal(1);
    console.log("openSubcontractModal passed!");

    console.log("Testing openAbstractModal...");
    window.openAbstractModal(null);
    console.log("openAbstractModal passed!");

    console.log("Testing openClientAbstractModal...");
    window.openClientAbstractModal(null);
    console.log("openClientAbstractModal passed!");

} catch (e) {
    console.error("Error:", e);
}
