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;

// Mock IndexedDB
window.indexedDB = { open: () => ({ onupgradeneeded: null, onsuccess: null, onerror: null }) };

try {
    const dbCode = fs.readFileSync('db.js', 'utf8');
    window.eval(dbCode);
    console.log("db.js loaded");

    const appCode = fs.readFileSync('app_final.js', 'utf8');
    window.eval(appCode);
    console.log("app_final.js loaded successfully!");
} catch (e) {
    console.error("Error at runtime in app_final.js:", e);
}
