const http = require('http');
const port = 34332;

async function doRPC(method, args) {
    return new Promise((resolve) => {
        const req = http.request(`http://127.0.0.1:${port}/test-rpc`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' }
        }, (res) => {
            let data = '';
            res.on('data', c => data += c);
            res.on('end', () => resolve(JSON.parse(data)));
        });
        req.write(JSON.stringify({ method, args }));
        req.end();
    });
}

async function main() {
    let allIds = [];
    let pageToken = "";
    
    for (let i = 0; i < 5; i++) {
        const args = { descending: true };
        if (pageToken) args.pageToken = pageToken;
        
        console.log(`Fetching page ${i+1} with pageToken='${pageToken}'...`);
        const res = await doRPC('GetAllCascadeTrajectories', args);
        if (!res.trajectorySummaries) {
            console.log("No summaries:", res);
            break;
        }
        
        const keys = Object.keys(res.trajectorySummaries);
        allIds.push(...keys);
        
        console.log(` Got ${keys.length} items`);
        if (keys.length > 0) {
            console.log(` First: ${keys[0]}`);
            console.log(` Last:  ${keys[keys.length-1]}`);
        }
        
        if (res.nextPageToken) {
            pageToken = res.nextPageToken;
        } else {
            console.log("No nextPageToken.");
            break;
        }
    }
    
    console.log(`Total collected: ${allIds.length}`);
    if (allIds.includes("370d1a09-1fa8-4aed-90d7-4024e36b3a2d")) {
        console.log(" FOUND 370d1a09-1fa8-4aed-90d7-4024e36b3a2d !!");
    } else {
        console.log(" Missing user active session.");
    }
}
main();
