fix: preserve large Unicode drafts with bounded keepalive transport
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { contextApi } from '../../resources/js/lib/investor-context.ts';
|
||||
|
||||
const success = { ok: true, json: async () => ({ saved: true }) };
|
||||
async function withTransport(fetch, callback) {
|
||||
const previousFetch = globalThis.fetch;
|
||||
const previousDocument = globalThis.document;
|
||||
globalThis.fetch = fetch;
|
||||
globalThis.document = { cookie: 'XSRF-TOKEN=synthetic%20token' };
|
||||
try {
|
||||
await callback();
|
||||
} finally {
|
||||
globalThis.fetch = previousFetch;
|
||||
globalThis.document = previousDocument;
|
||||
}
|
||||
}
|
||||
void test('context requests preserve Unicode payload and authentication while using byte-based keepalive eligibility', async () => {
|
||||
const requests = [];
|
||||
await withTransport(
|
||||
async (_url, options) => {
|
||||
requests.push(options);
|
||||
return success;
|
||||
},
|
||||
async () => {
|
||||
for (const text of [
|
||||
'Preparação de investimento',
|
||||
'ç'.repeat(33000),
|
||||
'a'.repeat(65000),
|
||||
]) {
|
||||
const body = { declarations: { project_summary: text } };
|
||||
assert.deepEqual(
|
||||
await contextApi('/synthetic', body, 'POST', true),
|
||||
{ saved: true },
|
||||
);
|
||||
const request = requests.at(-1);
|
||||
assert.equal(request.body, JSON.stringify(body));
|
||||
assert.equal(
|
||||
request.keepalive,
|
||||
new TextEncoder().encode(request.body).byteLength < 60000,
|
||||
);
|
||||
assert.equal(request.credentials, 'same-origin');
|
||||
assert.equal(
|
||||
request.headers['X-XSRF-TOKEN'],
|
||||
'synthetic token',
|
||||
);
|
||||
}
|
||||
assert.equal(requests[1].body.length < 60000, true);
|
||||
assert.equal(requests[1].keepalive, false);
|
||||
},
|
||||
);
|
||||
});
|
||||
void test('concurrent context requests share the keepalive budget and release it after completion', async () => {
|
||||
const requests = [];
|
||||
let finish;
|
||||
await withTransport(
|
||||
(_url, options) => {
|
||||
requests.push(options);
|
||||
return requests.length === 1
|
||||
? new Promise((resolve) => {
|
||||
finish = resolve;
|
||||
})
|
||||
: Promise.resolve(success);
|
||||
},
|
||||
async () => {
|
||||
const body = { text: 'a'.repeat(40000) };
|
||||
const first = contextApi('/first', body, 'POST', true);
|
||||
await contextApi('/second', body, 'POST', true);
|
||||
assert.deepEqual(
|
||||
requests.map((request) => request.keepalive),
|
||||
[true, false],
|
||||
);
|
||||
finish(success);
|
||||
await first;
|
||||
await contextApi('/third', body, 'POST', true);
|
||||
assert.equal(requests[2].keepalive, true);
|
||||
},
|
||||
);
|
||||
});
|
||||
void test('failed context transport releases the reserved budget without retrying a write', async () => {
|
||||
const requests = [];
|
||||
await withTransport(
|
||||
async (_url, options) => {
|
||||
requests.push(options);
|
||||
if (requests.length === 1)
|
||||
throw new Error('Synthetic transport error');
|
||||
return success;
|
||||
},
|
||||
async () => {
|
||||
const body = { text: 'a'.repeat(40000) };
|
||||
await assert.rejects(
|
||||
contextApi('/first', body, 'POST', true),
|
||||
/Synthetic transport error/,
|
||||
);
|
||||
await contextApi('/second', body, 'POST', true);
|
||||
assert.equal(requests.length, 2);
|
||||
assert.equal(
|
||||
requests.every((request) => request.keepalive),
|
||||
true,
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
void test('ordinary context writes and reads retain their existing method and error behavior', async () => {
|
||||
const requests = [];
|
||||
await withTransport(
|
||||
async (_url, options) => {
|
||||
requests.push(options);
|
||||
return requests.length === 3
|
||||
? {
|
||||
ok: false,
|
||||
status: 409,
|
||||
json: async () => ({
|
||||
message: 'Synthetic version conflict',
|
||||
}),
|
||||
}
|
||||
: success;
|
||||
},
|
||||
async () => {
|
||||
await contextApi('/read');
|
||||
await contextApi('/write', { value: 'ç' });
|
||||
await assert.rejects(
|
||||
contextApi('/conflict', { value: 'kept' }),
|
||||
/Synthetic version conflict/,
|
||||
);
|
||||
assert.equal(requests[0].method, 'GET');
|
||||
assert.equal(requests[0].body, undefined);
|
||||
assert.equal(requests[0].headers['X-XSRF-TOKEN'], undefined);
|
||||
assert.equal(requests[1].method, 'POST');
|
||||
assert.equal(
|
||||
requests.every((request) => request.keepalive === false),
|
||||
true,
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -581,6 +581,55 @@ const buttonNamed = (tree, label) =>
|
||||
(node) => node.type === 'button' && node.props.children === label,
|
||||
);
|
||||
const settlePreview = () => new Promise((resolve) => setImmediate(resolve));
|
||||
void test('large multi-opportunity draft is offered to the transport on exit without truncation', () => {
|
||||
const previousWindow = globalThis.window;
|
||||
let flush;
|
||||
globalThis.window = {
|
||||
addEventListener(_name, callback) {
|
||||
flush = callback;
|
||||
},
|
||||
removeEventListener() {},
|
||||
};
|
||||
let cleanup;
|
||||
try {
|
||||
const harness = contextPreviewHarness();
|
||||
const initial = harness.render();
|
||||
cleanup = harness.effects
|
||||
.find(
|
||||
(effect) =>
|
||||
effect.dependencies?.length === 1 &&
|
||||
effect.dependencies[0] === 'synthetic',
|
||||
)
|
||||
.callback();
|
||||
const choices = Array.from({ length: 100 }, (_, index) => ({
|
||||
opportunity: {
|
||||
key: `opportunity:${index + 1}`,
|
||||
title: `Synthetic ${index + 1}`,
|
||||
},
|
||||
territory: null,
|
||||
location_reason: 'Descrição '.repeat(80),
|
||||
}));
|
||||
flat(initial)
|
||||
.find(
|
||||
(node) =>
|
||||
Array.isArray(node.props.choices) && node.props.onChange,
|
||||
)
|
||||
.props.onChange(choices);
|
||||
harness.render();
|
||||
flush();
|
||||
assert.equal(harness.pending.length, 1);
|
||||
assert.match(harness.pending[0].url, /\/rascunho$/);
|
||||
assert.equal(harness.pending[0].body.opportunity_choices.length, 100);
|
||||
assert.equal(
|
||||
harness.pending[0].body.opportunity_choices[99].location_reason,
|
||||
choices[99].location_reason,
|
||||
);
|
||||
assert.ok(JSON.stringify(harness.pending[0].body).length > 60000);
|
||||
} finally {
|
||||
cleanup?.();
|
||||
globalThis.window = previousWindow;
|
||||
}
|
||||
});
|
||||
void test('successful save followed by failed recovery offers read-only retry and never restores a stale draft', async () => {
|
||||
const previousWindow = globalThis.window;
|
||||
const listeners = new Map();
|
||||
|
||||
Reference in New Issue
Block a user