Revert "fix: 对齐 OneBot schema 与合并转发消息处理行为 (#1694)" (#1701)
Release NapCat / validate-version (push) Canceled after 0s
Release NapCat / Build-Framework (push) Canceled after 0s
Release NapCat / Build-Shell (push) Canceled after 0s
Release NapCat / Download-QNX64 (push) Canceled after 0s
Release NapCat / release-napcat (push) Canceled after 0s

This reverts commit deae7fd467.
This commit is contained in:
时瑾 authored and GitHub committed 2026-03-13 15:26:53 +08:00
1 parent 9d5f0ee604
commit e6f6468377
54 files changed
+195 -1078

No files matched your search

+2 -217
View File
@@ -1,91 +1,12 @@
import { describe, expect, test, vi } from 'vitest';
import { describe, expect, test } from 'vitest';
import { TypeCompiler } from '@sinclair/typebox/compiler';
import { Value } from '@sinclair/typebox/value';
import { Type } from '@sinclair/typebox';
import { OB11MessageDataSchema, OB11MessageNodeSchema, OB11MessageSchema, OB11PostSendMsgSchema } from '../napcat-onebot/types/message';
import { OB11MessageDataSchema, OB11MessageSchema, OB11PostSendMsgSchema } from '../napcat-onebot/types/message';
import { OB11MessageSchema as OB11ActionMessageSchema } from '../napcat-onebot/action/schemas';
import { GoCQHTTPActionsExamples } from '../napcat-onebot/action/example/GoCQHTTPActionsExamples';
import { GuildActionsExamples } from '../napcat-onebot/action/example/GuildActionsExamples';
import { OneBotConfigSchema, loadConfig as loadOneBotConfig, OneBotConfig } from '../napcat-onebot/config';
import { NapcatConfigSchema } from '../napcat-core/helper/config';
import { GoCQHTTPSendForwardMsg, GoCQHTTPSendGroupForwardMsg, GoCQHTTPSendPrivateForwardMsg } from '../napcat-onebot/action/go-cqhttp/SendForwardMsg';
import { GetOnlineClient } from '../napcat-onebot/action/go-cqhttp/GetOnlineClient';
import { GoCQHTTPCheckUrlSafely } from '../napcat-onebot/action/go-cqhttp/GoCQHTTPCheckUrlSafely';
import { GoCQHTTPGetModelShow } from '../napcat-onebot/action/go-cqhttp/GoCQHTTPGetModelShow';
import { GoCQHTTPSetModelShow } from '../napcat-onebot/action/go-cqhttp/GoCQHTTPSetModelShow';
import { SetGroupAlbumMediaLike } from '../napcat-onebot/action/extends/SetGroupAlbumMediaLike';
import { ReceiveOnlineFile } from '../napcat-onebot/action/file/online/ReceiveOnlineFile';
import { GetGuildList } from '../napcat-onebot/action/guild/GetGuildList';
import { GetGuildProfile } from '../napcat-onebot/action/guild/GetGuildProfile';
import { MarkAllMsgAsRead } from '../napcat-onebot/action/msg/MarkMsgAsRead';
import { SetDoubtFriendsAddRequest } from '../napcat-onebot/action/new/SetDoubtFriendsAddRequest';
import { TestDownloadStream } from '../napcat-onebot/action/stream/TestStreamDownload';
import GetVersionInfo from '../napcat-onebot/action/system/GetVersionInfo';
vi.mock('napcat-core', () => ({
ChatType: {
KCHATTYPEGROUP: 2,
KCHATTYPEC2C: 1,
KCHATTYPETEMPC2CFROMGROUP: 3,
},
ElementType: {
TEXT: 'text',
PIC: 'pic',
REPLY: 'reply',
FILE: 'file',
VIDEO: 'video',
ARK: 'ark',
PTT: 'ptt',
},
NapCatCore: class {},
}));
vi.mock('napcat-core/types', () => ({
ChatType: {
KCHATTYPEGROUP: 2,
KCHATTYPEC2C: 1,
KCHATTYPETEMPC2CFROMGROUP: 3,
},
}));
function hasTopLevelProperty (schema: unknown, key: string): boolean {
if (!schema || typeof schema !== 'object') {
return false;
}
const typedSchema = schema as {
properties?: Record<string, unknown>,
allOf?: unknown[],
anyOf?: unknown[],
oneOf?: unknown[],
};
if (typedSchema.properties && key in typedSchema.properties) {
return true;
}
return ['allOf', 'anyOf', 'oneOf'].some((field) => {
const children = typedSchema[field as keyof typeof typedSchema];
return Array.isArray(children) && children.some(child => hasTopLevelProperty(child, key));
});
}
function createCoreStub () {
return {
context: {
logger: {
logError: vi.fn(),
logDebug: vi.fn(),
logWarn: vi.fn(),
},
},
} as any;
}
function createAction<T> (ActionClass: new (...args: any[]) => T): T {
return new ActionClass({} as any, createCoreStub());
}
describe('NapCat Schemas Compilation', () => {
test('should compile OB11MessageDataSchema without duplicate id error', () => {
@@ -142,142 +63,6 @@ describe('NapCat Schemas Validation & Coercion', () => {
const compiler = TypeCompiler.Compile(OB11PostSendMsgSchema);
expect(compiler.Check(data)).toBe(true);
});
test('should accept id-only forward nodes', () => {
const payload = {
type: 'node',
data: {
id: '123456',
},
};
let data: unknown = structuredClone(payload);
data = Value.Parse(OB11MessageNodeSchema, data);
const compiler = TypeCompiler.Compile(OB11MessageNodeSchema);
expect(compiler.Check(data)).toBe(true);
});
test('should reject forward reference nodes with empty id', () => {
const payload = {
type: 'node',
data: {
id: '',
},
};
const compiler = TypeCompiler.Compile(OB11MessageNodeSchema);
expect(compiler.Check(payload)).toBe(false);
});
test('should accept inline forward nodes with numeric time', () => {
const payload = {
type: 'node',
data: {
nickname: 'QQ用户',
content: [{ type: 'text', data: { text: 'hello' } }],
time: 1741600000,
},
};
let data: unknown = structuredClone(payload);
data = Value.Parse(OB11MessageNodeSchema, data);
const compiler = TypeCompiler.Compile(OB11MessageNodeSchema);
expect(compiler.Check(data)).toBe(true);
});
test('should reject forward nodes that mix reference and inline payload fields', () => {
const payload = {
type: 'node',
data: {
id: '123456',
content: [{ type: 'text', data: { text: 'hello' } }],
},
};
const compiler = TypeCompiler.Compile(OB11MessageNodeSchema);
expect(compiler.Check(payload)).toBe(false);
});
});
describe('NapCat Action Metadata', () => {
test('should export messages alias in forward message action schemas', () => {
const actions = [
createAction(GoCQHTTPSendForwardMsg),
createAction(GoCQHTTPSendPrivateForwardMsg),
createAction(GoCQHTTPSendGroupForwardMsg),
];
for (const action of actions) {
expect(hasTopLevelProperty((action as any).payloadSchema, 'messages')).toBe(true);
expect(hasTopLevelProperty((action as any).payloadSchema, 'message')).toBe(false);
}
});
test('should remove dead payload fields from action schemas', () => {
const doubtFriendAction = createAction(SetDoubtFriendsAddRequest);
const albumLikeAction = createAction(SetGroupAlbumMediaLike);
expect(hasTopLevelProperty((doubtFriendAction as any).payloadSchema, 'approve')).toBe(false);
expect(hasTopLevelProperty((albumLikeAction as any).payloadSchema, 'set')).toBe(false);
});
test('should keep examples aligned with fixed action contracts', () => {
const receiveOnlineFileAction = createAction(ReceiveOnlineFile);
const testStreamDownloadAction = createAction(TestDownloadStream);
const getModelShowAction = createAction(GoCQHTTPGetModelShow);
expect((receiveOnlineFileAction as any).payloadExample).toEqual({
user_id: '123456789',
msg_id: '123',
element_id: '456',
});
expect((testStreamDownloadAction as any).payloadExample).toEqual({ error: false });
expect((getModelShowAction as any).returnExample).toEqual([{
variants: {
model_show: 'napcat',
need_pay: false,
},
}]);
});
test('should keep compatibility placeholder metadata aligned with runtime responses', () => {
const compatibilityActions = [
createAction(GoCQHTTPCheckUrlSafely),
createAction(GetOnlineClient),
createAction(GoCQHTTPSetModelShow),
createAction(GetGuildList),
createAction(GetGuildProfile),
];
for (const action of compatibilityActions) {
expect((action as any).supported).toBe(true);
expect((action as any).unsupportedReason).toBeUndefined();
}
expect(GoCQHTTPActionsExamples.GetOnlineClient.payload).toEqual({});
expect(GoCQHTTPActionsExamples.GetOnlineClient.response).toEqual([]);
expect(GoCQHTTPActionsExamples.GoCQHTTPCheckUrlSafely.response).toEqual({ level: 1 });
expect(GoCQHTTPActionsExamples.GoCQHTTPSetModelShow.payload).toEqual({});
expect(GoCQHTTPActionsExamples.GoCQHTTPSetModelShow.response).toEqual(null);
expect(GuildActionsExamples.GetGuildList).toEqual({ payload: {}, response: null });
expect(GuildActionsExamples.GetGuildProfile).toEqual({ payload: {}, response: null });
expect((createAction(GetOnlineClient) as any).returnSchema).toMatchObject({ type: 'array' });
expect((createAction(GoCQHTTPCheckUrlSafely) as any).returnExample).toEqual({ level: 1 });
expect((createAction(GoCQHTTPSetModelShow) as any).returnSchema).toMatchObject({ type: 'null' });
expect((createAction(GetGuildList) as any).returnSchema).toMatchObject({ type: 'null' });
expect((createAction(GetGuildProfile) as any).returnSchema).toMatchObject({ type: 'null' });
});
test('should add explicit empty payload schemas to no-arg actions', () => {
const getVersionInfoAction = createAction(GetVersionInfo);
const markAllMsgAsReadAction = createAction(MarkAllMsgAsRead);
expect((getVersionInfoAction as any).payloadSchema).toMatchObject({ type: 'object' });
expect((markAllMsgAsReadAction as any).payloadSchema).toMatchObject({ type: 'object' });
});
});
describe('NapCat Configuration Loaders', () => {