LibreChat/e2e/specs/mock/scenarios/code-download-stream.spec.ts
Marco Beretta 698ee25fea
🧯 fix: Close Abandoned Download and Speech Streams and Cap Speech Input (#17270)
* fix: Contain Download and Speech Stream Failures and Cap Speech Input

The code output download piped the Code API stream with .pipe, which adds no
error listener, so a body that failed mid-stream (a bad gzip body, a reset)
became an uncaught exception and the process exited. pipeToResponse uses
stream.pipeline, ending the response and destroying the upstream when the
client leaves.

Text-to-speech piped provider streams the same way and waited only for
'end', so a provider error was uncaught and a client disconnect left the
chunk loop waiting. pipeSegment resolves on end and rejects on an error or
a disconnect, which ends the loop.

A speech request had no length cap, so one request could fan out into
thousands of serial paid provider calls. speech.tts.maxInputChars bounds it
(default 20,000 characters).

* docs: Document the Speech Input Cap in the Example Config

* fix: Reject a Speech Segment When the Client Already Left

* fix: Tear Down Both Ends of a Failed Speech Segment and Cap Streamed Speech

A failed provider segment now destroys the response as well as the source,
so a half-written answer cannot hang the client, and a response-side error
rejects the segment instead of going unhandled. Automatic playback reads a
message only up to speech.tts.maxInputChars, so long messages cannot fan
out past the cap either.

The download scenario now checks the behavior .pipe lacked: leaving a code
download closes the upstream connection right away.

* fix: Keep a Late Response Write Error From Going Unhandled

* test: Key Slow Code Downloads by File and Send a Browser User Agent

* fix: Read Speech Up to the Cap Instead of Refusing It

Manual speech now synthesizes the text up to speech.tts.maxInputChars, the
same as automatic playback, through capSpeechInput, so the CJS service only
passes the capped text along and the request keeps its existing error
contract. A streamed message that reaches the cap exactly now finishes, and
a segment that fails before any audio went out leaves the response open for
the caller's HTTP error.

* fix: Count Only Text Against the Speech Cap
2026-10-19 22:20:40 +02:00

80 lines
2.9 KiB
TypeScript

import http from 'node:http';
import { randomBytes } from 'node:crypto';
import { expect, test } from '@playwright/test';
import { getE2EUser } from '../../../setup/user';
import { CODE_API_BASE } from '../helpers';
/** The route accepts 21-character ids; a fresh file id keeps parallel projects apart. */
const SESSION_ID = 'e2esessionstream00001';
const newFileId = (): string => randomBytes(16).toString('hex').slice(0, 21);
const BROWSER_UA =
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
/** Starts a download and drops the connection as soon as the first bytes arrive. */
function downloadAndDisconnect(url: string, token: string): Promise<void> {
return new Promise((resolve, reject) => {
/** The API rejects requests without a browser user agent before any route runs. */
const headers = {
Authorization: `Bearer ${token}`,
'User-Agent': BROWSER_UA,
};
const req = http.get(url, { headers }, (res) => {
if (res.statusCode !== 200) {
reject(new Error(`download answered ${res.statusCode}`));
return;
}
res.once('data', () => {
req.destroy();
resolve();
});
});
req.on('error', () => undefined);
});
}
test.describe('code output downloads', () => {
/* The fake code API trickles a long body. Once the browser side leaves, LibreChat must close
its upstream download too instead of holding the connection open, paused, until a timeout. */
test('leaving a download closes the upstream code download @scenario:abandoned-code-download-closes-upstream', async ({
playwright,
baseURL,
request,
}) => {
const client = await playwright.request.newContext({ baseURL });
try {
const { email, password } = getE2EUser();
const login = await client.post('/api/auth/login', { data: { email, password } });
expect(login.ok()).toBeTruthy();
const { token } = (await login.json()) as { token: string };
const fileId = newFileId();
const slow = await request.post(`${CODE_API_BASE}/__debug/slow-downloads`, {
data: { fileId, slow: true },
});
expect(slow.ok()).toBeTruthy();
await downloadAndDisconnect(
new URL(`/api/files/code/download/${SESSION_ID}/${fileId}`, baseURL).toString(),
token,
);
await expect
.poll(
async () => {
const response = await request.get(
`${CODE_API_BASE}/__debug/downloads?fileId=${fileId}`,
);
return ((await response.json()) as { closed: number }).closed;
},
{ timeout: 5_000, intervals: [250] },
)
.toBe(1);
expect((await client.get('/health')).status()).toBe(200);
await request.post(`${CODE_API_BASE}/__debug/slow-downloads`, {
data: { fileId, slow: false },
});
} finally {
await client.dispose();
}
});
});