Skip to content

Commit 0bc56e8

Browse files
authored
test: configure automated server tests (CM-968) (#4156)
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
1 parent d7e9bb0 commit 0bc56e8

22 files changed

Lines changed: 1013 additions & 273 deletions

File tree

.env.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
NODE_ENV=test
2+
DB_HOST=localhost
3+
DB_PORT=5434
4+
DB_USER=postgres
5+
DB_PASSWORD=example
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
6+
# shellcheck source=scripts/utils
7+
source "${REPO_ROOT}/scripts/utils"
8+
9+
: "${DB_HOST:?DB_HOST is required}"
10+
: "${DB_PORT:?DB_PORT is required}"
11+
: "${DB_USER:?DB_USER is required}"
12+
: "${DB_PASSWORD:?DB_PASSWORD is required}"
13+
14+
say "Applying Sequin bootstrap SQL..."
15+
docker run --rm --network host \
16+
-v "${REPO_ROOT}/scripts/scaffold/sequin/postgres-docker-entrypoint-initdb.d/create-sequin-database.sql:/bootstrap.sql:ro" \
17+
-e "PGPASSWORD=${DB_PASSWORD}" \
18+
postgres:14-alpine \
19+
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres -v ON_ERROR_STOP=1 -f /bootstrap.sql
20+
21+
say "Recreating test_template..."
22+
docker run --rm --network host \
23+
-e "PGPASSWORD=${DB_PASSWORD}" \
24+
postgres:14-alpine \
25+
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres -v ON_ERROR_STOP=1 \
26+
-c "DROP DATABASE IF EXISTS test_template;" \
27+
-c "CREATE DATABASE test_template;"
28+
29+
if [[ "${SKIP_FLYWAY_BUILD:-0}" == "1" ]]; then
30+
say "Using pre-built crowd_flyway image."
31+
else
32+
say "Building flyway image..."
33+
docker build -t crowd_flyway -f "${REPO_ROOT}/backend/src/database/Dockerfile.flyway" "${REPO_ROOT}/backend/src/database"
34+
fi
35+
36+
say "Migrating test_template..."
37+
docker run --rm --network host \
38+
-e "PGHOST=${DB_HOST}" \
39+
-e "PGPORT=${DB_PORT}" \
40+
-e "PGUSER=${DB_USER}" \
41+
-e "PGPASSWORD=${DB_PASSWORD}" \
42+
-e PGDATABASE=test_template \
43+
crowd_flyway
44+
45+
say "Test template database ready."

.github/workflows/server-tests.yml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
name: Server Tests
2+
3+
on:
4+
pull_request:
5+
# Only trigger when relevant files change to save CI minutes
6+
paths-ignore:
7+
- 'frontend/**'
8+
- '**.md'
9+
- '.gitignore'
10+
- '.editorconfig'
11+
- '**/.eslintrc*'
12+
- '.prettierrc'
13+
- '.prettierignore'
14+
- 'LICENSE'
15+
16+
# Automatically cancel in-progress runs if you push new code to the same PR
17+
concurrency:
18+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
19+
cancel-in-progress: true
20+
21+
permissions:
22+
contents: read
23+
actions: write # GHA cache for Flyway image layers
24+
25+
jobs:
26+
test:
27+
runs-on: ubuntu-latest
28+
timeout-minutes: 30
29+
30+
services:
31+
postgres:
32+
image: postgres:14-alpine
33+
command: postgres -c wal_level=logical
34+
env:
35+
POSTGRES_USER: postgres
36+
POSTGRES_PASSWORD: example
37+
POSTGRES_DB: postgres
38+
ports:
39+
- 5432:5432
40+
options: >-
41+
--health-cmd "pg_isready -U postgres"
42+
--health-interval 2s
43+
--health-timeout 5s
44+
--health-retries 15
45+
46+
env:
47+
NODE_ENV: test
48+
DB_HOST: localhost
49+
DB_PORT: 5432
50+
DB_USER: postgres
51+
DB_PASSWORD: example
52+
53+
steps:
54+
- name: Check out repository code
55+
uses: actions/checkout@v4
56+
57+
- name: Setup pnpm
58+
uses: pnpm/action-setup@v4
59+
60+
- name: Setup Node
61+
uses: actions/setup-node@v4
62+
with:
63+
node-version: 20
64+
cache: 'pnpm'
65+
66+
- name: Install dependencies
67+
run: pnpm i --frozen-lockfile
68+
69+
- name: Set up Docker Buildx
70+
uses: docker/setup-buildx-action@v3
71+
72+
- name: Build Flyway image
73+
uses: docker/build-push-action@v6
74+
with:
75+
context: backend/src/database
76+
file: backend/src/database/Dockerfile.flyway
77+
load: true
78+
tags: crowd_flyway
79+
cache-from: type=gha,scope=flyway
80+
cache-to: type=gha,mode=max,scope=flyway
81+
82+
- name: Prepare test database
83+
env:
84+
SKIP_FLYWAY_BUILD: 1
85+
run: ./.github/scripts/prepare-test-template-db.sh
86+
87+
- name: Run server tests
88+
run: pnpm test:server

backend/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
"dotenv": "8.2.0",
9595
"dotenv-expand": "^8.0.3",
9696
"emoji-dictionary": "^1.0.11",
97-
"erlpack": "^0.1.4",
9897
"express": "4.17.1",
9998
"express-oauth2-jwt-bearer": "^1.7.4",
10099
"express-rate-limit": "6.5.1",
@@ -133,7 +132,6 @@
133132
"uuid": "^9.0.0",
134133
"validator": "^13.7.0",
135134
"verify-github-webhook": "^1.0.1",
136-
"zlib-sync": "^0.1.8",
137135
"zod": "^4.3.6"
138136
},
139137
"private": true,

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44
"node": ">=20.0.0"
55
},
66
"scripts": {
7-
"prepare": "husky"
7+
"prepare": "husky",
8+
"test": "vitest run",
9+
"test:server": "vitest run --project server",
10+
"test:changed": "vitest --changed",
11+
"test:watch": "vitest"
812
},
913
"devDependencies": {
1014
"@commitlint/cli": "^19.8.0",
1115
"@commitlint/config-conventional": "^19.8.0",
12-
"husky": "^9.1.7"
16+
"husky": "^9.1.7",
17+
"vite": "6.3.5",
18+
"vitest": "4.1.7"
1319
},
1420
"packageManager": "pnpm@9.11.0+sha512.0a203ffaed5a3f63242cd064c8fb5892366c103e328079318f78062f24ea8c9d50bc6a47aa3567cabefd824d170e78fa2745ed1f16b132e16436146b7688f19b"
1521
}

0 commit comments

Comments
 (0)