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:
2026-09-05 16:02:26 +02:00
parent 449edd6c68
commit 3ea9e630ed
67 changed files with 2504 additions and 6294 deletions
+32 -28
View File
@@ -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);