// Simple test script to simulate the background fetch of the extension
// Usage: node test/fetch_test.js

const https = require('https');
const API_BASE = 'https://blueprint.minionabigroadtrip.nl/wp-json/wp/v2/particle/';
const PER_PAGE = 5; // keep small for test

function fetchPage(page) {
    const url = `${API_BASE}?per_page=${PER_PAGE}&page=${page}`;
    return new Promise((resolve, reject) => {
        https.get(url, (res) => {
            let data = '';
            res.on('data', chunk => data += chunk);
            res.on('end', () => {
                try {
                    const list = JSON.parse(data);
                    const totalPages = parseInt(res.headers['x-wp-totalpages'] || '1', 10);
                    resolve({ success: true, items: list, totalPages, statusCode: res.statusCode });
                } catch (e) {
                    reject(e);
                }
            });
        }).on('error', (err) => reject(err));
    });
}

(async function(){
    try {
        console.log('Fetching page 1...');
        const r1 = await fetchPage(1);
        console.log('Page 1 status:', r1.statusCode, 'items:', Array.isArray(r1.items)? r1.items.length : 'not-array', 'totalPages:', r1.totalPages);
        if (r1.totalPages > 1) {
            console.log('Fetching page 2...');
            const r2 = await fetchPage(2);
            console.log('Page 2 status:', r2.statusCode, 'items:', Array.isArray(r2.items)? r2.items.length : 'not-array');
        }
        console.log('Test finished');
    } catch (e) {
        console.error('Fetch test error:', e.message || e);
        process.exit(1);
    }
})();
