Migrate the API to native ESM and vitest; pin Node 26
Prep PR for the admin auth module (docs/plan-admin-auth.md step 1).
better-auth 1.7 ships ESM only, so the API moves off CommonJS:
- "type": "module", module nodenext, target ES2024, .js suffixes on all
relative imports, require('mariadb'|'cors') replaced by imports, and
export= packages (winston, app-root-path, bcrypt) consumed via default
imports. The logger now uses appRoot.path explicitly.
- TypeScript 5.9, @types/node 26, tslint removed. Node 26 pinned via
engines and .nvmrc (Plesk runs 26).
- Jest 28 + ts-jest replaced by vitest 5. Eight test files depend on
hoisted module mocks with static imports and resetModules + require,
which Jest's ESM mode does not support; vitest keeps them nearly
verbatim. Coverage via @vitest/coverage-v8 (lcov), Sonar generic report
via vitest-sonar-reporter, so sonar-project.properties is unchanged.
vitest.config.ts sets FEEDBACK_IP_SALT so the suite passes without a
local .env.
- dotenv 8 -> 16 and axios 0.24 -> 1.x: their old typings are not
resolvable under nodenext.
- autoCommit: false dropped from the pool configs; it is not a mariadb
connector option and was silently ignored.
tsc clean, 96/96 tests green, compiled app boots and serves /, /docs and
CORS under Node ESM.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import {escapeCsvField} from '../../src/models/feedback/admin/csv.service';
|
||||
import {formatDatetime} from '../../src/models/feedback/feedback.dates';
|
||||
import {describe, it, expect} from 'vitest';
|
||||
import {escapeCsvField} from '../../src/models/feedback/admin/csv.service.js';
|
||||
import {formatDatetime} from '../../src/models/feedback/feedback.dates.js';
|
||||
|
||||
describe('escapeCsvField', () => {
|
||||
it('passes plain text through unchanged', () => {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {computeDefaultDeadline, slugBase, slugifyName} from '../../src/models/feedback/admin/events.admin.service';
|
||||
import {describe, it, expect} from 'vitest';
|
||||
import {computeDefaultDeadline, slugBase, slugifyName} from '../../src/models/feedback/admin/events.admin.service.js';
|
||||
|
||||
describe('slugifyName', () => {
|
||||
it('lowercases and hyphenates', () => {
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import {vi, describe, it, expect, beforeEach, type Mock} from 'vitest';
|
||||
import {Request, Response} from 'express';
|
||||
|
||||
jest.mock('../../src/models/calendar/users/users.service', () => ({
|
||||
checkSession: jest.fn()
|
||||
vi.mock('../../src/models/calendar/users/users.service.js', () => ({
|
||||
checkSession: vi.fn()
|
||||
}));
|
||||
|
||||
import * as UserService from '../../src/models/calendar/users/users.service';
|
||||
import {requireAdminAuth, sessionHeaderAuthenticator} from '../../src/models/feedback/feedback.auth';
|
||||
import * as UserService from '../../src/models/calendar/users/users.service.js';
|
||||
import {requireAdminAuth, sessionHeaderAuthenticator} from '../../src/models/feedback/feedback.auth.js';
|
||||
|
||||
const mockCheckSession = UserService.checkSession as jest.Mock;
|
||||
const mockCheckSession = UserService.checkSession as Mock;
|
||||
|
||||
const makeReq = (headers: Record<string, string>): Request => {
|
||||
return {
|
||||
@@ -18,8 +19,8 @@ const makeReq = (headers: Record<string, string>): Request => {
|
||||
|
||||
const makeRes = (): Response => {
|
||||
const res: any = {};
|
||||
res.status = jest.fn().mockReturnValue(res);
|
||||
res.send = jest.fn().mockReturnValue(res);
|
||||
res.status = vi.fn().mockReturnValue(res);
|
||||
res.send = vi.fn().mockReturnValue(res);
|
||||
res.locals = {};
|
||||
return res as Response;
|
||||
};
|
||||
@@ -65,7 +66,7 @@ describe('requireAdminAuth', () => {
|
||||
mockCheckSession.mockResolvedValue(null);
|
||||
const req = makeReq({});
|
||||
const res = makeRes();
|
||||
const next = jest.fn();
|
||||
const next = vi.fn();
|
||||
|
||||
await requireAdminAuth(req, res, next);
|
||||
|
||||
@@ -77,7 +78,7 @@ describe('requireAdminAuth', () => {
|
||||
mockCheckSession.mockResolvedValue({userId: 1, email: 'a@nachklang.art', fullName: 'Anna Admin', isActive: true});
|
||||
const req = makeReq({'X-Session-Id': '1', 'X-Session-Key': 'k'});
|
||||
const res = makeRes();
|
||||
const next = jest.fn();
|
||||
const next = vi.fn();
|
||||
|
||||
await requireAdminAuth(req, res, next);
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {isHoneypotTriggered} from '../../src/models/feedback/public/public.router';
|
||||
import {describe, it, expect} from 'vitest';
|
||||
import {isHoneypotTriggered} from '../../src/models/feedback/public/public.router.js';
|
||||
|
||||
describe('isHoneypotTriggered', () => {
|
||||
it('is false when the field is absent', () => {
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
// Isolated from ratelimit.test.ts because it needs to control whether
|
||||
// FEEDBACK_IP_SALT is present at module-load time, which a real dotenv.config()
|
||||
// call would silently repopulate from the repo's .env file.
|
||||
jest.mock('dotenv', () => ({config: jest.fn()}));
|
||||
jest.mock('../../src/models/feedback/Feedback.db', () => ({
|
||||
NachklangFeedbackDB: {getConnection: jest.fn()}
|
||||
import {vi, describe, it, expect, afterEach} from 'vitest';
|
||||
vi.mock('dotenv', () => ({config: vi.fn()}));
|
||||
vi.mock('../../src/models/feedback/Feedback.db.js', () => ({
|
||||
NachklangFeedbackDB: {getConnection: vi.fn()}
|
||||
}));
|
||||
|
||||
describe('FEEDBACK_IP_SALT enforcement', () => {
|
||||
@@ -11,18 +12,18 @@ describe('FEEDBACK_IP_SALT enforcement', () => {
|
||||
|
||||
afterEach(() => {
|
||||
process.env.FEEDBACK_IP_SALT = originalSalt;
|
||||
jest.resetModules();
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
it('throws at import time when FEEDBACK_IP_SALT is unset, rather than silently hashing unsalted', () => {
|
||||
jest.resetModules();
|
||||
it('throws at import time when FEEDBACK_IP_SALT is unset, rather than silently hashing unsalted', async () => {
|
||||
vi.resetModules();
|
||||
delete process.env.FEEDBACK_IP_SALT;
|
||||
expect(() => require('../../src/models/feedback/feedback.ratelimit')).toThrow(/FEEDBACK_IP_SALT/);
|
||||
await expect(import('../../src/models/feedback/feedback.ratelimit.js')).rejects.toThrow(/FEEDBACK_IP_SALT/);
|
||||
});
|
||||
|
||||
it('does not throw when FEEDBACK_IP_SALT is set', () => {
|
||||
jest.resetModules();
|
||||
it('does not throw when FEEDBACK_IP_SALT is set', async () => {
|
||||
vi.resetModules();
|
||||
process.env.FEEDBACK_IP_SALT = 'a-real-salt';
|
||||
expect(() => require('../../src/models/feedback/feedback.ratelimit')).not.toThrow();
|
||||
await expect(import('../../src/models/feedback/feedback.ratelimit.js')).resolves.toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {hashIp} from '../../src/models/feedback/feedback.ratelimit';
|
||||
import {describe, it, expect} from 'vitest';
|
||||
import {hashIp} from '../../src/models/feedback/feedback.ratelimit.js';
|
||||
|
||||
describe('hashIp', () => {
|
||||
it('never returns the raw IP', () => {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import {aggregateReport} from '../../src/models/feedback/admin/reports.admin.service';
|
||||
import {AnswerRow} from '../../src/models/feedback/admin/reports.admin.interface';
|
||||
import {describe, it, expect} from 'vitest';
|
||||
import {aggregateReport} from '../../src/models/feedback/admin/reports.admin.service.js';
|
||||
import {AnswerRow} from '../../src/models/feedback/admin/reports.admin.interface.js';
|
||||
|
||||
const eventMeta = {eventId: 1, name: 'Sommerkonzert', eventDate: '2026-08-01', feedbackDeadline: '2026-08-15T23:59:59'};
|
||||
const emptyNewsletter = {total: 0, sent: 0, pending: 0, failed: 0, skipped: 0};
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
// The module under test caches its OAuth token at module scope (see
|
||||
// salesforce.service.ts's `cachedToken`), so every test resets the module
|
||||
// registry for a clean cache. That also invalidates any jest.mock() factory
|
||||
// instance captured before the reset, so every mocked dependency (axios,
|
||||
// Feedback.db, the logger) is re-required fresh after each reset rather
|
||||
// than referenced from a top-level import.
|
||||
// registry for a clean cache. Every mocked dependency (axios, Feedback.db,
|
||||
// the logger) is re-imported after the reset rather than referenced from a
|
||||
// top-level import, so the test always holds the same instance the service
|
||||
// under test resolves. Mocked modules survive vi.resetModules(), so their
|
||||
// mock state is reset explicitly in beforeEach.
|
||||
|
||||
jest.mock('axios');
|
||||
jest.mock('../../src/models/feedback/Feedback.db', () => ({
|
||||
NachklangFeedbackDB: {getConnection: jest.fn()}
|
||||
import {vi, describe, it, expect, beforeEach, type Mock} from 'vitest';
|
||||
vi.mock('axios');
|
||||
vi.mock('../../src/models/feedback/Feedback.db.js', () => ({
|
||||
NachklangFeedbackDB: {getConnection: vi.fn()}
|
||||
}));
|
||||
jest.mock('../../src/middleware/logger', () => ({
|
||||
vi.mock('../../src/middleware/logger.js', () => ({
|
||||
__esModule: true,
|
||||
default: {info: jest.fn(), error: jest.fn()}
|
||||
default: {info: vi.fn(), error: vi.fn()}
|
||||
}));
|
||||
|
||||
const SIGNUP_ROW = {
|
||||
@@ -23,30 +25,31 @@ const SIGNUP_ROW = {
|
||||
};
|
||||
|
||||
const makeConn = (rows: any[]) => ({
|
||||
query: jest.fn().mockResolvedValue(rows),
|
||||
end: jest.fn().mockResolvedValue(undefined)
|
||||
query: vi.fn().mockResolvedValue(rows),
|
||||
end: vi.fn().mockResolvedValue(undefined)
|
||||
});
|
||||
|
||||
// Re-requires every mocked dependency fresh (see the note above) and
|
||||
// returns the live references plus the service under test.
|
||||
const freshImports = () => {
|
||||
const axios = require('axios');
|
||||
const {NachklangFeedbackDB} = require('../../src/models/feedback/Feedback.db');
|
||||
const logger = require('../../src/middleware/logger').default;
|
||||
const {syncNewsletterSignup} = require('../../src/models/feedback/integrations/salesforce.service');
|
||||
return {axios, mockGetConnection: NachklangFeedbackDB.getConnection as jest.Mock, logger, syncNewsletterSignup};
|
||||
// Re-imports every mocked dependency (see the note above) and returns the
|
||||
// live references plus the service under test.
|
||||
const freshImports = async () => {
|
||||
const axios: any = (await import('axios')).default;
|
||||
const {NachklangFeedbackDB} = await import('../../src/models/feedback/Feedback.db.js');
|
||||
const logger = (await import('../../src/middleware/logger.js')).default;
|
||||
const {syncNewsletterSignup} = await import('../../src/models/feedback/integrations/salesforce.service.js');
|
||||
return {axios, mockGetConnection: NachklangFeedbackDB.getConnection as Mock, logger, syncNewsletterSignup};
|
||||
};
|
||||
|
||||
const ORIGINAL_ENV = {...process.env};
|
||||
|
||||
describe('syncNewsletterSignup - disabled mode', () => {
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
vi.resetModules();
|
||||
vi.resetAllMocks();
|
||||
process.env = {...ORIGINAL_ENV, SALESFORCE_ENABLED: 'false'};
|
||||
});
|
||||
|
||||
it('logs the payload it would send and does not touch the network or write to the DB', async () => {
|
||||
const {axios, mockGetConnection, logger, syncNewsletterSignup} = freshImports();
|
||||
const {axios, mockGetConnection, logger, syncNewsletterSignup} = await freshImports();
|
||||
const conn = makeConn([SIGNUP_ROW]);
|
||||
mockGetConnection.mockResolvedValue(conn);
|
||||
|
||||
@@ -66,7 +69,7 @@ describe('syncNewsletterSignup - disabled mode', () => {
|
||||
});
|
||||
|
||||
it('logs and returns without calling the network when the signup row does not exist', async () => {
|
||||
const {axios, mockGetConnection, logger, syncNewsletterSignup} = freshImports();
|
||||
const {axios, mockGetConnection, logger, syncNewsletterSignup} = await freshImports();
|
||||
mockGetConnection.mockResolvedValue(makeConn([]));
|
||||
|
||||
await syncNewsletterSignup(999);
|
||||
@@ -78,7 +81,8 @@ describe('syncNewsletterSignup - disabled mode', () => {
|
||||
|
||||
describe('syncNewsletterSignup - enabled mode', () => {
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
vi.resetModules();
|
||||
vi.resetAllMocks();
|
||||
process.env = {
|
||||
...ORIGINAL_ENV,
|
||||
SALESFORCE_ENABLED: 'true',
|
||||
@@ -89,7 +93,7 @@ describe('syncNewsletterSignup - enabled mode', () => {
|
||||
});
|
||||
|
||||
it('fetches a token, posts the signup, and marks the row SENT with the returned record id', async () => {
|
||||
const {axios, mockGetConnection, syncNewsletterSignup} = freshImports();
|
||||
const {axios, mockGetConnection, syncNewsletterSignup} = await freshImports();
|
||||
const updateConn = makeConn([]);
|
||||
mockGetConnection.mockResolvedValueOnce(makeConn([SIGNUP_ROW])).mockResolvedValueOnce(updateConn);
|
||||
axios.post.mockImplementation((url: string) => {
|
||||
@@ -116,7 +120,7 @@ describe('syncNewsletterSignup - enabled mode', () => {
|
||||
});
|
||||
|
||||
it('reuses the cached token across two calls instead of fetching twice', async () => {
|
||||
const {axios, mockGetConnection, syncNewsletterSignup} = freshImports();
|
||||
const {axios, mockGetConnection, syncNewsletterSignup} = await freshImports();
|
||||
mockGetConnection
|
||||
.mockResolvedValueOnce(makeConn([SIGNUP_ROW]))
|
||||
.mockResolvedValueOnce(makeConn([]))
|
||||
@@ -135,7 +139,7 @@ describe('syncNewsletterSignup - enabled mode', () => {
|
||||
});
|
||||
|
||||
it('retries once with a fresh token on a 401, then succeeds', async () => {
|
||||
const {axios, mockGetConnection, syncNewsletterSignup} = freshImports();
|
||||
const {axios, mockGetConnection, syncNewsletterSignup} = await freshImports();
|
||||
const updateConn = makeConn([]);
|
||||
mockGetConnection.mockResolvedValueOnce(makeConn([SIGNUP_ROW])).mockResolvedValueOnce(updateConn);
|
||||
|
||||
@@ -163,7 +167,7 @@ describe('syncNewsletterSignup - enabled mode', () => {
|
||||
});
|
||||
|
||||
it('marks the row FAILED with the error message on a non-401 error, without throwing', async () => {
|
||||
const {axios, mockGetConnection, logger, syncNewsletterSignup} = freshImports();
|
||||
const {axios, mockGetConnection, logger, syncNewsletterSignup} = await freshImports();
|
||||
const updateConn = makeConn([]);
|
||||
mockGetConnection.mockResolvedValueOnce(makeConn([SIGNUP_ROW])).mockResolvedValueOnce(updateConn);
|
||||
axios.post.mockImplementation((url: string) => {
|
||||
@@ -184,7 +188,7 @@ describe('syncNewsletterSignup - enabled mode', () => {
|
||||
|
||||
it('marks the row FAILED with a clear message when client credentials are not configured', async () => {
|
||||
process.env.SALESFORCE_CLIENT_ID = '';
|
||||
const {mockGetConnection, syncNewsletterSignup} = freshImports();
|
||||
const {mockGetConnection, syncNewsletterSignup} = await freshImports();
|
||||
const updateConn = makeConn([]);
|
||||
mockGetConnection.mockResolvedValueOnce(makeConn([SIGNUP_ROW])).mockResolvedValueOnce(updateConn);
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {MAX_ANSWER_ROWS, validateAnswers, validateGuestBook, validateNewsletter} from '../../src/models/feedback/public/submissions.service';
|
||||
import {describe, it, expect} from 'vitest';
|
||||
import {MAX_ANSWER_ROWS, validateAnswers, validateGuestBook, validateNewsletter} from '../../src/models/feedback/public/submissions.service.js';
|
||||
|
||||
type QuestionLookup = Map<number, {eventQuestionId: number; questionId: number; type: 'SONG_PICK' | 'SONG_RATING' | 'FREE_TEXT'; label: string; position: number}>;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user