|
1 | 1 | import { FastifyInstance } from 'fastify'; |
2 | | -import { createTestServer } from '../src/test/testServer'; // Use simplified test server |
| 2 | +import { createServer } from '../src/server'; // Use main server |
3 | 3 | import * as fs from 'fs-extra'; |
4 | 4 | import * as path from 'path'; |
5 | 5 | import { setTestContext } from './testContext'; |
6 | 6 |
|
7 | 7 | const TEST_PORT = 3002; // Use a different port for testing |
8 | | -const PERSISTENT_DATA_PATH = path.join(__dirname, '..', 'persistent_data'); |
9 | | -const DB_FILE_PATH = path.join(PERSISTENT_DATA_PATH, 'database', 'deploystack.db'); |
10 | | -const DB_SELECTION_PATH = path.join(PERSISTENT_DATA_PATH, 'db.selection.json'); |
| 8 | +const PERSISTENT_DATA_PATH = path.join(__dirname, '..', 'persistent_data'); // This is services/backend/persistent_data |
| 9 | +const TEST_DB_FILE_PATH = path.join(PERSISTENT_DATA_PATH, 'database', 'deploystack.test.db'); // Test-specific DB |
| 10 | +const TEST_DB_SELECTION_PATH = path.join(PERSISTENT_DATA_PATH, 'db.selection.test.json'); // Test-specific selection |
11 | 11 |
|
12 | 12 | export default async function globalSetup() { |
13 | 13 | try { |
14 | 14 | // Set environment variables for testing |
15 | 15 | process.env.NODE_ENV = 'test'; |
16 | 16 | process.env.PORT = String(TEST_PORT); |
17 | 17 | process.env.DEPLOYSTACK_ENCRYPTION_SECRET = 'test-super-secret-key-for-jest'; |
| 18 | + process.env.COOKIE_SECRET = 'test-cookie-secret-for-jest'; // Add cookie secret for tests |
| 19 | + process.env.DEPLOYSTACK_FRONTEND_URL = 'http://localhost:5174'; // Add dummy frontend URL for tests |
18 | 20 |
|
19 | 21 | // Clean up persistent data before tests |
20 | | - if (await fs.pathExists(DB_FILE_PATH)) { |
21 | | - await fs.remove(DB_FILE_PATH); |
| 22 | + if (await fs.pathExists(TEST_DB_FILE_PATH)) { |
| 23 | + await fs.remove(TEST_DB_FILE_PATH); |
22 | 24 | } |
23 | | - if (await fs.pathExists(DB_SELECTION_PATH)) { |
24 | | - await fs.remove(DB_SELECTION_PATH); |
| 25 | + if (await fs.pathExists(TEST_DB_SELECTION_PATH)) { |
| 26 | + await fs.remove(TEST_DB_SELECTION_PATH); |
25 | 27 | } |
26 | 28 |
|
27 | | - // Ensure the database directory exists |
| 29 | + // Ensure the database directory exists (it's shared, but files are separate) |
28 | 30 | await fs.ensureDir(path.join(PERSISTENT_DATA_PATH, 'database')); |
29 | 31 |
|
30 | | - // Create simplified test server |
31 | | - const server = await createTestServer(); |
| 32 | + // IMPORTANT: We need to tell the application to USE this test database file. |
| 33 | + // The db/config.ts now uses 'db.selection.test.json' when NODE_ENV is 'test'. |
| 34 | + // globalSetup.ts deletes this file, so the DB system will likely see no config. |
| 35 | + // The db/index.ts or related setup logic should handle creating/using the |
| 36 | + // 'deploystack.test.db' when in test mode and no db.selection.test.json is found. |
| 37 | + // The line `process.env.DB_FILE_NAME = 'deploystack.test.db';` was removed as db/config.ts doesn't use it. |
| 38 | + |
| 39 | + // Create main server instance for testing |
| 40 | + const server = await createServer(); |
32 | 41 |
|
33 | 42 | // Start the server |
34 | 43 | await server.listen({ port: TEST_PORT, host: '0.0.0.0' }); |
|
0 commit comments