194 lines
6.0 KiB
JavaScript
194 lines
6.0 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import express from 'express';
|
|
import { once } from 'node:events';
|
|
import { createAdminApi } from './admin-routes.mjs';
|
|
|
|
async function startTestServer(router) {
|
|
const app = express();
|
|
app.use('/admin-api', router);
|
|
const server = app.listen(0, '127.0.0.1');
|
|
await once(server, 'listening');
|
|
const address = server.address();
|
|
return {
|
|
server,
|
|
baseUrl: `http://127.0.0.1:${address.port}`,
|
|
close: async () => {
|
|
server.close();
|
|
await once(server, 'close');
|
|
},
|
|
};
|
|
}
|
|
|
|
test('admin memory-v2 config routes expose config and runtime state', async () => {
|
|
const updates = [];
|
|
const router = createAdminApi({
|
|
jsonBody: express.json(),
|
|
getToken() {
|
|
return 'token-admin';
|
|
},
|
|
userAuth: {
|
|
async getMe(token) {
|
|
if (token !== 'token-admin') return null;
|
|
return { id: 'admin-1', role: 'admin' };
|
|
},
|
|
},
|
|
llmProviderService: null,
|
|
memoryV2ConfigService: {
|
|
async getAdminConfig() {
|
|
return { config: { chatIntentRouter: { enabled: true } }, updatedAt: 123 };
|
|
},
|
|
async updateAdminConfig(patch, { updatedBy }) {
|
|
updates.push({ patch, updatedBy });
|
|
return { config: patch, updatedAt: 456, updatedBy };
|
|
},
|
|
async getRuntimeState() {
|
|
return { source: 'admin-db', overrides: { MEMIND_CHAT_LLM_ROUTER_ENABLED: '1' } };
|
|
},
|
|
},
|
|
plazaPosts: null,
|
|
plazaOps: null,
|
|
wechatAdmin: null,
|
|
subscriptionService: null,
|
|
});
|
|
|
|
const server = await startTestServer(router);
|
|
try {
|
|
const configRes = await fetch(`${server.baseUrl}/admin-api/memory-v2/config`, {
|
|
headers: { cookie: 'h5_user_session=token-admin' },
|
|
});
|
|
assert.equal(configRes.status, 200);
|
|
assert.deepEqual(await configRes.json(), {
|
|
config: { chatIntentRouter: { enabled: true } },
|
|
updatedAt: 123,
|
|
});
|
|
|
|
const updateRes = await fetch(`${server.baseUrl}/admin-api/memory-v2/config`, {
|
|
method: 'PATCH',
|
|
headers: {
|
|
'content-type': 'application/json',
|
|
cookie: 'h5_user_session=token-admin',
|
|
},
|
|
body: JSON.stringify({ chatIntentRouter: { enabled: false } }),
|
|
});
|
|
assert.equal(updateRes.status, 200);
|
|
assert.deepEqual(updates, [{
|
|
patch: { chatIntentRouter: { enabled: false } },
|
|
updatedBy: 'admin-1',
|
|
}]);
|
|
|
|
const runtimeRes = await fetch(`${server.baseUrl}/admin-api/memory-v2/runtime`, {
|
|
headers: { cookie: 'h5_user_session=token-admin' },
|
|
});
|
|
assert.equal(runtimeRes.status, 200);
|
|
assert.deepEqual(await runtimeRes.json(), {
|
|
source: 'admin-db',
|
|
overrides: { MEMIND_CHAT_LLM_ROUTER_ENABLED: '1' },
|
|
});
|
|
} finally {
|
|
await server.close();
|
|
}
|
|
});
|
|
|
|
test('admin asset gateway routes preserve an explicit, admin-only control plane', async () => {
|
|
const calls = [];
|
|
const router = createAdminApi({
|
|
jsonBody: express.json(),
|
|
getToken() { return 'token-admin'; },
|
|
userAuth: { async getMe() { return { id: 'admin-1', role: 'admin' }; } },
|
|
llmProviderService: null,
|
|
memoryV2ConfigService: null,
|
|
assetGatewayConfigService: {
|
|
async getConfig() { return { enabled: false, plugins: [] }; },
|
|
async updateGlobalConfig(payload, context) { calls.push({ type: 'global', payload, context }); return { enabled: true }; },
|
|
async updatePluginConfig(pluginId, payload, context) {
|
|
calls.push({ type: 'plugin', pluginId, payload, context });
|
|
return { ok: true, config: { enabled: true, plugins: [] } };
|
|
},
|
|
},
|
|
plazaPosts: null,
|
|
plazaOps: null,
|
|
wechatAdmin: null,
|
|
subscriptionService: null,
|
|
});
|
|
const server = await startTestServer(router);
|
|
try {
|
|
const read = await fetch(`${server.baseUrl}/admin-api/asset-gateway/config`, {
|
|
headers: { cookie: 'h5_user_session=token-admin' },
|
|
});
|
|
assert.equal(read.status, 200);
|
|
assert.deepEqual(await read.json(), { enabled: false, plugins: [] });
|
|
|
|
const update = await fetch(`${server.baseUrl}/admin-api/asset-gateway/plugins/asset-generate`, {
|
|
method: 'PUT',
|
|
headers: { 'content-type': 'application/json', cookie: 'h5_user_session=token-admin' },
|
|
body: JSON.stringify({ enabled: true, provider: 'flux-schnell' }),
|
|
});
|
|
assert.equal(update.status, 200);
|
|
assert.equal(calls[0].pluginId, 'asset-generate');
|
|
assert.equal(calls[0].context.updatedBy, 'admin-1');
|
|
} finally {
|
|
await server.close();
|
|
}
|
|
});
|
|
|
|
test('admin system test route executes shared validation service', async () => {
|
|
const calls = [];
|
|
const router = createAdminApi({
|
|
jsonBody: express.json(),
|
|
getToken() {
|
|
return 'token-admin';
|
|
},
|
|
userAuth: {
|
|
async getMe(token) {
|
|
if (token !== 'token-admin') return null;
|
|
return { id: 'admin-1', role: 'admin' };
|
|
},
|
|
},
|
|
llmProviderService: null,
|
|
memoryV2ConfigService: null,
|
|
adminSystemTestService: {
|
|
async runSkillValidation(input) {
|
|
calls.push(input);
|
|
return {
|
|
ok: true,
|
|
selectedSkill: input.skillName,
|
|
account: { username: input.username },
|
|
summary: { passed: 1, warnings: 0, failed: 0 },
|
|
steps: [],
|
|
issues: [],
|
|
};
|
|
},
|
|
},
|
|
plazaPosts: null,
|
|
plazaOps: null,
|
|
wechatAdmin: null,
|
|
subscriptionService: null,
|
|
});
|
|
|
|
const server = await startTestServer(router);
|
|
try {
|
|
const response = await fetch(`${server.baseUrl}/admin-api/system-tests/skill-validation`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'content-type': 'application/json',
|
|
cookie: 'h5_user_session=token-admin',
|
|
},
|
|
body: JSON.stringify({
|
|
username: 'john',
|
|
password: 'secret',
|
|
skillName: 'service-integration-smoke',
|
|
}),
|
|
});
|
|
assert.equal(response.status, 200);
|
|
assert.deepEqual(calls, [{
|
|
username: 'john',
|
|
password: 'secret',
|
|
skillName: 'service-integration-smoke',
|
|
}]);
|
|
assert.equal((await response.json()).ok, true);
|
|
} finally {
|
|
await server.close();
|
|
}
|
|
});
|