Skip to content

Commit

Permalink
formatting, more test helpers, builtin middleware specs all cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Maj committed Sep 26, 2024
1 parent 354bb92 commit e5d6afb
Show file tree
Hide file tree
Showing 5 changed files with 388 additions and 643 deletions.
11 changes: 8 additions & 3 deletions src/middleware/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,16 @@ export const ignoreSelf: Middleware<AnyMiddlewareArgs> = async (args) => {
}
}

// Its an Events API event that isn't of type message, but the user ID might match our own app. Filter these out.
// It's an Events API event that isn't of type message, but the user ID might match our own app. Filter these out.
// However, some events still must be fired, because they can make sense.
const eventsWhichShouldBeKept = ['member_joined_channel', 'member_left_channel'];
const isEventShouldBeKept = eventsWhichShouldBeKept.includes(args.event.type);

if (botUserId !== undefined && 'user' in args.event && args.event.user === botUserId && !isEventShouldBeKept) {
if (
botUserId !== undefined &&
'user' in args.event &&
args.event.user === botUserId &&
!eventsWhichShouldBeKept.includes(args.event.type)
) {
return;
}
}
Expand All @@ -318,6 +322,7 @@ export const ignoreSelf: Middleware<AnyMiddlewareArgs> = async (args) => {
await args.next();
};

// TODO: breaking change: constrain the subtype argument to be a valid message subtype
/**
* Filters out any message events whose subtype does not match the provided subtype.
*/
Expand Down
22 changes: 11 additions & 11 deletions test/unit/helpers/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { WebClientOptions, AuthTestResponse } from '@slack/web-api';
import type { AuthTestResponse, WebClientOptions } from '@slack/web-api';
import rewiremock from 'rewiremock';
import sinon, { type SinonSpy } from 'sinon';

Expand All @@ -7,7 +7,7 @@ import sinon, { type SinonSpy } from 'sinon';
*/

// biome-ignore lint/suspicious/noExplicitAny: module overrides can be anything
export interface Override extends Record<string, Record<string, any>> { }
export interface Override extends Record<string, Record<string, any>> {}

export function mergeOverrides(...overrides: Override[]): Override {
let currentOverrides: Override = {};
Expand Down Expand Up @@ -48,17 +48,17 @@ export function withNoopWebClient(authTestResponse?: AuthTestResponse): Override
'@slack/web-api': {
WebClient: authTestResponse
? class {
public token?: string;
public token?: string;

public constructor(token?: string, _options?: WebClientOptions) {
this.token = token;
}
public constructor(token?: string, _options?: WebClientOptions) {
this.token = token;
}

public auth = {
test: sinon.fake.resolves(authTestResponse),
};
}
: class { },
public auth = {
test: sinon.fake.resolves(authTestResponse),
};
}
: class {},
},
};
}
Expand Down
100 changes: 96 additions & 4 deletions test/unit/helpers/events.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import type { AppMentionEvent, Block, KnownBlock, MessageEvent } from '@slack/types';
import type {
AppHomeOpenedEvent,
AppMentionEvent,
Block,
KnownBlock,
MessageEvent,
ReactionAddedEvent,
} from '@slack/types';
import { WebClient } from '@slack/web-api';
import sinon, { type SinonSpy } from 'sinon';
import { createFakeLogger } from '.';
import type {
AckFn,
AllMiddlewareArgs,
AnyMiddlewareArgs,
BaseSlackEvent,
BlockAction,
BlockElementAction,
Expand Down Expand Up @@ -38,22 +46,106 @@ const say: SayFn = (_msg) => Promise.resolve({ ok: true });
const respond: RespondFn = (_msg) => Promise.resolve();
const ack: AckFn<void> = (_r?) => Promise.resolve();

export function wrapMiddleware<Args extends SlackEventMiddlewareArgs>(
export function wrapMiddleware<Args extends AnyMiddlewareArgs>(
args: Args,
ctx?: Context,
): Args & AllMiddlewareArgs & { next: SinonSpy } {
return {
const wrapped = {
...args,
context: ctx || { isEnterpriseInstall: false },
logger: createFakeLogger(),
client: new WebClient(),
next: sinon.fake(),
};
return wrapped;
}

interface DummyAppHomeOpenedOverrides {
channel?: string;
user?: string;
}
export function createDummyAppHomeOpenedEventMiddlewareArgs(
eventOverrides?: DummyAppHomeOpenedOverrides,
// biome-ignore lint/suspicious/noExplicitAny: allow mocking tools to provide any override
bodyOverrides?: Record<string, any>,
): SlackEventMiddlewareArgs<'app_home_opened'> {
const event: AppHomeOpenedEvent = {
type: 'app_home_opened',
channel: eventOverrides?.channel || channel,
user: eventOverrides?.user || user,
tab: 'home',
event_ts: ts,
};
return {
payload: event,
event,
body: envelopeEvent(event, bodyOverrides),
say,
};
}

interface DummyMemberChannelOverrides<T> {
type: T;
channel?: string;
user?: string;
team?: string;
}
type MemberChannelEventTypes = 'member_joined_channel' | 'member_left_channel';
export function createDummyMemberChannelEventMiddlewareArgs(
eventOverrides: DummyMemberChannelOverrides<MemberChannelEventTypes>,
// biome-ignore lint/suspicious/noExplicitAny: allow mocking tools to provide any override
bodyOverrides?: Record<string, any>,
): SlackEventMiddlewareArgs<MemberChannelEventTypes> {
const event = {
type: eventOverrides.type,
user: eventOverrides?.user || user,
channel: eventOverrides?.channel || channel,
channel_type: 'channel',
team: eventOverrides?.team || team,
event_ts: ts,
};
return {
payload: event,
event,
body: envelopeEvent(event, bodyOverrides),
say,
};
}

interface DummyReactionAddedOverrides {
channel?: string;
user?: string;
reaction?: string;
}
export function createDummyReactionAddedEventMiddlewareArgs(
eventOverrides?: DummyReactionAddedOverrides,
// biome-ignore lint/suspicious/noExplicitAny: allow mocking tools to provide any override
bodyOverrides?: Record<string, any>,
): SlackEventMiddlewareArgs<'reaction_added'> {
const event: ReactionAddedEvent = {
type: 'reaction_added',
user: eventOverrides?.user || user,
reaction: eventOverrides?.reaction || 'lol',
item_user: 'wut',
item: {
type: 'message',
channel: eventOverrides?.channel || channel,
ts,
},
event_ts: ts,
};
return {
payload: event,
event,
body: envelopeEvent(event, bodyOverrides),
say,
};
}

interface DummyMessageOverrides {
message?: MessageEvent;
text?: string;
user?: string;
blocks?: (KnownBlock | Block)[];
}
export function createDummyMessageEventMiddlewareArgs(
Expand All @@ -67,7 +159,7 @@ export function createDummyMessageEventMiddlewareArgs(
event_ts: ts,
channel,
channel_type: 'channel',
user,
user: msgOverrides?.user || user,
ts,
text: msgOverrides?.text || 'hi',
blocks: msgOverrides?.blocks || [],
Expand Down
Loading

0 comments on commit e5d6afb

Please sign in to comment.