22import fs from "node:fs/promises" ;
33import os from "node:os" ;
44import path from "node:path" ;
5- import { describe , expect , it } from "vitest" ;
5+ import { beforeEach , describe , expect , it , vi } from "vitest" ;
66import {
77 auditGatewayServiceConfig ,
88 checkTokenDrift ,
@@ -11,6 +11,21 @@ import {
1111import { buildServiceEnvironment } from "./service-env.js" ;
1212import type { GatewayServiceEnvironmentValueSource } from "./service-types.js" ;
1313
14+ const execSystemctlUser = vi . hoisted ( ( ) =>
15+ vi . fn <
16+ (
17+ env : NodeJS . ProcessEnv ,
18+ args : string [ ] ,
19+ timeoutMs ?: number ,
20+ ) => Promise < { stdout : string ; stderr : string ; code : number } >
21+ > ( ) ,
22+ ) ;
23+
24+ vi . mock ( "./systemd-exec.js" , async ( importOriginal ) => ( {
25+ ...( await importOriginal < typeof import ( "./systemd-exec.js" ) > ( ) ) ,
26+ execSystemctlUser,
27+ } ) ) ;
28+
1429function buildMinimalServicePath ( options : {
1530 platform : NodeJS . Platform ;
1631 env : Record < string , string | undefined > ;
@@ -65,9 +80,13 @@ function createGatewayAudit({
6580 } ) ;
6681}
6782
68- async function writeSystemdUnitForAudit ( home : string , lines : string [ ] ) {
83+ async function writeSystemdUnitForAudit (
84+ home : string ,
85+ lines : string [ ] ,
86+ unitName = "openclaw-gateway.service" ,
87+ ) {
6988 const unitDir = path . join ( home , ".config" , "systemd" , "user" ) ;
70- const unitPath = path . join ( unitDir , "openclaw-gateway.service" ) ;
89+ const unitPath = path . join ( unitDir , unitName ) ;
7190 await fs . mkdir ( unitDir , { recursive : true } ) ;
7291 await fs . writeFile (
7392 unitPath ,
@@ -101,6 +120,11 @@ function expectTokenAudit(
101120}
102121
103122describe ( "auditGatewayServiceConfig" , ( ) => {
123+ beforeEach ( ( ) => {
124+ execSystemctlUser . mockReset ( ) ;
125+ execSystemctlUser . mockResolvedValue ( { stdout : "" , stderr : "systemd unavailable" , code : 1 } ) ;
126+ } ) ;
127+
104128 it ( "flags bun runtime" , async ( ) => {
105129 const audit = await auditGatewayServiceConfig ( {
106130 env : { HOME : "/tmp" } ,
@@ -500,6 +524,103 @@ describe("auditGatewayServiceConfig", () => {
500524 expectTokenAudit ( audit , { embedded : true , mismatch : true } ) ;
501525 } ) ;
502526
527+ it . each ( [
528+ {
529+ name : "uses manager KillMode instead of the base unit" ,
530+ unit : [
531+ "After=network-online.target" ,
532+ "Wants=network-online.target" ,
533+ "RestartSec=5" ,
534+ "KillMode=control-group" ,
535+ ] ,
536+ manager : [
537+ "KillMode=process" ,
538+ "RestartUSec=5s" ,
539+ "After=network-online.target" ,
540+ "Wants=network-online.target" ,
541+ ] ,
542+ code : SERVICE_AUDIT_CODES . systemdKillModeProcessOrNone ,
543+ expected : true ,
544+ } ,
545+ {
546+ name : "uses manager RestartUSec instead of the base unit" ,
547+ unit : [
548+ "After=network-online.target" ,
549+ "Wants=network-online.target" ,
550+ "RestartSec=100ms" ,
551+ "KillMode=control-group" ,
552+ ] ,
553+ manager : [
554+ "Wants=network-online.target" ,
555+ "KillMode=control-group" ,
556+ "RestartUSec=5s" ,
557+ "After=network-online.target" ,
558+ ] ,
559+ code : SERVICE_AUDIT_CODES . systemdRestartSec ,
560+ expected : false ,
561+ } ,
562+ {
563+ name : "uses manager After dependencies absent from the base unit" ,
564+ unit : [ "Wants=network-online.target" , "RestartSec=5" , "KillMode=control-group" ] ,
565+ manager : [
566+ "RestartUSec=5s" ,
567+ "After=basic.target network-online.target" ,
568+ "KillMode=control-group" ,
569+ "Wants=network-online.target" ,
570+ ] ,
571+ code : SERVICE_AUDIT_CODES . systemdAfterNetworkOnline ,
572+ expected : false ,
573+ } ,
574+ {
575+ name : "does not refill missing manager Wants from the base unit" ,
576+ unit : [
577+ "After=network-online.target" ,
578+ "Wants=network-online.target" ,
579+ "RestartSec=5" ,
580+ "KillMode=control-group" ,
581+ ] ,
582+ manager : [
583+ "After=network-online.target" ,
584+ "RestartUSec=5s" ,
585+ "Wants=basic.target" ,
586+ "KillMode=control-group" ,
587+ ] ,
588+ code : SERVICE_AUDIT_CODES . systemdWantsNetworkOnline ,
589+ expected : true ,
590+ } ,
591+ ] ) ( "respects systemd manager authority: $name" , async ( { unit, manager, code, expected } ) => {
592+ const home = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-service-audit-manager-" ) ) ;
593+ try {
594+ const unitName = "openclaw-audit.service" ;
595+ const env = { HOME : home , OPENCLAW_SYSTEMD_UNIT : unitName } ;
596+ await writeSystemdUnitForAudit ( home , unit , unitName ) ;
597+ execSystemctlUser . mockResolvedValueOnce ( {
598+ stdout : manager . join ( "\n" ) ,
599+ stderr : "" ,
600+ code : 0 ,
601+ } ) ;
602+
603+ const audit = await auditGatewayServiceConfig ( {
604+ env,
605+ platform : "linux" ,
606+ timeoutMs : 321 ,
607+ command : {
608+ programArguments : [ "/usr/bin/node" , "gateway" ] ,
609+ environment : { PATH : "/usr/bin:/bin" } ,
610+ } ,
611+ } ) ;
612+
613+ expect ( hasIssue ( audit , code ) ) . toBe ( expected ) ;
614+ expect ( execSystemctlUser ) . toHaveBeenCalledExactlyOnceWith (
615+ env ,
616+ [ "show" , unitName , "--no-page" , "--property" , "After,Wants,RestartUSec,KillMode" ] ,
617+ 321 ,
618+ ) ;
619+ } finally {
620+ await fs . rm ( home , { recursive : true , force : true } ) ;
621+ }
622+ } ) ;
623+
503624 it . each ( [ "process" , "none" ] ) (
504625 `warns when KillMode is %s in explicit unit file` ,
505626 async ( killMode ) => {
@@ -524,6 +645,7 @@ describe("auditGatewayServiceConfig", () => {
524645 ( entry ) => entry . code === SERVICE_AUDIT_CODES . systemdKillModeProcessOrNone ,
525646 ) ,
526647 ) . toBe ( true ) ;
648+ expect ( execSystemctlUser ) . toHaveBeenCalledWith ( { HOME : home } , expect . any ( Array ) , 10_000 ) ;
527649 } ,
528650 ) ;
529651
0 commit comments