Files
TREK/server/tests/unit/app-config/data-paths.test.ts
T
Maurice 5c054863b9 refactor(server): resolve the data directory in one place
The data and uploads directories were derived independently in about ten
files, each counting `..` hops from its own `__dirname`, and the file
logger used `process.cwd()` instead. Moving a file or starting the server
from another directory quietly moved where secrets, backups or logs went,
and the backup code named `data/travel.db` directly, so with TREK_DB_FILE
set a backup archived and a restore replaced a file the connection never
opened.

`resolveDataPaths()` in app-config now anchors data/, uploads/, backups,
tmp, logs, the JWT secret and encryption key files and the database file
(TREK_DB_FILE when set, else data/travel.db) on the server package root.
Code that runs before the container calls it directly; Nest classes inject
the new `DataPathsService`. Every previous anchor uses it, and the resolved
paths are unchanged for the image and for `npm run dev`.
2026-10-08 18:40:15 +02:00

73 lines
3.3 KiB
TypeScript

/**
* The data layout: one anchor for data/, uploads/ and the files inside them,
* shared by the pre-container code (resolveDataPaths) and the Nest classes
* (DataPathsService). These cases pin where each path lands, because a moved
* anchor silently moves the database, the key files or the backups.
*/
import { describe, it, expect, afterEach, vi } from 'vitest';
import path from 'node:path';
import { resolveDataPaths, SERVER_ROOT } from '../../../src/app-config/data-paths';
import { DataPathsService } from '../../../src/nest/app-config/data-paths.service';
import { DEFAULT_BACKUPS_ROOT, DEFAULT_UPLOADS_ROOT, DATA_ROOT, GLOBAL_TEMP_DIR } from '../../../src/nest/storage/storage-paths';
import { filesDir } from '../../../src/nest/files/files.constants';
const SERVER_DIR = path.resolve(__dirname, '..', '..', '..');
afterEach(() => {
vi.unstubAllEnvs();
});
describe('resolveDataPaths', () => {
it('DATAPATHS-001: anchors on the server package directory, not on the working directory', () => {
expect(SERVER_ROOT).toBe(SERVER_DIR);
const paths = resolveDataPaths({ db: { trekDbFile: undefined } });
expect(paths.dataDir).toBe(path.join(SERVER_DIR, 'data'));
expect(paths.uploadsDir).toBe(path.join(SERVER_DIR, 'uploads'));
});
it('DATAPATHS-002: places backups, scratch space, logs and the key files inside data/', () => {
const paths = resolveDataPaths({ db: { trekDbFile: undefined } }, '/srv/trek');
expect(paths).toEqual({
dataDir: path.join('/srv/trek', 'data'),
uploadsDir: path.join('/srv/trek', 'uploads'),
backupsDir: path.join('/srv/trek', 'data', 'backups'),
tmpDir: path.join('/srv/trek', 'data', 'tmp'),
logsDir: path.join('/srv/trek', 'data', 'logs'),
jwtSecretFile: path.join('/srv/trek', 'data', '.jwt_secret'),
encryptionKeyFile: path.join('/srv/trek', 'data', '.encryption_key'),
dbFile: path.join('/srv/trek', 'data', 'travel.db'),
});
});
it('DATAPATHS-003: TREK_DB_FILE moves only the database file', () => {
const paths = resolveDataPaths({ db: { trekDbFile: '/elsewhere/e2e.db' } }, '/srv/trek');
expect(paths.dbFile).toBe('/elsewhere/e2e.db');
expect(paths.dataDir).toBe(path.join('/srv/trek', 'data'));
});
it('DATAPATHS-004: reads TREK_DB_FILE from the live environment by default', () => {
vi.stubEnv('TREK_DB_FILE', '/tmp/live.db');
expect(resolveDataPaths().dbFile).toBe('/tmp/live.db');
vi.stubEnv('TREK_DB_FILE', '');
expect(resolveDataPaths().dbFile).toBe(path.join(SERVER_DIR, 'data', 'travel.db'));
});
});
describe('every consumer resolves the same layout', () => {
it('DATAPATHS-005: the storage defaults and the trip-files directory come from it', () => {
const paths = resolveDataPaths();
expect(DATA_ROOT).toBe(paths.dataDir);
expect(DEFAULT_UPLOADS_ROOT).toBe(paths.uploadsDir);
expect(DEFAULT_BACKUPS_ROOT).toBe(paths.backupsDir);
expect(GLOBAL_TEMP_DIR).toBe(paths.tmpDir);
expect(filesDir).toBe(path.join(paths.uploadsDir, 'files'));
});
it('DATAPATHS-006: DataPathsService hands Nest classes the same answer', () => {
vi.stubEnv('TREK_DB_FILE', '/tmp/service.db');
const service = new DataPathsService();
expect({ ...service }).toEqual(resolveDataPaths());
expect(service.dbFile).toBe('/tmp/service.db');
});
});