2026-08-01 16:06:14 +09:00
'use strict' ;
const assert = require ( 'assert/strict' ) ;
const fs = require ( 'fs' ) ;
const fsp = fs . promises ;
const os = require ( 'os' ) ;
const path = require ( 'path' ) ;
2026-08-01 22:31:04 +09:00
const { renderApacheBridge , renderApacheBootstrap , replaceManagedBlock , installApacheBridge , BEGIN _MARKER , END _MARKER } = require ( '../server/apache-bridge' ) ;
2026-08-01 16:06:14 +09:00
( async ( ) => {
const rendered = renderApacheBridge ( 8080 ) ;
assert . match ( rendered , /RewriteRule \^api\/\(\.\*\)\$ http:\/\/127\.0\.0\.1:8080\/api\/\$1 \[P,L\]/ ) ;
assert . match ( rendered , /ws:\/\/127\.0\.0\.1:8080\/api\/realtime/ ) ;
assert . match ( rendered , /api-bridge\.php\?path=\/api\/\$1 \[QSA,L\]/ ) ;
2026-08-01 22:31:04 +09:00
assert . match ( rendered , /RewriteRule \^\(\?:assets\|client\)/ ) ;
assert . match ( rendered , /RewriteRule \^ - \[F,L\]/ ) ;
assert . match ( rendered , /server\\\.js\$/ ) ;
assert . doesNotMatch ( rendered , /RewriteRule \^\(\.\*\)\$ - \[L\]/ ) ;
const bootstrap = renderApacheBootstrap ( ) ; assert . match ( bootstrap , /api-bridge\.php/ ) ; assert . match ( bootstrap , /RewriteRule \^ - \[F,L\]/ ) ; assert . doesNotMatch ( bootstrap , /127\.0\.0\.1:\d+/ ) ;
assert . equal ( ( await fsp . readFile ( path . join ( _ _dirname , '..' , '.htaccess' ) , 'utf8' ) ) . replace ( /\r\n?/g , '\n' ) , bootstrap , 'Checked-in Apache bootstrap drifted from the safe renderer' ) ;
2026-08-01 16:06:14 +09:00
assert . equal ( ( rendered . match ( new RegExp ( BEGIN _MARKER , 'g' ) ) || [ ] ) . length , 1 ) ;
assert . equal ( ( rendered . match ( new RegExp ( END _MARKER , 'g' ) ) || [ ] ) . length , 1 ) ;
const custom = 'Options -Indexes\n\n# custom rule\n' ;
const first = replaceManagedBlock ( custom , rendered ) ;
assert . match ( first , /Options -Indexes/ ) ;
assert . match ( first , /# custom rule/ ) ;
assert . match ( first , /127\.0\.0\.1:8080/ ) ;
const replaced = replaceManagedBlock ( first , renderApacheBridge ( 4312 ) ) ;
assert . match ( replaced , /127\.0\.0\.1:4312/ ) ;
assert . doesNotMatch ( replaced , /127\.0\.0\.1:8080/ ) ;
assert . equal ( ( replaced . match ( new RegExp ( BEGIN _MARKER , 'g' ) ) || [ ] ) . length , 1 ) ;
const root = await fsp . mkdtemp ( path . join ( os . tmpdir ( ) , 'linkfield-apache-' ) ) ;
try {
await fsp . writeFile ( path . join ( root , '.htaccess' ) , custom ) ;
const installed = await installApacheBridge ( { fsp , root , port : 9123 } ) ;
assert . equal ( installed . enabled , true ) ;
assert . equal ( installed . written , true ) ;
const actual = await fsp . readFile ( path . join ( root , '.htaccess' ) , 'utf8' ) ;
assert . match ( actual , /Options -Indexes/ ) ;
assert . match ( actual , /127\.0\.0\.1:9123/ ) ;
const unchanged = await installApacheBridge ( { fsp , root , port : 9123 } ) ;
assert . equal ( unchanged . written , false ) ;
} finally { await fsp . rm ( root , { recursive : true , force : true } ) }
console . log ( 'LinkField v48.0 Apache bridge smoke test passed' ) ;
} ) ( ) . catch ( error => { console . error ( error ) ; process . exitCode = 1 } ) ;