Skip to content
This repository was archived by the owner on Jan 29, 2026. It is now read-only.

[WIP] Add automated API test suite - #89

Draft
clduab11 with Copilot wants to merge 1 commit into
mainfrom
copilot/implement-api-test-suite
Draft

[WIP] Add automated API test suite#89
clduab11 with Copilot wants to merge 1 commit into
mainfrom
copilot/implement-api-test-suite

Conversation

Copilot AI commented Oct 27, 2025

Copy link
Copy Markdown
Contributor

Thanks for assigning this issue to me. I'm starting to work on it and will keep this PR's description up to date as I form a plan and make progress.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Testing] Implement Automated API Test Suite</issue_title>
<issue_description>## 🧪 Priority: LOW - Nice to Have

Background

The new backend API infrastructure introduced in PR #66 lacks automated tests. While the code is well-structured with comprehensive JSDoc comments, automated tests are essential for catching regressions, validating behavior, and enabling confident refactoring.

Current State - No Test Coverage

No test files exist for:

  • API controllers (WorkflowController.js, StoreController.js)
  • API services (WorkflowService.js, StoreService.js)
  • Middleware (auth.js, rateLimit.js, validation.js, errorHandler.js)
  • Database layer (database.js)
  • WebSocket server (websocket/server.js)

Recommended Solution

Part 1: Test Infrastructure Setup

npm install --save-dev jest supertest @types/jest @types/supertest
// package.json
{
  "scripts": {
    "test": "NODE_ENV=test jest",
    "test:watch": "NODE_ENV=test jest --watch",
    "test:coverage": "NODE_ENV=test jest --coverage"
  },
  "jest": {
    "testEnvironment": "node",
    "coverageDirectory": "coverage",
    "collectCoverageFrom": [
      "backend/src/**/*.js",
      "!backend/src/**/*.test.js"
    ],
    "testMatch": [
      "**/__tests__/**/*.js",
      "**/*.test.js"
    ],
    "setupFilesAfterEnv": ["<rootDir>/backend/test/setup.js"]
  }
}

Part 2: Test Setup and Utilities

// backend/test/setup.js
import fs from 'fs/promises';
import path from 'path';

const TEST_DATA_DIR = path.join(process.cwd(), '.data-test');

// Setup test environment
beforeAll(async () => {
  process.env.NODE_ENV = 'test';
  process.env.API_KEY = 'test-api-key-for-automated-tests';
  
  // Create test data directory
  await fs.mkdir(TEST_DATA_DIR, { recursive: true });
});

// Cleanup after all tests
afterAll(async () => {
  // Clean up test data
  await fs.rm(TEST_DATA_DIR, { recursive: true, force: true });
});

// Clear data between tests
beforeEach(async () => {
  // Clear test database files
  const files = ['workflows.json', 'store-state.json', 'sessions.json'];
  for (const file of files) {
    const filePath = path.join(TEST_DATA_DIR, file);
    try {
      await fs.unlink(filePath);
    } catch {
      // File might not exist, ignore
    }
  }
});
// backend/test/helpers.js
import request from 'supertest';

export const API_KEY = 'test-api-key-for-automated-tests';

export function createAuthenticatedRequest(app) {
  return {
    get: (url) => request(app).get(url).set('X-API-Key', API_KEY),
    post: (url) => request(app).post(url).set('X-API-Key', API_KEY),
    put: (url) => request(app).put(url).set('X-API-Key', API_KEY),
    delete: (url) => request(app).delete(url).set('X-API-Key', API_KEY)
  };
}

export function createTestWorkflow(overrides = {}) {
  return {
    metadata: {
      id: `test-${Date.now()}`,
      name: 'Test Workflow',
      description: 'Test workflow for automated tests',
      version: '1.0.0',
      ...overrides.metadata
    },
    nodes: overrides.nodes || [
      { id: 'node1', type: 'input', data: { label: 'Input' } }
    ],
    edges: overrides.edges || [],
    ...overrides
  };
}

Part 3: Workflow API Tests

// backend/src/api/routes/__tests__/workflows.test.js
import request from 'supertest';
import app from '../../../server.js';
import * as db from '../../../db/database.js';
import { createAuthenticatedRequest, createTestWorkflow } from '../../../../test/helpers.js';

describe('Workflow API', () => {
  let api;
  
  beforeAll(async () => {
    await db.initialize();
    api = createAuthenticatedRequest(app);
  });
  
  describe('POST /api/workflows', () => {
    it('should create a new workflow', async () => {
      const workflow = createTestWorkflow();
      
      const res = await api.post('/api/workflows').send(workflow).expect(201);
      
      expect(res.body.success).toBe(true);
      expect(res.body.data.metadata.id).toBeDefined();
      expect(res.body.data.metadata.name).toBe('Test Workflow');
    });
    
    it('should reject workflow without authentication', async () => {
      const workflow = createTestWorkflow();
      
      await request(app)
        .post('/api/workflows')
        .send(workflow)
        .expect(401);
    });
    
    it('should reject invalid workflow data', async () => {
      const invalidWorkflow = { name: 'Invalid' }; // Missing required fields
      
      const res = await api.post('/api/workflows').send(invalidWorkflow).expect(400);
      
      expect(res.body.error).toBeDefined();
    });
  });
  
  describe('GET /api/workflows', () => {
    beforeEach(async () => {
      // Create test workflows
      await api.post('/api/workflows').send(createTestWorkflow({ metadata: { name: 'Workflow 1' } }));
      await api.post('/api/workflows').send(createTestWorkflow(...

</details>

- Fixes clduab11/gemini-flow#79

<!-- START COPILOT CODING AGENT TIPS -->
---

✨ Let Copilot coding agent [set things up for you](https://github.com/clduab11/gemini-flow/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)  coding agent works faster and does higher quality work when set up for your repo.

@coderabbitai

coderabbitai Bot commented Oct 27, 2025

Copy link
Copy Markdown
Contributor

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

documentation Improvements or additions to documentation enhancement New feature or request gen/qol improves General code improvements and cleanup

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants