'use strict';
require('dotenv').config();
const supertest = require('supertest');
const { start } = require('../server');
(async () => {
  const app = await start();
  const agent = supertest.agent(app);

  const r1 = await agent.get('/dev-login');
  console.log('GET /dev-login status:', r1.status);

  const m = r1.text.match(/name="_csrf"\s+value="([^"]+)"/);
  console.log('csrf found:', !!m, m ? m[1].slice(0, 20) + '...' : '');

  if (!m) { process.exit(1); }

  const r2 = await agent.post('/dev-login').type('form').send({ preset: 'test', _csrf: m[1] });
  console.log('POST /dev-login status:', r2.status, 'location:', r2.headers.location);
  console.log('set-cookie:', r2.headers['set-cookie']);

  const r3 = await agent.get('/');
  console.log('GET / status:', r3.status, 'location:', r3.headers.location);
  if (r3.status === 200) console.log('snippet:', r3.text.slice(0, 300));

  process.exit(0);
})().catch(e => { console.error(e.message, e.stack); process.exit(1); });
