test(frontend): vitestによるワードミュートのテスト

This commit is contained in:
tai-cha 2025-02-09 07:54:10 +09:00
parent 2adf917fcf
commit 85b8af4b98
No known key found for this signature in database
GPG Key ID: 1D5EE39F870DC283
3 changed files with 121 additions and 1 deletions

View File

@ -18,7 +18,7 @@ type GlobalMisskeyWordMute = {
hard: WordMuteInfo;
};
function createWordMuteInfo(mutedWords: Array<string | string[]>) : WordMuteInfo {
export function createWordMuteInfo(mutedWords: Array<string | string[]>) : WordMuteInfo {
if (mutedWords.length <= 0) return false;
const normalTexts: string[] = [];
const andTexts: string[][] = [];

View File

@ -0,0 +1,44 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import type * as Misskey from 'misskey-js';
import { vi } from 'vitest';
export const UserLiteMock = vi.fn(() => {
return {
id: 'xxxxxxxx',
username: 'ai',
host: null,
name: '藍',
avatarUrl: null,
avatarBlurhash: null,
avatarDecorations: [],
emojis: {},
onlineStatus: 'online',
} as Misskey.entities.UserLite;
});
export const NoteMock = vi.fn((options?: {
text?: string,
cw?: string,
}) => {
const user = new UserLiteMock();
return {
id: 'xxxxxxxx',
// 2025/01/01 00:00:00 UTC on Unix time
createdAt: '1767225600000',
text: options?.text ?? 'Hello, Misskey!',
cw: options?.cw,
userId: user.id,
user: user,
visibility: 'public',
reactionAcceptance: null,
reactionEmojis: {},
reactions: {},
reactionCount: 0,
renoteCount: 0,
repliesCount: 0,
} as Misskey.entities.Note;
});

View File

@ -0,0 +1,76 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { describe, test, assert } from 'vitest';
import { createWordMuteInfo, checkWordMute } from '@/scripts/check-word-mute.js';
import { NoteMock } from './mocks.js';
type TestCases = {
text: string,
cw?: string,
mutedWords: Array<string | string[]>,
result: false | Array<string | string[]>,
}
describe('check-word-mute', () => {
const cases:Array<TestCases> = [
{
text: 'Hello, Misskey!',
mutedWords: ['Misskey'],
result: [['Misskey']],
},
// cw
{
text: 'Hello, Misskey!',
cw: 'ai',
mutedWords: ['ai'],
result: [['ai']],
},
// nothing
{
text: 'Hello, Misskey!',
mutedWords: [],
result: false,
},
// surrogate pair
{
text: '𠮟る',
mutedWords: ['𠮟'],
result: [['𠮟']],
},
// grapheme cluster
{
text: '👩‍❤️‍👨',
mutedWords: ['👩'],
result: false,
},
// regex
{
text: 'Hello, Misskey!',
mutedWords: ['/M[isk]*ey/'],
result: ['/M[isk]*ey/'],
},
// multi wordas
{
text: 'Hello, Misskey!',
mutedWords: [['Hello', 'Misskey'], ['Mi']],
result: [['Mi'],['Hello', 'Misskey']],
},
]
cases.forEach((c) => {
test(`text: ${c.text}, cw: ${c.cw}, mutedWords: ${c.mutedWords}` , async () => {
// initWordMuteInfoが実行されないので代わりにここで初期化
(globalThis as any)._misskeyWordMute = {
soft: createWordMuteInfo(c.mutedWords),
hard: createWordMuteInfo([]),
}
const note = NoteMock({ text: c.text, cw: c.cw });
const result = checkWordMute(note, null, 'soft');
assert.deepEqual(result, c.result);
});
});
});