diff --git a/src/puppet-web/firer.spec.ts b/src/puppet-web/firer.spec.ts index 2a5b781044926a020b85d5fe56d1d42ab3001211..f70b8f552a297ea8565c66d1be47bafbd12459c8 100644 --- a/src/puppet-web/firer.spec.ts +++ b/src/puppet-web/firer.spec.ts @@ -29,21 +29,20 @@ test('Firer.checkFriendConfirm', t => { '你已添加了李卓桓,现在可以开始聊天了。' , '李卓桓' ] - ] let result: boolean - contentList.forEach((content, friend) => { + contentList.forEach(([content]) => { result = Firer.checkFriendConfirm(content) - t.truthy(result, 'should be truthy for confirm msg: ' + content) + t.true(result, 'should be truthy for confirm msg: ' + content) }) result = Firer.checkFriendConfirm('fsdfsdfasdfasdfadsa') - t.falsy(result, 'should be falsy for other msg') + t.false(result, 'should be falsy for other msg') }) test('Firer.checkRoomJoin', t => { - const contentList = [ + const contentList: [string, string, string[]][] = [ [ `You've invited "李卓桓" to the group chat` , `You've` @@ -73,7 +72,7 @@ test('Firer.checkRoomJoin', t => { let result contentList.forEach(([content, inviter, inviteeList]) => { - result = Firer.checkRoomJoin(content as string) + result = Firer.checkRoomJoin(content) t.truthy(result, 'should check room join message right for ' + content) t.deepEqual(result[0], inviteeList, 'should get inviteeList right') t.is(result[1], inviter, 'should get inviter right') @@ -81,7 +80,7 @@ test('Firer.checkRoomJoin', t => { t.throws(() => { Firer.checkRoomJoin('fsadfsadfsdfsdfs') - }, 'should throws if message is not expected') + }, Error, 'should throws if message is not expected') }) test('Firer.checkRoomLeave', t => { @@ -105,7 +104,7 @@ test('Firer.checkRoomLeave', t => { t.throws(() => { Firer.checkRoomLeave('fafdsfsdfafa') - }, 'should throw if message is not expected') + }, Error, 'should throw if message is not expected') }) test('Firer.checkRoomTopic', t => { @@ -126,12 +125,12 @@ test('Firer.checkRoomTopic', t => { contentList.forEach(([content, changer, topic]) => { result = Firer.checkRoomTopic(content) t.truthy(result, 'should check topic right for content: ' + content) - t.is(topic , result[2], 'should get right topic') + t.is(topic , result[0], 'should get right topic') t.is(changer, result[1], 'should get right changer') }) t.throws(() => { Firer.checkRoomTopic('fafdsfsdfafa') - }, 'should throw if message is not expected') + }, Error, 'should throw if message is not expected') }) diff --git a/src/puppet-web/firer.ts b/src/puppet-web/firer.ts index 9dbbe4f841a3236c95d84911c3352b63658c08c0..4a1da86fe392862c97cfb7af6361902f842c969f 100644 --- a/src/puppet-web/firer.ts +++ b/src/puppet-web/firer.ts @@ -93,9 +93,10 @@ async function fireFriendRequest(m: Message) { /** * try to find FriendRequest Confirmation Message */ -function checkFriendConfirm(content) { +function checkFriendConfirm(content: string): boolean { const reList = regexConfig.friendConfirm let found = false + reList.some(re => !!(found = re.test(content))) if (found) { return true @@ -140,7 +141,7 @@ function checkRoomJoin(content: string): [string[], string] { let found: string[]|null = [] reList.some(re => !!(found = content.match(re))) if (!found || !found.length) { - throw new Error('checkRoomJoin() not found') + throw new Error('checkRoomJoin() not found matched re of ' + content) } const [inviter, inviteeStr] = [ found[1], found[2] ] @@ -280,7 +281,7 @@ function checkRoomLeave(content: string): string|null { let found: string[]|null = [] reList.some(re => !!(found = content.match(re))) if (!found || !found.length) { - return null + throw new Error('checkRoomLeave() no matched re for ' + content) } return found[1] // leaver } @@ -288,11 +289,13 @@ function checkRoomLeave(content: string): string|null { /** * You removed "Bruce LEE" from the group chat */ -async function fireRoomLeave(m: Message) { +async function fireRoomLeave(m: Message): Promise { log.verbose('PuppetWebFirer', 'fireRoomLeave(%s)', m.content()) - const leaver = checkRoomLeave(m.content()) - if (!leaver) { + let leaver + try { + leaver = checkRoomLeave(m.content()) + } catch (e) { return } log.silly('PuppetWebFirer', 'fireRoomLeave() got leaver: %s', leaver)