diff --git a/packages/engine-chronocat-api/tests/messager/fixtures/img/__snapshots__/index.test.ts.snap b/packages/engine-chronocat-api/tests/messager/fixtures/img/__snapshots__/index.test.ts.snap new file mode 100644 index 0000000..2728c4f --- /dev/null +++ b/packages/engine-chronocat-api/tests/messager/fixtures/img/__snapshots__/index.test.ts.snap @@ -0,0 +1,69 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Red 编码器应当正确编码 图片消息 1`] = ` +[ + [ + { + "chatType": 2, + "guildId": "", + "peerUid": "9998", + }, + [ + { + "elementId": "", + "elementType": 1, + "textElement": { + "atNtUid": "", + "atTinyId": "", + "atType": 0, + "atUid": "", + "content": "xxx", + }, + }, + { + "elementId": "", + "elementType": 2, + "extBufForUI": "", + "picElement": { + "fileName": "", + "fileSize": "0", + "fileSubId": "", + "fileUuid": "", + "md5HexStr": "", + "original": true, + "picHeight": 0, + "picSubType": 0, + "picType": 1001, + "picWidth": 0, + "sourcePath": "", + "summary": "", + "thumbFileSize": 0, + }, + }, + { + "elementId": "", + "elementType": 1, + "textElement": { + "atNtUid": "", + "atTinyId": "", + "atType": 0, + "atUid": "", + "content": "yyy", + }, + }, + ], + ], +] +`; + +exports[`Red 编码器应当正确编码 图片消息 2`] = ` +[ + [ + "file:///D:/Projects/chrononeko/chronocat02/packages/engine-chronocat-api/docs/static/chronocat.png", + { + "fileMime": undefined, + "fileName": undefined, + }, + ], +] +`; diff --git a/packages/engine-chronocat-api/tests/messager/fixtures/img/index.test.ts b/packages/engine-chronocat-api/tests/messager/fixtures/img/index.test.ts new file mode 100644 index 0000000..2410008 --- /dev/null +++ b/packages/engine-chronocat-api/tests/messager/fixtures/img/index.test.ts @@ -0,0 +1,38 @@ +import h from '@satorijs/element' +import { resolve } from 'node:path' +import { pathToFileURL } from 'node:url' +import { Messager } from '../../../../src/api/message/create/messager' +import { + commonSend, + commonSendForward, + ctx, + satoriConfig, + saveResult, +} from '../../../mocks' + +test('Red 编码器应当正确编码 图片消息', async () => { + const save = jest.fn().mockReturnValueOnce(saveResult) + + await new Messager( + ctx, + satoriConfig, + { + send: commonSend, + save, + sendForward: commonSendForward, + }, + '9998', + ).send( + h.parse( + `xxxyyy`, + ), + ) + + const sendCalls = commonSend.mock.calls.map((x) => x.slice(1)) + const saveCalls = save.mock.calls.map((x) => (x as unknown[]).slice(1)) + + expect(sendCalls).toMatchSnapshot() + expect(saveCalls).toMatchSnapshot() +}) diff --git a/packages/engine-chronocat-api/tests/messager/fixtures/text/__snapshots__/index.test.ts.snap b/packages/engine-chronocat-api/tests/messager/fixtures/text/__snapshots__/index.test.ts.snap new file mode 100644 index 0000000..8b5ef2f --- /dev/null +++ b/packages/engine-chronocat-api/tests/messager/fixtures/text/__snapshots__/index.test.ts.snap @@ -0,0 +1,26 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Red 编码器应当正确编码 纯文本消息 1`] = ` +[ + [ + { + "chatType": 2, + "guildId": "", + "peerUid": "9998", + }, + [ + { + "elementId": "", + "elementType": 1, + "textElement": { + "atNtUid": "", + "atTinyId": "", + "atType": 0, + "atUid": "", + "content": "xxx", + }, + }, + ], + ], +] +`; diff --git a/packages/engine-chronocat-api/tests/messager/fixtures/text/index.test.ts b/packages/engine-chronocat-api/tests/messager/fixtures/text/index.test.ts new file mode 100644 index 0000000..705e6a9 --- /dev/null +++ b/packages/engine-chronocat-api/tests/messager/fixtures/text/index.test.ts @@ -0,0 +1,26 @@ +import h from '@satorijs/element' +import { Messager } from '../../../../src/api/message/create/messager' +import { + commonSave, + commonSend, + commonSendForward, + ctx, + satoriConfig, +} from '../../../mocks' + +test('Red 编码器应当正确编码 纯文本消息', async () => { + await new Messager( + ctx, + satoriConfig, + { + send: commonSend, + save: commonSave, + sendForward: commonSendForward, + }, + '9998', + ).send(h.parse('xxx')) + + const sendCalls = commonSend.mock.calls.map((x) => x.slice(1)) + + expect(sendCalls).toMatchSnapshot() +}) diff --git a/packages/engine-chronocat-api/tests/mocks/index.ts b/packages/engine-chronocat-api/tests/mocks/index.ts new file mode 100644 index 0000000..b73a153 --- /dev/null +++ b/packages/engine-chronocat-api/tests/mocks/index.ts @@ -0,0 +1,58 @@ +import type { RedMessage } from '@chronocat/red' +import type { ChronocatContext } from '@chronocat/shell' +import h from '@satorijs/element' + +export const ctx: ChronocatContext = { + chronocat: { + api: { + 'chronocat.internal.red.message.parse': (() => { + const fn = (async () => + undefined) as unknown as ChronocatContext['chronocat']['api']['chronocat.internal.red.message.parse'] + + fn.notimpl = false + fn.engine = 'engine-mock' + fn.priority = 0 + + return fn + })(), + } as ChronocatContext['chronocat']['api'], + l: { + info: jest.fn(), + warn: jest.fn(), + error: jest.fn(), + debug: jest.fn(), + } as unknown as ChronocatContext['chronocat']['l'], + h, + } as ChronocatContext['chronocat'], +} + +export const satoriConfig = { + type: 'satori', + listen: '0.0.0.0', + port: 5500, + self_url: 'https://chronocat.vercel.app', + token: 'DEFINE_CHRONO_TOKEN', + enable: true, +} as const + +export const saveResult = { + filePath: '', + fileSize: 0, + fileName: '', + fileMime: '', + md5: '', + imageInfo: { + width: 0, + height: 0, + type: 'png', + mime: 'image/png', + wUnits: '1', + hUnits: '1', + }, +} + +export const commonSend = jest.fn( + async () => undefined as unknown as RedMessage, +) +export const commonSave = jest.fn(async () => saveResult) +export const commonSendForward = jest.fn(async () => {}) diff --git a/packages/engine-chronocat-api/tests/tsconfig.json b/packages/engine-chronocat-api/tests/tsconfig.json new file mode 100644 index 0000000..dd8328f --- /dev/null +++ b/packages/engine-chronocat-api/tests/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../../tsconfig.base", + "compilerOptions": { + "rootDir": ".." + }, + "include": [ + ".", + "../src" + ] +}