Files
memind/wechat-cursor-executor-policy.test.mjs
T
john fa20f735a5
Memind CI / Test, build, and release guards (push) Failing after 5m45s
Skip Cursor Chat Bridge for WeChat MP and page.generate sessions.
WeChat Goose turns always include tools, which the bridge PoC rejects with HTTP 501; keep Kimi/DeepSeek on the session while Cursor handles page execution separately.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-31 10:16:03 +08:00

231 lines
7.2 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,
resolveCursorChatBridgeEligible,
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('resolveCursorChatBridgeEligible is disabled for WeChat MP and page.generate', async () => {
const pool = createMemoryPool();
const service = createWechatCursorExecutorAdminConfigService(pool);
await service.updateAdminConfig({
enabled: true,
userAllowlist: ['john-uuid'],
features: {
pageGenerate: { enabled: true },
chatBridge: { enabled: true },
},
}, { updatedBy: 'admin-1' });
const policy = await service.getEffectivePolicy('john-uuid', { userId: 'john-uuid' });
assert.equal(
resolveCursorChatBridgeEligible({
user: { userId: 'john-uuid' },
channel: CURSOR_EXECUTOR_CHANNEL.WECHAT_MP,
policy,
env: { MEMIND_CURSOR_CHAT_BRIDGE_ENABLED: '1' },
}),
false,
);
assert.equal(
resolveCursorChatBridgeEligible({
user: { userId: 'john-uuid' },
channel: CURSOR_EXECUTOR_CHANNEL.H5,
intentKind: 'page.generate',
policy,
env: { MEMIND_CURSOR_CHAT_BRIDGE_ENABLED: '1' },
}),
false,
);
assert.equal(
resolveCursorChatBridgeEligible({
user: { userId: 'john-uuid' },
channel: CURSOR_EXECUTOR_CHANNEL.H5,
intentKind: 'chat.general',
policy,
env: { MEMIND_CURSOR_CHAT_BRIDGE_ENABLED: '1' },
}),
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);
});