mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-16 13:00:55 +00:00
1388 lines
45 KiB
TypeScript
1388 lines
45 KiB
TypeScript
import { EModelEndpoint } from 'librechat-data-provider';
|
|
import type { AppConfig } from '@librechat/data-schemas';
|
|
import { CodeEnvironmentLimitError, CodeEnvironmentValidationError } from './environments';
|
|
import { createCodeEnvironmentHttpHandlers } from './http';
|
|
|
|
function response() {
|
|
const res = {
|
|
statusCode: 200,
|
|
body: undefined as unknown,
|
|
status(code: number) {
|
|
this.statusCode = code;
|
|
return this;
|
|
},
|
|
json(body: unknown) {
|
|
this.body = body;
|
|
return this;
|
|
},
|
|
};
|
|
return res;
|
|
}
|
|
|
|
describe('code environment HTTP handlers', () => {
|
|
test('reports status only for an accessible worker through its current control plane', async () => {
|
|
const fetchImpl = jest.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: jest.fn().mockResolvedValue({
|
|
protocolVersion: 1,
|
|
workerId: 'personal-vm',
|
|
online: true,
|
|
ready: true,
|
|
leaseExpiresInMs: 50_000,
|
|
capabilities: { sandboxProfile: 'native-srt', runtimes: ['bash'] },
|
|
}),
|
|
});
|
|
const controlPlane = {
|
|
id: 'self-service',
|
|
name: 'Self service',
|
|
type: 'attached' as const,
|
|
baseURL: 'https://code.example.com/v1',
|
|
owner: 'deployment' as const,
|
|
pairing: { allowPrincipalWorkers: true, tokenEnv: 'CODE_ADMIN_TOKEN' },
|
|
};
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn().mockResolvedValue({
|
|
endpoints: {
|
|
[EModelEndpoint.agents]: { statefulCodeSessions: { environments: [controlPlane] } },
|
|
},
|
|
} as unknown as AppConfig),
|
|
registry: {
|
|
register: jest.fn(),
|
|
listAccessible: jest.fn(),
|
|
listAccessibleConfigurations: jest.fn().mockResolvedValue([
|
|
{
|
|
id: 'personal-vm',
|
|
name: 'Personal VM',
|
|
type: 'attached',
|
|
baseURL: 'https://stale.example.com/v1',
|
|
controlPlaneId: 'self-service',
|
|
owner: 'principal',
|
|
workerId: 'personal-vm',
|
|
},
|
|
]),
|
|
remove: jest.fn(),
|
|
},
|
|
readSecret: jest.fn(() => 'administrator-token'),
|
|
fetchImpl,
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.status(
|
|
{
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' },
|
|
params: { environmentId: 'personal-vm' },
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
expect(res.body).toEqual({
|
|
environmentId: 'personal-vm',
|
|
status: 'ready',
|
|
leaseExpiresInMs: 50_000,
|
|
sandboxProfile: 'native-srt',
|
|
runtimes: ['bash'],
|
|
});
|
|
expect(fetchImpl).toHaveBeenCalledWith(
|
|
'https://code.example.com/v1/bridge/workers/personal-vm/status',
|
|
expect.objectContaining({ headers: { Authorization: 'Bearer administrator-token' } }),
|
|
);
|
|
});
|
|
|
|
test('does not query status for an inaccessible environment', async () => {
|
|
const fetchImpl = jest.fn();
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn().mockResolvedValue({
|
|
endpoints: { [EModelEndpoint.agents]: { statefulCodeSessions: { environments: [] } } },
|
|
} as unknown as AppConfig),
|
|
registry: {
|
|
register: jest.fn(),
|
|
listAccessible: jest.fn(),
|
|
listAccessibleConfigurations: jest.fn().mockResolvedValue([]),
|
|
remove: jest.fn(),
|
|
},
|
|
fetchImpl,
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.status(
|
|
{
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' },
|
|
params: { environmentId: 'another-users-vm' },
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(404);
|
|
expect(fetchImpl).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('does not advertise principal pairing when Code API principal auth is disabled', async () => {
|
|
const listAccessible = jest.fn();
|
|
const listAccessibleConfigurations = jest.fn();
|
|
const listAccessibleDetails = jest.fn().mockResolvedValue({
|
|
summaries: [],
|
|
configurations: [],
|
|
});
|
|
const resolvedPrincipals = [
|
|
{ principalType: 'role', principalId: 'USER' },
|
|
{ principalType: 'user', principalId: '68b2f0c498f24c1e78fa0001' },
|
|
];
|
|
const resolvePrincipals = jest.fn().mockResolvedValue(resolvedPrincipals);
|
|
const getAppConfig = jest.fn().mockResolvedValue({
|
|
endpoints: {
|
|
[EModelEndpoint.agents]: {
|
|
statefulCodeSessions: {
|
|
environments: [
|
|
{
|
|
id: 'principal-workers',
|
|
name: 'Principal workers',
|
|
type: 'attached',
|
|
baseURL: 'https://code.example.com/v1',
|
|
owner: 'deployment',
|
|
pairing: { allowPrincipalWorkers: true },
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
} as AppConfig);
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig,
|
|
registry: {
|
|
register: jest.fn(),
|
|
listAccessible,
|
|
listAccessibleConfigurations,
|
|
listAccessibleDetails,
|
|
resolvePrincipals,
|
|
remove: jest.fn(),
|
|
},
|
|
principalAuthEnabled: () => false,
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.list(
|
|
{ user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' } } as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
expect(res.body).toEqual({ environments: [], controlPlanes: [] });
|
|
expect(listAccessibleDetails).toHaveBeenCalledTimes(1);
|
|
expect(resolvePrincipals).toHaveBeenCalledTimes(1);
|
|
expect(listAccessibleDetails).toHaveBeenCalledWith(
|
|
expect.objectContaining({ principals: resolvedPrincipals }),
|
|
);
|
|
expect(getAppConfig).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
resolvedPrincipals,
|
|
skipRuntimeAugmentation: true,
|
|
}),
|
|
);
|
|
expect(listAccessible).not.toHaveBeenCalled();
|
|
expect(listAccessibleConfigurations).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('lists only principal control planes present in the caller effective policy', async () => {
|
|
const getAppConfig = jest.fn().mockResolvedValue({
|
|
endpoints: { [EModelEndpoint.agents]: { statefulCodeSessions: { environments: [] } } },
|
|
} as unknown as AppConfig);
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig,
|
|
registry: {
|
|
register: jest.fn(),
|
|
listAccessible: jest.fn().mockResolvedValue([]),
|
|
remove: jest.fn(),
|
|
},
|
|
principalAuthEnabled: () => true,
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.list(
|
|
{
|
|
user: {
|
|
id: '68b2f0c498f24c1e78fa0001',
|
|
role: 'USER',
|
|
tenantId: 'tenant-1',
|
|
},
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.body).toEqual({ environments: [], controlPlanes: [] });
|
|
expect(getAppConfig).toHaveBeenCalledWith({
|
|
role: 'USER',
|
|
userId: '68b2f0c498f24c1e78fa0001',
|
|
idOnTheSource: undefined,
|
|
tenantId: 'tenant-1',
|
|
failClosed: true,
|
|
skipRuntimeAugmentation: true,
|
|
});
|
|
});
|
|
|
|
test('returns 400 when registration has no request body', async () => {
|
|
const register = jest.fn();
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn(),
|
|
registry: { register, listAccessible: jest.fn(), remove: jest.fn() },
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.register(
|
|
{ user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' }, body: null } as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(400);
|
|
expect(register).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('updates settings only through the selected control plane config schema', async () => {
|
|
const updateSettings = jest.fn().mockResolvedValue({
|
|
resourceId: '68b2f0c498f24c1e78fa0111',
|
|
id: 'personal-vm',
|
|
name: 'Personal VM',
|
|
type: 'attached',
|
|
canDelete: true,
|
|
});
|
|
const configSchema = {
|
|
permissions: {
|
|
fileWrite: { allowed: ['allow', 'ask', 'deny'] as const, default: 'ask' as const },
|
|
},
|
|
};
|
|
const resolvedPrincipals = [
|
|
{ principalType: 'role', principalId: 'USER' },
|
|
{ principalType: 'user', principalId: '68b2f0c498f24c1e78fa0001' },
|
|
];
|
|
const resolvePrincipals = jest.fn().mockResolvedValue(resolvedPrincipals);
|
|
const listAccessibleConfigurations = jest.fn().mockResolvedValue([
|
|
{
|
|
id: 'personal-vm',
|
|
name: 'Personal VM',
|
|
type: 'attached',
|
|
baseURL: 'https://code.example.com/v1',
|
|
controlPlaneId: 'self-service',
|
|
owner: 'principal',
|
|
},
|
|
]);
|
|
const getAppConfig = jest.fn().mockResolvedValue({
|
|
endpoints: {
|
|
[EModelEndpoint.agents]: {
|
|
statefulCodeSessions: {
|
|
environments: [
|
|
{
|
|
id: 'self-service',
|
|
name: 'Self service',
|
|
type: 'attached',
|
|
baseURL: 'https://code.example.com/v1',
|
|
owner: 'deployment',
|
|
configSchema,
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
} as unknown as AppConfig);
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig,
|
|
registry: {
|
|
register: jest.fn(),
|
|
listAccessible: jest.fn(),
|
|
listAccessibleConfigurations,
|
|
resolvePrincipals,
|
|
updateSettings,
|
|
remove: jest.fn(),
|
|
},
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.updateSettings(
|
|
{
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' },
|
|
params: { environmentId: 'personal-vm' },
|
|
body: { settings: { permissions: { fileWrite: 'allow' } } },
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
expect(resolvePrincipals).toHaveBeenCalledTimes(1);
|
|
expect(listAccessibleConfigurations).toHaveBeenCalledWith(
|
|
expect.objectContaining({ principals: resolvedPrincipals }),
|
|
);
|
|
expect(getAppConfig).toHaveBeenCalledWith(
|
|
expect.objectContaining({ resolvedPrincipals, skipRuntimeAugmentation: true }),
|
|
);
|
|
expect(updateSettings).toHaveBeenCalledWith({
|
|
actor: expect.objectContaining({
|
|
userId: '68b2f0c498f24c1e78fa0001',
|
|
principals: resolvedPrincipals,
|
|
}),
|
|
environmentId: 'personal-vm',
|
|
settings: { permissions: { fileWrite: 'allow' } },
|
|
});
|
|
expect(res.body).toEqual({
|
|
environment: expect.objectContaining({
|
|
id: 'personal-vm',
|
|
configSchema,
|
|
settings: { permissions: { fileWrite: 'allow' } },
|
|
}),
|
|
});
|
|
});
|
|
|
|
test.each([
|
|
[undefined, undefined, 5],
|
|
[{ maxPerUser: 100 }, { maxPerUser: 100 }, 100],
|
|
[{ maxPerUser: 100 }, { maxPerUser: 20 }, 20],
|
|
[{ maxPerUser: 10 }, { maxPerUser: 100 }, 10],
|
|
[{ enabled: false }, { enabled: true }, 0],
|
|
[{ maxPerUser: 10 }, { maxPerUser: 0 }, 0],
|
|
])(
|
|
'pairs with deployment %j and effective policy %j (limit %i)',
|
|
async (deployment, effective, limit) => {
|
|
const register = jest.fn().mockResolvedValue({
|
|
resourceId: '68b2f0c498f24c1e78fa0111',
|
|
id: 'code-generated',
|
|
name: 'Personal VM',
|
|
type: 'attached',
|
|
});
|
|
const fetchImpl = jest.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: jest.fn().mockResolvedValue({
|
|
protocolVersion: 1,
|
|
workerId: 'code-generated',
|
|
code: 'a'.repeat(32),
|
|
expiresAt: new Date(Date.now() + 60_000).toISOString(),
|
|
}),
|
|
});
|
|
const appConfig = {
|
|
endpoints: {
|
|
[EModelEndpoint.agents]: {
|
|
statefulCodeSessions: {
|
|
allowedEnvironments: ['user'],
|
|
environments: [
|
|
{
|
|
id: 'shared-code-api',
|
|
name: 'Shared Code API',
|
|
type: 'attached',
|
|
baseURL: 'https://code.librechat.example/v1',
|
|
owner: 'deployment',
|
|
pairing: {
|
|
allowPrincipalWorkers: true,
|
|
tokenEnv: 'CODE_ADMIN_TOKEN',
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
} as AppConfig;
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn(
|
|
async (options) =>
|
|
({
|
|
...appConfig,
|
|
endpoints: {
|
|
...appConfig.endpoints,
|
|
agents: {
|
|
...appConfig.endpoints?.agents,
|
|
statefulCodeSessions: {
|
|
...appConfig.endpoints?.agents?.statefulCodeSessions,
|
|
principalWorkers: options?.baseOnly ? deployment : effective,
|
|
},
|
|
},
|
|
},
|
|
}) as AppConfig,
|
|
),
|
|
registry: {
|
|
register,
|
|
listAccessible: jest.fn().mockResolvedValue([{ id: 'existing-machine' }]),
|
|
remove: jest.fn(),
|
|
},
|
|
createEnvironmentId: () => 'code-generated',
|
|
readSecret: jest.fn(() => 'administrator-token'),
|
|
resolveTenantId: jest.fn(() => 'tenant-1'),
|
|
principalAuthEnabled: jest.fn(() => true),
|
|
principalAuthReady: jest.fn(),
|
|
fetchImpl,
|
|
});
|
|
const req = {
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' },
|
|
body: {
|
|
name: 'Personal VM',
|
|
controlPlaneId: 'shared-code-api',
|
|
workerId: 'attacker-worker',
|
|
baseURL: 'https://attacker.example',
|
|
},
|
|
};
|
|
const res = response();
|
|
|
|
await handlers.pair(req as never, res as never);
|
|
|
|
if (limit === 0) {
|
|
expect(res.statusCode).toBe(403);
|
|
expect(fetchImpl).not.toHaveBeenCalled();
|
|
expect(register).not.toHaveBeenCalled();
|
|
const discovery = response();
|
|
await handlers.list(req as never, discovery as never);
|
|
expect(discovery.statusCode).toBe(200);
|
|
expect(discovery.body).toEqual({
|
|
environments: [{ id: 'existing-machine' }],
|
|
controlPlanes: [],
|
|
});
|
|
return;
|
|
}
|
|
expect(res.statusCode).toBe(201);
|
|
expect(fetchImpl).toHaveBeenCalledWith(
|
|
'https://code.librechat.example/v1/bridge/pairings',
|
|
expect.objectContaining({
|
|
method: 'POST',
|
|
headers: expect.objectContaining({ Authorization: 'Bearer administrator-token' }),
|
|
body: JSON.stringify({
|
|
workerId: 'code-generated',
|
|
binding: {
|
|
tenantId: 'tenant-1',
|
|
principal: { type: 'user', id: '68b2f0c498f24c1e78fa0001' },
|
|
},
|
|
}),
|
|
}),
|
|
);
|
|
expect(register).toHaveBeenCalledWith({
|
|
actor: {
|
|
userId: '68b2f0c498f24c1e78fa0001',
|
|
role: 'USER',
|
|
idOnTheSource: null,
|
|
},
|
|
maxOwned: limit,
|
|
environment: {
|
|
id: 'code-generated',
|
|
name: 'Personal VM',
|
|
type: 'attached' as const,
|
|
baseURL: 'https://code.librechat.example/v1',
|
|
workerId: 'code-generated',
|
|
controlPlaneId: 'shared-code-api',
|
|
revocationTokenEnv: 'CODE_ADMIN_TOKEN',
|
|
workerPrincipal: { type: 'user', id: '68b2f0c498f24c1e78fa0001' },
|
|
},
|
|
});
|
|
expect(res.body).toEqual({
|
|
environment: expect.objectContaining({ id: 'code-generated' }),
|
|
pairing: expect.objectContaining({
|
|
workerId: 'code-generated',
|
|
code: 'a'.repeat(32),
|
|
endpoint: 'https://code.librechat.example/v1',
|
|
}),
|
|
});
|
|
},
|
|
);
|
|
|
|
test('revokes an upstream pairing when the atomic owner quota is exhausted', async () => {
|
|
const fetchImpl = jest.fn(
|
|
async (input: string | URL | Request) =>
|
|
({
|
|
ok: true,
|
|
json: async () =>
|
|
String(input).endsWith('/bridge/pairings')
|
|
? {
|
|
protocolVersion: 1,
|
|
workerId: 'code-generated',
|
|
code: 'a'.repeat(32),
|
|
expiresAt: new Date(Date.now() + 60_000).toISOString(),
|
|
}
|
|
: { protocolVersion: 1, revoked: true },
|
|
}) as Response,
|
|
);
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn().mockResolvedValue({
|
|
endpoints: {
|
|
[EModelEndpoint.agents]: {
|
|
statefulCodeSessions: {
|
|
allowedEnvironments: ['user'],
|
|
environments: [
|
|
{
|
|
id: 'shared-code-api',
|
|
name: 'Shared Code API',
|
|
type: 'attached',
|
|
baseURL: 'https://code.librechat.example/v1',
|
|
owner: 'deployment',
|
|
pairing: {
|
|
allowPrincipalWorkers: true,
|
|
tokenEnv: 'CODE_ADMIN_TOKEN',
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
} as AppConfig),
|
|
registry: {
|
|
register: jest.fn().mockRejectedValue(new CodeEnvironmentLimitError()),
|
|
listAccessible: jest.fn(),
|
|
remove: jest.fn(),
|
|
},
|
|
createEnvironmentId: () => 'code-generated',
|
|
readSecret: () => 'administrator-token',
|
|
principalAuthEnabled: () => true,
|
|
principalAuthReady: jest.fn().mockResolvedValue(undefined),
|
|
fetchImpl,
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.pair(
|
|
{
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' },
|
|
body: { name: 'Personal VM', controlPlaneId: 'shared-code-api' },
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(409);
|
|
expect(res.body).toEqual({ error: 'Personal code environment limit reached' });
|
|
expect(fetchImpl).toHaveBeenCalledWith(
|
|
'https://code.librechat.example/v1/bridge/workers/code-generated/revoke',
|
|
expect.objectContaining({ method: 'POST' }),
|
|
);
|
|
});
|
|
|
|
test('returns 503 without issuing a pairing when the initial principal check is unavailable', async () => {
|
|
const fetchImpl = jest.fn();
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn().mockResolvedValue({
|
|
endpoints: {
|
|
[EModelEndpoint.agents]: {
|
|
statefulCodeSessions: {
|
|
allowedEnvironments: ['user'],
|
|
environments: [
|
|
{
|
|
id: 'shared-code-api',
|
|
name: 'Shared Code API',
|
|
type: 'attached',
|
|
baseURL: 'https://code.librechat.example/v1',
|
|
owner: 'deployment',
|
|
pairing: { allowPrincipalWorkers: true, tokenEnv: 'CODE_ADMIN_TOKEN' },
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
} as AppConfig),
|
|
registry: { register: jest.fn(), listAccessible: jest.fn(), remove: jest.fn() },
|
|
createEnvironmentId: () => 'code-generated',
|
|
readSecret: () => 'administrator-token',
|
|
principalAuthEnabled: () => true,
|
|
principalAuthReady: jest.fn(),
|
|
principalIsActive: jest.fn().mockRejectedValue(new Error('user store unavailable')),
|
|
fetchImpl,
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.pair(
|
|
{
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' },
|
|
body: { name: 'Personal VM', controlPlaneId: 'shared-code-api' },
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(503);
|
|
expect(res.body).toEqual({ error: 'Account status could not be confirmed' });
|
|
expect(fetchImpl).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('revokes through the registry fence when the principal becomes inactive after registration', async () => {
|
|
const remove = jest.fn(
|
|
async ({ beforeDelete }: { beforeDelete?: (target: never) => Promise<void> }) => {
|
|
await beforeDelete?.({} as never);
|
|
return { id: 'code-generated' } as never;
|
|
},
|
|
);
|
|
const principalIsActive = jest
|
|
.fn()
|
|
.mockResolvedValueOnce(true)
|
|
.mockResolvedValueOnce(true)
|
|
.mockResolvedValueOnce(false);
|
|
const fetchImpl = jest.fn(
|
|
async (input: string | URL | Request) =>
|
|
({
|
|
ok: true,
|
|
json: async () =>
|
|
String(input).endsWith('/bridge/pairings')
|
|
? {
|
|
protocolVersion: 1,
|
|
workerId: 'code-generated',
|
|
code: 'a'.repeat(32),
|
|
expiresAt: new Date(Date.now() + 60_000).toISOString(),
|
|
}
|
|
: { protocolVersion: 1, revoked: true },
|
|
}) as Response,
|
|
);
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn().mockResolvedValue({
|
|
endpoints: {
|
|
[EModelEndpoint.agents]: {
|
|
statefulCodeSessions: {
|
|
allowedEnvironments: ['user'],
|
|
environments: [
|
|
{
|
|
id: 'shared-code-api',
|
|
name: 'Shared Code API',
|
|
type: 'attached',
|
|
baseURL: 'https://code.librechat.example/v1',
|
|
owner: 'deployment',
|
|
pairing: { allowPrincipalWorkers: true, tokenEnv: 'CODE_ADMIN_TOKEN' },
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
} as AppConfig),
|
|
registry: {
|
|
register: jest.fn().mockResolvedValue({ id: 'code-generated' }),
|
|
listAccessible: jest.fn(),
|
|
remove,
|
|
},
|
|
createEnvironmentId: () => 'code-generated',
|
|
readSecret: () => 'administrator-token',
|
|
principalAuthEnabled: () => true,
|
|
principalAuthReady: jest.fn(),
|
|
principalIsActive,
|
|
fetchImpl,
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.pair(
|
|
{
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' },
|
|
body: { name: 'Personal VM', controlPlaneId: 'shared-code-api' },
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(409);
|
|
expect(remove).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
environmentId: 'code-generated',
|
|
beforeDelete: expect.any(Function),
|
|
}),
|
|
);
|
|
expect(fetchImpl).toHaveBeenLastCalledWith(
|
|
'https://code.librechat.example/v1/bridge/workers/code-generated/revoke',
|
|
expect.objectContaining({ method: 'POST' }),
|
|
);
|
|
});
|
|
|
|
test('persists cleanup intent before compensating a registered pairing after removal fails', async () => {
|
|
const markRevocationPending = jest.fn().mockResolvedValue(undefined);
|
|
const remove = jest.fn().mockRejectedValue(new Error('registry removal failed'));
|
|
const principalIsActive = jest
|
|
.fn()
|
|
.mockResolvedValueOnce(true)
|
|
.mockResolvedValueOnce(true)
|
|
.mockResolvedValueOnce(false);
|
|
const fetchImpl = jest.fn(
|
|
async (input: string | URL | Request) =>
|
|
({
|
|
ok: true,
|
|
json: async () =>
|
|
String(input).endsWith('/bridge/pairings')
|
|
? {
|
|
protocolVersion: 1,
|
|
workerId: 'code-generated',
|
|
code: 'a'.repeat(32),
|
|
expiresAt: new Date(Date.now() + 60_000).toISOString(),
|
|
}
|
|
: { protocolVersion: 1, revoked: true },
|
|
}) as Response,
|
|
);
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn().mockResolvedValue({
|
|
endpoints: {
|
|
[EModelEndpoint.agents]: {
|
|
statefulCodeSessions: {
|
|
allowedEnvironments: ['user'],
|
|
environments: [
|
|
{
|
|
id: 'shared-code-api',
|
|
name: 'Shared Code API',
|
|
type: 'attached',
|
|
baseURL: 'https://code.librechat.example/v1',
|
|
owner: 'deployment',
|
|
pairing: { allowPrincipalWorkers: true, tokenEnv: 'CODE_ADMIN_TOKEN' },
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
} as AppConfig),
|
|
registry: {
|
|
register: jest.fn().mockResolvedValue({ id: 'code-generated' }),
|
|
listAccessible: jest.fn(),
|
|
remove,
|
|
markRevocationPending,
|
|
},
|
|
createEnvironmentId: () => 'code-generated',
|
|
readSecret: () => 'administrator-token',
|
|
principalAuthEnabled: () => true,
|
|
principalAuthReady: jest.fn(),
|
|
principalIsActive,
|
|
fetchImpl,
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.pair(
|
|
{
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' },
|
|
body: { name: 'Personal VM', controlPlaneId: 'shared-code-api' },
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(500);
|
|
expect(markRevocationPending).toHaveBeenCalledWith('code-generated');
|
|
expect(markRevocationPending.mock.invocationCallOrder[0]).toBeLessThan(
|
|
fetchImpl.mock.invocationCallOrder[fetchImpl.mock.invocationCallOrder.length - 1] ?? 0,
|
|
);
|
|
});
|
|
|
|
test('revokes an issued pairing when the post-issue principal check is unavailable', async () => {
|
|
const register = jest.fn();
|
|
const principalIsActive = jest
|
|
.fn()
|
|
.mockResolvedValueOnce(true)
|
|
.mockRejectedValueOnce(new Error('user store unavailable'));
|
|
const fetchImpl = jest.fn(
|
|
async (input: string | URL | Request) =>
|
|
({
|
|
ok: true,
|
|
json: async () =>
|
|
String(input).endsWith('/bridge/pairings')
|
|
? {
|
|
protocolVersion: 1,
|
|
workerId: 'code-generated',
|
|
code: 'a'.repeat(32),
|
|
expiresAt: new Date(Date.now() + 60_000).toISOString(),
|
|
}
|
|
: { protocolVersion: 1, revoked: true },
|
|
}) as Response,
|
|
);
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn().mockResolvedValue({
|
|
endpoints: {
|
|
[EModelEndpoint.agents]: {
|
|
statefulCodeSessions: {
|
|
allowedEnvironments: ['user'],
|
|
environments: [
|
|
{
|
|
id: 'shared-code-api',
|
|
name: 'Shared Code API',
|
|
type: 'attached',
|
|
baseURL: 'https://code.librechat.example/v1',
|
|
owner: 'deployment',
|
|
pairing: { allowPrincipalWorkers: true, tokenEnv: 'CODE_ADMIN_TOKEN' },
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
} as AppConfig),
|
|
registry: { register, listAccessible: jest.fn(), remove: jest.fn() },
|
|
createEnvironmentId: () => 'code-generated',
|
|
readSecret: () => 'administrator-token',
|
|
principalAuthEnabled: () => true,
|
|
principalAuthReady: jest.fn(),
|
|
principalIsActive,
|
|
fetchImpl,
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.pair(
|
|
{
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' },
|
|
body: { name: 'Personal VM', controlPlaneId: 'shared-code-api' },
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(503);
|
|
expect(register).not.toHaveBeenCalled();
|
|
expect(fetchImpl).toHaveBeenLastCalledWith(
|
|
'https://code.librechat.example/v1/bridge/workers/code-generated/revoke',
|
|
expect.objectContaining({ method: 'POST' }),
|
|
);
|
|
});
|
|
|
|
test('rejects self-service pairing without principal-aware Code API auth', async () => {
|
|
const fetchImpl = jest.fn();
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn(),
|
|
registry: { register: jest.fn(), listAccessible: jest.fn(), remove: jest.fn() },
|
|
principalAuthEnabled: jest.fn(() => false),
|
|
fetchImpl,
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.pair(
|
|
{
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' },
|
|
body: { name: 'Personal VM', controlPlaneId: 'self-service' },
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(409);
|
|
expect(fetchImpl).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('does not pair through a control plane removed from the caller effective config', async () => {
|
|
const fetchImpl = jest.fn();
|
|
const baseConfig = {
|
|
endpoints: {
|
|
[EModelEndpoint.agents]: {
|
|
statefulCodeSessions: {
|
|
environments: [
|
|
{
|
|
id: 'self-service',
|
|
name: 'Self-service',
|
|
type: 'attached',
|
|
baseURL: 'https://code.librechat.example',
|
|
owner: 'deployment',
|
|
pairing: { allowPrincipalWorkers: true, tokenEnv: 'CODE_ADMIN_TOKEN' },
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
} as AppConfig;
|
|
const effectiveConfig = {
|
|
endpoints: { [EModelEndpoint.agents]: { statefulCodeSessions: { environments: [] } } },
|
|
} as unknown as AppConfig;
|
|
const getAppConfig = jest.fn(async (options) =>
|
|
options.baseOnly === true ? baseConfig : effectiveConfig,
|
|
);
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig,
|
|
registry: { register: jest.fn(), listAccessible: jest.fn(), remove: jest.fn() },
|
|
readSecret: jest.fn(() => 'administrator-token'),
|
|
principalAuthEnabled: jest.fn(() => true),
|
|
principalAuthReady: jest.fn(),
|
|
fetchImpl,
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.pair(
|
|
{
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER', tenantId: 'tenant-1' },
|
|
body: { name: 'Personal VM', controlPlaneId: 'self-service' },
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(404);
|
|
expect(getAppConfig).toHaveBeenCalledWith({
|
|
role: 'USER',
|
|
userId: '68b2f0c498f24c1e78fa0001',
|
|
idOnTheSource: undefined,
|
|
tenantId: 'tenant-1',
|
|
failClosed: true,
|
|
});
|
|
expect(fetchImpl).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('fails closed when effective pairing policy cannot be loaded', async () => {
|
|
const fetchImpl = jest.fn();
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn(async (options) => {
|
|
if (options.baseOnly === true) return {} as AppConfig;
|
|
throw new Error('authorization unavailable');
|
|
}),
|
|
registry: { register: jest.fn(), listAccessible: jest.fn(), remove: jest.fn() },
|
|
principalAuthEnabled: jest.fn(() => true),
|
|
principalAuthReady: jest.fn(),
|
|
fetchImpl,
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.pair(
|
|
{
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' },
|
|
body: { name: 'Personal VM', controlPlaneId: 'self-service' },
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(503);
|
|
expect(fetchImpl).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('authorizes pairing effectively but resolves destinations and secrets from deployment config', async () => {
|
|
const fetchImpl = jest.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: jest.fn().mockResolvedValue({
|
|
protocolVersion: 1,
|
|
workerId: 'code-generated',
|
|
code: 'a'.repeat(32),
|
|
expiresAt: new Date(Date.now() + 60_000).toISOString(),
|
|
}),
|
|
});
|
|
const deploymentConfig = {
|
|
endpoints: {
|
|
[EModelEndpoint.agents]: {
|
|
statefulCodeSessions: {
|
|
environments: [
|
|
{
|
|
id: 'self-service',
|
|
name: 'Self-service',
|
|
type: 'attached',
|
|
baseURL: 'https://code.librechat.example',
|
|
owner: 'deployment',
|
|
pairing: { allowPrincipalWorkers: true, tokenEnv: 'CODE_ADMIN_TOKEN' },
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
} as AppConfig;
|
|
const effectiveConfig = {
|
|
endpoints: {
|
|
[EModelEndpoint.agents]: {
|
|
statefulCodeSessions: {
|
|
environments: [
|
|
{
|
|
id: 'self-service',
|
|
name: 'Override attempt',
|
|
type: 'attached',
|
|
baseURL: 'https://attacker.example',
|
|
owner: 'deployment',
|
|
pairing: { allowPrincipalWorkers: true, tokenEnv: 'DATABASE_URL' },
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
} as AppConfig;
|
|
const readSecret = jest.fn((name) =>
|
|
name === 'CODE_ADMIN_TOKEN' ? 'administrator-token' : 'database-secret',
|
|
);
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn(async (options) =>
|
|
options.baseOnly === true ? deploymentConfig : effectiveConfig,
|
|
),
|
|
registry: {
|
|
register: jest.fn().mockResolvedValue({ id: 'code-generated' }),
|
|
listAccessible: jest.fn(),
|
|
remove: jest.fn(),
|
|
},
|
|
createEnvironmentId: () => 'code-generated',
|
|
readSecret,
|
|
resolveTenantId: jest.fn(() => 'tenant-1'),
|
|
principalAuthEnabled: jest.fn(() => true),
|
|
principalAuthReady: jest.fn(),
|
|
fetchImpl,
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.pair(
|
|
{
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' },
|
|
body: { name: 'Personal VM', controlPlaneId: 'self-service' },
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(201);
|
|
expect(readSecret).toHaveBeenCalledWith('CODE_ADMIN_TOKEN');
|
|
expect(readSecret).not.toHaveBeenCalledWith('DATABASE_URL');
|
|
expect(fetchImpl).toHaveBeenCalledWith(
|
|
'https://code.librechat.example/bridge/pairings',
|
|
expect.any(Object),
|
|
);
|
|
});
|
|
|
|
test('validates JWT signing before creating upstream pairing state', async () => {
|
|
const fetchImpl = jest.fn();
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn(),
|
|
registry: { register: jest.fn(), listAccessible: jest.fn(), remove: jest.fn() },
|
|
principalAuthEnabled: jest.fn(() => true),
|
|
principalAuthReady: jest.fn(() => {
|
|
throw new Error('invalid signing key');
|
|
}),
|
|
fetchImpl,
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.pair(
|
|
{
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' },
|
|
body: { name: 'Personal VM', controlPlaneId: 'self-service' },
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(503);
|
|
expect(fetchImpl).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('registers against an operator-configured control plane and ignores client URLs', async () => {
|
|
const register = jest.fn().mockResolvedValue({
|
|
resourceId: '68b2f0c498f24c1e78fa0111',
|
|
id: 'personal-vm',
|
|
name: 'Personal VM',
|
|
type: 'attached',
|
|
});
|
|
const appConfig = {
|
|
endpoints: {
|
|
[EModelEndpoint.agents]: {
|
|
statefulCodeSessions: {
|
|
allowedEnvironments: ['user'],
|
|
environments: [
|
|
{
|
|
id: 'shared-code-api',
|
|
name: 'Shared Code API',
|
|
type: 'attached',
|
|
baseURL: 'https://code.librechat.example',
|
|
owner: 'deployment',
|
|
pairing: { workerId: 'deployment-worker', tokenEnv: 'CODE_ADMIN_TOKEN' },
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
} as AppConfig;
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn().mockResolvedValue(appConfig),
|
|
registry: { register, listAccessible: jest.fn(), remove: jest.fn() },
|
|
createEnvironmentId: () => 'personal-vm',
|
|
});
|
|
const req = {
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' },
|
|
body: {
|
|
name: 'Personal VM',
|
|
controlPlaneId: 'shared-code-api',
|
|
workerId: 'attacker-worker',
|
|
baseURL: 'https://attacker.example',
|
|
},
|
|
};
|
|
const res = response();
|
|
|
|
await handlers.register(req as never, res as never);
|
|
|
|
expect(res.statusCode).toBe(201);
|
|
expect(register).toHaveBeenCalledWith({
|
|
actor: {
|
|
userId: '68b2f0c498f24c1e78fa0001',
|
|
role: 'USER',
|
|
idOnTheSource: null,
|
|
},
|
|
environment: {
|
|
id: 'personal-vm',
|
|
name: 'Personal VM',
|
|
type: 'attached',
|
|
baseURL: 'https://code.librechat.example',
|
|
controlPlaneId: 'shared-code-api',
|
|
workerId: 'deployment-worker',
|
|
workerPrincipal: { type: 'deployment', id: 'shared-code-api' },
|
|
},
|
|
});
|
|
});
|
|
|
|
test('removes a fixed environment when its principal becomes inactive after registration', async () => {
|
|
const register = jest.fn().mockResolvedValue({
|
|
resourceId: '68b2f0c498f24c1e78fa0111',
|
|
id: 'personal-vm',
|
|
name: 'Personal VM',
|
|
type: 'attached',
|
|
});
|
|
const remove = jest.fn().mockResolvedValue({ id: 'personal-vm' });
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn().mockResolvedValue({
|
|
endpoints: {
|
|
[EModelEndpoint.agents]: {
|
|
statefulCodeSessions: {
|
|
environments: [
|
|
{
|
|
id: 'shared-code-api',
|
|
name: 'Shared Code API',
|
|
type: 'attached',
|
|
baseURL: 'https://code.librechat.example',
|
|
owner: 'deployment',
|
|
pairing: { workerId: 'deployment-worker' },
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
} as AppConfig),
|
|
registry: { register, listAccessible: jest.fn(), remove },
|
|
createEnvironmentId: () => 'personal-vm',
|
|
principalIsActive: jest.fn().mockResolvedValueOnce(true).mockResolvedValueOnce(false),
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.register(
|
|
{
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' },
|
|
body: { name: 'Personal VM', controlPlaneId: 'shared-code-api' },
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(409);
|
|
expect(remove).toHaveBeenCalledWith({
|
|
actor: {
|
|
userId: '68b2f0c498f24c1e78fa0001',
|
|
role: 'USER',
|
|
idOnTheSource: null,
|
|
},
|
|
environmentId: 'personal-vm',
|
|
});
|
|
});
|
|
|
|
test('returns 503 without fixed registration when the principal check is unavailable', async () => {
|
|
const register = jest.fn();
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn().mockResolvedValue({
|
|
endpoints: {
|
|
[EModelEndpoint.agents]: {
|
|
statefulCodeSessions: {
|
|
environments: [
|
|
{
|
|
id: 'shared-code-api',
|
|
name: 'Shared Code API',
|
|
type: 'attached',
|
|
baseURL: 'https://code.librechat.example',
|
|
owner: 'deployment',
|
|
pairing: { workerId: 'deployment-worker' },
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
} as AppConfig),
|
|
registry: { register, listAccessible: jest.fn(), remove: jest.fn() },
|
|
principalIsActive: jest.fn().mockRejectedValue(new Error('user store unavailable')),
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.register(
|
|
{
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' },
|
|
body: { name: 'Personal VM', controlPlaneId: 'shared-code-api' },
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(503);
|
|
expect(register).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('does not register a fixed environment on a self-service-only control plane', async () => {
|
|
const register = jest.fn();
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn().mockResolvedValue({
|
|
endpoints: {
|
|
[EModelEndpoint.agents]: {
|
|
statefulCodeSessions: {
|
|
allowedEnvironments: ['user'],
|
|
environments: [
|
|
{
|
|
id: 'self-service',
|
|
name: 'Self-service',
|
|
type: 'attached',
|
|
baseURL: 'https://code.librechat.example',
|
|
owner: 'deployment',
|
|
pairing: {
|
|
allowPrincipalWorkers: true,
|
|
tokenEnv: 'CODE_ADMIN_TOKEN',
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
} as AppConfig),
|
|
registry: { register, listAccessible: jest.fn(), remove: jest.fn() },
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.register(
|
|
{
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'ADMIN' },
|
|
body: { name: 'Invalid fixed route', controlPlaneId: 'self-service' },
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(404);
|
|
expect(register).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('validates a pairing name before creating upstream state', async () => {
|
|
const fetchImpl = jest.fn();
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn().mockResolvedValue({
|
|
endpoints: {
|
|
[EModelEndpoint.agents]: {
|
|
statefulCodeSessions: {
|
|
allowedEnvironments: ['user'],
|
|
environments: [
|
|
{
|
|
id: 'self-service',
|
|
name: 'Self-service',
|
|
type: 'attached',
|
|
baseURL: 'https://code.librechat.example',
|
|
owner: 'deployment',
|
|
pairing: {
|
|
allowPrincipalWorkers: true,
|
|
tokenEnv: 'CODE_ADMIN_TOKEN',
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
} as AppConfig),
|
|
registry: { register: jest.fn(), listAccessible: jest.fn(), remove: jest.fn() },
|
|
readSecret: jest.fn(() => 'administrator-token'),
|
|
principalAuthEnabled: jest.fn(() => true),
|
|
principalAuthReady: jest.fn(),
|
|
fetchImpl,
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.pair(
|
|
{
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' },
|
|
body: { name: 'x'.repeat(101), controlPlaneId: 'self-service' },
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(400);
|
|
expect(fetchImpl).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('revokes a user-bound worker before deleting its registry entry', async () => {
|
|
const remove = jest.fn(async ({ beforeDelete }) => {
|
|
await beforeDelete({
|
|
resourceId: '68b2f0c498f24c1e78fa0111',
|
|
id: 'code-generated',
|
|
name: 'Personal VM',
|
|
type: 'attached',
|
|
baseURL: 'https://code.librechat.example/v1',
|
|
workerId: 'code-generated',
|
|
controlPlaneId: 'self-service',
|
|
revocationTokenEnv: 'CODE_ADMIN_TOKEN',
|
|
workerPrincipal: { type: 'user', id: '68b2f0c498f24c1e78fa0001' },
|
|
});
|
|
return {
|
|
resourceId: '68b2f0c498f24c1e78fa0111',
|
|
id: 'code-generated',
|
|
name: 'Personal VM',
|
|
type: 'attached' as const,
|
|
canDelete: true,
|
|
};
|
|
});
|
|
const fetchImpl = jest.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: async () => ({ protocolVersion: 1, revoked: true }),
|
|
});
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn().mockResolvedValue({
|
|
endpoints: {
|
|
[EModelEndpoint.agents]: {
|
|
statefulCodeSessions: {
|
|
allowedEnvironments: ['user'],
|
|
environments: [
|
|
{
|
|
id: 'self-service',
|
|
name: 'Self-service',
|
|
type: 'attached',
|
|
baseURL: 'https://code.librechat.example/v1',
|
|
owner: 'deployment',
|
|
pairing: {
|
|
allowPrincipalWorkers: true,
|
|
tokenEnv: 'CODE_ADMIN_TOKEN',
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
} as AppConfig),
|
|
registry: { register: jest.fn(), listAccessible: jest.fn(), remove },
|
|
readSecret: jest.fn(() => 'administrator-token'),
|
|
fetchImpl,
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.remove(
|
|
{
|
|
user: { id: '68b2f0c498f24c1e78fa0001', role: 'USER' },
|
|
params: { environmentId: 'code-generated' },
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
expect(fetchImpl).toHaveBeenCalledWith(
|
|
'https://code.librechat.example/v1/bridge/workers/code-generated/revoke',
|
|
expect.objectContaining({
|
|
method: 'POST',
|
|
headers: { Authorization: 'Bearer administrator-token' },
|
|
}),
|
|
);
|
|
expect(remove).toHaveBeenCalled();
|
|
});
|
|
|
|
test.each([
|
|
{
|
|
error: new Error('mongo connection details'),
|
|
status: 500,
|
|
body: { error: 'Code environment registration failed' },
|
|
},
|
|
{
|
|
error: new CodeEnvironmentValidationError('Code environment id is invalid'),
|
|
status: 400,
|
|
body: { error: 'Code environment id is invalid' },
|
|
},
|
|
])('classifies registration failure with status $status', async ({ error, status, body }) => {
|
|
const handlers = createCodeEnvironmentHttpHandlers({
|
|
getAppConfig: jest.fn().mockResolvedValue({
|
|
endpoints: {
|
|
[EModelEndpoint.agents]: {
|
|
statefulCodeSessions: {
|
|
environments: [
|
|
{
|
|
id: 'shared-code-api',
|
|
name: 'Shared Code API',
|
|
type: 'attached',
|
|
baseURL: 'https://code.librechat.example',
|
|
owner: 'deployment',
|
|
pairing: { workerId: 'deployment-worker' },
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
} as AppConfig),
|
|
registry: {
|
|
register: jest.fn().mockRejectedValue(error),
|
|
listAccessible: jest.fn(),
|
|
remove: jest.fn(),
|
|
},
|
|
});
|
|
const res = response();
|
|
|
|
await handlers.register(
|
|
{
|
|
user: { id: '68b2f0c498f24c1e78fa0001' },
|
|
body: { name: 'Personal VM', controlPlaneId: 'shared-code-api' },
|
|
} as never,
|
|
res as never,
|
|
);
|
|
|
|
expect(res.statusCode).toBe(status);
|
|
expect(res.body).toEqual(body);
|
|
});
|
|
});
|