@@ -5,7 +5,7 @@ import { isAbsolute, join, posix, win32 } from "node:path";
55import * as serviceModule from "../src/service" ;
66import { saveConfig } from "../src/config" ;
77import { windowsEnvIndirectBatchValue } from "../src/lib/win-paths" ;
8- import { assertServiceAuthEnvironment , assertServiceEnvironmentMatchesInstall , bakedServicePathsDiagnostic , confirmServiceServing , launchdListenPort , systemdListenPort , buildPlist , buildUnit , buildWindowsLauncherVbs , buildWindowsSchtasksCreateArgs , buildWindowsSchtasksCreateArgsForXml , buildWindowsServiceScript , buildWindowsTaskXml , deriveWindowsServiceDiagnostic , installFreshWindowsSchedulerSafely , installServiceSafely , launchctlLoadFailed , launchdJobMatchesPlist , normalizeServiceSubcommand , parseServiceArgs , parseServiceInstallState , planServiceCommand , prepareServiceInstall , probeServiceInstallation , readWindowsSchedulerXmlState , registerFreshWindowsSchedulerTask , removeNativeWindowsServiceForScheduler , repairService , resolveServiceListenPort , runLaunchctl , selectServiceSubcommand , serviceLogPath , serviceStartableFromTray , serviceStatusReport , serviceRetryCommand , serviceStatusSummary , systemdNeedsDaemonReload , windowsListenPort , winswListenPort , startLaunchd , windowsTaskRegistrationHealthy } from "../src/service" ;
8+ import { assertServiceAuthEnvironment , assertServiceEnvironmentMatchesInstall , bakedServicePathsDiagnostic , confirmServiceServing , launchdListenPort , systemdListenPort , buildPlist , buildUnit , buildWindowsLauncherVbs , buildWindowsSchtasksCreateArgs , buildWindowsSchtasksCreateArgsForXml , buildWindowsServiceScript , buildWindowsTaskXml , deriveWindowsServiceDiagnostic , installFreshWindowsSchedulerSafely , installServiceSafely , launchctlLoadFailed , launchdJobMatchesPlist , normalizeServiceSubcommand , parseServiceArgs , parseServiceInstallState , planServiceCommand , prepareServiceInstall , probeServiceInstallation , readWindowsSchedulerXmlState , registerFreshWindowsSchedulerTask , removeNativeWindowsServiceForScheduler , repairService , resolveServiceListenPort , runLaunchctl , selectServiceSubcommand , serviceLogPath , serviceStartableFromTray , serviceStatusReport , serviceRetryCommand , serviceStatusSummary , systemdNeedsDaemonReload , systemdServiceInstallCleanupOps , uninstallSystemd , windowsListenPort , winswListenPort , startLaunchd , windowsTaskRegistrationHealthy } from "../src/service" ;
99import type { ServiceDiagnostic } from "../src/service" ;
1010import { definitionCarriesCredential , resolvedProxyEnv , writeServiceDefinitionFile } from "../src/service" ;
1111import { buildWinswXml } from "../src/lib/winsw" ;
@@ -185,13 +185,12 @@ describe("systemd service unit", () => {
185185 expect ( nativeUnknown . detail ) . toContain ( "WinSW status" ) ;
186186 } ) ;
187187
188- test ( "uses unquoted append targets for service logs " , ( ) => {
188+ test ( "redirects service output through the ExecStart shell for legacy systemd " , ( ) => {
189189 const unit = buildUnit ( ) ;
190190
191- expect ( unit ) . toContain ( "StandardOutput=append:" ) ;
192- expect ( unit ) . toContain ( "StandardError=append:" ) ;
193- expect ( unit ) . not . toContain ( 'StandardOutput="append:' ) ;
194- expect ( unit ) . not . toContain ( 'StandardError="append:' ) ;
191+ expect ( unit ) . toMatch ( / E x e c S t a r t = .* s t a r t - - p o r t \d + > > ' [ ^ ' \n ] * s e r v i c e \. l o g ' 2 > & 1 / ) ;
192+ expect ( unit ) . not . toContain ( "StandardOutput=" ) ;
193+ expect ( unit ) . not . toContain ( "StandardError=" ) ;
195194 } ) ;
196195
197196 test ( "bakes outbound proxy env into the unit so the service is not cut off from upstream (#2107)" , ( ) => {
@@ -1449,6 +1448,95 @@ describe("service lifecycle cleanup ordering", () => {
14491448 ] ) ;
14501449 } ) ;
14511450
1451+ test ( "legacy systemd reports an absent unit without stopping cleanup or blocking install" , async ( ) => {
1452+ const commands : string [ ] = [ ] ;
1453+ const manager = systemdServiceInstallCleanupOps ( {
1454+ run : command => {
1455+ commands . push ( command ) ;
1456+ return "LoadState=not-found\n" ;
1457+ } ,
1458+ } ) ;
1459+
1460+ expect ( manager . status ( ) ) . toBeNull ( ) ;
1461+ expect ( commands ) . toEqual ( [ "systemctl --user show -p LoadState opencodex-proxy" ] ) ;
1462+ expect ( commands [ 0 ] ) . not . toContain ( "--value" ) ;
1463+
1464+ commands . length = 0 ;
1465+ let installed = false ;
1466+ await installServiceSafely ( "scheduler" , ( ) => { installed = true ; } , {
1467+ platform : "linux" ,
1468+ managerOps : ( ) => manager ,
1469+ stopTrackedProxy : async ( ) => { } ,
1470+ } ) ;
1471+
1472+ expect ( installed ) . toBe ( true ) ;
1473+ expect ( commands ) . toEqual ( [ "systemctl --user show -p LoadState opencodex-proxy" ] ) ;
1474+ } ) ;
1475+
1476+ test ( "legacy systemd still stops a loaded unit before installation" , async ( ) => {
1477+ const commands : string [ ] = [ ] ;
1478+ const manager = systemdServiceInstallCleanupOps ( {
1479+ run : command => {
1480+ commands . push ( command ) ;
1481+ return command . includes ( " show " ) ? "LoadState=loaded\n" : "" ;
1482+ } ,
1483+ } ) ;
1484+ let installed = false ;
1485+
1486+ await installServiceSafely ( "scheduler" , ( ) => { installed = true ; } , {
1487+ platform : "linux" ,
1488+ managerOps : ( ) => manager ,
1489+ stopTrackedProxy : async ( ) => { } ,
1490+ } ) ;
1491+
1492+ expect ( installed ) . toBe ( true ) ;
1493+ expect ( commands ) . toEqual ( [
1494+ "systemctl --user show -p LoadState opencodex-proxy" ,
1495+ "systemctl --user stop opencodex-proxy" ,
1496+ ] ) ;
1497+ } ) ;
1498+
1499+ test ( "legacy systemd status fails closed when LoadState is missing or empty" , async ( ) => {
1500+ for ( const output of [ "ActiveState=inactive\n" , "LoadState=\n" ] ) {
1501+ const commands : string [ ] = [ ] ;
1502+ let installed = false ;
1503+ const manager = systemdServiceInstallCleanupOps ( {
1504+ run : command => {
1505+ commands . push ( command ) ;
1506+ return output ;
1507+ } ,
1508+ } ) ;
1509+
1510+ await expect ( installServiceSafely ( "scheduler" , ( ) => { installed = true ; } , {
1511+ platform : "linux" ,
1512+ managerOps : ( ) => manager ,
1513+ stopTrackedProxy : async ( ) => { } ,
1514+ } ) ) . rejects . toThrow ( "systemd service status could not be verified" ) ;
1515+ expect ( installed ) . toBe ( false ) ;
1516+ expect ( commands ) . toEqual ( [ "systemctl --user show -p LoadState opencodex-proxy" ] ) ;
1517+ }
1518+ } ) ;
1519+
1520+ test ( "legacy systemd uninstall stops and disables separately even when stop fails" , ( ) => {
1521+ const commands : string [ ] = [ ] ;
1522+
1523+ uninstallSystemd ( {
1524+ run : command => {
1525+ commands . push ( command ) ;
1526+ if ( command . includes ( " stop " ) ) throw new Error ( "not running" ) ;
1527+ return "" ;
1528+ } ,
1529+ unitExists : ( ) => false ,
1530+ } ) ;
1531+
1532+ expect ( commands ) . toEqual ( [
1533+ "systemctl --user stop opencodex-proxy" ,
1534+ "systemctl --user disable opencodex-proxy" ,
1535+ "systemctl --user daemon-reload" ,
1536+ ] ) ;
1537+ expect ( commands . join ( " " ) ) . not . toContain ( "--now" ) ;
1538+ } ) ;
1539+
14521540 test ( "service install fails closed before install on manager or standalone cleanup errors" , async ( ) => {
14531541 for ( const failure of [ "status" , "stop" , "standalone" ] as const ) {
14541542 let installed = false ;
0 commit comments