From c51e3720171cc24e7fb771ec25c9ac2c8a10fc90 Mon Sep 17 00:00:00 2001 From: arsam Date: Fri, 18 Sep 2026 23:09:43 +0330 Subject: [PATCH] Import tests: backend/tests/integration/config-validator Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality. --- .../integration/config-validator.test.ts | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 backend/tests/integration/config-validator.test.ts diff --git a/backend/tests/integration/config-validator.test.ts b/backend/tests/integration/config-validator.test.ts new file mode 100644 index 0000000..52342a0 --- /dev/null +++ b/backend/tests/integration/config-validator.test.ts @@ -0,0 +1,76 @@ +import { describe, it, expect } from 'vitest'; +import { spawnSync } from 'child_process'; +import path from 'path'; + +describe('Config Validator & Invalid Connections', () => { + const backendDir = path.resolve(__dirname, '../../'); + + it('should fail validation when DATABASE_URL is missing', () => { + const res = spawnSync('npx', ['tsx', 'src/config/env.ts'], { + cwd: backendDir, + env: { + ...process.env, + DATABASE_URL: '', + JWT_SECRET: 'supersecretjwtkeyforcoaxyspanelbackendproject12345', + }, + }); + + expect(res.status).toBe(1); + const stderr = res.stderr.toString(); + expect(stderr).toContain('Invalid environment configuration'); + expect(stderr).toContain('DATABASE_URL'); + }); + + it('should fail validation when DATABASE_URL is not a valid URL', () => { + const res = spawnSync('npx', ['tsx', 'src/config/env.ts'], { + cwd: backendDir, + env: { + ...process.env, + DATABASE_URL: 'invalid-url-format', + JWT_SECRET: 'supersecretjwtkeyforcoaxyspanelbackendproject12345', + }, + }); + + expect(res.status).toBe(1); + const stderr = res.stderr.toString(); + expect(stderr).toContain('Invalid environment configuration'); + expect(stderr).toContain('DATABASE_URL'); + }); + + it('should fail validation when JWT_SECRET is too short', () => { + const res = spawnSync('npx', ['tsx', 'src/config/env.ts'], { + cwd: backendDir, + env: { + ...process.env, + DATABASE_URL: 'postgresql://coaxys_panel:coaxys_pass@127.0.0.1:5432/coaxys_panel?schema=public', + JWT_SECRET: 'short', + }, + }); + + expect(res.status).toBe(1); + const stderr = res.stderr.toString(); + expect(stderr).toContain('Invalid environment configuration'); + expect(stderr).toContain('JWT_SECRET'); + }); + + it('should propagate connection error when using an invalid database port', () => { + // Attempt to start index.ts with an invalid DB port, which should cause connection to fail on startup. + // We set PORT to a random high port so it doesn't conflict, and invalid database port with connection timeout. + const res = spawnSync('npx', ['tsx', 'src/index.ts'], { + cwd: backendDir, + env: { + ...process.env, + PORT: '14002', + DATABASE_URL: 'postgresql://coaxys_panel:coaxys_pass@127.0.0.1:9999/coaxys_panel?schema=public&connect_timeout=2', + JWT_SECRET: 'supersecretjwtkeyforcoaxyspanelbackendproject12345', + }, + timeout: 10000, // Timeout after 10s if it hangs + }); + + // It should exit with 1 because the database connection fails on startup + expect(res.status).toBe(1); + const stderr = res.stderr.toString() || res.stdout.toString(); + // Verify connection error details are propagated + expect(stderr).toContain('Can\'t reach database server at'); + }); +});