Skip to content

Commit

Permalink
Add Deno tests
Browse files Browse the repository at this point in the history
  • Loading branch information
d-gubert committed Dec 18, 2023
1 parent 1654d9a commit 074c648
Showing 1 changed file with 155 additions and 0 deletions.
155 changes: 155 additions & 0 deletions deno-runtime/handlers/tests/slashcommand-handler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// deno-lint-ignore-file no-explicit-any
import { assertInstanceOf, assertEquals } from 'https://deno.land/[email protected]/assert/mod.ts';
import { beforeEach, describe, it } from 'https://deno.land/[email protected]/testing/bdd.ts';
import { spy } from "https://deno.land/[email protected]/testing/mock.ts";
import { Room as _Room } from '@rocket.chat/apps-engine/server/rooms/Room.ts';

import { AppObjectRegistry } from '../../AppObjectRegistry.ts';
import { AppAccessors } from '../../lib/accessors/mod.ts';
import { handleExecutor, handlePreviewItem } from '../slashcommand-handler.ts';
import { require } from '../../lib/require.ts';

const { Room } = require('@rocket.chat/apps-engine/server/rooms/Room.js') as { Room: typeof _Room } ;

describe('handlers > slashcommand', () => {
const mockAppAccessors = {
getReader: () => ({ __type: 'reader' }),
getHttp: () => ({ __type: 'http' }),
getModifier: () => ({ __type: 'modifier' }),
getPersistence: () => ({ __type: 'persistence' }),
getSenderFn: () => (id: string) => Promise.resolve([{ __type: 'bridgeCall' }, { id }]),
} as unknown as AppAccessors;

const mockCommandExecutorOnly = {
command: 'executor-only',
i18nParamsExample: 'test',
i18nDescription: 'test',
providesPreview: false,
// deno-lint-ignore no-unused-vars
async executor(context: any, read: any, modify: any, http: any, persis: any): Promise<void> {},
};

const mockCommandExecutorAndPreview = {
command: 'executor-and-preview',
i18nParamsExample: 'test',
i18nDescription: 'test',
providesPreview: true,
// deno-lint-ignore no-unused-vars
async executor(context: any, read: any, modify: any, http: any, persis: any): Promise<void> {},
// deno-lint-ignore no-unused-vars
async previewer(context: any, read: any, modify: any, http: any, persis: any): Promise<void> {},
// deno-lint-ignore no-unused-vars
async executePreviewItem(previewItem: any,context: any, read: any, modify: any, http: any, persis: any): Promise<void> {},
};

const mockCommandPreviewWithNoExecutor = {
command: 'preview-with-no-executor',
i18nParamsExample: 'test',
i18nDescription: 'test',
providesPreview: true,
// deno-lint-ignore no-unused-vars
async previewer(context: any, read: any, modify: any, http: any, persis: any): Promise<void> {},
// deno-lint-ignore no-unused-vars
async executePreviewItem(previewItem: any,context: any, read: any, modify: any, http: any, persis: any): Promise<void> {},
};

beforeEach(() => {
AppObjectRegistry.clear();
AppObjectRegistry.set('slashcommand:executor-only', mockCommandExecutorOnly);
AppObjectRegistry.set('slashcommand:executor-and-preview', mockCommandExecutorAndPreview);
AppObjectRegistry.set('slashcommand:preview-with-no-executor', mockCommandPreviewWithNoExecutor);
});

it('correctly handles execution of a slash command', async () => {
const mockContext = {
sender: { __type: 'sender' },
room: { __type: 'room' },
params: { __type: 'params' },
threadId: 'threadId',
triggerId: 'triggerId',
};

const _spy = spy(mockCommandExecutorOnly, 'executor');

await handleExecutor({ AppAccessorsInstance: mockAppAccessors }, mockCommandExecutorOnly, 'executor', [mockContext]);

const context = _spy.calls[0].args[0];

assertInstanceOf(context.getRoom(), Room);
assertEquals(context.getSender(), { __type: 'sender' });
assertEquals(context.getArguments(), { __type: 'params' });
assertEquals(context.getThreadId(), 'threadId');
assertEquals(context.getTriggerId(), 'triggerId');

assertEquals(_spy.calls[0].args[1], mockAppAccessors.getReader());
assertEquals(_spy.calls[0].args[2], mockAppAccessors.getModifier());
assertEquals(_spy.calls[0].args[3], mockAppAccessors.getHttp());
assertEquals(_spy.calls[0].args[4], mockAppAccessors.getPersistence());

_spy.restore();
});

it('correctly handles execution of a slash command previewer', async () => {
const mockContext = {
sender: { __type: 'sender' },
room: { __type: 'room' },
params: { __type: 'params' },
threadId: 'threadId',
triggerId: 'triggerId',
};

const _spy = spy(mockCommandExecutorAndPreview, 'previewer');

await handleExecutor({ AppAccessorsInstance: mockAppAccessors }, mockCommandExecutorAndPreview, 'previewer', [mockContext]);

const context = _spy.calls[0].args[0];

assertInstanceOf(context.getRoom(), Room);
assertEquals(context.getSender(), { __type: 'sender' });
assertEquals(context.getArguments(), { __type: 'params' });
assertEquals(context.getThreadId(), 'threadId');
assertEquals(context.getTriggerId(), 'triggerId');

assertEquals(_spy.calls[0].args[1], mockAppAccessors.getReader());
assertEquals(_spy.calls[0].args[2], mockAppAccessors.getModifier());
assertEquals(_spy.calls[0].args[3], mockAppAccessors.getHttp());
assertEquals(_spy.calls[0].args[4], mockAppAccessors.getPersistence());

_spy.restore();
});

it('correctly handles execution of a slash command preview item executor', async () => {
const mockContext = {
sender: { __type: 'sender' },
room: { __type: 'room' },
params: { __type: 'params' },
threadId: 'threadId',
triggerId: 'triggerId',
};

const mockPreviewItem = {
id: 'previewItemId',
type: 'image',
value: 'https://example.com/image.png',
};

const _spy = spy(mockCommandExecutorAndPreview, 'executePreviewItem');

await handlePreviewItem({ AppAccessorsInstance: mockAppAccessors }, mockCommandExecutorAndPreview, [mockPreviewItem, mockContext]);

const context = _spy.calls[0].args[1];

assertInstanceOf(context.getRoom(), Room);
assertEquals(context.getSender(), { __type: 'sender' });
assertEquals(context.getArguments(), { __type: 'params' });
assertEquals(context.getThreadId(), 'threadId');
assertEquals(context.getTriggerId(), 'triggerId');

assertEquals(_spy.calls[0].args[2], mockAppAccessors.getReader());
assertEquals(_spy.calls[0].args[3], mockAppAccessors.getModifier());
assertEquals(_spy.calls[0].args[4], mockAppAccessors.getHttp());
assertEquals(_spy.calls[0].args[5], mockAppAccessors.getPersistence());

_spy.restore();
});
});

0 comments on commit 074c648

Please sign in to comment.