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
40 changes: 39 additions & 1 deletion packages/backend/src/shared/services/suppressor.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,44 @@ describe('SuppressorService', () => {
expect(loggerSpy).toHaveBeenCalled();
});

it('sendSuppressedMessage uwuifies text by replacing r and l with w roughly 5% of the time when there are at least 4 eligible letters', async () => {
vi.spyOn(Math, 'random').mockReturnValue(0.04);

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

expect(suppressorService.translationService.translate).not.toHaveBeenCalled();
expect(suppressorService.webService.sendMessage).toHaveBeenCalledWith('C123', '<@U1> says "hewwo wowwd"');
});
Comment on lines +121 to +135

it('sendSuppressedMessage skips uwu when the text has fewer than 4 r or l letters', async () => {
vi.spyOn(Math, 'random').mockReturnValue(0.04);

await suppressorService.sendSuppressedMessage(
'C123',
'U1',
'hi there',
'123',
1,
suppressorService.muzzlePersistenceService as never,
);

expect(suppressorService.translationService.translate).toHaveBeenCalledWith('hi there');
expect(suppressorService.webService.sendMessage).toHaveBeenCalledWith(
'C123',
expect.stringContaining('<@U1> says'),
);
});

it('sendSuppressedMessage uses translation for normal channels', async () => {
vi.spyOn(Math, 'random').mockReturnValue(0.5);

await suppressorService.sendSuppressedMessage(
'C123',
'U1',
Expand All @@ -128,14 +165,15 @@ describe('SuppressorService', () => {
suppressorService.muzzlePersistenceService as never,
);

expect(suppressorService.translationService.translate).toHaveBeenCalled();
expect(suppressorService.translationService.translate).toHaveBeenCalledWith('hello world');
expect(suppressorService.webService.sendMessage).toHaveBeenCalledWith(
'C123',
expect.stringContaining('<@U1> says'),
);
});

it('sendSuppressedMessage falls back when translation fails', async () => {
vi.spyOn(Math, 'random').mockReturnValue(0.5);
(suppressorService.translationService.translate as Mock).mockRejectedValue(new Error('translate fail'));

await suppressorService.sendSuppressedMessage(
Expand Down
17 changes: 14 additions & 3 deletions packages/backend/src/shared/services/suppressor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ export class SuppressorService {
}
}

public uwuifyText(text: string): string {
return text.replace(/[rRlL]/g, 'w');
}

public shouldUwuifyText(text: string): boolean {
Comment on lines +206 to +210
return (text.match(/[rRlL]/g) ?? []).length >= 4;
}

public async sendSuppressedMessage(
channel: string,
userId: string,
Expand All @@ -218,7 +226,6 @@ export class SuppressorService {
}

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

const shouldMuzzle = words.length <= 250;

if (shouldMuzzle) {
Expand Down Expand Up @@ -246,8 +253,12 @@ export class SuppressorService {
return null;
});
} else {
await this.translationService
.translate(textWithFallbackReplacments)
const shouldUwuify = Math.random() < 0.05 && this.shouldUwuifyText(textWithFallbackReplacments);
await (
shouldUwuify
? Promise.resolve(this.uwuifyText(textWithFallbackReplacments))
: this.translationService.translate(textWithFallbackReplacments)
Comment on lines +256 to +260
)
.then(async (message) => {
await this.logTranslateSuppression(text, dbId, persistenceService);
await this.webService
Expand Down
Loading