import assert from 'node:assert/strict'; import fs from 'node:fs/promises'; const base = 'http://127.0.0.1:8005'; function client() { const cookies = new Map(); let csrf; let version; async function request(path, method = 'GET', data, inertia = true) { const response = await fetch(base + path, { method, redirect: 'manual', headers: { Accept: inertia ? 'application/json' : 'text/html', Cookie: [...cookies].map(([k, v]) => `${k}=${v}`).join('; '), ...(inertia ? { 'X-Inertia': 'true', 'X-Inertia-Version': version ?? '', } : {}), ...(data ? { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': csrf, } : {}), }, ...(data ? { body: JSON.stringify(data) } : {}), }); for (const cookie of response.headers.getSetCookie()) { const pair = cookie.split(';', 1)[0], i = pair.indexOf('='); cookies.set(pair.slice(0, i), pair.slice(i + 1)); } const text = await response.text(); return { status: response.status, location: response.headers.get('location'), text, page: response.headers .get('content-type') ?.includes('application/json') ? JSON.parse(text) : null, }; } return { request, async enter(role) { let page = await request('/avaliar', 'GET', undefined, false); assert.equal(page.status, 200); csrf = page.text.match(/name="_token" value="([^"]+)"/)[1]; assert.equal( (await request('/avaliar/entrar', 'POST', { role }, false)) .status, 302, ); page = await request('/avaliar', 'GET', undefined, false); csrf = page.text.match(/name="_token" value="([^"]+)"/)[1]; const dashboard = await request( '/dashboard', 'GET', undefined, false, ); const encoded = dashboard.text.match( /