yorkie-team / codepair

Build your own AI-powered collaborative markdown editor in just 5 minutes
https://codepair.yorkie.dev
Apache License 2.0
46 stars 19 forks source link

Add build tests for Frontend / Backend to CI (GitHub Actions) #247

Closed devleejb closed 2 weeks ago

devleejb commented 1 month ago

What would you like to be added: Currently, our CI setup using GitHub Actions does not include build tests for both Frontend and Backend applications, making it difficult to determine if the build is successful or not.

To improve the CI pipeline, it is recommended to add build tests for both the Frontend and Backend applications. This will allow us to validate the build process and catch any potential errors early on.

Why is this needed: Adding build tests for both the Frontend and Backend applications in our CI pipeline will help ensure the stability and quality of our codebase. It will provide a more comprehensive testing environment and help prevent issues from being deployed to production.

having-dlrow commented 1 month ago

Could I work on this issue?

devleejb commented 1 month ago

@having-dlrow Sure! This seems to be a good issue to understand CodePair's CI!

having-dlrow commented 1 month ago

Add tests for backend failed.

I plan to add tasks as shown below.

1.Build was successful.2.Test failed due to missing environment variables.

  1. Build

    - name: Run build
    run: npm run build
    working-directory: ${{ env.working-directory }}
  2. Test

    - name: Run test
    run: npm run test

2-1. Test Fail Log

Test Suites: 20 failed, 20 total
Tests:       20 failed, 20 total
Snapshots:   0 total
Time:        8.534 s
Ran all test suites.

    22 afterEach(async () => {
  → 23  const deleteWorkspaces = prismaService.workspace.deleteMany(
  error: Environment variable not found: DATABASE_URL.
    -->  schema.prisma:10
      | 
    9 |   provider = "mongodb"
  10 |   url      = env("DATABASE_URL")
      | 

  Validation Error Count: 1

For more detailed error information, please refer to https://github.com/having-dlrow/codepair/actions/runs/10114844891.


I think there are three possible solutions:

  1. Using an in-memory database when in a Node.js CI environment.

      // Example
      const Datastore = require('nedb');
      const mongoose = require('mongoose');
    
      let db;
    
      if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'ci') {
        // NeDB setup
        db = new Datastore({ filename: 'datafile', autoload: true });
      } else {
        // MongoDB setup
        mongoose.connect(process.env.DATABASE_URL, {
          useNewUrlParser: true,
          useUnifiedTopology: true,
        });
        db = mongoose.connection;
        db.on('error', console.error.bind(console, 'connection error:'));
        db.once('open', function() {
          console.log('Connected to MongoDB');
        });
      }
    
      module.exports = db;
  2. Installing MongoDB instance on the runner

      jobs:
        test:
          name: Check the source code
          runs-on: ubuntu-latest
          services:
            mongodb:
              image: mongo:latest
              ports:
                - 27017:27017
              options: >-
                --health-cmd "mongo --eval 'db.runCommand({ ping: 1 })'"
                --health-interval 10s
                --health-timeout 5s
                --health-retries 5
          strategy:
            matrix:
              node-version: [18.x]
          env:
            working-directory: ./backend
            DATABASE_URL: mongodb://localhost:27017/testdb
  3. Providing a CI environment (MongoDB) and an env.ci file

Using GitHub secrets to inject values. However, a MongoDB connection endpoint is required for the CI environment.


  # Method of replacing keys
  - name: Replace environment variables in .env.development
    run: |
      sed -i 's/^DATABASE_URL=.*/DATABASE_URL=${{ secrets.DATABASE_URL }}/g' .env.development
      sed -i 's/^OPENAI_API_KEY=.*/OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }}/g' .env.development

  # Method of replacing files          
  - name: Make .env.development
    run: |
      touch .env.development
      echo "${{ secrets.env }}" >> .env.development
    shell: bash

if anyone has other solutions or opinions, I would be grateful to hear them. @devleejb

devleejb commented 1 month ago

@having-dlrow It seems that we need to discuss the test environment. I think it is out of scope.

How about we only add build test to this issue and create a new issue for the test environment? (If you are interested in the new issue, would you enroll the issue? I like your approaches.)

I also have a idea. I think we can handle the test environment using docker. In yorkie, docker container runs for using mongodb.

https://github.com/yorkie-team/yorkie/blob/52d2732e4a9892db3c1dc91cc033edd004722e1b/.github/workflows/ci.yml#L49-L50

devleejb commented 3 weeks ago

@having-dlrow Could you share your progress?