/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import assert from 'node:assert/strict'; import test from 'node:test'; import { createServer } from 'node:http'; import { mkdtemp, mkdir, writeFile, rm } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { findDevService, logAddresses } from '../../scripts/dev-service.mjs'; test('dev discovery reuses a matching project after a stale log and rejects a different source', async (t) => { const root = await mkdtemp(path.join(tmpdir(), 'monkey-service-test-')); t.after(() => rm(root, { recursive: true, force: true })); await mkdir(path.join(root, 'src')); await writeFile(path.join(root, 'src/index.jsx'), 'project-entry'); const servers = []; const listen = async (source) => { const server = createServer((req, res) => { res.end( '//# sourceMappingURL=data:application/json;base64,' + Buffer.from(JSON.stringify({ sourcesContent: [source] })).toString( 'base64', ), ); }); servers.push(server); await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); return server.address().port; }; t.after(() => Promise.all( servers.map((server) => new Promise((resolve) => server.close(resolve))), ), ); const wrong = await listen('another-project'); const correct = await listen('project-entry'); await writeFile( path.join(root, 'dev.log'), 'Local: http://127.0.0.1:' + wrong + '/', ); assert.equal( await findDevService({ root, getPorts: async () => [wrong, correct] }), 'http://127.0.0.1:' + correct + '/', ); assert.equal( await findDevService({ root, getPorts: async () => [wrong] }), null, ); }); test('logs support UTF-16LE and newest Local addresses first', () => { const text = 'Local: http://127.0.0.1:5174/\nLocal: http://127.0.0.1:4176/'; const expected = ['http://127.0.0.1:4176/', 'http://127.0.0.1:5174/']; assert.deepEqual(logAddresses(Buffer.from(text)), expected); assert.deepEqual( logAddresses(Buffer.from('\ufeff' + text, 'utf16le')), expected, ); });