From 7452bb003864b0d23a71fc7597d8e15d1ba480ed Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Sat, 9 Mar 2024 10:04:43 -0800 Subject: [PATCH] Finish muteword tests --- packages/api/src/mocker.ts | 4 + .../api/tests/moderation-mutewords.test.ts | 111 ++++++++++++------ 2 files changed, 79 insertions(+), 36 deletions(-) diff --git a/packages/api/src/mocker.ts b/packages/api/src/mocker.ts index d608c8a1abe..556dba965c8 100644 --- a/packages/api/src/mocker.ts +++ b/packages/api/src/mocker.ts @@ -13,16 +13,19 @@ const FAKE_CID = 'bafyreiclp443lavogvhj3d2ob2cxbfuscni2k5jk7bebjzg7khl3esabwq' export const mock = { post({ text, + facets, reply, embed, }: { text: string + facets?: AppBskyFeedPost.Record['facets'] reply?: AppBskyFeedPost.ReplyRef embed?: AppBskyFeedPost.Record['embed'] }): AppBskyFeedPost.Record { return { $type: 'app.bsky.feed.post', text, + facets, reply, embed, langs: ['en'], @@ -50,6 +53,7 @@ export const mock = { labels?: ComAtprotoLabelDefs.Label[] }): AppBskyFeedDefs.PostView { return { + $type: 'app.bsky.feed.defs#postView', uri: `at://${author.did}/app.bsky.feed.post/fake`, cid: FAKE_CID, author, diff --git a/packages/api/tests/moderation-mutewords.test.ts b/packages/api/tests/moderation-mutewords.test.ts index de69f679413..18a2f556887 100644 --- a/packages/api/tests/moderation-mutewords.test.ts +++ b/packages/api/tests/moderation-mutewords.test.ts @@ -1,4 +1,4 @@ -import { RichText } from '../src/' +import { RichText, mock, moderatePost } from '../src/' import { hasMutedWord } from '../src/moderation/mutewords' @@ -602,51 +602,90 @@ describe(`hasMutedWord`, () => { }) }) - // TODO - describe.skip(`doesn't mute own post`, () => { + describe(`doesn't mute own post`, () => { it(`does mute if it isn't own post`, () => { - const rt = new RichText({ - text: `Mute words!`, - }) - - const match = hasMutedWord({ - mutedWords: [{ value: 'words', targets: ['content'] }], - text: rt.text, - facets: rt.facets, - outlineTags: [], - }) - - expect(match).toBe(true) + const res = moderatePost( + mock.postView({ + record: mock.post({ + text: 'Mute words!', + }), + author: mock.profileViewBasic({ + handle: 'bob.test', + displayName: 'Bob', + }), + labels: [], + }), + { + userDid: 'did:web:alice.test', + prefs: { + adultContentEnabled: false, + labels: {}, + labelers: [], + mutedWords: [{ value: 'words', targets: ['content'] }], + hiddenPosts: [], + }, + labelDefs: {}, + }, + ) + expect(res.causes[0].type).toBe('mute-word') }) it(`doesn't mute own post when muted word is in text`, () => { - const rt = new RichText({ - text: `Mute words!`, - }) - - const match = hasMutedWord({ - mutedWords: [{ value: 'words', targets: ['content'] }], - text: rt.text, - facets: rt.facets, - outlineTags: [], - }) - - expect(match).toBe(false) + const res = moderatePost( + mock.postView({ + record: mock.post({ + text: 'Mute words!', + }), + author: mock.profileViewBasic({ + handle: 'bob.test', + displayName: 'Bob', + }), + labels: [], + }), + { + userDid: 'did:web:bob.test', + prefs: { + adultContentEnabled: false, + labels: {}, + labelers: [], + mutedWords: [{ value: 'words', targets: ['content'] }], + hiddenPosts: [], + }, + labelDefs: {}, + }, + ) + expect(res.causes.length).toBe(0) }) it(`doesn't mute own post when muted word is in tags`, () => { const rt = new RichText({ text: `Mute #words!`, }) - - const match = hasMutedWord({ - mutedWords: [{ value: 'words', targets: ['tags'] }], - text: rt.text, - facets: rt.facets, - outlineTags: [], - }) - - expect(match).toBe(false) + const res = moderatePost( + mock.postView({ + record: mock.post({ + text: rt.text, + facets: rt.facets, + }), + author: mock.profileViewBasic({ + handle: 'bob.test', + displayName: 'Bob', + }), + labels: [], + }), + { + userDid: 'did:web:bob.test', + prefs: { + adultContentEnabled: false, + labels: {}, + labelers: [], + mutedWords: [{ value: 'words', targets: ['tags'] }], + hiddenPosts: [], + }, + labelDefs: {}, + }, + ) + expect(res.causes.length).toBe(0) }) }) })