Jelajahi Sumber

Add acceptance test to notifications

GitOrigin-RevId: de2d0ed8cab15c4347ddecf227073a6d4e06120f
Andrew Rumble 11 bulan lalu
induk
melakukan
aef9639405

+ 6 - 3
services/notifications/app.js

@@ -69,6 +69,9 @@ try {
   process.exit(1)
 }
 
-app.listen(port, host, () =>
-  logger.debug({}, `notifications starting up, listening on ${host}:${port}`)
-)
+if (import.meta.url === `file://${process.argv[1]}`) {
+  app.listen(port, host, () =>
+    logger.debug({}, `notifications starting up, listening on ${host}:${port}`)
+  )
+}
+export default app

+ 1 - 0
services/notifications/buildscript.txt

@@ -6,5 +6,6 @@ notifications
 --esmock-loader=False
 --node-version=22.18.0
 --public-repo=True
+--test-acceptance-vitest=True
 --test-unit-vitest=True
 --tsconfig-extra-includes=vitest.config.unit.cjs,vitest.config.acceptance.cjs

+ 1 - 0
services/notifications/docker-compose.ci.yml

@@ -33,6 +33,7 @@ services:
       NODE_OPTIONS: "--unhandled-rejections=strict"
     volumes:
       - ../../bin/shared/wait_for_it:/overleaf/bin/shared/wait_for_it
+      - ../../tsconfig.backend.json:/overleaf/tsconfig.backend.json
     depends_on:
       mongo:
         condition: service_started

+ 1 - 0
services/notifications/docker-compose.yml

@@ -32,6 +32,7 @@ services:
       - ../../node_modules:/overleaf/node_modules
       - ../../libraries:/overleaf/libraries
       - ../../bin/shared/wait_for_it:/overleaf/bin/shared/wait_for_it
+      - ../../tsconfig.backend.json:/overleaf/tsconfig.backend.json
     working_dir: /overleaf/services/notifications
     environment:
       ELASTIC_SEARCH_DSN: es:9200

+ 4 - 4
services/notifications/package.json

@@ -6,13 +6,13 @@
   "type": "module",
   "scripts": {
     "start": "node app.js",
-    "test:acceptance:_run": "mocha --recursive --reporter spec --timeout 15000 --exit $@ test/acceptance/js",
-    "test:acceptance": "npm run test:acceptance:_run -- --grep=$MOCHA_GREP",
+    "test:acceptance:_run": "vitest --config ./vitest.config.acceptance.cjs",
+    "test:acceptance": "npm run test:acceptance:_run",
     "test:unit:_run": "vitest --config ./vitest.config.unit.cjs",
     "test:unit": "npm run test:unit:_run",
     "lint": "eslint --max-warnings 0 --format unix .",
-    "format": "prettier --list-different $PWD/'**/{*.*js,Jenkinsfile}'",
-    "format:fix": "prettier --write $PWD/'**/{*.*js,Jenkinsfile}'",
+    "format": "prettier --list-different $PWD/'**/{*.*js, *.*ts,Jenkinsfile}'",
+    "format:fix": "prettier --write $PWD/'**/{*.*js,*.*ts,Jenkinsfile}'",
     "lint:fix": "eslint --fix .",
     "types:check": "tsc --noEmit",
     "nodemon": "node --watch app.js"

+ 31 - 0
services/notifications/test/acceptance/js/HealthCheck.test.ts

@@ -0,0 +1,31 @@
+import { beforeAll, describe, it, expect } from 'vitest'
+import { fetchStringWithResponse } from '@overleaf/fetch-utils'
+import app from '../../../app.js'
+import logger from '@overleaf/logger'
+
+let runAppPromise: Promise<void> | null = null
+
+async function ensureRunning(hostname: string, port: number) {
+  if (!runAppPromise) {
+    runAppPromise = new Promise(resolve => {
+      app.listen(port, hostname, () => {
+        logger.info({ port, hostname }, 'notifications running in dev mode')
+
+        resolve()
+      })
+    })
+  }
+  await runAppPromise
+}
+
+describe('HealthCheck endpoint', () => {
+  beforeAll(async () => {
+    await ensureRunning('127.0.0.1', 3042)
+  })
+  it('should return 200 for GET /health_check', async () => {
+    const { response } = await fetchStringWithResponse(
+      'http://localhost:3042/health_check'
+    )
+    expect(response.status).toBe(200)
+  })
+})

+ 8 - 0
services/notifications/vitest.config.acceptance.cjs

@@ -0,0 +1,8 @@
+const { defineConfig } = require('vitest/config')
+
+module.exports = defineConfig({
+  test: {
+    include: ['test/acceptance/js/**/*.test.{js,ts}'],
+    isolate: false,
+  },
+})