mirror of
https://github.com/chrononeko/chronocat.git
synced 2024-11-22 07:07:53 +00:00
test(api): init tests
This commit is contained in:
parent
a43066f17b
commit
2fa2342465
@ -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,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
]
|
||||||
|
`;
|
@ -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(
|
||||||
|
`xxx<img src="${pathToFileURL(
|
||||||
|
resolve(__dirname, '../../../../docs/static/chronocat.png'),
|
||||||
|
).toString()}" />yyy`,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
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()
|
||||||
|
})
|
@ -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",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]
|
||||||
|
`;
|
@ -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()
|
||||||
|
})
|
58
packages/engine-chronocat-api/tests/mocks/index.ts
Normal file
58
packages/engine-chronocat-api/tests/mocks/index.ts
Normal file
@ -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 () => {})
|
10
packages/engine-chronocat-api/tests/tsconfig.json
Normal file
10
packages/engine-chronocat-api/tests/tsconfig.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"extends": "../../../tsconfig.base",
|
||||||
|
"compilerOptions": {
|
||||||
|
"rootDir": ".."
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
".",
|
||||||
|
"../src"
|
||||||
|
]
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user