diff --git a/packages/api/src/routers/svc/integrations.ts b/packages/api/src/routers/svc/integrations.ts index 74928f8e4..a07a6b451 100644 --- a/packages/api/src/routers/svc/integrations.ts +++ b/packages/api/src/routers/svc/integrations.ts @@ -7,7 +7,7 @@ import { getRepository } from '../../entity/utils' import { Integration, IntegrationType } from '../../entity/integration' import { buildLogger } from '../../utils/logger' import { syncWithIntegration } from '../../services/integrations' -import { getPageById, getPageByParam, searchPages } from '../../elastic/pages' +import { getPageById, searchPages } from '../../elastic/pages' import { Page } from '../../elastic/types' import { DateFilter } from '../../utils/search' @@ -63,19 +63,19 @@ export function integrationsServiceRouter() { const action = req.params.action.toUpperCase() if (action === 'SYNC_UPDATED') { // get updated page by id - const id = data.id - let page: Page | undefined + let id = '' switch (type) { case EntityType.PAGE: - page = await getPageById(id) + id = data.id break case EntityType.HIGHLIGHT: - page = await getPageByParam({ 'highlights.id': id }) + id = data.articleId break case EntityType.LABEL: - page = await getPageByParam({ 'labels.id': id }) + id = data.pageId break } + const page = await getPageById(id) if (!page) { logger.info('No page found for id', id) res.status(200).send('No page found') diff --git a/packages/api/test/elastic/highlights.test.ts b/packages/api/test/elastic/highlights.test.ts new file mode 100644 index 000000000..4950e5f62 --- /dev/null +++ b/packages/api/test/elastic/highlights.test.ts @@ -0,0 +1,57 @@ +import 'mocha' +import { expect } from 'chai' +import { Highlight, Page, PageContext } from '../../src/elastic/types' +import { createPubSubClient } from '../../src/datalayer/pubsub' +import { deletePage } from '../../src/elastic/pages' +import { + addHighlightToPage, + searchHighlights, +} from '../../src/elastic/highlights' +import { createTestElasticPage } from '../util' + +describe('highlights in elastic', () => { + const userId = 'userId' + const ctx: PageContext = { + pubsub: createPubSubClient(), + refresh: true, + uid: userId, + } + + describe('searchHighlights', () => { + const highlightId = 'highlightId' + let page: Page + + before(async () => { + // create a testing page + page = await createTestElasticPage(userId) + const highlightData: Highlight = { + patch: 'test patch', + quote: 'test content', + shortId: 'test shortId', + id: highlightId, + userId: page.userId, + createdAt: new Date(), + updatedAt: new Date(), + } + + await addHighlightToPage(page.id, highlightData, ctx) + }) + + after(async () => { + // delete the testing page + await deletePage(page.id, ctx) + }) + + it('searches highlights', async () => { + const [searchResults, count] = (await searchHighlights( + { + query: 'test', + }, + page.userId + )) || [[], 0] + + expect(count).to.eq(1) + expect(searchResults[0].id).to.eq(highlightId) + }) + }) +}) diff --git a/packages/api/test/elastic/labels.test.ts b/packages/api/test/elastic/labels.test.ts new file mode 100644 index 000000000..685e0a773 --- /dev/null +++ b/packages/api/test/elastic/labels.test.ts @@ -0,0 +1,141 @@ +import 'mocha' +import { expect } from 'chai' +import { + ArticleSavingRequestStatus, + Highlight, + Label, + Page, + PageContext, + PageType, +} from '../../src/elastic/types' +import { createPubSubClient } from '../../src/datalayer/pubsub' +import { createPage, deletePage, getPageById } from '../../src/elastic/pages' +import { addLabelInPage, setLabelsForHighlight } from '../../src/elastic/labels' +import { addHighlightToPage } from '../../src/elastic/highlights' + +describe('labels in elastic', () => { + const userId = 'userId' + const ctx: PageContext = { + pubsub: createPubSubClient(), + refresh: true, + uid: userId, + } + + describe('addLabelInPage', () => { + let page: Page + let label: Label = { + id: 'Test label id', + name: 'test label', + color: '#07D2D1', + } + let newLabel: Label + + before(async () => { + // create a testing page + page = { + id: '', + hash: 'test hash', + userId: userId, + pageType: PageType.Article, + title: 'test title', + content: '
test
', + slug: 'test slug', + createdAt: new Date(), + updatedAt: new Date(), + savedAt: new Date(), + readingProgressPercent: 100, + readingProgressAnchorIndex: 0, + url: 'https://blog.omnivore.app/p/getting-started-with-omnivore', + archivedAt: new Date(), + labels: [label], + state: ArticleSavingRequestStatus.Succeeded, + } + page.id = (await createPage(page, ctx))! + }) + + after(async () => { + // delete the testing page + await deletePage(page.id, ctx) + }) + + context('when the label not exist in the page', () => { + before(() => { + newLabel = { + id: 'new label id', + name: 'new label', + color: '#07D2D1', + } + }) + it('adds the label to the page', async () => { + const result = await addLabelInPage(page.id, newLabel, ctx) + expect(result).to.be.true + + const updatedPage = await getPageById(page.id) + expect(updatedPage?.labels).to.deep.include(label) + }) + }) + + context('when the label exists in the page', () => { + before(() => { + newLabel = label + }) + + it('does not add the label to the page', async () => { + const result = await addLabelInPage(page.id, newLabel, ctx) + + expect(result).to.be.false + }) + }) + }) + + describe('setLabelsForHighlight', () => { + const page = { + id: 'testPageId', + hash: 'test set labels for highlight hash', + userId: userId, + pageType: PageType.Article, + title: 'test set labels for highlight title', + content: 'test
', + slug: 'test set labels for highlight slug', + createdAt: new Date(), + savedAt: new Date(), + readingProgressPercent: 100, + readingProgressAnchorIndex: 0, + url: 'https://blog.omnivore.app/p/setting-labels-for-highlight', + state: ArticleSavingRequestStatus.Succeeded, + } + const highlightId = 'highlightId' + const label: Label = { + id: 'test label id', + name: 'test label', + color: '#07D2D1', + } + + before(async () => { + // create a testing page + await createPage(page, ctx) + + const highlightData: Highlight = { + patch: 'test set labels patch', + quote: 'test set labels quote', + shortId: 'test set labels shortId', + id: highlightId, + userId: page.userId, + createdAt: new Date(), + updatedAt: new Date(), + } + + await addHighlightToPage(page.id, highlightData, ctx) + }) + + after(async () => { + await deletePage(page.id, ctx) + }) + + it('sets labels for highlights', async () => { + const result = await setLabelsForHighlight(highlightId, [label], ctx) + + expect(result).to.be.true + }) + }) +}) diff --git a/packages/api/test/elastic/index.test.ts b/packages/api/test/elastic/pages.test.ts similarity index 55% rename from packages/api/test/elastic/index.test.ts rename to packages/api/test/elastic/pages.test.ts index ff415a41d..3b6959eb2 100644 --- a/packages/api/test/elastic/index.test.ts +++ b/packages/api/test/elastic/pages.test.ts @@ -3,8 +3,6 @@ import { expect } from 'chai' import { InFilter, ReadFilter } from '../../src/utils/search' import { ArticleSavingRequestStatus, - Highlight, - Label, Page, PageContext, PageType, @@ -21,13 +19,9 @@ import { searchPages, updatePage, } from '../../src/elastic/pages' -import { addLabelInPage, setLabelsForHighlight } from '../../src/elastic/labels' -import { - addHighlightToPage, - searchHighlights, -} from '../../src/elastic/highlights' +import { createTestElasticPage } from '../util' -describe('elastic api', () => { +describe('pages in elastic', () => { const userId = 'userId' const ctx: PageContext = { pubsub: createPubSubClient(), @@ -35,49 +29,6 @@ describe('elastic api', () => { uid: userId, } - let page: Page - - before(async () => { - // create a testing page - page = { - id: '', - hash: 'test hash', - userId: userId, - pageType: PageType.Article, - title: 'test title', - content: 'test
', - slug: 'test slug', - createdAt: new Date(), - updatedAt: new Date(), - savedAt: new Date(), - readingProgressPercent: 100, - readingProgressAnchorIndex: 0, - url: 'https://blog.omnivore.app/p/getting-started-with-omnivore', - archivedAt: new Date(), - labels: [ - { - id: 'Test label id', - name: 'test label', - color: '#ffffff', - createdAt: new Date(), - }, - { - id: 'Test label id 2', - name: 'test label 2', - color: '#eeeeee', - createdAt: new Date(), - }, - ], - state: ArticleSavingRequestStatus.Succeeded, - } - page.id = (await createPage(page, ctx))! - }) - - after(async () => { - // delete the testing page - await deletePage(page.id, ctx) - }) - describe('createPage', () => { let newPageId: string @@ -108,6 +59,18 @@ describe('elastic api', () => { }) describe('getPageByParam', () => { + let page: Page + + before(async () => { + // create a testing page + page = await createTestElasticPage(userId) + }) + + after(async () => { + // delete the testing page + await deletePage(page.id, ctx) + }) + it('gets a page by url', async () => { const pageFound = await getPageByParam({ userId: page.userId, @@ -119,6 +82,18 @@ describe('elastic api', () => { }) describe('getPageById', () => { + let page: Page + + before(async () => { + // create a testing page + page = await createTestElasticPage(userId) + }) + + after(async () => { + // delete the testing page + await deletePage(page.id, ctx) + }) + it('gets a page by id', async () => { const pageFound = await getPageById(page.id) expect(pageFound).not.undefined @@ -126,6 +101,18 @@ describe('elastic api', () => { }) describe('updatePage', () => { + let page: Page + + before(async () => { + // create a testing page + page = await createTestElasticPage(userId) + }) + + after(async () => { + // delete the testing page + await deletePage(page.id, ctx) + }) + it('updates a page', async () => { const newTitle = 'new title' const updatedPageData: Partialtest
', - slug: 'test set labels for highlight slug', - createdAt: new Date(), - savedAt: new Date(), - readingProgressPercent: 100, - readingProgressAnchorIndex: 0, - url: 'https://blog.omnivore.app/p/setting-labels-for-highlight', - state: ArticleSavingRequestStatus.Succeeded, - } - const highlightId = 'highlightId' - const label: Label = { - id: 'test label id', - name: 'test label', - color: '#07D2D1', - } - - before(async () => { - // create a testing page - await createPage(page, ctx) - - const highlightData: Highlight = { - patch: 'test set labels patch', - quote: 'test set labels quote', - shortId: 'test set labels shortId', - id: highlightId, - userId: page.userId, - createdAt: new Date(), - updatedAt: new Date(), - } - - await addHighlightToPage(page.id, highlightData, ctx) + page.id = (await createPage(page, ctx))! }) after(async () => { await deletePage(page.id, ctx) }) - it('sets labels for highlights', async () => { - const result = await setLabelsForHighlight(highlightId, [label], ctx) - - expect(result).to.be.true + it('counts pages by createdAt', async () => { + const count = await countByCreatedAt(userId, createdAt, createdAt) + expect(count).to.eq(1) }) }) diff --git a/packages/api/test/resolvers/article.test.ts b/packages/api/test/resolvers/article.test.ts index 6e5c2e442..f9175d599 100644 --- a/packages/api/test/resolvers/article.test.ts +++ b/packages/api/test/resolvers/article.test.ts @@ -694,7 +694,7 @@ describe('Article API', () => { let pageId = '' before(async () => { - pageId = (await createTestElasticPage(user)).id! + pageId = (await createTestElasticPage(user.id)).id! }) after(async () => { diff --git a/packages/api/test/resolvers/highlight.test.ts b/packages/api/test/resolvers/highlight.test.ts index fbd3158e5..d13300cd9 100644 --- a/packages/api/test/resolvers/highlight.test.ts +++ b/packages/api/test/resolvers/highlight.test.ts @@ -104,7 +104,7 @@ describe('Highlights API', () => { .send({ fakeEmail: user.email }) authToken = res.body.authToken - pageId = (await createTestElasticPage(user)).id + pageId = (await createTestElasticPage(user.id)).id ctx = { pubsub: createPubSubClient(), uid: user.id } }) diff --git a/packages/api/test/resolvers/labels.test.ts b/packages/api/test/resolvers/labels.test.ts index 8dca95289..d2729c4d9 100644 --- a/packages/api/test/resolvers/labels.test.ts +++ b/packages/api/test/resolvers/labels.test.ts @@ -246,7 +246,7 @@ describe('Labels API', () => { before(async () => { toDeleteLabel = await createTestLabel(user, 'page label', '#ffffff') labelId = toDeleteLabel.id - page = await createTestElasticPage(user, [toDeleteLabel]) + page = await createTestElasticPage(user.id, [toDeleteLabel]) }) after(async () => { @@ -267,7 +267,7 @@ describe('Labels API', () => { let page: Page before(async () => { - page = await createTestElasticPage(user) + page = await createTestElasticPage(user.id) toDeleteLabel = await createTestLabel( user, 'highlight label', @@ -340,7 +340,7 @@ describe('Labels API', () => { const label1 = await createTestLabel(user, 'label_1', '#ffffff') const label2 = await createTestLabel(user, 'label_2', '#eeeeee') labels = [label1, label2] - page = await createTestElasticPage(user) + page = await createTestElasticPage(user.id) }) after(async () => { @@ -497,7 +497,7 @@ describe('Labels API', () => { let page: Page before(async () => { - page = await createTestElasticPage(user, [toUpdateLabel]) + page = await createTestElasticPage(user.id, [toUpdateLabel]) }) after(async () => { @@ -542,7 +542,7 @@ describe('Labels API', () => { const label1 = await createTestLabel(user, 'label_1', '#ffffff') const label2 = await createTestLabel(user, 'label_2', '#eeeeee') labels = [label1, label2] - page = await createTestElasticPage(user) + page = await createTestElasticPage(user.id) }) after(async () => { diff --git a/packages/api/test/resolvers/reminders.test.ts b/packages/api/test/resolvers/reminders.test.ts index 907c5269a..16e616ca5 100644 --- a/packages/api/test/resolvers/reminders.test.ts +++ b/packages/api/test/resolvers/reminders.test.ts @@ -38,7 +38,7 @@ describe('Reminders API', () => { authToken = res.body.authToken // create page, link and reminders test data - page = await createTestElasticPage(user) + page = await createTestElasticPage(user.id) reminder = await createTestReminder(user, page.id) }) diff --git a/packages/api/test/resolvers/report.test.ts b/packages/api/test/resolvers/report.test.ts index c8fafe754..1a4ba6ea1 100644 --- a/packages/api/test/resolvers/report.test.ts +++ b/packages/api/test/resolvers/report.test.ts @@ -24,7 +24,7 @@ describe('Report API', () => { authToken = res.body.authToken // create a page - page = await createTestElasticPage(user) + page = await createTestElasticPage(user.id) }) after(async () => { diff --git a/packages/api/test/resolvers/update.test.ts b/packages/api/test/resolvers/update.test.ts index abdccdf21..a742454fb 100644 --- a/packages/api/test/resolvers/update.test.ts +++ b/packages/api/test/resolvers/update.test.ts @@ -1,9 +1,5 @@ import { createTestUser, deleteTestUser } from '../db' -import { - createTestElasticPage, - graphqlRequest, - request, -} from '../util' +import { createTestElasticPage, graphqlRequest, request } from '../util' import { expect } from 'chai' import 'mocha' import { User } from '../../src/entity/user' @@ -24,7 +20,7 @@ describe('Update API', () => { .send({ fakeEmail: user.email }) authToken = res.body.authToken - page = await createTestElasticPage(user) + page = await createTestElasticPage(user.id) }) after(async () => { @@ -34,8 +30,8 @@ describe('Update API', () => { describe('update page', () => { let query: string - let title = "New Title" - let description = "New Description" + let title = 'New Title' + let description = 'New Description' beforeEach(() => { query = ` @@ -59,14 +55,14 @@ describe('Update API', () => { } } ` - }) - - it('should update page', async () => { - const res = await graphqlRequest(query, authToken).expect(200) - - const updatedPage = res?.body.data.updatePage.updatedPage - expect(updatedPage?.title).to.eql(title) - expect(updatedPage?.description).to.eql(description) - }) }) + + it('should update page', async () => { + const res = await graphqlRequest(query, authToken).expect(200) + + const updatedPage = res?.body.data.updatePage.updatedPage + expect(updatedPage?.title).to.eql(title) + expect(updatedPage?.description).to.eql(description) + }) + }) }) diff --git a/packages/api/test/util.ts b/packages/api/test/util.ts index f63fca1a3..53bc16612 100644 --- a/packages/api/test/util.ts +++ b/packages/api/test/util.ts @@ -4,9 +4,8 @@ import { v4 } from 'uuid' import { corsConfig } from '../src/utils/corsConfig' import { ArticleSavingRequestStatus, Label, Page } from '../src/elastic/types' import { PageType } from '../src/generated/graphql' -import { User } from '../src/entity/user' import { createPubSubClient } from '../src/datalayer/pubsub' -import { createPage, getPageById } from '../src/elastic/pages' +import { createPage } from '../src/elastic/pages' const { app, apollo } = createApp() export const request = supertest(app) @@ -39,13 +38,13 @@ export const generateFakeUuid = () => { } export const createTestElasticPage = async ( - user: User, + userId: string, labels?: Label[] ): Promisetest content
', @@ -59,19 +58,10 @@ export const createTestElasticPage = async ( state: ArticleSavingRequestStatus.Succeeded, } - const pageId = await createPage(page, { + page.id = (await createPage(page, { pubsub: createPubSubClient(), refresh: true, - uid: user.id, - }) - if (pageId) { - page.id = pageId - } - - const res = await getPageById(page.id) - console.log('got page', res) - if (!res) { - throw new Error('Failed to create page') - } - return res + uid: userId, + }))! + return page }