Files
memind/wechat-cursor-executor-policy.test.mjs
T
john 617ff0d1dd
Memind CI / Test, build, and release guards (push) Has been cancelled
Add per-feature Cursor channel toggles for admin and runtime.
Expose page/data/scheduled-task/chat-bridge switches in the智趣体验通道 config so Tang can roll out Cursor paths independently with DeepSeek fallback.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-31 09:40:52 +08:00

187 lines
5.9 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {
createWechatCursorExecutorAdminConfigService,
isChannelAllowedByCursorPolicy,
isUserAllowedByWechatCursorPolicy,
isIntentAllowedByWechatCursorPolicy,
} from './wechat-cursor-executor-admin-config.mjs';
import {
CURSOR_EXECUTOR_CHANNEL,
resolveCursorChannelEligible,
resolveCursorScheduledTaskEligible,
resolveWechatCursorExecutorEligible,
} from './wechat-cursor-executor-policy.mjs';
function createMemoryPool() {
const rows = new Map();
return {
async query(sql, params = []) {
const normalized = String(sql).replace(/\s+/g, ' ').trim();
if (normalized.startsWith('CREATE TABLE')) return [[]];
if (normalized.startsWith('INSERT INTO h5_wechat_cursor_executor_config')) {
rows.set('global', {
config_json: params[1],
updated_by: params[2],
updated_at: params[3],
});
return [{ affectedRows: 1 }];
}
if (normalized.startsWith('SELECT config_json')) {
const row = rows.get('global');
return [row ? [row] : []];
}
throw new Error(`Unexpected SQL: ${normalized}`);
},
};
}
test('default policy disables cursor channel for everyone', async () => {
const service = createWechatCursorExecutorAdminConfigService(createMemoryPool());
const policy = await service.getEffectivePolicy('user-1', { userId: 'user-1' });
assert.equal(policy.enabled, false);
assert.equal(policy.userAllowed, false);
assert.deepEqual(policy.intentAllowlist, ['page.generate']);
});
test('allowlisted user can use cursor channel for page.generate only', async () => {
const pool = createMemoryPool();
const service = createWechatCursorExecutorAdminConfigService(pool);
await service.updateAdminConfig({
enabled: true,
userAllowlist: ['john-uuid'],
features: {
pageGenerate: { enabled: true },
pageData: { enabled: false },
excelAnalysis: { enabled: false },
chatBridge: { enabled: false },
scheduledTasks: { enabled: false },
},
}, { updatedBy: 'admin-1' });
const policy = await service.getEffectivePolicy('john-uuid', { userId: 'john-uuid' });
assert.equal(policy.userAllowed, true);
assert.equal(policy.features.pageGenerate.enabled, true);
assert.equal(
resolveWechatCursorExecutorEligible({
user: { userId: 'john-uuid' },
intentKind: 'page.generate',
policy,
}),
true,
);
assert.equal(
resolveWechatCursorExecutorEligible({
user: { userId: 'john-uuid' },
intentKind: 'chat.general',
policy,
}),
false,
);
assert.equal(
resolveWechatCursorExecutorEligible({
user: { userId: 'other-user' },
intentKind: 'page.generate',
policy,
}),
false,
);
});
test('resolveCursorScheduledTaskEligible follows scheduledTasks feature toggle', async () => {
const pool = createMemoryPool();
const service = createWechatCursorExecutorAdminConfigService(pool);
await service.updateAdminConfig({
enabled: true,
userAllowlist: ['john-uuid'],
features: {
pageGenerate: { enabled: true },
scheduledTasks: { enabled: true },
},
}, { updatedBy: 'admin-1' });
const policy = await service.getEffectivePolicy('john-uuid', { userId: 'john-uuid' });
assert.equal(
resolveCursorScheduledTaskEligible({ user: { userId: 'john-uuid' }, policy }),
true,
);
});
test('isUserAllowedByWechatCursorPolicy matches username aliases', () => {
const policy = { enabled: true, userAllowlist: ['john'] };
assert.equal(
isUserAllowedByWechatCursorPolicy({ userId: 'x', username: 'john' }, policy),
true,
);
assert.equal(isUserAllowedByWechatCursorPolicy({ userId: 'x' }, policy), false);
});
test('isIntentAllowedByWechatCursorPolicy respects allowlist', () => {
const policy = {
enabled: true,
intentAllowlist: ['page.generate'],
features: {
pageGenerate: { enabled: true },
pageData: { enabled: false },
excelAnalysis: { enabled: false },
chatBridge: { enabled: false },
scheduledTasks: { enabled: false },
},
};
assert.equal(isIntentAllowedByWechatCursorPolicy('page.generate', policy), true);
assert.equal(isIntentAllowedByWechatCursorPolicy('chat.general', policy), false);
});
test('H5 channel does not require intent allowlist', async () => {
const pool = createMemoryPool();
const service = createWechatCursorExecutorAdminConfigService(pool);
await service.updateAdminConfig({
enabled: true,
userAllowlist: ['john-uuid'],
channelAllowlist: ['h5', 'wechat_mp'],
}, { updatedBy: 'admin-1' });
const policy = await service.getEffectivePolicy('john-uuid', { userId: 'john-uuid' });
assert.equal(
resolveCursorChannelEligible({
user: { userId: 'john-uuid' },
channel: CURSOR_EXECUTOR_CHANNEL.H5,
policy,
}),
true,
);
});
test('H5 channel respects channelAllowlist', async () => {
const pool = createMemoryPool();
const service = createWechatCursorExecutorAdminConfigService(pool);
await service.updateAdminConfig({
enabled: true,
userAllowlist: ['john-uuid'],
channelAllowlist: ['wechat_mp'],
}, { updatedBy: 'admin-1' });
const policy = await service.getEffectivePolicy('john-uuid', { userId: 'john-uuid' });
assert.equal(
resolveCursorChannelEligible({
user: { userId: 'john-uuid' },
channel: CURSOR_EXECUTOR_CHANNEL.H5,
policy,
}),
false,
);
assert.equal(
resolveCursorChannelEligible({
user: { userId: 'john-uuid' },
channel: CURSOR_EXECUTOR_CHANNEL.WECHAT_MP,
intentKind: 'page.generate',
policy,
}),
true,
);
});
test('isChannelAllowedByCursorPolicy defaults to both channels', () => {
const policy = { channelAllowlist: ['h5', 'wechat_mp'] };
assert.equal(isChannelAllowedByCursorPolicy('h5', policy), true);
assert.equal(isChannelAllowedByCursorPolicy('wechat_mp', policy), true);
assert.equal(isChannelAllowedByCursorPolicy('other', policy), false);
});