#!/usr/bin/env node const assert = require('assert'); const fs = require('fs'); const os = require('os'); const path = require('path'); const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'qiwei-group-ops-auto-')); process.env.QIWEI_OUTPUTS_DIR = path.join(tempDir, 'outputs'); delete process.env.QIWEI_GROUP_OPS_LIVE_SEND; const { getGroupOperationsStore } = require('../mcp/src/core/group-operations-store'); const { qiweiGroupOpsAutomation, qiweiGroupOpsManagePlaybook } = require('../mcp/src/tools/qiwei-group-operations-run'); const store = getGroupOperationsStore(); (async () => { try { const key = 'automation-smoke'; const confirmationPlaybook = store.createPlaybook(key, { name: '确认测试 SOP', content: { cadence: [{ template: '确认测试' }] } }, 'test'); const publishDenied = await qiweiGroupOpsManagePlaybook({ accountKey: key, action: 'publish', playbookId: confirmationPlaybook.id }); assert.equal(publishDenied.status, 'error'); assert.match(publishDenied.assistantMessage, /confirmed=true/); const publishConfirmed = await qiweiGroupOpsManagePlaybook({ accountKey: key, action: 'publish', playbookId: confirmationPlaybook.id, confirmed: true }); assert.equal(publishConfirmed.status, 'ok'); const playbook = store.createPlaybook(key, { name: '自动化测试 SOP', content: { cadence: [{ time: '09:30', type: 'content', objective: '测试', template: '安全测试话术 {{room_name}}' }] } }, 'test'); store.publishPlaybook(key, playbook.id, 1, 'test'); store.upsertGroup(key, { roomId: 'room-safe', roomName: '安全测试群', playbookId: playbook.id }, 'test'); const plan = store.generatePlan(key, { roomId: 'room-safe', planDate: '2026-07-20' }, 'test'); store.updatePlanItem(key, plan.items[0].id, 'approve', {}, 'test'); const policy = store.createAutomationPolicy(key, { policy: { enabled: true, mode: 'auto', allowedGroupIds: ['room-safe'], allowedTypes: ['content'], windows: [{ days: [1], start: '09:00', end: '18:00' }], dailyLimitPerGroup: 1, weeklyLimitPerGroup: 7, consecutiveFailureLimit: 3, grayPercent: 100, requireNoOpenHighRisk: true } }, 'test'); store.activateAutomationPolicy(key, policy.id, 'test'); store.updateAutomationState(key, 'resume', 'test'); const preview = await qiweiGroupOpsAutomation({ accountKey: key, action: 'preview', at: '2026-07-20T10:00:00+08:00' }); assert.equal(preview.status, 'ok'); assert.equal(preview.summary.allowed, 1); const dryRun = await qiweiGroupOpsAutomation({ accountKey: key, action: 'run', at: '2026-07-20T10:00:00+08:00', execute: false }); assert.equal(dryRun.status, 'ok'); assert.equal(dryRun.summary.dryRun, 1); const liveBlocked = await qiweiGroupOpsAutomation({ accountKey: key, action: 'run', at: '2026-07-20T10:00:00+08:00', execute: true, confirmed: true }); assert.equal(liveBlocked.status, 'error'); assert.match(liveBlocked.assistantMessage, /环境开关未开启/); console.log('group-operations automation smoke: ok'); } finally { store.close(); fs.rmSync(tempDir, { recursive: true, force: true }); } })().catch(error => { console.error(error.stack || error.message); process.exit(1); });