diff --git a/.github/workflows/backend.yml b/.github/workflows/backend.yml new file mode 100644 index 000000000..3f88ea958 --- /dev/null +++ b/.github/workflows/backend.yml @@ -0,0 +1,57 @@ +name: Backend +on: + pull_request: + branches: + - "master" + paths: + - "packages/auth/**" + - "packages/models/**" + - "packages/atlas/**" + - "services/server/**" + + workflow_dispatch: + +# @ref https://docs.github.com/en/actions/use-cases-and-examples/using-containerized-services/creating-postgresql-service-containers +jobs: + main: + runs-on: ubuntu-latest + container: node:20-bookworm-slim + services: + postgres: + image: postgres + env: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: litespace + POSTGRES_DB: test + # Set health checks to wait until postgres has started + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + steps: + - name: Check out repository code + uses: actions/checkout@v4 + - name: Setup Node + uses: actions/setup-node@v3 + with: + node-version: "20.11.1" + - name: Enable pnpm + run: corepack enable pnpm + - name: Get pnpm store directory + shell: bash + run: | + echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV + - uses: actions/cache@v3 + name: Setup pnpm cache + with: + path: ${{ env.STORE_PATH }} + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('pnpm-lock.yaml') }} + restore-keys: | + ${{ runner.os }}-pnpm-store- + - name: Install dependencies + run: pnpm install + - name: Build packages + run: pnpm build:server:pkgs + - name: Run models tests + run: pnpm models test:ci diff --git a/package.json b/package.json index a7773471c..c718cd402 100644 --- a/package.json +++ b/package.json @@ -5,16 +5,17 @@ "license": "MIT", "private": true, "scripts": { - "build:pkg:assets": "pnpm --filter @litespace/assets build", - "build:pkg:atlas": "pnpm --filter @litespace/atlas build", - "build:pkg:auth": "pnpm --filter @litespace/auth build", - "build:pkg:emails": "pnpm --filter @litespace/emails build", - "build:pkg:luna": "pnpm --filter @litespace/luna build", - "build:pkg:models": "pnpm --filter @litespace/models build", - "build:pkg:sol": "pnpm --filter @litespace/sol build", - "build:pkg:types": "pnpm --filter @litespace/types build", - "build:pkg:headless": "pnpm --filter @litespace/headless build", + "build:pkg:assets": "pnpm assets build", + "build:pkg:atlas": "pnpm atlas build", + "build:pkg:auth": "pnpm auth build", + "build:pkg:emails": "pnpm emails build", + "build:pkg:luna": "pnpm luna build", + "build:pkg:models": "pnpm models build", + "build:pkg:sol": "pnpm sol build", + "build:pkg:types": "pnpm types build", + "build:pkg:headless": "pnpm headless build", "build:pkgs": "pnpm build:pkg:assets && pnpm build:pkg:types && pnpm build:pkg:sol && pnpm build:pkg:emails && pnpm build:pkg:models && pnpm build:pkg:atlas && pnpm build:pkg:headless && pnpm build:pkg:auth && pnpm build:pkg:luna", + "build:server:pkgs": "pnpm build:pkg:types && pnpm build:pkg:sol && pnpm build:pkg:emails && pnpm build:pkg:models && pnpm build:pkg:atlas && pnpm build:pkg:auth", "watch:atlas": "pnpm build:pkg:atlas -w", "watch:auth": "pnpm auth watch", "watch:emails": "pnpm emails watch", diff --git a/packages/models/.env.test.ci b/packages/models/.env.test.ci new file mode 100644 index 000000000..2033cbca4 --- /dev/null +++ b/packages/models/.env.test.ci @@ -0,0 +1,6 @@ +DATABASE_URL=postgres://postgres:litespace@postgres:5432/test +PG_USER='postgres' +PG_PASSWORD='litespace' +PG_HOST='postgres' +PG_PORT='5432' +PG_DATABASE='test' \ No newline at end of file diff --git a/packages/models/fixtures/shared.ts b/packages/models/fixtures/shared.ts index 2d7a12639..5eee408bc 100644 --- a/packages/models/fixtures/shared.ts +++ b/packages/models/fixtures/shared.ts @@ -10,10 +10,16 @@ async function execute(command: string): Promise { }); } +const ci = !!process.env.CI; + export async function down() { - await execute("pnpm migrate:test down"); + const command = ci + ? "pnpm migrate:test:ci down" + : "pnpm migrate:test:local down"; + await execute(command); } export async function up() { - await execute("pnpm migrate:test up"); + const command = ci ? "pnpm migrate:test:ci up" : "pnpm migrate:test:local up"; + await execute(command); } diff --git a/packages/models/package.json b/packages/models/package.json index a452d717b..ab22b7532 100644 --- a/packages/models/package.json +++ b/packages/models/package.json @@ -12,16 +12,19 @@ "build": "rollup -c --bundleConfigAsCjs", "watch": "pnpm build -w", "migrate": "env-cmd -f .env node-pg-migrate", - "migrate:test": "env-cmd -f .env.test node-pg-migrate", + "migrate:test:local": "env-cmd -f .env.test node-pg-migrate", + "migrate:test:ci": "env-cmd -f .env.test.ci node-pg-migrate", "seed": "env-cmd -f .env vite-node scripts/seed.ts", "reseed": "pnpm migrate down && pnpm migrate up && pnpm seed", - "test": "env-cmd -f .env.test jest -i --runInBand --detectOpenHandles" + "test": "jest --runInBand --detectOpenHandles --forceExit", + "test:local": "env-cmd -f .env.test pnpm run test", + "test:ci": "env-cmd -f .env.test.ci pnpm run test" }, "devDependencies": { "@litespace/models": "workspace:^", + "@litespace/sol": "workspace:^", "@faker-js/faker": "^9.0.3", "@jest/globals": "^29.7.0", - "@litespace/sol": "workspace:^", "@rollup/plugin-alias": "^5.1.0", "@rollup/plugin-commonjs": "^25.0.8", "@rollup/plugin-json": "^6.1.0", diff --git a/packages/models/tests/calls.test.ts b/packages/models/tests/calls.test.ts index 1cd929e40..b61a982fc 100644 --- a/packages/models/tests/calls.test.ts +++ b/packages/models/tests/calls.test.ts @@ -6,13 +6,12 @@ import dayjs from "@/lib/dayjs"; describe(nameof(Calls), () => { beforeEach(async () => { - await flush(); + return await flush(); }); describe(nameof(calls.create), () => { it("should insert new call row", async () => { const call = await calls.create(); - expect(call.id).to.be.eq(1); expect(dayjs.utc(call.createdAt).isValid()).to.be.true; expect(dayjs.utc(call.updatedAt).isValid()).to.be.true; expect(call.processingTime).to.be.eq(null); diff --git a/packages/models/tests/interviews.test.ts b/packages/models/tests/interviews.test.ts index 2d7029534..7200078c3 100644 --- a/packages/models/tests/interviews.test.ts +++ b/packages/models/tests/interviews.test.ts @@ -6,7 +6,7 @@ import { expect } from "chai"; describe("Interviews", () => { beforeEach(async () => { - await fixtures.flush(); + return await fixtures.flush(); }); describe(nameof(interviews.find), () => { diff --git a/packages/models/tests/lessons.test.ts b/packages/models/tests/lessons.test.ts index f7d7807e3..4cd8707fd 100644 --- a/packages/models/tests/lessons.test.ts +++ b/packages/models/tests/lessons.test.ts @@ -9,7 +9,7 @@ import dayjs from "@/lib/dayjs"; describe("Lessons", () => { beforeEach(async () => { - await fixtures.flush(); + return await fixtures.flush(); }); describe(nameof(lessons.countCounterpartMembers), () => { diff --git a/packages/models/tests/messages.test.ts b/packages/models/tests/messages.test.ts index 72d467c0d..7d25be56d 100644 --- a/packages/models/tests/messages.test.ts +++ b/packages/models/tests/messages.test.ts @@ -5,7 +5,7 @@ import { expect } from "chai"; describe("Messages", () => { beforeEach(async () => { - await fixtures.flush(); + return await fixtures.flush(); }); describe(nameof(messages.countUserMessages), () => { diff --git a/packages/models/tests/ratings.test.ts b/packages/models/tests/ratings.test.ts index f3084d2ca..c0871a664 100644 --- a/packages/models/tests/ratings.test.ts +++ b/packages/models/tests/ratings.test.ts @@ -5,7 +5,7 @@ import { expect } from "chai"; describe("Ratings", () => { beforeEach(async () => { - await fixtures.flush(); + return await fixtures.flush(); }); describe(nameof(ratings.create), () => { diff --git a/packages/models/tests/rooms.test.ts b/packages/models/tests/rooms.test.ts index 4047d4d7b..22954c94b 100644 --- a/packages/models/tests/rooms.test.ts +++ b/packages/models/tests/rooms.test.ts @@ -6,7 +6,7 @@ import { IUser } from "@litespace/types"; describe("Rooms", () => { beforeEach(async () => { - await fixtures.flush(); + return await fixtures.flush(); }); describe(nameof(rooms.create), () => { diff --git a/packages/models/tests/topics.test.ts b/packages/models/tests/topics.test.ts index ba3ff748c..a6544d3ab 100644 --- a/packages/models/tests/topics.test.ts +++ b/packages/models/tests/topics.test.ts @@ -6,7 +6,7 @@ import dayjs from "@/lib/dayjs"; describe("Topics", () => { beforeEach(async () => { - await fixtures.flush(); + return await fixtures.flush(); }); describe(nameof(topics.create), () => { diff --git a/packages/models/tests/users.test.ts b/packages/models/tests/users.test.ts index 4a0ade4f3..7540fc190 100644 --- a/packages/models/tests/users.test.ts +++ b/packages/models/tests/users.test.ts @@ -24,7 +24,6 @@ describe("Users", () => { gender: IUser.Gender.Male, }); - expect(user.id).to.be.eq(1); expect(user.name).to.be.eq(name); expect(user.role).to.be.eq(IUser.Role.Student); expect(user.gender).to.be.eq(IUser.Gender.Male);