Commit 4f9049c4 authored by Jure's avatar Jure

test(job-xsweet): initial test for job-xsweet

parent ef496e13
Pipeline #13055 failed with stages
in 6 minutes and 57 seconds
......@@ -128,7 +128,10 @@ test:
script:
- cd ${HOME}
# specify host here else it confuses the linked postgres image
- docker build -t pubsweet/job-xsweet components/server/job-xsweet
- docker run -e DATABASE_URL="postgres://test:pw@postgres/test" --name job-xsweet pubsweet/job-xsweet
- PGHOST=postgres yarn test
- docker stop job-xsweet
# if tests pass we will push latest, labelled with current commit hash
push:latest:
......
const path = require('path')
module.exports = {
'pubsweet-server': {
db: {
database: 'test',
},
pool: { min: 0, max: 10, idleTimeoutMillis: 1000 },
ignoreTerminatedConnectionError: true,
port: 4000,
secret: 'test',
sse: false,
},
pubsweet: {
components: [
'@pubsweet/model-user',
'@pubsweet/model-team',
path.join(__dirname, '../src'),
],
},
authsome: {
mode: 'pubsweet-server/test/helpers/authsome_mode',
},
}
......@@ -30,14 +30,11 @@ const resolvers = {
logger.info(pubsubChannel, status)
if (status === 'Conversion complete') {
await waait(1000)
const job = await db('pgboss.job').whereRaw(
"data->'request'->>'id' = ?",
[queueJobId],
)
// console.log(job, queueJobId, db)
pubsub.publish(pubsubChannel, {
docxToHTMLJob: {
status: 'Result',
html: job[0].data.response.html,
status: 'Done',
id: queueJobId,
},
})
}
......@@ -89,6 +86,18 @@ const resolvers = {
}
},
},
Query: {
docxToHTMLJob: async (_, { jobId }, context) => {
const job = await db('pgboss.job').whereRaw(
"data->'request'->>'id' = ?",
[jobId],
)
return {
status: 'Final',
html: job[0].data.response.html,
}
},
},
Subscription: {
docxToHTMLJob: {
subscribe: async (_, { jobId }, context) => {
......@@ -109,6 +118,10 @@ const typeDefs = `
docxToHTMLJob(jobId: String!): DocxToHTMLJob!
}
extend type Query {
docxToHTMLJob(jobId: String!): DocxToHTMLJob!
}
type DocxToHTMLJob {
id: String
status: String!
......
const path = require('path')
process.env.NODE_CONFIG_DIR = path.resolve(__dirname, '..', 'config')
const WebSocket = require('ws')
Object.assign(global, {
WebSocket,
})
// const { model: User } = require('@pubsweet/model-user')
// const { fixtures } = require('@pubsweet/model-user/test')
// const cleanDB = require('pubsweet-server/test/helpers/db_cleaner')
// const api = require('pubsweet-server/test/helpers/api')
const authentication = require('pubsweet-server/src/authentication')
const { startServer } = require('pubsweet-server')
const fs = require('fs')
const path = require('path')
const { SubscriptionClient } = require('subscriptions-transport-ws')
const superagent = require('superagent')
jest.setTimeout(60000)
describe('XSweet job', () => {
let token
// let user
let server
beforeEach(async () => {
// await cleanDB()
server = await startServer()
// user = await new User(fixtures.user).save()
token = authentication.token.create({ id: 1, username: 'test' })
})
afterAll(done => server.close(done))
it('can process it', async done => {
const { body } = await superagent
.post('http://localhost:4000/graphql')
.field(
'operations',
JSON.stringify({
operationName: null,
variables: { file: null },
query: `mutation createDocxToHTMLJob($file: Upload!) {
createDocxToHTMLJob(file: $file) {
status
id
}
}`,
}),
)
.field('map', JSON.stringify({ '0': ['variables.file'] }))
.attach(
'0',
fs.readFileSync(path.join(__dirname, '../src/test.docx')),
'test.docx',
)
.set('Authorization', `Bearer ${token}`)
expect(body.data.createDocxToHTMLJob.status).toBe('Uploading file')
const client = new SubscriptionClient(`ws://localhost:4000/subscriptions`, {
connectionParams: {
authToken: token,
},
})
// client.onConnected(() => {
// // console.log('HELLLO!!!')
// })
// client.onError(e => console.log(e))
const subscriptionPromise = new Promise((resolve, reject) => {
client
.request({
query: `subscription docxToHTMLJob($jobId: String!) {
docxToHTMLJob(jobId: $jobId) {
id
status
html
}
}
`,
variables: {
jobId: body.data.createDocxToHTMLJob.id,
},
})
.subscribe({
next: async res => {
// console.log(res)
if (res.data.docxToHTMLJob.status === 'Done') {
const { body } = await superagent
.post('http://localhost:4000/graphql')
.set('Content-Type', 'application/json')
.set('Authorization', `Bearer ${token}`)
.send({
variables: { jobId: res.data.docxToHTMLJob.id },
query: `query docxToHTMLJob($jobId: String!) {
docxToHTMLJob(jobId: $jobId) {
status
html
}
}`,
})
expect(body.data.docxToHTMLJob.html).toMatch(/Test/)
resolve(true)
}
return true
},
error: reject,
})
})
await subscriptionPromise
client.unsubscribeAll()
done()
})
})
......@@ -196,6 +196,15 @@
"<rootDir>/test/jest-setup.js"
],
"testEnvironment": "jest-environment-db"
},
{
"rootDir": "<rootDir>/components/server/job-xsweet",
"displayName": "job-xsweet",
"testRegex": "/test/.*_test.js$",
"setupFilesAfterEnv": [
"<rootDir>/test/jest-setup.js"
],
"testEnvironment": "node"
}
]
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment