Files
memind/wechat-cursor-executor-policy.test.mjs
T
john 644f7fc632
Memind CI / Test, build, and release guards (push) Has been cancelled
fix(wechat): add cursor channel modules required by wechat-mp imports
Ship the WeChat Cursor executor helpers referenced by the page delivery path so tests and runtime imports resolve consistently.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-26 21:14:36 +08:00

92 lines
3.0 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {
createWechatCursorExecutorAdminConfigService,
isUserAllowedByWechatCursorPolicy,
isIntentAllowedByWechatCursorPolicy,
} from './wechat-cursor-executor-admin-config.mjs';
import { 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'],
intentAllowlist: ['page.generate'],
}, { updatedBy: 'admin-1' });
const policy = await service.getEffectivePolicy('john-uuid', { userId: 'john-uuid' });
assert.equal(policy.userAllowed, 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('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 = { intentAllowlist: ['page.generate'] };
assert.equal(isIntentAllowedByWechatCursorPolicy('page.generate', policy), true);
assert.equal(isIntentAllowedByWechatCursorPolicy('chat.general', policy), false);
});