Fix tests

This commit is contained in:
Hongbo Wu 2023-03-13 22:09:53 +08:00
parent b0e91cbbbb
commit 9df1546323
8 changed files with 58 additions and 40 deletions

View file

@ -292,8 +292,6 @@ export const updateHighlight = async (
conflicts: 'proceed',
})
if (body.updated === 0) return false
await ctx.pubsub.entityUpdated<Highlight>(
EntityType.HIGHLIGHT,
highlight,

View file

@ -147,38 +147,32 @@ export const mergeHighlightResolver = authorized<
const articleHighlights = page.highlights
/* Compute merged annotation form the order of highlights appearing on page */
const overlappings: { annotation?: string | null; labels?: Label[] }[] = []
articleHighlights.forEach((highlight, index) => {
// only consider highlights that are in the overlap list
const mergedAnnotations: string[] = []
const mergedLabels: Label[] = []
const pageHighlights = page.highlights.filter((highlight) => {
// filter out highlights that are in the overlap list
// and are of type highlight (not annotation or note)
if (
overlapHighlightIdList.includes(highlight.id) &&
highlight.type === HighlightType.Highlight
) {
articleHighlights.splice(index, 1)
overlappings.push({
annotation: highlight.annotation,
labels: highlight.labels,
})
if (highlight.annotation) {
mergedAnnotations.push(highlight.annotation)
}
if (highlight.labels) {
// remove duplicates from labels by checking id
highlight.labels.forEach((label) => {
if (
!mergedLabels.find((mergedLabel) => mergedLabel.id === label.id)
) {
mergedLabels.push(label)
}
})
}
return false
}
return true
})
console.log(overlapHighlightIdList)
const mergedAnnotation: string[] = []
const mergedLabels: Label[] = []
overlappings.forEach((highlight) => {
if (highlight.annotation) {
mergedAnnotation.push(highlight.annotation)
}
if (highlight.labels) {
// remove duplicates from labels by checking id
highlight.labels.forEach((label) => {
if (!mergedLabels.find((mergedLabel) => mergedLabel.id === label.id)) {
mergedLabels.push(label)
}
})
}
})
try {
const highlight: HighlightData = {
...newHighlightInput,
@ -186,14 +180,14 @@ export const mergeHighlightResolver = authorized<
createdAt: new Date(),
userId: claims.uid,
annotation:
mergedAnnotation.length > 0 ? mergedAnnotation.join('\n') : null,
mergedAnnotations.length > 0 ? mergedAnnotations.join('\n') : null,
type: HighlightType.Highlight,
labels: mergedLabels,
}
const merged = await updatePage(
pageId,
{ highlights: articleHighlights.concat(highlight) },
{ highlights: pageHighlights.concat(highlight) },
{ pubsub, uid: claims.uid }
)
if (!merged) {

View file

@ -1,6 +1,11 @@
import 'mocha'
import { expect } from 'chai'
import { Highlight, Page, PageContext } from '../../src/elastic/types'
import {
Highlight,
HighlightType,
Page,
PageContext,
} from '../../src/elastic/types'
import { createPubSubClient } from '../../src/datalayer/pubsub'
import { deletePage } from '../../src/elastic/pages'
import {
@ -32,6 +37,7 @@ describe('highlights in elastic', () => {
userId: page.userId,
createdAt: new Date(),
updatedAt: new Date(),
type: HighlightType.Highlight,
}
await addHighlightToPage(page.id, highlightData, ctx)

View file

@ -3,6 +3,7 @@ import { expect } from 'chai'
import {
ArticleSavingRequestStatus,
Highlight,
HighlightType,
Label,
Page,
PageContext,
@ -123,6 +124,7 @@ describe('labels in elastic', () => {
userId: page.userId,
createdAt: new Date(),
updatedAt: new Date(),
type: HighlightType.Highlight,
}
await addHighlightToPage(page.id, highlightData, ctx)

View file

@ -15,6 +15,7 @@ import {
import {
ArticleSavingRequestStatus,
Highlight,
HighlightType,
Page,
PageContext,
PageType,
@ -476,6 +477,7 @@ describe('Article API', () => {
quote: 'test quote',
updatedAt: new Date(),
userId: user.id,
type: HighlightType.Highlight,
},
],
}
@ -861,6 +863,7 @@ describe('Article API', () => {
quote: '<p>search highlight</p>',
createdAt: new Date(),
updatedAt: new Date(),
type: HighlightType.Highlight,
}
await addHighlightToPage(page.id, highlight, ctx)
highlights.push(highlight)

View file

@ -1,11 +1,3 @@
import * as chai from 'chai'
import { expect } from 'chai'
import chaiString from 'chai-string'
import 'mocha'
import { createPubSubClient } from '../../src/datalayer/pubsub'
import { deletePage, updatePage } from '../../src/elastic/pages'
import { PageContext } from '../../src/elastic/types'
import { User } from '../../src/entity/user'
import { createTestUser, deleteTestUser } from '../db'
import {
createTestElasticPage,
@ -13,6 +5,14 @@ import {
graphqlRequest,
request,
} from '../util'
import * as chai from 'chai'
import { expect } from 'chai'
import 'mocha'
import { User } from '../../src/entity/user'
import chaiString from 'chai-string'
import { createPubSubClient } from '../../src/datalayer/pubsub'
import { HighlightType, PageContext } from '../../src/elastic/types'
import { deletePage, updatePage } from '../../src/elastic/pages'
chai.use(chaiString)
@ -269,6 +269,7 @@ describe('Highlights API', () => {
quote: '',
createdAt: new Date(),
updatedAt: new Date(),
type: HighlightType.Highlight,
},
],
},

View file

@ -14,7 +14,12 @@ import { Label } from '../../src/entity/label'
import { expect } from 'chai'
import 'mocha'
import { User } from '../../src/entity/user'
import { Highlight, Page, PageContext } from '../../src/elastic/types'
import {
Highlight,
HighlightType,
Page,
PageContext,
} from '../../src/elastic/types'
import { getRepository } from '../../src/entity/utils'
import { deletePage, getPageById } from '../../src/elastic/pages'
import { createPubSubClient } from '../../src/datalayer/pubsub'
@ -287,6 +292,7 @@ describe('Labels API', () => {
createdAt: new Date(),
labels: [toDeleteLabel],
updatedAt: new Date(),
type: HighlightType.Highlight,
}
await addHighlightToPage(page.id, highlight, ctx)
})
@ -596,6 +602,7 @@ describe('Labels API', () => {
shortId: 'test shortId',
userId: user.id,
updatedAt: new Date(),
type: HighlightType.Highlight,
}
await addHighlightToPage(page.id, highlight, ctx)
labelIds = [labels[0].id, labels[1].id]
@ -620,6 +627,7 @@ describe('Labels API', () => {
shortId: 'test shortId',
userId: user.id,
updatedAt: new Date(),
type: HighlightType.Highlight,
}
await addHighlightToPage(page.id, highlight, ctx)
labelIds = [generateFakeUuid(), generateFakeUuid()]

View file

@ -10,7 +10,12 @@ import { User } from '../../src/entity/user'
import { createTestUser, deleteTestIntegrations, deleteTestUser } from '../db'
import { Integration, IntegrationType } from '../../src/entity/integration'
import { getRepository } from '../../src/entity/utils'
import { Highlight, Page, PageContext } from '../../src/elastic/types'
import {
Highlight,
HighlightType,
Page,
PageContext,
} from '../../src/elastic/types'
import nock from 'nock'
import { READWISE_API_URL } from '../../src/services/integrations'
import { addHighlightToPage } from '../../src/elastic/highlights'
@ -142,6 +147,7 @@ describe('Integrations routers', () => {
updatedAt: new Date(),
userId: user.id,
highlightPositionPercent,
type: HighlightType.Highlight,
}
await addHighlightToPage(page.id, highlight, ctx)
// create highlights data for integration request