wechaty / puppet-mock

Puppet Mocker for Wechaty (& A Puppet Template Starter)
https://paka.dev/npm/wechaty-puppet-mock
Apache License 2.0
45 stars 14 forks source link

More mock cases coverage #17

Closed suntong closed 4 years ago

suntong commented 5 years ago

Originally posted in dev chat group:

我现在想改动wechaty底层.ts代码,是想把mock 的信息多样化,比如增加url, smiley 等的情况,这样的mock 就会更有实用性。 不反对吧? 我先问一下,主要是因为真要这么做的话,会带来一个很大的问题...

Basically I'm planning to add more mock cases to mock output. Instead of outputting merely mock text, it'll loop through a predefined array, so the mock output message will be something like this:

mock output
mock text with smiley
mock text contains only url
mock text contains url + text
... and other cases can be easily added by just adding to the predefined array... 
then repeat all above over and over

Would that be a good idea?

However, there is a big problem, I'm thinking, which is that, the wechaty-puppet-puppeteer and the wechaty-puppet-padpro have different output formats, e.g., smiley raw string will be different between the two, IIRC. I.e., we might need wechaty-puppet-mock to be the base class, and define/use wechaty-puppet-mock-puppeteer and wechaty-puppet-mock-padpro instead.

Just a thought. Comments?

huan commented 5 years ago

If I understand you right, you want to control the messages that emit from the mocked puppet.

Currently, the wechaty-puppet-mock just send a text message periodly, that's because it's not fully implemented yet.

In order to let us control what message wechaty can receive(so that we could do unit tests), there was an idea in my head and I'd like to share it with you.

The sample code should look like this:

import { PuppetMock } from `wechaty-puppet-mock`
import { Wechaty } from 'wechaty'

const puppet = new PuppetMock()
const bot = new Wechaty({ puppet })

bot.on('message', msg => console.log(msg))
await bot.start()

const from = await bot.Contact.find({ name: 'huan' })
const to = await bot.userSelf()

puppet.mockMessage(from, to, ';)')
puppet.mockMessage(from, to, 'https://chatie.io/wechaty/images/bot-qr-code.png')
puppet.mockMessage(from, to, 'hello - https://chatie.io/wechaty/images/bot-qr-code.png')
puppet.mockMessage(from, to, FileBox.fromUrl('https://chatie.io/wechaty/images/bot-qr-code.png'))
suntong commented 5 years ago

Oh, then it is harder than I thought, which was to alter this loop,

https://github.com/Chatie/wechaty-puppet-mock/blob/027465c3d08cb61df4dda26ac0a66581c5f46a20/src/puppet-mock.ts#L106-L109

to send from/loop through a predefined message array, instead of sending a pre-cached one.

OK. I'll wait for your first move then.

BTW, as for the smiley, this is what I meant:

https://github.com/Chatie/wechaty/blob/cd7db731f1dc37c08df2eaab212cc8d8724bc538/src/misc.spec.ts#L56-L59

    '<img class="emoji emoji1f4a4" text="[流汗]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />',
    '<img class="qqemoji qqemoji13" text="[呲牙]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />',
    '<img class="emoji emoji1f44d" text="_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />',
    '<span class="emoji emoji1f334"></span>',
huan commented 5 years ago

It's not hard but we need to clarify your use case first.

Why do you care about this XML <img class="emoji emoji1f4a4" text="[流汗]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />? It's a little tricky because that the wechaty has no standard for this kind of message yet. It might be [流汗] or 😅 dependes on the puppet implementation.

Could you explain your use case in detail?

suntong commented 5 years ago

Those are what I actually received when using the wechaty-puppet-puppeteer, although what appeared on the phone are different. Here are some more real examples in my log (via console.log(msg.toString())):

能抗得住<img class="qqemoji qqemoji20" text="[偷笑]_web" src="/...
死机了<img class="qqemoji qqemoji10" text="[尴尬]_web" src="/zh_CN/...
万全之策?<img class="qqemoji qqemoji13" text="[呲牙]_web...
<img class="qqemoji qqemoji81" text="[握手]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" /><img class="qqemoji qqemoji81" text="[握手]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" /><img class="qqemoji qqemoji81" text="[握手]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />
<img class="qqemoji qqemoji81" text="[Shake]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />早<img class="qqemoji qqemoji0" text="[Smile]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />' 
踢! <img class="emoji emoji26bd" text="_web" src="/zh_CN/htmle...
<a target="_blank" href="/cgi-bin/mmwebwx-bin/webwxcheckurl?requrl=htt...

Thus, I'm trying to simulate those actual raw messages so that I can better filter/handle them.

async function onMessage (msg) {
  console.log(msg.toString())
}
huan commented 5 years ago

Understood.

So if you can create a new mock message as you want, and you can let wechaty emit it as a new message, does that fulfill your requirement?

suntong commented 5 years ago

Hmm... as long as there is a place in wechaty-puppet-mock/src/puppet-mock.ts that I/people can add new extra messages that wechaty-puppet-mock can emit, besides all existing ones, say for e.g., by just adding to the predefined array etc, then I'm happy.

I.e., it can be predefined and no need for any dynamically creating new mock messages.

huan commented 4 years ago

I believe the latest PR #36 is related to this discussion.

Closed due to inactivity for years.