LibreChat/e2e/specs/mock/scenarios/tts-cache.spec.ts
Danny Avila 5ca49b5f4e
🪢 fix: Resolve the Races Behind Four Intermittent Session and Scenario E2E Tests (#18061)
* 🔑 fix: Replay a Request Rejected for a Superseded Bearer Instead of Refreshing Again

A burst of requests leaves with one bearer. The first 401 starts a refresh and the
rest share it only while it is in flight: a 401 delivered after the refresh settled
found no recovery running and started a second one, although the session had already
recovered and the response described a bearer that was no longer in use.

Compare the bearer the rejected request carried with the current one. When it has
been replaced and no recovery is running, replay the request with the current
bearer. A replay that is rejected again carries the current bearer, so it takes the
ordinary refresh path. The fetch transport follows the same rule.

This is what failed "recovers from an expired bearer during app bootstrap without
redirect looping": 9 of the 21 bootstrap 401s were delivered after the first
recovery finished, so the test counted a third refresh and a second recovery cycle.

* 🔊 test: Play Decodable Audio and Hold the Reply's Button in the Read-Aloud Replay Scenario

The fixture answered the speech request with four bytes the MP3 parser rejects, so
every playback failed. A failed playback cancels its request, and the audio is
cached only once the response has been read to the end, so the replay was served
from memory only when the response finished before the decoder gave up. When the
decoder won, the replay issued the second request the scenario forbids.

The test also located the button by its name. While a message is read aloud its
button is named "Stop", so the locator resolved to the user message's read-aloud
button: the wait for playback to end passed at once, and the second click could
land on a different message.

Serve a third of a second of silent MPEG frames so playback succeeds and ends on
its own, and locate the reply's button by its test id so the wait and the replay
address the same control.

* 📅 test: Wait for the Schedule Save to Land Before Reloading

The edit scenario clicked Save and reloaded the page in the next step. A reload
discards requests still in flight, so when it won the race the PATCH was cancelled
before the server answered and the edited schedule never appeared. Await the PATCH
response, and require it to succeed, before reloading.

* 🔗 fix: Grant a Legacy Shared Link's Permissions Once When Its First Views Race

The share page requests a link's config and its messages together, and each request
migrates a legacy link on the way in. Both find no grant and both write one. The
grant is an upsert matched on the principal and resource, which no unique index
covers, so the two writes inserted two rows: a second public entry, a second owner
entry, or both.

`grantPermission` now accepts the `_id` to give the entry if the grant creates it.
The migration derives that id from the link and the principal, so the second insert
collides on `_id` and is replayed as an update of the first entry. It holds across
processes because the database enforces it. Grants that pass no id are unchanged.

"manages a shared-link snapshot and preserves legacy public links through runtime
migration" asserts one public entry and saw two.

* 🔑 fix: Refresh When a Replayed Fetch Is Rejected With the Current Bearer

The fetch transport replayed a request whose bearer had been superseded and
returned whatever came back. A second 401 there means the current bearer is not
accepted either, which is the case the refresh exists for, and the axios path
already handles it that way. Replay first, then fall through to the ordinary
recovery when the replay is rejected too.

* 🔗 refactor: Derive a Unique Grant's Entry Id in the Data Layer

`AccessControlService.grantPermission` took the entry's `_id` as a Mongoose
`ObjectId`, which put a storage type in an exported `packages/api` signature. The
service now takes `uniqueEntry: true`, and `grantPermission` in data-schemas
derives the id from the principal and resource it already matches on. A caller can
no longer pass an id that names a different grant.
2026-11-11 16:22:39 -05:00

234 lines
9.1 KiB
TypeScript

import { randomUUID } from 'node:crypto';
import { expect, test } from '@playwright/test';
import type { Browser, BrowserContext, Page } from '@playwright/test';
import type { User } from '../../../types';
import {
MOCK_ENDPOINTS,
NEW_CHAT_PATH,
replyPrompt,
replyText,
selectMockEndpoint,
sendMessageAndWaitForCompletion,
} from '../helpers';
import { deleteConversations } from '../db';
import cleanupUser from '../../../setup/cleanupUser';
/**
* Read-aloud audio is cached only in the tab's memory: a replay plays without a second TTS
* request, nothing is written to Cache Storage, logout empties the cache, and the cache that
* earlier versions persisted to disk is deleted when the app starts.
*/
test.describe.configure({ timeout: 120_000 });
/** The Cache Storage name earlier versions persisted audio under, keyed by message text. */
const LEGACY_TTS_CACHE = 'tts-responses';
const createdConversationIds: string[] = [];
test.afterAll(async () => {
await deleteConversations(createdConversationIds);
});
/**
* A third of a second of silence as MPEG-1 Layer III frames. Read-aloud streams the response into
* a MediaSource, and bytes the decoder rejects fail the playback, which cancels the request: the
* audio was then cached only when the response happened to finish before the decoder gave up.
*/
const SILENT_MP3_FRAME = Buffer.alloc(417);
SILENT_MP3_FRAME.set([0xff, 0xfb, 0x90, 0x00]);
const SILENT_MP3 = Buffer.concat(Array.from({ length: 12 }, () => SILENT_MP3_FRAME));
/** Stands in for a configured external TTS provider: the e2e config has none. */
async function routeExternalSpeech(
target: Page | BrowserContext,
): Promise<{ requests: () => number }> {
let ttsRequests = 0;
await target.route('**/api/files/speech/config/get', (route) =>
route.fulfill({
json: { textToSpeech: true, engineTTS: 'external', cacheTTS: true, ttsExternal: true },
}),
);
await target.route('**/api/files/speech/tts/voices', (route) =>
route.fulfill({ json: ['alloy'] }),
);
await target.route('**/api/files/speech/tts/manual', (route) => {
ttsRequests += 1;
return route.fulfill({ body: SILENT_MP3, contentType: 'audio/mpeg' });
});
return { requests: () => ttsRequests };
}
const useExternalEngine = (target: Page | BrowserContext) =>
target.addInitScript(() => {
localStorage.setItem('engineTTS', JSON.stringify('external'));
localStorage.setItem('voice', JSON.stringify('alloy'));
});
/** Sends a message to the mock model and returns the read-aloud button of its reply. */
async function replyToReadAloud(page: Page, prefix: string) {
await selectMockEndpoint(page, MOCK_ENDPOINTS[0]);
const label = `${prefix}-${randomUUID().slice(0, 8)}`;
await sendMessageAndWaitForCompletion(page, replyPrompt(label));
await expect(page.getByText(replyText(label)).first()).toBeVisible({ timeout: 20000 });
const conversationId = new URL(page.url()).pathname.split('/c/')[1];
if (conversationId) createdConversationIds.push(conversationId);
/** By test id, not by name: the button is named "Stop" while its message is read aloud, and a
* name-based locator then resolves to the user message's read-aloud button instead. */
return page.getByTestId('read-aloud-button');
}
const cacheStorageNames = (page: Page) => page.evaluate(() => caches.keys());
test.describe('read-aloud audio cache', () => {
test('replaying a message plays from memory without a second TTS request @scenario:tts-replay-served-from-memory', async ({
page,
}) => {
await useExternalEngine(page);
const speech = await routeExternalSpeech(page);
await page.goto(NEW_CHAT_PATH, { timeout: 10000 });
const readAloud = await replyToReadAloud(page, 'tts');
await readAloud.click();
await expect.poll(() => speech.requests()).toBe(1);
await expect(readAloud).toHaveAccessibleName('Read aloud');
await readAloud.click();
/** Give a second request time to start, if one were going to. */
await page.waitForTimeout(1500);
expect(speech.requests()).toBe(1);
});
test('read-aloud audio is never written to Cache Storage @scenario:tts-audio-never-persisted', async ({
page,
}) => {
await useExternalEngine(page);
const speech = await routeExternalSpeech(page);
await page.goto(NEW_CHAT_PATH, { timeout: 10000 });
const readAloud = await replyToReadAloud(page, 'disk');
await readAloud.click();
await expect.poll(() => speech.requests()).toBe(1);
await page.waitForTimeout(1000);
expect(await cacheStorageNames(page)).not.toContain(LEGACY_TTS_CACHE);
expect((await cacheStorageNames(page)).filter((name) => name.startsWith('tts'))).toEqual([]);
});
test('logging out empties the read-aloud cache @scenario:tts-memory-cleared-on-logout', async ({
browser,
baseURL,
}) => {
const { context, page, user } = await signInFreshUser(browser, baseURL);
try {
await useExternalEngine(context);
const speech = await routeExternalSpeech(context);
await page.goto(NEW_CHAT_PATH, { timeout: 10000 });
const readAloud = await replyToReadAloud(page, 'logout');
const conversationPath = new URL(page.url()).pathname;
await readAloud.click();
await expect.poll(() => speech.requests()).toBe(1);
/** Same tab, no reload: only the logout can have emptied the in-memory cache. The marker
* would be gone if the document had been replaced. */
await page.evaluate(() => Object.assign(window, { ttsSameDocument: true }));
await logOut(page);
await logIn(page, user, /\/c\//);
if (new URL(page.url()).pathname !== conversationPath) {
await page.evaluate((path) => {
window.history.pushState({}, '', path);
window.dispatchEvent(new PopStateEvent('popstate'));
}, conversationPath);
}
await expect(page).toHaveURL(new RegExp(conversationPath));
expect(await page.evaluate(() => 'ttsSameDocument' in window)).toBe(true);
/** On a phone the sidebar drawer reopens over the conversation after signing in. */
if ((page.viewportSize()?.width ?? 0) < 768) {
const closeSidebar = page.getByTestId('close-sidebar-button');
if (await closeSidebar.isVisible()) {
await closeSidebar.click();
}
}
await page.getByRole('button', { name: 'Read aloud' }).last().click();
await expect.poll(() => speech.requests()).toBe(2);
} finally {
await context.close().catch(() => undefined);
await cleanupUser(user);
}
});
test('audio earlier versions saved to Cache Storage is deleted when the app starts @scenario:tts-legacy-disk-cache-purged-at-startup', async ({
page,
}) => {
await page.goto(NEW_CHAT_PATH, { timeout: 10000 });
await page.evaluate(async (name) => {
const cache = await caches.open(name);
await cache.put(
'/E2E%20legacy%20reply%20text',
new Response(new Blob([new Uint8Array([1])])),
);
}, LEGACY_TTS_CACHE);
await page.reload();
await expect
.poll(() => page.evaluate((name) => caches.has(name), LEGACY_TTS_CACHE))
.toBe(false);
});
});
/** A dedicated account, so logging out does not end the session other specs share. */
async function signInFreshUser(
browser: Browser,
baseURL: string | undefined,
): Promise<{ context: BrowserContext; page: Page; user: User }> {
if (typeof baseURL !== 'string') {
throw new Error('baseURL must be configured for mock scenarios');
}
const user: User = {
name: 'TTS Cache',
email: `tts-cache-${randomUUID().slice(0, 8)}@example.com`,
password: `Pw-${randomUUID()}`,
};
await cleanupUser(user);
const context = await browser.newContext({ storageState: undefined, baseURL });
await context.addInitScript(() => {
localStorage.setItem('navVisible', 'true');
/** The shared storage state opts into sending with Enter; a fresh account has to as well. */
localStorage.setItem('enterToSend', 'true');
});
const registered = await context.request.post('/api/auth/register', {
data: {
email: user.email,
name: user.name,
password: user.password,
confirm_password: user.password,
},
});
expect(registered.ok()).toBeTruthy();
const page = await context.newPage();
await page.goto('/login', { timeout: 10000 });
await logIn(page, user);
return { context, page, user };
}
async function logIn(page: Page, user: User, landing = /\/c\/new/): Promise<void> {
await page.getByLabel('Email').fill(user.email);
await page.getByLabel('Password').fill(user.password);
await page.getByTestId('login-button').click();
await page.waitForURL(landing, { timeout: 10000 });
}
async function logOut(page: Page): Promise<void> {
/** The sidebar toggle is only meaningful once the app has rendered its chrome. */
await expect(page.getByRole('textbox', { name: 'Message input' })).toBeVisible({
timeout: 20000,
});
const navUser = page.getByTestId('nav-user');
if (!(await navUser.isVisible())) {
await page.getByTestId('header-open-sidebar-button').click();
}
await navUser.click();
await page.getByRole('menuitem', { name: 'Log out' }).click();
await page.waitForURL(/\/login/, { timeout: 10000 });
}