#!/usr/bin/env node /** * Enqueue one help ticket and process it with real Cursor agent CLI (--once). * Usage: node scripts/run-cursor-help-real-test.mjs */ import crypto from 'node:crypto'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { setTimeout as sleep } from 'node:timers/promises'; import { createDbPool, migrateSchema } from '../db.mjs'; import { HELP_ESCALATION_STATUS, createHelpEscalationService, } from '../help-escalation.mjs'; import { processOneHelpEscalation } from '../cursor-help-worker.mjs'; import { createScheduleService } from '../schedule-service.mjs'; import { loadH5Environment } from './load-env.mjs'; loadH5Environment(path.dirname(fileURLToPath(import.meta.url))); const TEST_MARKER = '[CURSOR-REAL-TEST]'; const USER_ID = process.env.MEMIND_HELP_TEST_USER_ID ?? '1c99b83b-0454-474f-a5d2-129d34506a32'; const PAGE = process.env.MEMIND_HELP_TEST_PAGE ?? 'public/tang-dynasty.html'; const pool = createDbPool(); await migrateSchema(pool); const helpEscalationService = createHelpEscalationService({ pool }); if (!helpEscalationService.enabled) { console.error('MEMIND_CURSOR_HELP_ENABLED is off'); process.exit(1); } const helpText = [ `help ${TEST_MARKER} 我刚做了诗歌/主题 HTML 页面,但聊天里没有收到可打开的链接。`, `页面文件: ${PAGE}`, '请检查 delivery contract、修复交付,并给用户可打开的 Portal 链接(8081,不要用 5173)。', '完成后在 JSON userMessage 里给出链接。', ].join('\n'); const escalation = await helpEscalationService.enqueue({ userId: USER_ID, channel: 'h5', userText: helpText, context: { origin: 'cursor-real-test', testMarker: TEST_MARKER, relativePath: PAGE, requestId: `cursor-real-${Date.now()}`, }, }); console.log('enqueued:', { id: escalation.id, status: escalation.status }); console.log('spawning Cursor agent via processOneHelpEscalation...'); console.log('agent bin:', process.env.MEMIND_CURSOR_HELP_AGENT_BIN ?? '~/.local/bin/agent'); const scheduleService = createScheduleService(pool, { defaultTimezone: process.env.MEMIND_DEFAULT_TIMEZONE ?? 'Asia/Shanghai', }); const started = Date.now(); const result = await processOneHelpEscalation({ helpEscalationService, scheduleService, workerId: 'cursor-real-test-script', logger: console, }); const elapsedSec = Math.round((Date.now() - started) / 1000); const finalRow = await helpEscalationService.getById(escalation.id); console.log('\n=== Cursor Real Test Result ==='); console.log(JSON.stringify({ elapsedSec, id: finalRow?.id, status: finalRow?.status, wasClaimed: finalRow?.startedAt != null, resultPreview: String(finalRow?.resultText ?? '').slice(0, 200), agentOutputPreview: String(finalRow?.agentOutput ?? '').slice(0, 300), errorMessage: finalRow?.errorMessage ?? null, }, null, 2)); await pool.end(); if (finalRow?.status !== HELP_ESCALATION_STATUS.SUCCEEDED || finalRow?.startedAt == null) { process.exit(1); }