refactor(protocol,onebot): add message-element four-direction reconciliation manifest
消息元素散在 4 处派发(收·解 / 收·转 / 发·解 / 发·打包),历史上各写各的、
无对账,导致字段名对不上、漏写方向走 default 静默失败。
阶段 1(零行为改动):立一份权威清单 element-manifest + 两道架构护栏测试。
- element-manifest.ts:每种元素类型 × 四向支持('yes'/'no'/'by-design-no')
+ 字段契约 + typesForDirection() + 输入糖白名单。清单逐字反映当前行为。
- 两道对账测试(因包边界拆两处、共用清单):正则扫 4 处派发代码,比对清单。
谁增删一个方向却不同步清单,测试当场报红,不再运行时静默。
- 取值域用 [A-Za-z0-9_]+,避免未来驼峰/下划线类型名被静默漏抓致护栏失效。
- poke 的 W=by-design-no:QQ 不允许戳一戳当消息段发送,缺席正确。
对抗性 Agent 两轮复审通过。
This commit is contained in:
4 files changed
+241
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import {
|
||||
typesForDirection,
|
||||
INPUT_SUGAR_SEGMENTS,
|
||||
} from '@snowluma/protocol/element-manifest';
|
||||
|
||||
// 对账测试(onebot 侧)—— 拿 element-manifest 核对「按 element.type 收敛」的两个
|
||||
// onebot 方向:
|
||||
// S 收·转 to-segment.ts 的 `element.type === 'X'` 分支集合
|
||||
// P 发·解 message-parser.ts 的 `case 'X'` 分支集合(剔除纯输入糖)
|
||||
// protocol 侧的 D/W 由 protocol/tests/element-manifest.test.ts 核对(包边界所限)。
|
||||
//
|
||||
// 架构护栏:谁在这两处增删一种元素方向却没同步 element-manifest,测试当场报红。
|
||||
// 纯读源码、零运行时行为改动。
|
||||
|
||||
function readSrc(relFromTestDir: string): string {
|
||||
const abs = fileURLToPath(new URL(relFromTestDir, import.meta.url));
|
||||
const raw = readFileSync(abs, 'utf8');
|
||||
return raw.replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/.*$/gm, '');
|
||||
}
|
||||
|
||||
function extractTypes(src: string, pattern: RegExp): string[] {
|
||||
const found = new Set<string>();
|
||||
for (const m of src.matchAll(pattern)) found.add(m[1]!);
|
||||
return [...found].sort();
|
||||
}
|
||||
|
||||
const sorted = (s: Iterable<string>): string[] => [...s].sort();
|
||||
|
||||
describe('element-manifest 对账(onebot 侧:S 收·转 / P 发·解)', () => {
|
||||
it('S:to-segment 的 element.type 分支 == 清单声明的 S=yes', () => {
|
||||
const src = readSrc('../src/event-converter/to-segment.ts');
|
||||
const handled = extractTypes(src, /element\.type\s*===\s*'([A-Za-z0-9_]+)'/g);
|
||||
expect(handled).toEqual(sorted(typesForDirection('S')));
|
||||
});
|
||||
|
||||
it('P:message-parser 的 case 分支(剔除输入糖)== 清单声明的 P=yes', () => {
|
||||
const src = readSrc('../src/message-parser.ts');
|
||||
// 值域放宽到 `[A-Za-z0-9_]+`:见 protocol/tests/element-manifest.test.ts 同处说明。
|
||||
const allCases = extractTypes(src, /case\s*'([A-Za-z0-9_]+)'\s*:/g);
|
||||
const realElementCases = allCases.filter((t) => !INPUT_SUGAR_SEGMENTS.has(t));
|
||||
expect(realElementCases).toEqual(sorted(typesForDirection('P')));
|
||||
|
||||
// 输入糖必须确实出现在 message-parser 里(否则白名单与代码脱节)。
|
||||
const sugarPresent = allCases.filter((t) => INPUT_SUGAR_SEGMENTS.has(t)).sort();
|
||||
expect(sugarPresent).toEqual(sorted(INPUT_SUGAR_SEGMENTS));
|
||||
});
|
||||
});
|
||||
@@ -32,6 +32,10 @@
|
||||
"types": "./src/element-builder.ts",
|
||||
"default": "./src/element-builder.ts"
|
||||
},
|
||||
"./element-manifest": {
|
||||
"types": "./src/element-manifest.ts",
|
||||
"default": "./src/element-manifest.ts"
|
||||
},
|
||||
"./bridge-oidb": {
|
||||
"types": "./src/bridge-oidb.ts",
|
||||
"default": "./src/bridge-oidb.ts"
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
/**
|
||||
* 消息元素「四向」对账清单 —— 单一权威。
|
||||
*
|
||||
* 一种消息元素(文字/图片/戳一戳/商城表情…)要在两个流向、共 4 处处理,散在
|
||||
* 不同文件、不同包。历史上这 4 处各写各的、没人对账,导致字段名对不上、漏写
|
||||
* 方向无人知(都走运行时兜底 `default`,悄悄失败)。
|
||||
*
|
||||
* 本清单声明「每种元素类型应支持哪几个方向」,配套的两道对账测试
|
||||
* (`protocol/tests/element-manifest.test.ts` 与 `onebot/tests/element-manifest.test.ts`,
|
||||
* 因包边界拆两处、共用本清单)拿它逐一核对那 4 处代码 —— 漏写或多写一个方向
|
||||
* 立即报红,而不再是运行时静默。
|
||||
*
|
||||
* 四个方向:
|
||||
* D 收·解 QQ 原始数据 → MessageElement `protocol/msg-push/rich-body-decoder.ts`
|
||||
* S 收·转 MessageElement → OneBot 段 `onebot/event-converter/to-segment.ts`
|
||||
* P 发·解 OneBot 段 → MessageElement `onebot/message-parser.ts`
|
||||
* W 发·打包 MessageElement → QQ 原始数据 `protocol/element-builder.ts`
|
||||
*
|
||||
* 注:D(收·解)按 proto 字段分派、一字段可扇出多种元素、多字段可收敛为一种,
|
||||
* 与另外三个「按 element.type 分派」的方向异构,故它保持独立开关、不进 onebot
|
||||
* 侧的合并表;但它产出的元素类型集合仍受本清单核对。
|
||||
*
|
||||
* 取值语义:
|
||||
* 'yes' 该方向有对应处理。
|
||||
* 'no' 该方向暂无(记录在案的缺口,本次重构不补,另立任务再议)。
|
||||
* 'by-design-no' 该方向按设计不支持(QQ 协议限制),缺席是正确的。
|
||||
*/
|
||||
|
||||
export const ELEMENT_DIRECTIONS = ['D', 'S', 'P', 'W'] as const;
|
||||
export type ElementDirection = (typeof ELEMENT_DIRECTIONS)[number];
|
||||
|
||||
export type DirectionSupport = 'yes' | 'no' | 'by-design-no';
|
||||
|
||||
export interface ElementSpec {
|
||||
/** 每个方向的支持状态。 */
|
||||
readonly directions: Readonly<Record<ElementDirection, DirectionSupport>>;
|
||||
/**
|
||||
* 该元素类型在 `MessageElement` 上「属于自己」的字段 —— 字段契约的文档基准,
|
||||
* 供各方向读写时对齐(例如 poke 统一用 `subType`,不再收侧 `subType` / 发侧
|
||||
* `faceId` 打架)。当前为文档性声明,字段级强制留待后续阶段。
|
||||
*/
|
||||
readonly fields: readonly string[];
|
||||
/** 备注:媒体上传、三角映射、协议限制等需要跨方向知道的事。 */
|
||||
readonly note?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 全部规范元素类型 × 四向支持。此表须与那 4 处派发代码逐字一致,否则对账测试报红。
|
||||
*/
|
||||
export const ELEMENT_MANIFEST = {
|
||||
text: {
|
||||
directions: { D: 'yes', S: 'yes', P: 'yes', W: 'yes' },
|
||||
fields: ['text'],
|
||||
},
|
||||
at: {
|
||||
directions: { D: 'yes', S: 'yes', P: 'yes', W: 'yes' },
|
||||
fields: ['targetUin', 'uid', 'text'],
|
||||
},
|
||||
face: {
|
||||
directions: { D: 'yes', S: 'yes', P: 'yes', W: 'yes' },
|
||||
fields: ['faceId'],
|
||||
},
|
||||
reply: {
|
||||
directions: { D: 'yes', S: 'yes', P: 'yes', W: 'yes' },
|
||||
fields: ['replySeq', 'replyMessageId', 'replySenderUin', 'replyTime', 'replyRandom', 'replyElements'],
|
||||
},
|
||||
json: {
|
||||
directions: { D: 'yes', S: 'yes', P: 'yes', W: 'yes' },
|
||||
fields: ['text'],
|
||||
},
|
||||
xml: {
|
||||
directions: { D: 'yes', S: 'yes', P: 'yes', W: 'yes' },
|
||||
fields: ['text', 'subType'],
|
||||
},
|
||||
forward: {
|
||||
directions: { D: 'yes', S: 'yes', P: 'yes', W: 'yes' },
|
||||
fields: ['resId', 'forwardSource', 'forwardSummary', 'forwardPrompt', 'forwardNews', 'forwardTSum', 'forwardUuid'],
|
||||
},
|
||||
image: {
|
||||
directions: { D: 'yes', S: 'yes', P: 'yes', W: 'yes' },
|
||||
fields: ['imageUrl', 'fileId', 'fileName', 'fileSize', 'url', 'subType', 'summary', 'width', 'height', 'flash', 'md5Hex', 'sha1Hex', 'picFormat', 'noByteFallback', 'mediaNode'],
|
||||
note: 'media:W(打包)经 highway 上传、异步,需 SendContext',
|
||||
},
|
||||
record: {
|
||||
directions: { D: 'yes', S: 'yes', P: 'yes', W: 'yes' },
|
||||
fields: ['fileName', 'fileId', 'url', 'duration', 'fileSize', 'md5Hex', 'sha1Hex', 'voiceFormat', 'noByteFallback', 'mediaNode'],
|
||||
note: 'media',
|
||||
},
|
||||
video: {
|
||||
directions: { D: 'yes', S: 'yes', P: 'yes', W: 'yes' },
|
||||
fields: ['fileName', 'fileId', 'url', 'thumbUrl', 'thumbFileId', 'duration', 'width', 'height', 'fileSize', 'md5Hex', 'sha1Hex', 'videoFormat', 'noByteFallback', 'mediaNode'],
|
||||
note: 'media',
|
||||
},
|
||||
mface: {
|
||||
directions: { D: 'yes', S: 'yes', P: 'yes', W: 'yes' },
|
||||
fields: ['emojiId', 'emojiPackageId', 'emojiKey', 'text', 'summary'],
|
||||
note: '商城表情三角:S(转 OneBot)输出 image 段并挂 emoji_id/emoji_package_id/key 标记,P(解)再从该 image 段认回',
|
||||
},
|
||||
file: {
|
||||
directions: { D: 'yes', S: 'yes', P: 'yes', W: 'yes' },
|
||||
fields: ['fileId', 'thumbFileId', 'fileName', 'fileSize', 'url', 'fileHash', 'md5Hex', 'sha1Hex', 'resId'],
|
||||
note: 'W 特殊:live-send 在 OneBot 层被拆走走独立上传管线,仅 forwardFake 时落 transElem(24)',
|
||||
},
|
||||
poke: {
|
||||
directions: { D: 'yes', S: 'yes', P: 'yes', W: 'by-design-no' },
|
||||
fields: ['subType'],
|
||||
note: 'QQ 不允许戳一戳当消息段发送,故 W(打包)按设计不支持;戳一戳作为独立动作走 OIDB send-poke,与本表无关。字段契约以收侧既有 subType 为准',
|
||||
},
|
||||
markdown: {
|
||||
directions: { D: 'no', S: 'no', P: 'yes', W: 'yes' },
|
||||
fields: ['text'],
|
||||
note: '收侧(D/S)无对应,仅发送方向存在',
|
||||
},
|
||||
} as const satisfies Record<string, ElementSpec>;
|
||||
|
||||
export type ElementType = keyof typeof ELEMENT_MANIFEST;
|
||||
|
||||
/**
|
||||
* 某方向应被处理的元素类型集合(`directions[dir] === 'yes'`)—— 对账测试的基准。
|
||||
*/
|
||||
export function typesForDirection(dir: ElementDirection): Set<string> {
|
||||
const out = new Set<string>();
|
||||
for (const [type, spec] of Object.entries(ELEMENT_MANIFEST)) {
|
||||
if (spec.directions[dir] === 'yes') out.add(type);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发·解(P)侧还接收一批「纯 OneBot 输入糖」:它们不是某种真实消息元素,只是被
|
||||
* 塌缩成 json / face / poke 的输入便利(如骰子、猜拳、位置/音乐/名片分享、转发
|
||||
* node、匿名标记),没有收侧对应,也没有专属的 wire 形态,故不进本清单。对账 P
|
||||
* 时须先从代码里剔除这些类型。
|
||||
*/
|
||||
export const INPUT_SUGAR_SEGMENTS: ReadonlySet<string> = new Set([
|
||||
'node', 'share', 'music', 'location', 'contact', 'rps', 'dice', 'shake', 'anonymous',
|
||||
]);
|
||||
@@ -0,0 +1,50 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { typesForDirection } from '../src/element-manifest';
|
||||
|
||||
// 对账测试(protocol 侧)—— 拿 element-manifest 核对「按 element.type 收敛」的
|
||||
// 两个 protocol 方向:
|
||||
// D 收·解 rich-body-decoder.ts 产出的 MessageElement 类型集合
|
||||
// W 发·打包 element-builder.ts 的 switch 分支集合
|
||||
// onebot 侧的 S/P 由 onebot/tests/element-manifest.test.ts 核对(包边界所限)。
|
||||
//
|
||||
// 这是一道架构护栏(fitness function):谁在这两处新增/删除一种元素方向,却没有
|
||||
//同步更新 element-manifest,测试立即报红——把「漏写一个方向」从运行时兜底静默
|
||||
// 变成 CI 当场可见。纯读源码、零运行时行为改动。
|
||||
|
||||
function readSrc(relFromTestDir: string): string {
|
||||
const abs = fileURLToPath(new URL(relFromTestDir, import.meta.url));
|
||||
const raw = readFileSync(abs, 'utf8');
|
||||
// 剥掉块注释与行注释,避免注释里出现的类型字面量污染扫描结果。
|
||||
return raw.replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/.*$/gm, '');
|
||||
}
|
||||
|
||||
/** 抽出所有匹配 `pattern` 第 1 捕获组的去重、排序类型名。 */
|
||||
function extractTypes(src: string, pattern: RegExp): string[] {
|
||||
const found = new Set<string>();
|
||||
for (const m of src.matchAll(pattern)) found.add(m[1]!);
|
||||
return [...found].sort();
|
||||
}
|
||||
|
||||
const sorted = (s: Iterable<string>): string[] => [...s].sort();
|
||||
|
||||
describe('element-manifest 对账(protocol 侧:D 收·解 / W 发·打包)', () => {
|
||||
it('D:rich-body-decoder 产出的元素类型 == 清单声明的 D=yes', () => {
|
||||
const src = readSrc('../src/msg-push/rich-body-decoder.ts');
|
||||
// 解码产出形如 `result.push({ type: 'image', ... })` / `{ type: 'reply', ... }`。
|
||||
// 值域放宽到 `[A-Za-z0-9_]+`(而非仅小写):类型名当前虽全小写,但收紧到
|
||||
// `[a-z]+` 会让未来的驼峰/下划线类型名被静默漏抓,护栏就形同虚设——正是本
|
||||
// 重构要消灭的"漏一个方向、走 default 静默"失效模式,不能在护栏自身重演。
|
||||
const handled = extractTypes(src, /(?<![A-Za-z])type:\s*'([A-Za-z0-9_]+)'/g);
|
||||
expect(handled).toEqual(sorted(typesForDirection('D')));
|
||||
});
|
||||
|
||||
it('W:element-builder 的 switch 分支 == 清单声明的 W=yes', () => {
|
||||
const src = readSrc('../src/element-builder.ts');
|
||||
const handled = extractTypes(src, /case\s*'([A-Za-z0-9_]+)'\s*:/g);
|
||||
// poke 按设计不支持发送(QQ 限制),element-builder 必须没有 poke 分支。
|
||||
expect(handled).not.toContain('poke');
|
||||
expect(handled).toEqual(sorted(typesForDirection('W')));
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user