Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 85 additions & 6 deletions packages/backend/src/shared/services/suppressor.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,32 +135,111 @@ describe('SuppressorService', () => {
);
});

it('sendSuppressedMessage uses corpo mode for libworkchat', async () => {
it('sendSuppressedMessage falls back when translation fails', async () => {
(suppressorService.translationService.translate as Mock).mockRejectedValue(new Error('translate fail'));

await suppressorService.sendSuppressedMessage(
'#libworkchat',
'C123',
'U1',
'hello world',
'123',
1,
suppressorService.muzzlePersistenceService as never,
);

expect(suppressorService.webService.sendMessage).toHaveBeenCalled();
});

it('sendSuppressedMessage returns early when text is undefined', async () => {
await suppressorService.sendSuppressedMessage(
'C123',
'U1',
undefined,
'123',
1,
suppressorService.muzzlePersistenceService as never,
);

expect(suppressorService.translationService.translate).not.toHaveBeenCalled();
expect(suppressorService.webService.sendMessage).not.toHaveBeenCalled();
});

it('sendSuppressedMessage uses corpo mode for C023B688SLT with more than 10 words', async () => {
const longEnoughText = new Array(11).fill('word').join(' ');
await suppressorService.sendSuppressedMessage(
'C023B688SLT',
'U1',
longEnoughText,
'123',
1,
suppressorService.muzzlePersistenceService as never,
);

expect(suppressorService.aiService.generateCorpoSpeak).toHaveBeenCalled();
expect(suppressorService.translationService.translate).not.toHaveBeenCalled();
});

it('sendSuppressedMessage falls back when translation fails', async () => {
(suppressorService.translationService.translate as Mock).mockRejectedValue(new Error('translate fail'));
it('sendSuppressedMessage does not use corpo for C023B688SLT with 10 or fewer words', async () => {
const shortText = new Array(10).fill('word').join(' ');
await suppressorService.sendSuppressedMessage(
'C023B688SLT',
'U1',
shortText,
'123',
1,
suppressorService.muzzlePersistenceService as never,
);

expect(suppressorService.translationService.translate).toHaveBeenCalled();
expect(suppressorService.aiService.generateCorpoSpeak).not.toHaveBeenCalled();
});

it('sendSuppressedMessage uses corpo mode for #libworkchat with more than 10 words', async () => {
const longEnoughText = new Array(11).fill('word').join(' ');
await suppressorService.sendSuppressedMessage(
'C123',
'#libworkchat',
'U1',
longEnoughText,
'123',
1,
suppressorService.muzzlePersistenceService as never,
);

expect(suppressorService.aiService.generateCorpoSpeak).toHaveBeenCalled();
expect(suppressorService.translationService.translate).not.toHaveBeenCalled();
});

it('sendSuppressedMessage does not use corpo for #libworkchat with 10 or fewer words', async () => {
const shortText = new Array(10).fill('word').join(' ');
await suppressorService.sendSuppressedMessage(
'#libworkchat',
'U1',
shortText,
'123',
1,
suppressorService.muzzlePersistenceService as never,
);

expect(suppressorService.translationService.translate).toHaveBeenCalled();
expect(suppressorService.aiService.generateCorpoSpeak).not.toHaveBeenCalled();
});

it('sendSuppressedMessage falls back when corpo speak fails', async () => {
(suppressorService.aiService.generateCorpoSpeak as Mock).mockRejectedValue(new Error('ai fail'));

await suppressorService.sendSuppressedMessage(
'#libworkchat',
'U1',
'hello world',
Comment on lines +227 to 233
'123',
1,
suppressorService.muzzlePersistenceService as never,
);

expect(suppressorService.webService.sendMessage).toHaveBeenCalled();
expect(suppressorService.webService.sendMessage).toHaveBeenCalledWith(
'#libworkchat',
expect.stringContaining('<@U1> says'),
);
});

it('sendSuppressedMessage skips when too many words', async () => {
Expand Down
12 changes: 8 additions & 4 deletions packages/backend/src/shared/services/suppressor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,20 @@ export class SuppressorService {
public async sendSuppressedMessage(
channel: string,
userId: string,
text: string,
text: string | undefined,
timestamp: string,
dbId: number,
persistenceService: MuzzlePersistenceService | BackFirePersistenceService | CounterPersistenceService,
): Promise<void> {
await this.webService.deleteMessage(channel, timestamp, userId);

const words = text.split(' ');
if (!text) {
return;
}

const words: string[] | undefined = text.split(' ');

const shouldMuzzle = words.length > 0 && words.length <= 250;
const shouldMuzzle = words.length <= 250;

if (shouldMuzzle) {
const textWithFallbackReplacments = words
Expand All @@ -224,7 +228,7 @@ export class SuppressorService {
)
.join(' ');

const shouldCorpo = channel === '#libworkchat' || (channel === 'C023B688SLT' && words.length > 10);
const shouldCorpo = (channel === '#libworkchat' || channel === 'C023B688SLT') && words.length > 10;

if (shouldCorpo) {
await this.aiService
Expand Down
Loading