From aac1fb5cf668568bb6312a446ce44e35a7a33c0d Mon Sep 17 00:00:00 2001 From: moughxyz Date: Tue, 20 Feb 2024 13:07:13 -0600 Subject: [PATCH] refactor: exclude protected previews from linking search --- .../Search/doesItemMatchSearchQuery.spec.ts | 58 +++++++++++++++++++ .../Items/Search/doesItemMatchSearchQuery.ts | 6 +- 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 packages/web/src/javascripts/Utils/Items/Search/doesItemMatchSearchQuery.spec.ts diff --git a/packages/web/src/javascripts/Utils/Items/Search/doesItemMatchSearchQuery.spec.ts b/packages/web/src/javascripts/Utils/Items/Search/doesItemMatchSearchQuery.spec.ts new file mode 100644 index 00000000000..d82bae2b291 --- /dev/null +++ b/packages/web/src/javascripts/Utils/Items/Search/doesItemMatchSearchQuery.spec.ts @@ -0,0 +1,58 @@ +import { ContentType } from '@standardnotes/domain-core' +import { getItemTitleInContextOfLinkBubble, doesItemMatchSearchQuery } from './doesItemMatchSearchQuery' +import { DecryptedItemInterface, ItemContent, SNNote, SNTag, isNote, isTag } from '@standardnotes/snjs' +import { WebApplicationInterface } from '@standardnotes/ui-services' + +describe('getItemTitleInContextOfLinkBubble', () => { + it('returns the title if present', () => { + const item = { title: 'Test Title', content_type: ContentType.TYPES.Note } as jest.Mocked + expect(getItemTitleInContextOfLinkBubble(item as DecryptedItemInterface)).toBe('Test Title') + }) + + it('returns the note preview if title is empty and item is a note', () => { + const item = { + preview_plain: 'Note Preview', + title: '', + content_type: ContentType.TYPES.Note, + } as jest.Mocked + expect(getItemTitleInContextOfLinkBubble(item as DecryptedItemInterface)).toBe('Note Preview') + }) + + it('returns empty string if title is empty and item is not a note', () => { + const item = { title: '', content_type: ContentType.TYPES.Tag } as jest.Mocked + expect(getItemTitleInContextOfLinkBubble(item as DecryptedItemInterface)).toBe('') + }) +}) + +describe('doesItemMatchSearchQuery', () => { + const application = {} as WebApplicationInterface + + it('returns false for a protected note even if the title matches the search query', () => { + const item = { + title: '', + preview_plain: 'Protected Note Content', + protected: true, + archived: false, + trashed: false, + content_type: ContentType.TYPES.Note, + } as jest.Mocked + expect( + doesItemMatchSearchQuery(item as DecryptedItemInterface, 'protected note content', application), + ).toBeFalsy() + }) + + it('returns true if the item title matches the search query', () => { + const item = { title: 'Matched Item', archived: false, trashed: false } + expect(doesItemMatchSearchQuery(item as DecryptedItemInterface, 'matched', application)).toBeTruthy() + }) + + it('returns false if the item is archived', () => { + const item = { title: 'Archived Item', archived: true, trashed: false } + expect(doesItemMatchSearchQuery(item as DecryptedItemInterface, 'archived', application)).toBeFalsy() + }) + + it('returns false if the item is trashed', () => { + const item = { title: 'Trashed Item', archived: false, trashed: true } + expect(doesItemMatchSearchQuery(item as DecryptedItemInterface, 'trashed', application)).toBeFalsy() + }) +}) diff --git a/packages/web/src/javascripts/Utils/Items/Search/doesItemMatchSearchQuery.ts b/packages/web/src/javascripts/Utils/Items/Search/doesItemMatchSearchQuery.ts index 4df02061cf3..83eeb6e3cb4 100644 --- a/packages/web/src/javascripts/Utils/Items/Search/doesItemMatchSearchQuery.ts +++ b/packages/web/src/javascripts/Utils/Items/Search/doesItemMatchSearchQuery.ts @@ -7,7 +7,11 @@ export function getItemTitleInContextOfLinkBubble(item: DecryptedItemInterface, application: WebApplicationInterface) { if (isNote(item)) { - return item.title.length > 0 ? item.title : item.preview_plain + if (item.title.length > 0) { + return item.title + } else if (!item.protected) { + return item.preview_plain + } } else if (isTag(item)) { return application.items.getTagLongTitle(item) }