This commit is contained in:
2026-11-02 20:31:15 +02:00
parent 8ddfa93f45
commit 1da273f1f3
131 changed files with 14802 additions and 526 deletions
@@ -3,6 +3,32 @@ import assert from 'node:assert/strict';
import { contextApi } from '../../resources/js/lib/investor-context.ts';
const success = { ok: true, json: async () => ({ saved: true }) };
await test('context handles compact CSRF cookies and rejects malformed writes before transport', async () => {
let calls = 0;
await withTransport(
async (_url, options) => {
calls++;
if (options.method !== 'GET')
assert.equal(
options.headers['X-XSRF-TOKEN'],
'synthetic/token',
);
return success;
},
async () => {
globalThis.document.cookie = 'other=x;XSRF-TOKEN=synthetic%2Ftoken';
await contextApi('/synthetic', {});
globalThis.document.cookie = 'XSRF-TOKEN=%broken';
await assert.rejects(
contextApi('/synthetic', {}, 'POST', true),
/sessão/,
);
assert.equal(calls, 1);
await contextApi('/synthetic');
assert.equal(calls, 2);
},
);
});
async function withTransport(fetch, callback) {
const previousFetch = globalThis.fetch;
const previousDocument = globalThis.document;
@@ -134,3 +160,37 @@ void test('ordinary context writes and reads retain their existing method and er
},
);
});
void test('invalid successful context response remains a failure and releases keepalive budget', async () => {
let calls = 0;
await withTransport(
async (_url, options) => {
calls++;
assert.equal(options.keepalive, true);
return calls === 1
? new Response('<html>unexpected</html>', { status: 200 })
: success;
},
async () => {
await assert.rejects(
contextApi(
'/synthetic',
{ text: 'a'.repeat(40000) },
'POST',
true,
),
/HTTP 200/,
);
assert.equal(calls, 1);
assert.deepEqual(
await contextApi(
'/synthetic',
{ text: 'a'.repeat(40000) },
'POST',
true,
),
{ saved: true },
);
},
);
});