450 lines
22 KiB
JavaScript
450 lines
22 KiB
JavaScript
// 阶段 02 补充矩阵探测:正向 WS 内可自动触发的消息段 / 事件样本。
|
||||
|
|
// 默认 dry-run,只有显式 --yes 才产生真实外部副作用(真实发消息/上传文件)。
|
|||
|
|
// 凭据只从环境变量读取,脚本内不留明文。
|
|||
|
|
//
|
|||
|
|
// 用法:
|
|||
|
|
// NAPCAT_WS_TOKEN=xxx node tools/probe/matrix-probe.mjs # dry-run,仅打印计划
|
|||
|
|
// NAPCAT_WS_TOKEN=xxx node tools/probe/matrix-probe.mjs --yes # 真实执行
|
|||
|
|
// NAPCAT_WS_TOKEN=xxx node tools/probe/matrix-probe.mjs --yes --only=g_record_url,g_image_url
|
|||
|
|
// 可选:
|
|||
|
|
// NAPCAT_WS_URL 默认 ws://192.168.5.32:16099
|
|||
|
|
// PROBE_GROUP 默认 933280401
|
|||
|
|
// PROBE_FRIEND 默认 2161656029
|
|||
|
|
// PROBE_SELF 默认 3575608514(转发节点署名用)
|
|||
|
|
// SETTLE_MS 每条用例发送后等待事件的时间,默认 5000
|
|||
|
|
// ALLOW_ADMIN=1 允许尝试 set_group_admin(会改群状态,默认跳过)
|
|||
|
|
|
|||
|
|
import fs from 'node:fs';
|
|||
|
|
import path from 'node:path';
|
|||
|
|
|
|||
|
|
const WS_URL = process.env.NAPCAT_WS_URL || 'ws://192.168.5.32:16099';
|
|||
|
|
const TOKEN = process.env.NAPCAT_WS_TOKEN;
|
|||
|
|
const GROUP = Number(process.env.PROBE_GROUP || 933280401);
|
|||
|
|
const FRIEND = Number(process.env.PROBE_FRIEND || 2161656029);
|
|||
|
|
const SELF = Number(process.env.PROBE_SELF || 3575608514);
|
|||
|
|
const SETTLE_MS = Number(process.env.SETTLE_MS || 5000);
|
|||
|
|
const OUT_DIR = process.env.PROBE_OUT || 'local';
|
|||
|
|
|
|||
|
|
const argv = process.argv.slice(2);
|
|||
|
|
const DO_WRITE = argv.includes('--yes');
|
|||
|
|
const only = (argv.find((a) => a.startsWith('--only=')) || '').slice(7);
|
|||
|
|
const ONLY = only ? new Set(only.split(',')) : null;
|
|||
|
|
|
|||
|
|
if (!TOKEN) { console.error('缺少环境变量 NAPCAT_WS_TOKEN。凭据不得硬编码。'); process.exit(1); }
|
|||
|
|
|
|||
|
|
// 1x1 PNG,用于图片段
|
|||
|
|
const PNG_B64 =
|
|||
|
|
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==';
|
|||
|
|
// 明显不是 MP4 的内容,用于观察上游是否校验媒体内容
|
|||
|
|
const FAKE_MP4_B64 = Buffer.from('NOTAREALMP4FILE-QLINKER-PROBE').toString('base64');
|
|||
|
|
const TXT_B64 = Buffer.from('QLinker 阶段02 探测文件内容\n第二行\n', 'utf8').toString('base64');
|
|||
|
|
|
|||
|
|
const ts = Date.now();
|
|||
|
|
const stamp = new Date(ts).toISOString().replace(/[:.]/g, '-');
|
|||
|
|
fs.mkdirSync(OUT_DIR, { recursive: true });
|
|||
|
|
const EV_FILE = path.join(OUT_DIR, `matrix-ev-${stamp}.jsonl`);
|
|||
|
|
const RS_FILE = path.join(OUT_DIR, `matrix-result-${stamp}.jsonl`);
|
|||
|
|
const API_FILE = path.join(OUT_DIR, `matrix-api-${stamp}.jsonl`);
|
|||
|
|
const writeLine = (f, o) => fs.appendFileSync(f, JSON.stringify(o) + '\n');
|
|||
|
|
|
|||
|
|
const T0 = Date.now();
|
|||
|
|
const say = (...a) => console.log(`[+${((Date.now() - T0) / 1000).toFixed(1)}s]`, ...a);
|
|||
|
|
|
|||
|
|
// ---------- WS 客户端:同一连接既收事件又调 API ----------
|
|||
|
|
const pending = new Map();
|
|||
|
|
let ws = null;
|
|||
|
|
let authenticated = false;
|
|||
|
|
const events = [];
|
|||
|
|
|
|||
|
|
function api(action, params, timeoutMs = 25000) {
|
|||
|
|
const echo = `mp-${action}-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`;
|
|||
|
|
return new Promise((resolve) => {
|
|||
|
|
const to = setTimeout(() => {
|
|||
|
|
pending.delete(echo);
|
|||
|
|
resolve({ __timeout: true, action });
|
|||
|
|
}, timeoutMs);
|
|||
|
|
pending.set(echo, { resolve: (o) => { clearTimeout(to); resolve(o); }, to });
|
|||
|
|
ws.send(JSON.stringify({ action, params, echo }));
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function waitLifecycle(ms = 15000) {
|
|||
|
|
return new Promise((resolve) => {
|
|||
|
|
const t = setTimeout(() => resolve(false), ms);
|
|||
|
|
const iv = setInterval(() => {
|
|||
|
|
if (authenticated) { clearTimeout(t); clearInterval(iv); resolve(true); }
|
|||
|
|
}, 100);
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function open() {
|
|||
|
|
return new Promise((resolve, reject) => {
|
|||
|
|
ws = new WebSocket(`${WS_URL.replace(/\/$/, '')}/?access_token=${encodeURIComponent(TOKEN)}`);
|
|||
|
|
// 处理器必须在 resolve 之前挂好,否则握手到挂载之间的事件会丢
|
|||
|
|
ws.onmessage = (ev) => {
|
|||
|
|
let o;
|
|||
|
|
try { o = JSON.parse(String(ev.data)); } catch { return; }
|
|||
|
|
if (o.echo && pending.has(o.echo)) {
|
|||
|
|
const p = pending.get(o.echo);
|
|||
|
|
pending.delete(o.echo);
|
|||
|
|
p.resolve(o);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (o.post_type === 'meta_event' && o.meta_event_type === 'lifecycle') {
|
|||
|
|
authenticated = true;
|
|||
|
|
say(`收到 lifecycle(${o.sub_type}) —— 认证确认`);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (o.post_type === 'meta_event') return;
|
|||
|
|
const rec = { t: new Date().toISOString(), el: Date.now() - T0, data: o };
|
|||
|
|
events.push(rec);
|
|||
|
|
writeLine(EV_FILE, rec);
|
|||
|
|
};
|
|||
|
|
ws.onopen = () => resolve();
|
|||
|
|
ws.onerror = (e) => reject(new Error('ws error: ' + String(e?.message ?? e)));
|
|||
|
|
setTimeout(() => reject(new Error('ws 连接超时')), 15000);
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
await open();
|
|||
|
|
say('握手成功,等待 lifecycle 以确认认证');
|
|||
|
|
|
|||
|
|
const ok = await waitLifecycle();
|
|||
|
|
say(ok ? '认证已确认,可以安全发送' : '!! 未收到 lifecycle,API 可能全部 1403');
|
|||
|
|
if (!ok && DO_WRITE) { console.error('未确认认证,中止真实发送。'); try { ws.close(); } catch {} process.exit(2); }
|
|||
|
|
|
|||
|
|
// ---------- 素材准备 ----------
|
|||
|
|
const ctx = { group: GROUP, friend: FRIEND, self: SELF, mid: {}, url: {}, b64: {} };
|
|||
|
|
|
|||
|
|
async function prepRecord() {
|
|||
|
|
const r = await api('get_ai_record', { group_id: GROUP, character: 'lucy-voice-houge', text: 'QLinker 语音探测' });
|
|||
|
|
if (r.retcode !== 0 || typeof r.data !== 'string') {
|
|||
|
|
say('get_ai_record 失败:' + JSON.stringify(r).slice(0, 200));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
ctx.url.record = r.data;
|
|||
|
|
writeLine(path.join(OUT_DIR, `matrix-material-${stamp}.jsonl`), { kind: 'ai_record_url', url: r.data });
|
|||
|
|
try {
|
|||
|
|
const resp = await fetch(r.data);
|
|||
|
|
const buf = Buffer.from(await resp.arrayBuffer());
|
|||
|
|
const magic = buf.subarray(0, 12).toString('hex');
|
|||
|
|
ctx.b64.record = buf.toString('base64');
|
|||
|
|
ctx.recordBytes = buf.length;
|
|||
|
|
ctx.recordMagic = magic;
|
|||
|
|
say(`语音素材下载完成 ${buf.length} 字节 magic=${magic} ascii=${JSON.stringify(buf.subarray(0, 6).toString('latin1'))}`);
|
|||
|
|
writeLine(path.join(OUT_DIR, `matrix-material-${stamp}.jsonl`),
|
|||
|
|
{ kind: 'ai_record_bin', bytes: buf.length, magic, ascii: buf.subarray(0, 12).toString('latin1') });
|
|||
|
|
} catch (e) { say('语音素材下载失败:' + e.message); }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
async function prepMediaUrl() {
|
|||
|
|
// 用一条真实 base64 图片换回 CDN url,再用该 url 测 URL 形式
|
|||
|
|
const r = await api('send_group_msg', {
|
|||
|
|
group_id: GROUP,
|
|||
|
|
message: [{ type: 'image', data: { file: 'base64://' + PNG_B64 } },
|
|||
|
|
{ type: 'text', data: { text: '[QLm:prep_img]' } }],
|
|||
|
|
});
|
|||
|
|
if (r.retcode !== 0) { say('prep 图片发送失败:' + JSON.stringify(r).slice(0, 200)); return; }
|
|||
|
|
ctx.mid.prep_img = r.data?.message_id;
|
|||
|
|
const t = Date.now();
|
|||
|
|
while (Date.now() - t < SETTLE_MS) {
|
|||
|
|
const hit = events.find((e) => e.data.post_type === 'message_sent'
|
|||
|
|
&& String(e.data.raw_message || '').includes('[QLm:prep_img]')
|
|||
|
|
&& (e.data.message || []).some((s) => s.type === 'image' && s.data.url));
|
|||
|
|
if (hit) {
|
|||
|
|
const seg = hit.data.message.find((s) => s.type === 'image');
|
|||
|
|
ctx.url.image = seg.data.url;
|
|||
|
|
ctx.url.imageFileId = seg.data.file;
|
|||
|
|
say('拿到图片 CDN url: ' + String(seg.data.url).slice(0, 80));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
await new Promise((r2) => setTimeout(r2, 200));
|
|||
|
|
}
|
|||
|
|
say('未从事件里拿到图片 url');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ---------- 用例定义 ----------
|
|||
|
|
const mark = (id) => ({ type: 'text', data: { text: `[QLm:${id}]` } });
|
|||
|
|
const segs = (...s) => s;
|
|||
|
|
|
|||
|
|
// node.content 有两种可能形态:OneBot 段数组,或段数组的 JSON 字符串。
|
|||
|
|
// 上游对两者的接受度必须实测,不能猜。
|
|||
|
|
function fwdNodes(list, form) {
|
|||
|
|
return list.map((n) => ({
|
|||
|
|
type: 'node',
|
|||
|
|
data: {
|
|||
|
|
user_id: String(n.uid),
|
|||
|
|
nickname: n.nick,
|
|||
|
|
content: form === 'str' ? JSON.stringify(n.content) : n.content,
|
|||
|
|
},
|
|||
|
|
}));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const CASES = [
|
|||
|
|
{ id: 'g_text', kind: 'msg', desc: '群纯文本(作为后续 reply 的目标)',
|
|||
|
|
build: () => ({ action: 'send_group_msg', params: { group_id: GROUP, message: segs(mark('g_text')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'g_at_all', kind: 'msg', desc: '群 @全体',
|
|||
|
|
build: () => ({ action: 'send_group_msg', params: { group_id: GROUP,
|
|||
|
|
message: segs({ type: 'at', data: { qq: 'all' } }, mark('g_at_all')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'g_face', kind: 'msg', desc: '群 face 段(无文本)',
|
|||
|
|
build: () => ({ action: 'send_group_msg', params: { group_id: GROUP,
|
|||
|
|
message: segs({ type: 'face', data: { id: '178' } }, mark('g_face')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'g_record_url', kind: 'msg', desc: '群语音:真实 QQ 语音 CDN URL 形式',
|
|||
|
|
need: () => ctx.url.record,
|
|||
|
|
build: () => ({ action: 'send_group_msg', params: { group_id: GROUP,
|
|||
|
|
message: segs({ type: 'record', data: { file: ctx.url.record } }, mark('g_record_url')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'g_record_b64', kind: 'msg', desc: '群语音:真实语音 base64 形式',
|
|||
|
|
need: () => ctx.b64.record,
|
|||
|
|
build: () => ({ action: 'send_group_msg', params: { group_id: GROUP,
|
|||
|
|
message: segs({ type: 'record', data: { file: 'base64://' + ctx.b64.record } }, mark('g_record_b64')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'g_image_url', kind: 'msg', desc: '群图片:URL 形式(非 base64)',
|
|||
|
|
need: () => ctx.url.image,
|
|||
|
|
build: () => ({ action: 'send_group_msg', params: { group_id: GROUP,
|
|||
|
|
message: segs({ type: 'image', data: { file: ctx.url.image } }, mark('g_image_url')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'g_video_url', kind: 'msg', desc: '群视频:URL 形式',
|
|||
|
|
need: () => ctx.url.video,
|
|||
|
|
build: () => ({ action: 'send_group_msg', params: { group_id: GROUP,
|
|||
|
|
message: segs({ type: 'video', data: { file: ctx.url.video } }, mark('g_video_url')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'g_video_b64', kind: 'msg', desc: '群视频:base64(非合法 MP4 内容)',
|
|||
|
|
build: () => ({ action: 'send_group_msg', params: { group_id: GROUP,
|
|||
|
|
message: segs({ type: 'video', data: { file: 'base64://' + FAKE_MP4_B64 } }, mark('g_video_b64')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'g_reply_1', kind: 'msg', desc: '群引用第一层(reply -> g_text)',
|
|||
|
|
need: () => ctx.mid.g_text,
|
|||
|
|
build: () => ({ action: 'send_group_msg', params: { group_id: GROUP,
|
|||
|
|
message: segs({ type: 'reply', data: { id: String(ctx.mid.g_text) } }, mark('g_reply_1')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'g_reply_nested', kind: 'msg', desc: '群引用嵌套(reply -> 一条本身就带 reply 的消息)',
|
|||
|
|
need: () => ctx.mid.g_reply_1,
|
|||
|
|
build: () => ({ action: 'send_group_msg', params: { group_id: GROUP,
|
|||
|
|
message: segs({ type: 'reply', data: { id: String(ctx.mid.g_reply_1) } }, mark('g_reply_nested')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'g_forward_api', kind: 'msg', desc: '服务端合并转发 send_group_forward_msg',
|
|||
|
|
nodes: [
|
|||
|
|
{ uid: SELF, nick: '雪珂', content: [{ type: 'text', data: { text: '转发条目A' } }] },
|
|||
|
|
{ uid: FRIEND, nick: '小王', content: [{ type: 'text', data: { text: '转发条目B' } }] },
|
|||
|
|
{ uid: SELF, nick: '雪珂', content: [{ type: 'image', data: { file: 'base64://' + PNG_B64 } }] },
|
|||
|
|
],
|
|||
|
|
build: function () { return { action: 'send_group_forward_msg', params: { group_id: GROUP, messages: fwdNodes(this.nodes, 'str') } }; },
|
|||
|
|
alt: function () { return { group_id: GROUP, messages: fwdNodes(this.nodes, 'arr') }; } },
|
|||
|
|
|
|||
|
|
{ id: 'g_forward_read', kind: 'api', desc: 'get_forward_msg 读回上一步的合并转发',
|
|||
|
|
need: () => ctx.mid.g_forward_api,
|
|||
|
|
build: () => ({ action: 'get_forward_msg', params: { message_id: ctx.mid.g_forward_api } }) },
|
|||
|
|
|
|||
|
|
{ id: 'g_forward_read_self', kind: 'api', desc: 'get_forward_msg 读回 node 段自发合并转发',
|
|||
|
|
build: () => ({ action: 'get_forward_msg', params: { message_id: 497853103 } }) },
|
|||
|
|
|
|||
|
|
{ id: 'g_forward_read_resid', kind: 'api', desc: 'get_forward_msg 用 resid/uniseq 读回 node 段转发',
|
|||
|
|
build: () => ({ action: 'get_forward_msg', params: {
|
|||
|
|
message_id: 497853103,
|
|||
|
|
resid: '2Fxh/sSm2xg93V25/m929koqkzXAguVg0YQ5H7KoKwAgU5MIzx3yAJzNACKfCOls',
|
|||
|
|
uniseq: '55ba6c8c-d10f-4e02-ad78-9e43e30e2436' } }) },
|
|||
|
|
|
|||
|
|
{ id: 'g_music_url', kind: 'msg', desc: '音乐分享 {type,url} 形式(163 之外)',
|
|||
|
|
build: () => ({ action: 'send_group_msg', params: { group_id: GROUP,
|
|||
|
|
message: segs({ type: 'music', data: { type: '163', url: 'https://music.163.com/song?id=186016',
|
|||
|
|
audio: 'https://music.163.com/song/media/outer/url?id=186016.mp3', title: 'QLinker探测', singer: '未知' } }, mark('g_music_url')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'g_ark', kind: 'msg', desc: 'ARK 段',
|
|||
|
|
build: () => ({ action: 'send_group_msg', params: { group_id: GROUP,
|
|||
|
|
message: segs({ type: 'ark', data: { descriptor: { function: '1', msg: 'QLinker 探测' },
|
|||
|
|
prompt: '[{"app":"com.tencent.miniapp","desc":"QLinker","prompt":"QLinker 探测"}]',
|
|||
|
|
meta: { detail: { appid: '1109935564', appType: 1, title: 'QLinker 探测', desc: '探测',
|
|||
|
|
ctime: String(Math.floor(Date.now() / 1000)) } } } }, mark('g_ark')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'g_location', kind: 'msg', desc: '群位置段(复测:第19章为静默丢弃)',
|
|||
|
|
build: () => ({ action: 'send_group_msg', params: { group_id: GROUP,
|
|||
|
|
message: segs({ type: 'location', data: { lat: '31.230416', lon: '121.473701', title: '外滩', content: '上海市黄浦区' } }, mark('g_location')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'p_text', kind: 'msg', desc: '私聊文本',
|
|||
|
|
build: () => ({ action: 'send_private_msg', params: { user_id: FRIEND, message: segs(mark('p_text')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'p_face', kind: 'msg', desc: '私聊 face 段',
|
|||
|
|
build: () => ({ action: 'send_private_msg', params: { user_id: FRIEND,
|
|||
|
|
message: segs({ type: 'face', data: { id: '178' } }, mark('p_face')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'p_image_b64', kind: 'msg', desc: '私聊图片 base64',
|
|||
|
|
build: () => ({ action: 'send_private_msg', params: { user_id: FRIEND,
|
|||
|
|
message: segs({ type: 'image', data: { file: 'base64://' + PNG_B64 } }, mark('p_image_b64')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'p_record_url', kind: 'msg', desc: '私聊语音 URL 形式',
|
|||
|
|
need: () => ctx.url.record,
|
|||
|
|
build: () => ({ action: 'send_private_msg', params: { user_id: FRIEND,
|
|||
|
|
message: segs({ type: 'record', data: { file: ctx.url.record } }, mark('p_record_url')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'p_json', kind: 'msg', desc: '私聊 json 段',
|
|||
|
|
build: () => ({ action: 'send_private_msg', params: { user_id: FRIEND,
|
|||
|
|
message: segs({ type: 'json', data: { data: JSON.stringify({ app: 'com.tencent.subbubble',
|
|||
|
|
view: 'news', config: { autosize: true, forward: 1 },
|
|||
|
|
extra: { serviceId: '1600000203', templateId: '1', refstrain: 'news' },
|
|||
|
|
meta: { detail: { news: [{ text: 'QLinker 私聊 json 探测' }] } } }) } }, mark('p_json')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'p_reply', kind: 'msg', desc: '私聊引用(reply -> p_text)',
|
|||
|
|
need: () => ctx.mid.p_text,
|
|||
|
|
build: () => ({ action: 'send_private_msg', params: { user_id: FRIEND,
|
|||
|
|
message: segs({ type: 'reply', data: { id: String(ctx.mid.p_text) } }, mark('p_reply')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'p_forward_api', kind: 'msg', desc: '私聊服务端合并转发 send_private_forward_msg',
|
|||
|
|
nodes: [
|
|||
|
|
{ uid: SELF, nick: '雪珂', content: [{ type: 'text', data: { text: '私聊转发条目A' } }] },
|
|||
|
|
{ uid: FRIEND, nick: '小王', content: [{ type: 'text', data: { text: '私聊转发条目B' } }] },
|
|||
|
|
],
|
|||
|
|
build: function () { return { action: 'send_private_forward_msg', params: { user_id: FRIEND, messages: fwdNodes(this.nodes, 'str') } }; },
|
|||
|
|
alt: function () { return { user_id: FRIEND, messages: fwdNodes(this.nodes, 'arr') }; } },
|
|||
|
|
|
|||
|
|
{ id: 'p_at', kind: 'msg', desc: '私聊 at 段(观察是否被丢弃)',
|
|||
|
|
build: () => ({ action: 'send_private_msg', params: { user_id: FRIEND,
|
|||
|
|
message: segs({ type: 'at', data: { qq: String(FRIEND) } }, mark('p_at')) } }) },
|
|||
|
|
|
|||
|
|
{ id: 'g_upload_file', kind: 'file', desc: '群文件上传(观察 upload_file / group_upload_file 通知)',
|
|||
|
|
build: () => ({ action: 'upload_group_file', params: { group_id: GROUP,
|
|||
|
|
file: 'base64://' + TXT_B64, name: `qlinker-probe-${stamp}.txt` } }) },
|
|||
|
|
|
|||
|
|
{ id: 'p_upload_file', kind: 'file', desc: '私聊文件上传',
|
|||
|
|
build: () => ({ action: 'upload_private_file', params: { user_id: FRIEND,
|
|||
|
|
file: 'base64://' + TXT_B64, name: `qlinker-probe-p-${stamp}.txt` } }) },
|
|||
|
|
|
|||
|
|
{ id: 'g_admin', kind: 'risky', desc: '群管理员变动(会改群状态,需 ALLOW_ADMIN=1)',
|
|||
|
|
gate: () => process.env.ALLOW_ADMIN === '1',
|
|||
|
|
build: () => ({ action: 'set_group_admin', params: { group_id: GROUP, user_id: FRIEND, enable: true } }) },
|
|||
|
|
];
|
|||
|
|
|
|||
|
|
const results = [];
|
|||
|
|
|
|||
|
|
function eventsFor(t0, t1, id) {
|
|||
|
|
return events
|
|||
|
|
.filter((e) => e.el >= t0 && e.el <= t1)
|
|||
|
|
.filter((e) => {
|
|||
|
|
const d = e.data;
|
|||
|
|
if (d.post_type === 'message' || d.post_type === 'message_sent') {
|
|||
|
|
return String(d.raw_message || '').includes(`[QLm:${id}]`)
|
|||
|
|
|| (ctx.mid[id] && d.message_id === ctx.mid[id]);
|
|||
|
|
}
|
|||
|
|
return true;
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function segTypes(msg) {
|
|||
|
|
return Array.isArray(msg) ? msg.map((s) => s.type).join('+') : '(无)';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const planned = CASES.filter((c) => (!ONLY || ONLY.has(c.id)) && (c.gate ? c.gate() : true));
|
|||
|
|
|
|||
|
|
say(`模式=${DO_WRITE ? '真实发送' : 'DRY-RUN'} 用例=${planned.length}/${CASES.length} 事件文件=${EV_FILE}`);
|
|||
|
|
|
|||
|
|
if (!DO_WRITE) {
|
|||
|
|
for (const c of planned) console.log(` [计划] ${c.id.padEnd(20)} ${c.desc}`);
|
|||
|
|
for (const c of CASES.filter((x) => x.gate && !x.gate())) {
|
|||
|
|
console.log(` [跳过] ${c.id.padEnd(20)} 门禁未开启(需 ALLOW_ADMIN=1)`);
|
|||
|
|
}
|
|||
|
|
console.log('\nDRY-RUN 未产生任何外部调用。加 --yes 执行真实发送。');
|
|||
|
|
try { ws.close(); } catch {}
|
|||
|
|
process.exit(0);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
await prepRecord();
|
|||
|
|
await prepMediaUrl();
|
|||
|
|
|
|||
|
|
for (const c of planned) {
|
|||
|
|
if (c.need && !c.need()) {
|
|||
|
|
say(`跳过 ${c.id}:前置素材缺失`);
|
|||
|
|
results.push({ id: c.id, desc: c.desc, status: 'skipped', reason: '前置素材缺失' });
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
const { action, params } = c.build();
|
|||
|
|
const t0 = Date.now() - T0;
|
|||
|
|
let r = await api(action, params);
|
|||
|
|
let attempts = [{ params, resp: r }];
|
|||
|
|
// node.content 的形态(JSON 字符串 / 段数组)在不同版本上不一致,失败时用备用形态重试一次
|
|||
|
|
if (c.alt && (r.__timeout || r.retcode !== 0)) {
|
|||
|
|
const altParams = c.alt();
|
|||
|
|
const r2 = await api(action, altParams);
|
|||
|
|
attempts.push({ params: altParams, resp: r2 });
|
|||
|
|
if (!r2.__timeout && r2.retcode === 0) r = r2;
|
|||
|
|
}
|
|||
|
|
if (r.__timeout) r = { __timeout: true };
|
|||
|
|
const mid = r?.data?.message_id;
|
|||
|
|
if (mid) ctx.mid[c.id] = mid;
|
|||
|
|
if (c.id === 'g_video_b64' && r?.retcode === 0) {
|
|||
|
|
// 视频段回推里取 url 供 g_video_url 使用
|
|||
|
|
const t = Date.now();
|
|||
|
|
while (Date.now() - t < SETTLE_MS) {
|
|||
|
|
const hit = events.find((e) => String(e.data.raw_message || '').includes('[QLm:g_video_b64]')
|
|||
|
|
&& (e.data.message || []).some((s) => s.type === 'video' && s.data.url));
|
|||
|
|
if (hit) { ctx.url.video = hit.data.message.find((s) => s.type === 'video').data.url; break; }
|
|||
|
|
await new Promise((r2) => setTimeout(r2, 200));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
await new Promise((r2) => setTimeout(r2, SETTLE_MS));
|
|||
|
|
const t1 = Date.now() - T0;
|
|||
|
|
const evs = eventsFor(t0, t1, c.id);
|
|||
|
|
const msgEv = evs.find((e) => e.data.post_type === 'message_sent');
|
|||
|
|
if (c.kind === 'api' || attempts.length > 1) {
|
|||
|
|
writeLine(API_FILE, { id: c.id, desc: c.desc, action, attempts: attempts.map((a) => ({ params: a.params, resp: a.resp })) });
|
|||
|
|
}
|
|||
|
|
const rec = {
|
|||
|
|
id: c.id, desc: c.desc, action,
|
|||
|
|
retcode: r.__timeout ? 'TIMEOUT' : r.retcode,
|
|||
|
|
message: r.message ?? r.wording ?? '',
|
|||
|
|
message_id: mid ?? null,
|
|||
|
|
real_seq: msgEv?.data?.real_seq ?? null,
|
|||
|
|
event_segs: msgEv ? segTypes(msgEv.data.message) : (evs.length ? `(非消息事件 ${evs.length} 条)` : 'NO_EVENT'),
|
|||
|
|
notice_types: [...new Set(evs.filter((e) => e.data.post_type === 'notice').map((e) => e.data.notice_type))],
|
|||
|
|
request_types: [...new Set(evs.filter((e) => e.data.post_type === 'request').map((e) => e.data.request_type))],
|
|||
|
|
event_count: evs.length,
|
|||
|
|
retcode_trail: attempts.map((a) => (a.resp?.__timeout ? 'TIMEOUT' : a.resp?.retcode)).join('/'),
|
|||
|
|
status: r.__timeout ? 'timeout' : (r.retcode === 0 ? 'sent' : 'rejected'),
|
|||
|
|
};
|
|||
|
|
results.push(rec);
|
|||
|
|
writeLine(RS_FILE, rec);
|
|||
|
|
say(`${rec.id.padEnd(20)} retcode=${String(rec.retcode).padEnd(7)} seq=${String(rec.real_seq ?? '-').padEnd(5)} 事件段=${rec.event_segs}${rec.notice_types.length ? ' notice=' + rec.notice_types.join(',') : ''}`);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ---------- history 回读 ----------
|
|||
|
|
async function readHistory(action, key, val) {
|
|||
|
|
const r = await api(action, { [key]: val, count: 60 });
|
|||
|
|
const list = r?.data?.messages ?? [];
|
|||
|
|
const map = new Map();
|
|||
|
|
for (const m of list) {
|
|||
|
|
const raw = String(m.raw_message ?? '');
|
|||
|
|
const hit = results.find((x) => raw.includes(`[QLm:${x.id}]`));
|
|||
|
|
if (hit && !map.has(hit.id)) map.set(hit.id, { segs: segTypes(m.message), real_seq: m.real_seq, message_id: m.message_id });
|
|||
|
|
}
|
|||
|
|
return map;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
say('history 回读中…');
|
|||
|
|
const hg = await readHistory('get_group_msg_history', 'group_id', GROUP);
|
|||
|
|
const hp = await readHistory('get_friend_msg_history', 'user_id', FRIEND);
|
|||
|
|
const hm = new Map([...hg, ...hp]);
|
|||
|
|
|
|||
|
|
for (const r of results) {
|
|||
|
|
const h = hm.get(r.id);
|
|||
|
|
r.history_segs = h ? h.segs : (r.kind === 'msg' ? 'NOT_IN_HISTORY' : '-');
|
|||
|
|
r.history_seq = h?.real_seq ?? null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ---------- 摘要 ----------
|
|||
|
|
console.log('\n================ 矩阵结果 ================');
|
|||
|
|
console.log('用例'.padEnd(20) + 'retcode'.padEnd(9) + 'seq'.padEnd(7) + '事件段'.padEnd(26) + 'history段');
|
|||
|
|
for (const r of results) {
|
|||
|
|
console.log(String(r.id).padEnd(20) + String(r.retcode ?? r.status).padEnd(9)
|
|||
|
|
+ String(r.real_seq ?? '-').padEnd(7) + String(r.event_segs).padEnd(26) + String(r.history_segs));
|
|||
|
|
}
|
|||
|
|
console.log('\n语音素材: ' + (ctx.recordBytes ? `${ctx.recordBytes} 字节 magic=${ctx.recordMagic}` : '未取得'));
|
|||
|
|
console.log('事件 ' + events.length + ' 条 -> ' + EV_FILE);
|
|||
|
|
console.log('结果 -> ' + RS_FILE);
|
|||
|
|
writeLine(RS_FILE, { __summary: true, record_magic: ctx.recordMagic ?? null,
|
|||
|
|
record_bytes: ctx.recordBytes ?? null, image_url: ctx.url.image ?? null,
|
|||
|
|
video_url: ctx.url.video ?? null, record_url: ctx.url.record ?? null, events: events.length });
|
|||
|
|
try { ws.close(); } catch {}
|
|||
|
|
process.exit(0);
|