LibreChat/packages/api/src/pulls/capabilities.spec.ts
Marco Beretta 0bc344fcb9
🔑 feat: Let Users Connect Their Own Account for Pull Request Lookups (#16989)
* feat: Let Users Connect Their Own Account for Pull Request Lookups

* fix: Refuse Cross-Tenant Pull Request Callbacks and Surface Key Failures

* fix: Keep Pull Request Grants Through Outages and Remove Them on Disconnect

A token endpoint server error is an outage even with an OAuth error body, so
it no longer deletes the grant, and a refresh outage reaches the lookup as
unavailable instead of falling back to no token. Disconnect removes every
pr_oauth row of the user, whatever app or host is configured now. A grant that
never expires extends its retention when used, and the flow, request and
retention limits are configurable. The connection routes are built in
packages/api, mounted behind the pre-auth tenant middleware in both servers,
and answer failures with stable codes.

* fix: Keep the Pull Request OAuth App Out of Overrides

A role, group, user or tenant override can no longer change or remove pullRequests.oauth, so the connect capability and the callback see the operator's app.

* fix: Read a User's Pull Request Token Only With the Feature On

* fix: Report a Malformed Stored Pull Request Grant as a Failure

* fix: Remove a User's Stored Tokens When an Administrator Deletes the User

* fix: Answer No Pull Request for a Conversation Without a Lane When the User Token Fails

* fix: Share One Grant Read Between Concurrent Callers and Report an Expired Grant Disconnected

* fix: Remove a Pull Request Grant Only on a Definite Refusal and Past Its Retention

* fix: Version Pull Request Grants and Coordinate Refresh, Refusal and Retention Through Them

* fix: Invalidate a Refused User Token and Read It Only for a Lane That Needs It

* fix: Finish a Pull Request Connection Under the User's Effective Config Behind the Login Limiter

* fix: Type the Pull Request User Token for Isolated Declarations and Settle Retention Before Answering

* fix: Bind a Pull Request Connection Flow to the Generation a Disconnect Replaces

* fix: Start a Batch's User Token Read Only Within Its Deadline

* fix: Fence Pull Request Token Reads Against a Disconnect and Report a Failed Retention Cleanup

* fix: Fence Connection Status Like Token Reads and Keep a Newer Read Joinable

* fix: Report a Pull Request Grant Without Its App and Name the Sign-In That Made It

* fix: Read a User's Unexpired Pull Request Grants Newest First

* fix: Mark the Connection Callback as an OAuth Navigation and Report a Failed Extension

* fix: Name the Pull Request Sign-In Flow on Its Landing Page

* fix: Remove a Pull Request Grant of a Replaced App When It Is Read

* fix: Keep a Replaced App's Grant Untouched and Extend Retention From Each Day's Use

* fix: Accept a Pull Request Grant Only Under the User's Current Connection Generation

* fix: Renew the Connection Generation With a Grant Still in Use

* fix: Restore the Previous Connection When a Sign-In Cannot Store Its Grant

* fix: Decide a Sign-In by What Was Stored When a Write Does Not Answer

* fix: Roll Back a Sign-In's Generation Along With a Grant It Must Remove

* fix: Refresh an Access Token a Lookup Was Refused With Before Ending the Connection

* fix: Undo a Sign-In Only for a Confirmed Fence, Not a Failed Check

* fix: Read a User Token Again When the Connection Changes Under the Read

* fix: Apply the Current Retention Again When Another Replica Changed the Grant First

* test: Answer Each Concurrent Sign-In by Its Own Code

* fix: Skip Retention Rewrites for Grants That Expire Without a Refresh Token

* fix: Retry a Token Rejection That Loses to a Concurrent Grant Write

* fix: Skip User Token Reads for Lanes Without a Commit and Reread Status After a Reconnect
2026-10-12 15:58:23 +02:00

64 lines
2.6 KiB
TypeScript

import { PULL_REQUEST_BATCH_VERSION } from 'librechat-data-provider';
import type { AppConfig } from '@librechat/data-schemas';
import { resolvePullRequestCapabilities } from './capabilities';
const configWith = (pullRequests?: Record<string, unknown>) =>
({ endpoints: { agents: pullRequests ? { pullRequests } : {} } }) as unknown as AppConfig;
const env = { GITHUB_TOKEN: 't' };
const scoped = { allowAllRepositories: true };
describe('resolvePullRequestCapabilities', () => {
it('advertises the flag, the batch version and the lookup limit together when the feature is on', () => {
expect(resolvePullRequestCapabilities(configWith(scoped), env)).toEqual({
pullRequestsEnabled: true,
pullRequestsBatchVersion: PULL_REQUEST_BATCH_VERSION,
pullRequestsMaxConcurrentLookups: 4,
});
});
it('advertises the provider users can connect to, only when an app is configured', () => {
const oauth = { clientId: '${ID}', clientSecret: '${SECRET}' };
expect(
resolvePullRequestCapabilities(configWith({ provider: 'gitlab', oauth }), {
ID: 'i',
SECRET: 's',
}),
).toMatchObject({ pullRequestsEnabled: true, pullRequestsConnect: 'gitlab' });
expect(resolvePullRequestCapabilities(configWith(scoped), env)).not.toHaveProperty(
'pullRequestsConnect',
);
});
it('advertises the configured lookup limit', () => {
expect(
resolvePullRequestCapabilities(configWith({ ...scoped, maxConcurrentLookups: 2 }), env),
).toMatchObject({ pullRequestsMaxConcurrentLookups: 2 });
});
it.each([
['no config', undefined],
['a null config', null],
['a config without endpoints', {} as AppConfig],
['no pull request settings', configWith()],
['the feature switched off', configWith({ ...scoped, enabled: false })],
['no repository scope', configWith({ enabled: true })],
])('advertises neither the flag nor a version with %s', (_label, appConfig) => {
const capabilities = resolvePullRequestCapabilities(appConfig, env);
expect(capabilities).toEqual({ pullRequestsEnabled: false });
expect(capabilities).not.toHaveProperty('pullRequestsBatchVersion');
expect(capabilities).not.toHaveProperty('pullRequestsMaxConcurrentLookups');
});
it('is on by default once a token exists and a scope is set, with no enabled switch', () => {
expect(resolvePullRequestCapabilities(configWith(scoped), env)).toMatchObject({
pullRequestsEnabled: true,
});
});
it('advertises nothing when there is no token to read with', () => {
expect(resolvePullRequestCapabilities(configWith(scoped), {})).toEqual({
pullRequestsEnabled: false,
});
});
});