Files
lolympus-tournament-support/apps/api/src/modules/dakao/dakao.publisher.spec.ts
T

110 lines
3.4 KiB
TypeScript

import { SecretService } from '../global/secret/secret.service';
import { DakaoCommandEnvelope, DakaoSendMessagePayload } from './dakao.types';
import { DakaoPublisher } from './dakao.publisher';
import { DakaoOutboxService } from './dakao-outbox.service';
function makePublisher() {
const outboxService = {
enqueueAndPublish: jest.fn().mockResolvedValue(undefined),
} as unknown as DakaoOutboxService;
const secretService = {} as SecretService;
const previousNodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'staging';
try {
const publisher = new DakaoPublisher(secretService, outboxService, {
instanceId: 'staging-instance',
});
return { publisher, outboxService };
} finally {
process.env.NODE_ENV = previousNodeEnv;
}
}
function getPublishedEnvelope(
outboxService: DakaoOutboxService,
): DakaoCommandEnvelope<DakaoSendMessagePayload> {
const enqueue = outboxService.enqueueAndPublish as jest.Mock;
return enqueue.mock.calls[0][0]
.envelope as DakaoCommandEnvelope<DakaoSendMessagePayload>;
}
describe('DakaoPublisher', () => {
it('keeps valid mention segments intact', async () => {
const { publisher, outboxService } = makePublisher();
await publisher.sendMessage({
chatroomId: '18474390111480678',
segments: [
{
kind: 'mention',
userId: '6215844374579825921',
displayText: '날으는꼴뚜기/95/여/D',
},
{ kind: 'text', text: ' 신청 완료' },
],
});
expect(getPublishedEnvelope(outboxService).payload.segments).toEqual([
{
kind: 'mention',
userId: '6215844374579825921',
displayText: '날으는꼴뚜기/95/여/D',
},
{ kind: 'text', text: ' 신청 완료' },
]);
});
it('downgrades invalid mention segments to text before publishing', async () => {
const { publisher, outboxService } = makePublisher();
await publisher.sendMessage({
chatroomId: '18474390111480678',
segments: [
{
kind: 'mention',
userId: '-123',
displayText: '소쿠리',
},
{ kind: 'text', text: ' 신청 완료' },
],
});
expect(getPublishedEnvelope(outboxService).payload.segments).toEqual([
{ kind: 'text', text: '@소쿠리' },
{ kind: 'text', text: ' 신청 완료' },
]);
});
it('uses deterministic event ids and the shared Dakao send subject', async () => {
const { publisher, outboxService } = makePublisher();
await publisher.sendMessage({
chatroomId: '18474390111480678',
segments: [{ kind: 'text', text: '신청 완료' }],
correlationId: 'chat-log-1',
});
const enqueue = outboxService.enqueueAndPublish as jest.Mock;
expect(enqueue.mock.calls[0][0]).toMatchObject({
subject: 'staging.dakao.chat.send-message.v1',
eventId: 'lolympus:chat.send-message:chat-log-1',
});
expect(getPublishedEnvelope(outboxService)).toMatchObject({
schema: 'dakao.command.v1',
eventId: 'lolympus:chat.send-message:chat-log-1',
eventSource: 'lolympus',
type: 'chat.send-message',
correlationId: 'chat-log-1',
resultSubjects: {
succeeded: 'staging.lolympus.dakao.chat.send-message.succeeded.v1',
failed: 'staging.lolympus.dakao.chat.send-message.failed.v1',
},
});
expect(getPublishedEnvelope(outboxService)).not.toHaveProperty(
'replySubject',
);
});
});