import { readFileSync } from 'node:fs' import { isIP } from 'node:net' import { tmpdir } from 'node:os' import path from 'node:path' const requiredURLs = [ 'frontendUrl', 'apiUrl', 'databaseUrl', 'openFigiFakeUrl', 'scraperFakeUrl', 'layaFakeUrl', 'fakeControlUrl', ] function loopbackURL(value, field, protocols) { let parsed try { parsed = new URL(value) } catch { throw new Error(`E2E fake stack manifest field ${field} must be a URL.`) } if (!protocols.includes(parsed.protocol)) { throw new Error(`E2E fake stack manifest field ${field} uses an unsupported protocol.`) } const hostname = parsed.hostname.replace(/^\[|\]$/g, '') const loopback = hostname === 'localhost' || hostname.endsWith('.localhost') || (isIP(hostname) === 4 && hostname.startsWith('127.')) || hostname === '::1' if (!loopback) { throw new Error(`E2E fake stack manifest field ${field} must use a loopback host.`) } return parsed } export function stackManifestPath() { return process.env.TRADARR_E2E_STACK_MANIFEST || path.join(process.env.TMPDIR || tmpdir(), 'tradarr-e2e', 'stack.json') } export function readStackManifest() { const manifestPath = stackManifestPath() let manifest try { manifest = JSON.parse(readFileSync(manifestPath, 'utf8')) } catch { throw new Error([ `Safe E2E composition is missing: no readable stack manifest at ${manifestPath}.`, 'No browser, application server, database, or provider was started by this run.', 'The manifest must be produced by a loopback-only fake stack and point to a disposable E2E database.', ].join(' ')) } if (manifest.networkPolicy !== 'loopback-only') { throw new Error('E2E fake stack manifest must declare networkPolicy "loopback-only".') } if (manifest.generationProvider !== 'fake-laya') { throw new Error('E2E fake stack manifest must declare generationProvider "fake-laya".') } for (const field of requiredURLs) { if (typeof manifest[field] !== 'string' || !manifest[field]) { throw new Error(`E2E fake stack manifest is missing ${field}.`) } loopbackURL(manifest[field], field, field === 'databaseUrl' ? ['postgres:', 'postgresql:'] : ['http:', 'https:']) } if (typeof manifest.psqlPath !== 'string' || !manifest.psqlPath.startsWith('/')) { throw new Error('E2E fake stack manifest must provide an absolute psqlPath for database assertions.') } if (!manifest.bootstrapAdmin?.email || !manifest.bootstrapAdmin?.password) { throw new Error('E2E fake stack manifest must provide disposable bootstrap admin credentials.') } for (const field of ['companyQuery', 'companyID', 'companyName', 'companySymbol', 'companyFIGI', 'summaryModelID', 'summarySentinel', 'articleTitle', 'articleURL', 'articleContent', 'candidateID']) { if (typeof manifest.fixtures?.[field] !== 'string' || !manifest.fixtures[field]) { throw new Error(`E2E fake stack manifest is missing fixtures.${field}.`) } } const databaseURL = new URL(manifest.databaseUrl) const databaseName = decodeURIComponent(databaseURL.pathname.replace(/^\//, '')) if (!/(^|[-_])(e2e|test)([-_]|$)/i.test(databaseName)) { throw new Error('E2E database name must identify a disposable database with an e2e or test marker.') } if (!manifest.fakeAuditPath || !manifest.fakeAuditPath.startsWith('/')) { throw new Error('E2E fake stack manifest must provide an absolute fakeAuditPath.') } return { ...manifest, databaseURL } }