Skip to content
/ mocker Public

Wechaty mock library generates mock wechat account for testing purpose

License

Notifications You must be signed in to change notification settings

wechaty/mocker

Repository files navigation

wechaty-mocker

NPM Version NPM TypeScript Powered by Wechaty ES Modules

Wechaty Mocker

Image: The Matrix

Wechaty Mocker provides the mocking system for testing Wechaty Chatbots.

What is this

This is an essential component when you are trying to test your wechaty puppet implementation. This project is aim to provide a tool that helps you mock a wechat account, which could mock an account with mock contacts and mock rooms, and generate messages (in different types), events (like friendship events, room events etc) for you.

Why do we need this

Currently there are several wechaty puppets available, like wechaty-puppet-macpro or wechaty-puppet-padplus, but we don't know how many instance we could run on a 2 core cpu 4G memory machine, when it came to industry usage of these project, the performance is a key attribute. The reason that we don't know the answer to the question is that we can not evaluate this, there is no test server that we can use since we are trying to connect to Wechat. Thus we need a mock tool to mock these things so we can do some pressure testing on our puppet code, then we will have confidence to say how many wechat accounts could be run on a 2 core cpu 4G memory machine. From that point, we will be able to find a number to optimize.

Features

  • Generate wechat account, includes wxid, name, weixin, avatar, gender
  • Generate contacts and rooms: according to config (contact count, room count, room member distribution)
  • Emit messages: according to config (frequency, message types, room message ratio and contact message ratio)
  • Emit friendship events: according to config (frequency) emit friendship events
  • Emit room events; according to config (frequency of different room events)

Usage

import { createFixture } from 'wechaty-mocker'

for await (const { wechaty, mocker } of createFixture()) {
  // 1. Log Wechaty messages
  wechaty.wechaty.on('message', console.info)
  // 2. Make player say something to our bot
  mocker.player.say().to(mocker.bot)
  // 3. Wait and see. That's it!
  await new Promise(setImmediate)
}

Example

Here's a example for testing Vorpal Game: Math Master by Wechaty Mocker.

Math Master is a text game that the bot will ask player to calculate numbers, and the player will get 1 score for each right answer. The game will increase the difficulty by asking bigger numbers, and each round the player must text the right answer in no more than 10 seconds.

In this example, we will use our mocking system to pretend we are a player, to play the game to get a very high score (like 13)!

/**
 * Player Logic
 */
const onMessageMock = async (message: mock.MessageMock) => {
  if (message.type() !== Message.Type.Text)   { return }
  const text = message.text()
  if (!text)                                  { return }

  const MATH_RE = /Score: (\d+).+?(\d+) \+ (\d+) = \?/s
  const match = text?.match(MATH_RE)
  if (!match)                                 { return }

  const score = parseInt(match[1], 10)
  if (score > 13)                             { return }

  const x = parseInt(match[2], 10)
  const y = parseInt(match[3], 10)

  const result = String(x + y)

  await new Promise(resolve => setTimeout(resolve, 500))
  mocker.player.say(result).to(mocker.bot)
}

mocker.player.on('message', onMessageMock)

The full source code can be found at: examples/math-master.ts

Reference

To be written.

Mocker

ContactMock

RoomMock

MessageMock

History

master v1.0 (Oct 30, 2021)

Release v1.0 of Wechaty Mocker.

  1. v0.9 (Sep 15, 2021): ES Modules support

v0.4 (July 27, 2020)

  1. Add the Math Master Example.
  2. Support EnvironmentMock for createFixture(...)

v0.2 (July 25, 2020)

  1. Move createFixture from wechaty and mocker.* from wechaty-puppet-mock to prevent circler dependencies.

Author

Huan LI (李卓桓), Tencent TVP of Chatbot, <zixia@zixia.net>

Profile of Huan LI (李卓桓) on StackOverflow

Copyright & License

  • Code & Docs © 2020 Huan LI <zixia@zixia.net>
  • Docs released under Creative Commons
  • Code released under the Apache-2.0 License