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, 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); }); 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); });