LibreChat/packages/data-provider/specs/api-endpoints.spec.ts
Danny Avila be42a7d6e3
🐨 feat: Read Subagent Messages Through Their Parent Conversation (#18187)
A subagent child thread is reachable only through the parent the requester
owns: /api/messages/<threadId> answers 404 by design, and the durable thread
view sends a bounded projection, not messages. A chat pane pointed at a child
thread therefore had nothing to load.

GET /api/convos/:parent/subagents/:thread/messages returns the thread's rows
with the conversation route's projection and tool-call previews, after the
thread view's parent, lineage and tenant check. An event thread's input is
the raw payload of an external event, so its row is sent with empty text, as
the thread view already does. A second route reads the full tool call behind
a preview, reusing the conversation route's reader.
2026-11-17 10:05:42 -05:00

172 lines
5.9 KiB
TypeScript

/**
* @jest-environment jsdom
*/
import {
agentQueuedTurn,
buildLoginRedirectUrl,
getSharedLinks,
agentQueuedTurnsByConversation,
enableTwoFactorSetup,
confirmTwoFactorSetup,
acknowledgeTwoFactorSetup,
finalizeTwoFactorSetup,
cancelTwoFactorSetup,
messageToolCallPart,
subagentThreadMessages,
subagentThreadToolCallPart,
} from '../src/api-endpoints';
describe('buildLoginRedirectUrl', () => {
afterEach(() => {
window.history.replaceState({}, '', '/');
});
it('builds a login URL from explicit args', () => {
const result = buildLoginRedirectUrl('/c/new', '?q=hello', '');
expect(result).toBe('/login?redirect_to=%2Fc%2Fnew%3Fq%3Dhello');
});
it('encodes complex paths with query and hash', () => {
const result = buildLoginRedirectUrl('/c/new', '?q=hello&submit=true', '#section');
expect(result).toContain('redirect_to=');
const encoded = result.split('redirect_to=')[1];
expect(decodeURIComponent(encoded)).toBe('/c/new?q=hello&submit=true#section');
});
it('falls back to window.location when no args provided', () => {
window.history.replaceState({}, '', '/c/abc123?model=gpt-4#msg-5');
const result = buildLoginRedirectUrl();
const encoded = result.split('redirect_to=')[1];
expect(decodeURIComponent(encoded)).toBe('/c/abc123?model=gpt-4#msg-5');
});
it('returns plain /login when all location parts are empty (root)', () => {
window.history.replaceState({}, '', '/');
const result = buildLoginRedirectUrl();
expect(result).toBe('/login');
});
it('returns plain /login when pathname is /login (prevents recursive redirect)', () => {
const result = buildLoginRedirectUrl('/login', '?redirect_to=%2Fc%2Fnew', '');
expect(result).toBe('/login');
});
it('returns plain /login when window.location is already /login', () => {
window.history.replaceState({}, '', '/login?redirect_to=%2Fc%2Fabc');
const result = buildLoginRedirectUrl();
expect(result).toBe('/login');
});
it('returns plain /login for /login sub-paths', () => {
const result = buildLoginRedirectUrl('/login/2fa', '', '');
expect(result).toBe('/login');
});
it('returns plain /login for basename-prefixed /login (e.g. /librechat/login)', () => {
window.history.replaceState({}, '', '/librechat/login?redirect_to=%2Fc%2Fabc');
const result = buildLoginRedirectUrl();
expect(result).toBe('/login');
});
it('returns plain /login for basename-prefixed /login sub-paths', () => {
const result = buildLoginRedirectUrl('/librechat/login/2fa', '', '');
expect(result).toBe('/login');
});
it('returns plain /login for root path (no pointless redirect_to=/)', () => {
const result = buildLoginRedirectUrl('/', '', '');
expect(result).toBe('/login');
expect(result).not.toContain('redirect_to');
});
it('does NOT match paths where "login" is a substring of a segment', () => {
const result = buildLoginRedirectUrl('/c/loginhistory', '', '');
expect(result).toContain('redirect_to=');
expect(decodeURIComponent(result.split('redirect_to=')[1])).toBe('/c/loginhistory');
});
});
describe('getSharedLinks', () => {
it('encodes search and cursor values exactly once', () => {
const result = getSharedLinks(
25,
'createdAt',
'desc',
'100% ready & waiting',
'2030-01-01T00:00:00.000Z',
);
expect(result).toBe(
'/api/share?pageSize=25&sortBy=createdAt&sortDirection=desc&search=100%25%20ready%20%26%20waiting&cursor=2030-01-01T00%3A00%3A00.000Z',
);
});
});
describe('agent queued turns', () => {
it('encodes conversation identity exactly once', () => {
expect(agentQueuedTurnsByConversation('conversation/a b')).toBe(
'/api/agents/chat/queued-turns?conversationId=conversation%2Fa%20b',
);
});
it('encodes deduplicated known request identities as repeated bounded query values', () => {
expect(
agentQueuedTurnsByConversation('conversation/one', [
'request/one',
'request,two',
'request/one',
]),
).toBe(
'/api/agents/chat/queued-turns?conversationId=conversation%2Fone&clientRequestIds=request%2Fone&clientRequestIds=request%2Ctwo',
);
});
it('encodes queued-turn identity as one path segment', () => {
expect(agentQueuedTurn('turn/a b')).toBe('/api/agents/chat/queued-turns/turn%2Fa%20b');
});
});
describe('required two-factor setup endpoints', () => {
it('uses the purpose-specific setup paths', () => {
expect(enableTwoFactorSetup()).toBe('/api/auth/2fa/setup');
expect(confirmTwoFactorSetup()).toBe('/api/auth/2fa/setup/confirm');
expect(acknowledgeTwoFactorSetup()).toBe('/api/auth/2fa/setup/acknowledge');
expect(finalizeTwoFactorSetup()).toBe('/api/auth/2fa/setup/finalize');
expect(cancelTwoFactorSetup()).toBe('/api/auth/2fa/setup/cancel');
});
});
describe('child thread read endpoints', () => {
it('reads a child thread through its parent, encoding both ids', () => {
expect(subagentThreadMessages('parent/1', 'thread 2')).toBe(
'/api/convos/parent%2F1/subagents/thread%202/messages',
);
});
it('addresses a tool-call part under the thread, with the same identity query as a chat', () => {
const params = {
conversationId: 'thread 2',
messageId: 'task:assistant',
partIndex: 3,
toolCallId: 'call/1',
stepId: 'step 1',
};
expect(subagentThreadToolCallPart('parent/1', params)).toBe(
'/api/convos/parent%2F1/subagents/thread%202/messages/task%3Aassistant/parts/3?toolCallId=call%2F1&stepId=step%201',
);
expect(messageToolCallPart(params)).toBe(
'/api/messages/thread%202/task%3Aassistant/parts/3?toolCallId=call%2F1&stepId=step%201',
);
});
it('omits the query when the part has no identity', () => {
expect(
subagentThreadToolCallPart('parent', {
conversationId: 'thread',
messageId: 'm',
partIndex: 0,
}),
).toBe('/api/convos/parent/subagents/thread/messages/m/parts/0');
});
});