mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-01 11:36:29 +00:00
* feat: add self-service code worker pairing * fix: enforce code worker principal boundaries * fix: separate pairing control planes from execution * fix: preserve principal worker routing * fix: pin artifact traffic to code workers * fix: authenticate code worker selection * test: mock worker-aware artifact headers * fix: harden self-service code worker routing * fix: fail closed for principal code pairing * feat: manage personal code environments * fix: harden personal code environment lifecycle * fix: satisfy backend lifecycle types * fix: harden principal environment lifecycle and quotas * fix: fence environment lifecycle races * fix: reconcile code environment stack * style: format code execution headers * fix: reconcile code environment lifecycle * fix: close self-service pairing gaps * fix: close code environment reconciliation gaps * fix: close code environment UI review findings * fix: satisfy code environment type checks * fix: route code priming to selected worker * fix: harden code environment lifecycle fences * fix: type lease renewal interval * fix: harden environment lifecycle recovery * fix: close lifecycle recovery races * fix: type compensated agent timestamps * fix: fence lifecycle cleanup by tenant and revision * fix: scope environment revision fencing
105 lines
3.7 KiB
JavaScript
105 lines
3.7 KiB
JavaScript
const express = require('express');
|
|
const request = require('supertest');
|
|
|
|
const middlewareCalls = [];
|
|
const mockRequireJwtAuth = jest.fn((req, _res, next) => {
|
|
middlewareCalls.push('jwt');
|
|
req.user = { id: '68b2f0c498f24c1e78fa0001', role: 'USER' };
|
|
next();
|
|
});
|
|
const mockRequireCapability = jest.fn((capability) => (req, _res, next) => {
|
|
middlewareCalls.push(capability);
|
|
next();
|
|
});
|
|
const mockCodeEnvironmentPairingLimiter = jest.fn((_req, _res, next) => {
|
|
middlewareCalls.push('pairing-limit');
|
|
next();
|
|
});
|
|
const mockRegistry = {};
|
|
const mockGetCodeEnvironmentRegistry = jest.fn(() => mockRegistry);
|
|
const mockHandlers = {
|
|
list: jest.fn((_req, res) => res.status(200).json({ environments: [] })),
|
|
register: jest.fn((_req, res) => res.status(201).json({ environment: { id: 'code-1' } })),
|
|
pair: jest.fn((_req, res) => res.status(201).json({ environment: { id: 'code-1' } })),
|
|
remove: jest.fn((_req, res) => res.status(200).json({ environment: { id: 'code-1' } })),
|
|
};
|
|
|
|
jest.mock('@librechat/data-schemas', () => ({
|
|
SystemCapabilities: { MANAGE_CODE_ENVIRONMENTS: 'manage:code_environments' },
|
|
}));
|
|
|
|
jest.mock('@librechat/api', () => ({
|
|
createCodeEnvironmentRegistry: jest.fn(() => mockRegistry),
|
|
createCodeEnvironmentHttpHandlers: jest.fn(() => mockHandlers),
|
|
startCodeEnvironmentLifecycleReconciler: jest.fn(),
|
|
}));
|
|
|
|
jest.mock('~/server/middleware/roles/capabilities', () => ({
|
|
requireCapability: mockRequireCapability,
|
|
}));
|
|
jest.mock('~/server/middleware/limiters/code', () => ({
|
|
codeEnvironmentPairingLimiter: mockCodeEnvironmentPairingLimiter,
|
|
}));
|
|
|
|
jest.mock('~/server/middleware', () => ({ requireJwtAuth: mockRequireJwtAuth }));
|
|
jest.mock('~/server/services/Config', () => ({
|
|
getAppConfig: jest.fn(),
|
|
getCodeEnvironmentRegistry: mockGetCodeEnvironmentRegistry,
|
|
}));
|
|
jest.mock('~/models', () => ({ isAgentTriggerPrincipalActive: jest.fn() }));
|
|
|
|
function createApp() {
|
|
delete require.cache[require.resolve('./code-environments')];
|
|
const router = require('./code-environments');
|
|
const app = express();
|
|
app.use(express.json());
|
|
app.use('/api/code-environments', router);
|
|
return app;
|
|
}
|
|
|
|
describe('code environment routes', () => {
|
|
beforeEach(() => {
|
|
middlewareCalls.length = 0;
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('defers registry initialization until the route is requested', () => {
|
|
createApp();
|
|
|
|
expect(mockGetCodeEnvironmentRegistry).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('allows authenticated discovery without the management capability', async () => {
|
|
await request(createApp()).get('/api/code-environments').expect(200, { environments: [] });
|
|
|
|
expect(middlewareCalls).toEqual(['jwt']);
|
|
expect(mockHandlers.list).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('requires the management capability before registration', async () => {
|
|
await request(createApp())
|
|
.post('/api/code-environments')
|
|
.send({ name: 'Personal VM' })
|
|
.expect(201);
|
|
|
|
expect(middlewareCalls).toEqual(['jwt', 'manage:code_environments']);
|
|
expect(mockHandlers.register).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('allows an authenticated user to pair through an opted-in control plane', async () => {
|
|
await request(createApp())
|
|
.post('/api/code-environments/pairings')
|
|
.send({ name: 'Personal VM', controlPlaneId: 'shared-code-api' })
|
|
.expect(201);
|
|
|
|
expect(middlewareCalls).toEqual(['jwt', 'pairing-limit']);
|
|
expect(mockHandlers.pair).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('allows an authenticated owner to remove an environment', async () => {
|
|
await request(createApp()).delete('/api/code-environments/code-1').expect(200);
|
|
|
|
expect(middlewareCalls).toEqual(['jwt']);
|
|
expect(mockHandlers.remove).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|