Files
omnivore/packages/api/test/resolvers/labels.test.ts
Jackson Harper e652a6ea8c Rebased version of the elastic PR (#225)
* Add elastic to our docker compose

* add AND/OR/NOT search operations

* add elastic and create article in elastic

* change error code when elastic throws error

* add search pages in elastic

* add search by labels

* Add elastic to GitHub Action

* Update elastic version

* Fix port for elastic

* add url in search query

* Set elastic features when running tests

* add debug logs

* Use localhost instead of service hostname

* refresh elastic after create/update

* update search labels query

* add typescript support

* search pages in elastic

* fix search queries

* use elastic for saving page

* fix test failure

* update getArticle api to use elastic

* use generic get page function

* add elastic migration python script

* fix bulk helper param

* save elastic page id in article_saving_request instead of postgres article_id

* fix page archiving and deleting

* add tests for deleteArticle

* remove custom date type in elastic mappings which not exist in older version of elastic

* fix timestamp format issue

* add tests for save reading progress

* add tests for save file

* optimize search results

* add alias to index

* update migration script to receive env var as params

* Add failing test to validate we don't decrease reading progress

This test is failing with Elastic because we aren't fetching
the reading progress from elastic here, and are fetching it
from postgres.

* Rename readingProgress to readingProgressPercent

This is the name stored in elastic, so fixes issues pulling the
value out.

* Linting

* Add failing test for creating highlights w/elastic

This test fails because the highlight can't be looked up. Is there
a different ID we should be passing in to query for highlights,
or do we need to update the query to look for elastic_id?

* add tests code coverage threshold

* update nyc config

* include more files in test coverage

* change alias name

* update updateContent to update pages in elastic

* remove debug log

* fix createhighlight test

* search pages by alias in elastic

* update set labels and delete labels in elastic

* migration script enumeration

* make BULK_SIZE an env var

* fix pdf search indexing

* debug github action exit issue

* call pubsub when create/update/delete page in elastic

* fix json parsing bug and reduce reading data from file

* replace a depreciated pubsub api call

* debug github action exit issue

* debug github action exit issue

* add handler to upload elastic page data to GCS

* fix tests

* Use http_auth instead of basic_auth

* add index creation and existing postgres tables update in migration script

* fix a typo to connect to elastic

* rename readingProgress to readingProgressPercent

* migrate elastic_page_id in highlights and article_saving_request tables

* update migration script to include number of updated rows

* update db migration query

* read index mappings from file

* fix upload pages to gcs

* fix tests failure due to pageContext

* fix upload file id not exist error

* Handle savedAt & isArchived attributes w/out quering elastic

* Fix prettier issues

* fix content-type mismatching

* revert pageId to linkId because frontend was not deployed yet

* fix newsletters and attachment not saved in elastic

* put linkId in article for setting labels

* exclude orginalHtml in the result of searching to improve performace

* exclude content in the result of searching to improve performace

* remove score sorting

* do not refresh immediately to reduce searching and indexing time

* do not replace the backup data in gcs

* fix no article id defined in articleSavingRequest

* add logging of elastic api running time

* reduce home feed pagination size to 15

* reduce home feed pagination size to 10

* stop revalidating first page

* do not use a separate api to fetch reading progress

* Remove unused comment

* get reading progress if not exists

* replace ngram tokenizer with standard tokenizer

* fix tests

* remove .env.local

* add sort keyword in searching to sort by score

Co-authored-by: Hongbo Wu <hongbo@omnivore.app>
2022-03-16 12:08:59 +08:00

321 lines
8.0 KiB
TypeScript

import { createTestLabel, createTestUser, deleteTestUser } from '../db'
import {
createTestElasticPage,
generateFakeUuid,
graphqlRequest,
request,
} from '../util'
import { Label } from '../../src/entity/label'
import { expect } from 'chai'
import { getRepository } from 'typeorm'
import 'mocha'
import { User } from '../../src/entity/user'
import { Page } from '../../src/elastic/types'
import { getPageById } from '../../src/elastic'
describe('Labels API', () => {
const username = 'fakeUser'
let user: User
let authToken: string
let page: Page
let labels: Label[]
before(async () => {
// create test user and login
user = await createTestUser(username)
const res = await request
.post('/local/debug/fake-user-login')
.send({ fakeEmail: user.email })
authToken = res.body.authToken
// create testing labels
const label1 = await createTestLabel(user, 'label_1', '#ffffff')
const label2 = await createTestLabel(user, 'label_2', '#eeeeee')
labels = [label1, label2]
// create a page with label
const existingLabelOfLink = await createTestLabel(
user,
'different_label',
'#dddddd'
)
page = await createTestElasticPage(user, [existingLabelOfLink])
})
after(async () => {
// clean up
await deleteTestUser(username)
})
describe('GET labels', () => {
let query: string
beforeEach(() => {
query = `
query {
labels {
... on LabelsSuccess {
labels {
id
name
color
description
createdAt
}
}
... on LabelsError {
errorCodes
}
}
}
`
})
it('should return labels', async () => {
const res = await graphqlRequest(query, authToken).expect(200)
const labels = await getRepository(Label).find({ where: { user } })
expect(res.body.data.labels.labels).to.eql(
labels.map((label) => ({
id: label.id,
name: label.name,
color: label.color,
description: label.description,
createdAt: new Date(label.createdAt.setMilliseconds(0)).toISOString(),
}))
)
})
it('responds status code 400 when invalid query', async () => {
const invalidQuery = `
query {
labels {}
}
`
return graphqlRequest(invalidQuery, authToken).expect(400)
})
it('responds status code 500 when invalid user', async () => {
const invalidAuthToken = 'Fake token'
return graphqlRequest(query, invalidAuthToken).expect(500)
})
})
describe('Create label', () => {
let query: string
let name: string
beforeEach(() => {
query = `
mutation {
createLabel(
input: {
color: "#ffffff"
name: "${name}"
}
) {
... on CreateLabelSuccess {
label {
id
name
}
}
... on CreateLabelError {
errorCodes
}
}
}
`
})
context('when name not exists', () => {
before(() => {
name = 'label3'
})
it('should create label', async () => {
const res = await graphqlRequest(query, authToken).expect(200)
const label = await getRepository(Label).findOne(
res.body.data.createLabel.label.id
)
expect(label).to.exist
})
})
context('when name exists', () => {
before(() => {
name = labels[0].name
})
it('should return error code LABEL_ALREADY_EXISTS', async () => {
const res = await graphqlRequest(query, authToken).expect(200)
expect(res.body.data.createLabel.errorCodes).to.eql([
'LABEL_ALREADY_EXISTS',
])
})
})
it('responds status code 400 when invalid query', async () => {
const invalidQuery = `
mutation {
createLabel {}
}
`
return graphqlRequest(invalidQuery, authToken).expect(400)
})
it('responds status code 500 when invalid user', async () => {
const invalidAuthToken = 'Fake token'
return graphqlRequest(query, invalidAuthToken).expect(500)
})
})
describe('Delete label', () => {
let query: string
let labelId: string
beforeEach(() => {
query = `
mutation {
deleteLabel(id: "${labelId}") {
... on DeleteLabelSuccess {
label {
id
name
}
}
... on DeleteLabelError {
errorCodes
}
}
}
`
})
context('when label exists', () => {
before(async () => {
const toDeleteLabel = await createTestLabel(user, 'label4', '#ffffff')
labelId = toDeleteLabel.id
})
it('should delete label', async () => {
await graphqlRequest(query, authToken).expect(200)
const label = await getRepository(Label).findOne(labelId)
expect(label).to.not.exist
})
})
context('when label not exist', () => {
before(() => {
labelId = generateFakeUuid()
})
it('should return error code NOT_FOUND', async () => {
const res = await graphqlRequest(query, authToken).expect(200)
expect(res.body.data.deleteLabel.errorCodes).to.eql(['NOT_FOUND'])
})
})
it('responds status code 400 when invalid query', async () => {
const invalidQuery = `
mutation {
deleteLabel {}
}
`
return graphqlRequest(invalidQuery, authToken).expect(400)
})
it('responds status code 500 when invalid user', async () => {
const invalidAuthToken = 'Fake token'
return graphqlRequest(query, invalidAuthToken).expect(500)
})
})
describe('Set labels', () => {
let query: string
let pageId: string
let labelIds: string[] = []
beforeEach(() => {
query = `
mutation {
setLabels(
input: {
linkId: "${pageId}",
labelIds: [
"${labelIds[0]}",
"${labelIds[1]}"
]
}
) {
... on SetLabelsSuccess {
labels {
id
name
}
}
... on SetLabelsError {
errorCodes
}
}
}
`
})
context('when labels exists', () => {
before(() => {
pageId = page.id
labelIds = [labels[0].id, labels[1].id]
})
it('should set labels', async () => {
await graphqlRequest(query, authToken).expect(200)
const page = await getPageById(pageId)
expect(page?.labels?.map((l) => l.id)).to.eql(labelIds)
})
})
context('when labels not exist', () => {
before(() => {
pageId = page.id
labelIds = [generateFakeUuid(), generateFakeUuid()]
})
it('should return error code NOT_FOUND', async () => {
const res = await graphqlRequest(query, authToken).expect(200)
expect(res.body.data.setLabels.errorCodes).to.eql(['NOT_FOUND'])
})
})
context('when link not exist', () => {
before(() => {
pageId = generateFakeUuid()
labelIds = [labels[0].id, labels[1].id]
})
it('should return error code NOT_FOUND', async () => {
const res = await graphqlRequest(query, authToken).expect(200)
expect(res.body.data.setLabels.errorCodes).to.eql(['NOT_FOUND'])
})
})
it('responds status code 400 when invalid query', async () => {
const invalidQuery = `
mutation {
setLabels {}
}
`
return graphqlRequest(invalidQuery, authToken).expect(400)
})
it('responds status code 500 when invalid user', async () => {
const invalidAuthToken = 'Fake token'
return graphqlRequest(query, invalidAuthToken).expect(500)
})
})
})