From 4ac63f28893ada0143e19ccebda54b81f7dc9e12 Mon Sep 17 00:00:00 2001 From: Ivan Fontosh Date: Sun, 28 Jun 2026 12:54:56 +0800 Subject: [PATCH] Ensure license server always listens under PM2. Skip auto-listen only when SKIP_LICENSE_LISTEN is set for tests. Co-authored-by: Cursor --- src/server.mjs | 7 +------ test/adminApi.test.mjs | 12 ++++++------ 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/server.mjs b/src/server.mjs index 21e4d0d..a07f7d7 100644 --- a/src/server.mjs +++ b/src/server.mjs @@ -190,12 +190,7 @@ export function createServer(options = {}) { }); } -const entryPath = process.argv[1] ? path.resolve(process.argv[1]) : ''; -const isMain = - entryPath !== '' && - (fileURLToPath(import.meta.url) === entryPath || entryPath.endsWith(`${path.sep}server.mjs`)); - -if (isMain) { +if (!process.env.SKIP_LICENSE_LISTEN) { const privateKeyPem = process.env.LICENSE_PRIVATE_KEY_PEM; if (!privateKeyPem) { console.error('Задайте LICENSE_PRIVATE_KEY_PEM (PKCS#8 PEM, Ed25519)'); diff --git a/test/adminApi.test.mjs b/test/adminApi.test.mjs index ecc008c..c82109e 100644 --- a/test/adminApi.test.mjs +++ b/test/adminApi.test.mjs @@ -8,8 +8,6 @@ import { fileURLToPath } from 'node:url'; import { generateKeyPairSync } from 'node:crypto'; -import { createServer } from '../src/server.mjs'; - const __dirname = path.dirname(fileURLToPath(import.meta.url)); const root = path.resolve(__dirname, '..'); @@ -42,7 +40,9 @@ function request(port, method, pathname, { token, body } = {}) { }); } -function makeServer(dataPath, adminToken = 'test-admin') { +async function makeServer(dataPath, adminToken = 'test-admin') { + process.env.SKIP_LICENSE_LISTEN = '1'; + const { createServer } = await import('../src/server.mjs'); const { privateKey } = generateKeyPairSync('ed25519'); const pem = privateKey.export({ type: 'pkcs8', format: 'pem' }); process.env.DND_LICENSE_DATA_PATH = dataPath; @@ -70,7 +70,7 @@ void test('GET /v1/admin/licenses returns enriched product keys', async () => { }), ); - const { server, adminToken } = makeServer(dataPath); + const { server, adminToken } = await makeServer(dataPath); const port = await listen(server); try { const res = await request(port, 'GET', '/v1/admin/licenses', { token: adminToken }); @@ -91,7 +91,7 @@ void test('POST /v1/admin/product-keys creates a new product key', async () => { const dataPath = path.join(tmp, 'data.json'); fs.copyFileSync(path.join(root, 'data.example.json'), dataPath); - const { server, adminToken } = makeServer(dataPath); + const { server, adminToken } = await makeServer(dataPath); const port = await listen(server); try { const res = await request(port, 'POST', '/v1/admin/product-keys', { @@ -118,7 +118,7 @@ void test('admin endpoints reject missing token', async () => { const dataPath = path.join(tmp, 'data.json'); fs.copyFileSync(path.join(root, 'data.example.json'), dataPath); - const { server } = makeServer(dataPath); + const { server } = await makeServer(dataPath); const port = await listen(server); try { const res = await request(port, 'GET', '/v1/admin/licenses');