Skip to content

Commit

Permalink
Merge pull request #845 from tsg-ut/slack-mock-ts
Browse files Browse the repository at this point in the history
refactor: lib/slackMockをTypeScript化
  • Loading branch information
hakatashi authored Oct 18, 2023
2 parents 8fe341d + 0a24db4 commit d43e27e
Show file tree
Hide file tree
Showing 43 changed files with 174 additions and 142 deletions.
5 changes: 2 additions & 3 deletions achievement-quiz/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import achievementQuiz from './index';
import Slack from '../lib/slackMock';

const Slack = require('../lib/slackMock.js');

let slack: typeof Slack;
let slack: Slack;

beforeEach(() => {
slack = new Slack();
Expand Down
3 changes: 1 addition & 2 deletions achievements/index_production.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import noop from 'lodash/noop';
// @ts-expect-error
import MockFirebase from 'mock-cloud-firestore';
// @ts-expect-error
import Slack from '../lib/slackMock.js';
import Slack from '../lib/slackMock';

import achievements from './index_production';

Expand Down
2 changes: 1 addition & 1 deletion ahokusa/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
jest.mock('../achievements');

const ahokusa = require('./index.js');
const Slack = require('../lib/slackMock.js');
const {default: Slack} = require('../lib/slackMock.ts');

let slack = null;

Expand Down
6 changes: 3 additions & 3 deletions amongyou/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {constants, promises as fs} from 'fs';
import path from 'path';
import type {TeamEventClient} from '../lib/slackEventClient';
import type {EventEmitter} from 'events';
import {ChatPostMessageArguments, WebClient} from '@slack/web-api';
import {stripIndent} from 'common-tags';
import type {FastifyPluginCallback} from 'fastify';
Expand Down Expand Up @@ -131,7 +131,7 @@ const getBlocks = () => [
];

class Among {
eventClient: TeamEventClient;
eventClient: EventEmitter;

slack: WebClient;

Expand All @@ -149,7 +149,7 @@ class Among {
slack,
slackInteractions,
}: {
eventClient: TeamEventClient,
eventClient: EventEmitter,
slack: WebClient,
slackInteractions: any,
}) {
Expand Down
4 changes: 2 additions & 2 deletions atequiz/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WebAPICallOptions, WebClient } from '@slack/web-api';
import type { TeamEventClient } from '../lib/slackEventClient';
import type { EventEmitter } from 'events';
import { SlackInterface } from '../lib/slack';
import { ChatPostMessageArguments } from '@slack/web-api/dist/methods';
import assert from 'assert';
Expand Down Expand Up @@ -52,7 +52,7 @@ export const typicalMessageTextsGenerator = {
* To use other judge/watSecGen/ngReaction, please extend this class.
*/
export class AteQuiz {
eventClient: TeamEventClient;
eventClient: EventEmitter;
slack: WebClient;
problem: AteQuizProblem;
ngReaction: string | null = 'no_good';
Expand Down
2 changes: 1 addition & 1 deletion channel-notifier/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

jest.mock('axios');

const Slack = require('../lib/slackMock.js');
const {default: Slack} = require('../lib/slackMock.ts');
const channelNotifier = require('./index.js');

let slack = null;
Expand Down
2 changes: 1 addition & 1 deletion checkin/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
jest.mock('axios');

const axios = require('axios');
const Slack = require('../lib/slackMock.js');
const {default: Slack} = require('../lib/slackMock.ts');
const checkin = require('./index.js');

let slack = null;
Expand Down
2 changes: 1 addition & 1 deletion dajare/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jest.mock('./tokenize.js');
jest.mock('../achievements');

const dajare = require('./index.js');
const Slack = require('../lib/slackMock.js');
const {default: Slack} = require('../lib/slackMock.ts');
const tokenize = require('./tokenize.js');

tokenize.virtualTokens = {
Expand Down
5 changes: 5 additions & 0 deletions discord/hayaoshiUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import {inspect} from 'util';
import {extractValidAnswers} from './hayaoshiUtils';

jest.mock('../hayaoshi', () => ({
isCorrectAnswer: jest.fn(),
normalize: jest.fn(),
}));

const testCases: [string, string, string[]][] = [
['', 'リトグラフ[lithograph]【「石版画」「石版印刷」「リトグラフィー[lithographie]」も○】',
['リトグラフ', 'lithograph', '石版画', '石版印刷', 'リトグラフィー', 'lithographie'],
Expand Down
2 changes: 1 addition & 1 deletion emoji-notifier/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env node, jest */

const Slack = require('../lib/slackMock.js');
const {default: Slack} = require('../lib/slackMock.ts');
const emojiNotifier = require('./index.js');

let slack = null;
Expand Down
5 changes: 2 additions & 3 deletions hitandblow/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { stripIndent } from 'common-tags';
import hitandblow from './';
import Slack from '../lib/slackMock';

const Slack = require('../lib/slackMock.js');

let slack: typeof Slack;
let slack: Slack;

beforeEach(() => {
slack = new Slack();
Expand Down
6 changes: 3 additions & 3 deletions hitandblow/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SlackEventAdapter } from '@slack/events-api';
import { WebClient } from '@slack/web-api';
import { range, shuffle, round } from 'lodash';
import { stripIndent } from 'common-tags';
import { unlock } from '../achievements';
import assert from 'assert';
import type { EventEmitter } from 'events';

interface HitAndBlowHistory {
call: number[];
Expand All @@ -17,7 +17,7 @@ class HitAndBlowState {
thread: string | null = null;
startDate: number | null = null;
timer: NodeJS.Timeout | null = null;
inGame: boolean = false;
inGame = false;
clear() {
this.answer = [];
this.history = [];
Expand Down Expand Up @@ -90,7 +90,7 @@ export default ({
eventClient,
webClient: slack,
}: {
eventClient: SlackEventAdapter;
eventClient: EventEmitter;
webClient: WebClient;
}) => {
const state = new HitAndBlowState();
Expand Down
3 changes: 2 additions & 1 deletion lib/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import * as sqlite from 'sqlite';
import sqlite3 from 'sqlite3';
import path from 'path';
import {TeamEventClient} from './slackEventClient';
import type {EventEmitter} from 'events';
import {Deferred} from './utils';
import {Token} from '../oauth/tokens';

export interface SlackInterface {
webClient: WebClient;
eventClient: TeamEventClient;
eventClient: EventEmitter & Pick<TeamEventClient, keyof TeamEventClient>;
messageClient: ReturnType<typeof createMessageAdapter>;
};

Expand Down
1 change: 0 additions & 1 deletion lib/slackCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type {
EmojiListArguments,
EmojiListResponse,
} from '@slack/web-api';
// @ts-expect-error
import Slack from './slackMock';
import SlackCache from './slackCache';

Expand Down
4 changes: 2 additions & 2 deletions lib/slackCache.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {get} from 'lodash';
import type {EventEmitter} from 'events';
import type {Token} from '../oauth/tokens';
import {Deferred} from './utils';
import {TeamEventClient} from './slackEventClient';
import logger from './logger';

import type {SlackEventAdapter} from '@slack/events-api';
import type {Reaction} from '@slack/web-api/dist/response/ConversationsHistoryResponse';
import type {Member} from '@slack/web-api/dist/response/UsersListResponse';
import type {
Expand Down Expand Up @@ -32,7 +32,7 @@ interface WebClient {

interface Config {
token: Token;
eventClient: SlackEventAdapter;
eventClient: EventEmitter;
webClient: WebClient;
enableReactions?: boolean;
}
Expand Down
18 changes: 9 additions & 9 deletions lib/slackEventClient.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import type {SlackEventAdapter} from '@slack/events-api';
import type {EventEmitter} from 'events';

export class TeamEventClient {
private readonly eventAdapter: SlackEventAdapter;
private readonly team: string;
readonly #eventAdapter: EventEmitter;
readonly #team: string;

// contract: 渡されるeventAdapterは、EventAdapterOptions.includeBodyがtrueでなければならない。
constructor(eventAdapter: SlackEventAdapter, team: string) {
this.eventAdapter = eventAdapter;
this.team = team;
constructor(eventAdapter: EventEmitter, team: string) {
this.#eventAdapter = eventAdapter;
this.#team = team;
}

// listen on events against all teams.
onAllTeam(event: string, listener: (...args: any[]) => void): any {
return this.eventAdapter.on(event, listener);
return this.#eventAdapter.on(event, listener);
}
// listen on events against the team.
on(event: string, listener: (...args: any[]) => void): any {
return this.eventAdapter.on(event, (...args: any[]) => {
return this.#eventAdapter.on(event, (...args: any[]) => {
// https://slack.dev/node-slack-sdk/events-api#receive-additional-event-data
// https://github.com/slackapi/node-slack-sdk/blob/3e9c483c593d6aa28f6f5680f287722df3327609/packages/events-api/src/http-handler.ts#L212-L223
// https://api.slack.com/apis/connections/events-api#the-events-api__receiving-events__events-dispatched-as-json
// args: [body.event, body: {team_id: string}]
if (args[1].team_id === this.team) {
if (args[1].team_id === this.#team) {
listener(...args);
}
});
Expand Down
Loading

0 comments on commit d43e27e

Please sign in to comment.