@@ -36,6 +36,67 @@ import Testing
3636 }
3737 }
3838
39+ @ Test ( arguments : [ " start:stop " , " stop:start " , " restart:stop " ] )
40+ func `node lifecycle commands finish in request order` ( _ transition : String ) async throws {
41+ let actions = transition . split ( separator : " : " ) . map ( String . init )
42+ let previousAction = try #require ( actions . first )
43+ let nextAction = try #require ( actions . last )
44+ let root = try makeTempDirForTests ( )
45+ defer { try? FileManager . default . removeItem ( at : root ) }
46+
47+ try await TestIsolation . withIsolatedState (
48+ env : [
49+ " OPENCLAW_NODE_SERVICE_TEST_ROOT " : root . path ,
50+ " OPENCLAW_NODE_SERVICE_DELAYED_ACTION " : previousAction ,
51+ ] ,
52+ defaults : [ " openclaw.gatewayProjectRootPath " : nil ] )
53+ {
54+ CommandResolver . setProjectRoot ( root . path )
55+ let executable = root . appendingPathComponent ( " node_modules/.bin/openclaw " )
56+ try makeExecutableForTests ( at : executable )
57+ let script = """
58+ #!/bin/sh
59+ action= " $2 "
60+ : > " $OPENCLAW_NODE_SERVICE_TEST_ROOT/$action.entered "
61+ if [ " $action " = " $OPENCLAW_NODE_SERVICE_DELAYED_ACTION " ]; then
62+ while [ ! -f " $OPENCLAW_NODE_SERVICE_TEST_ROOT/release " ]; do
63+ sleep 0.01
64+ done
65+ fi
66+ if [ " $action " = " stop " ]; then
67+ printf stopped > " $OPENCLAW_NODE_SERVICE_TEST_ROOT/state "
68+ else
69+ printf running > " $OPENCLAW_NODE_SERVICE_TEST_ROOT/state "
70+ fi
71+ printf '{ " ok " :true}'
72+ """
73+ try script . write ( to : executable , atomically : false , encoding : . utf8 )
74+
75+ let release = root . appendingPathComponent ( " release " )
76+ defer { try? Data ( ) . write ( to : release ) }
77+ let profile = AppProfile ( environment : [ : ] )
78+ let previous = Task { await self . runNodeServiceAction ( previousAction , profile : profile ) }
79+ let previousEntered = root . appendingPathComponent ( " \( previousAction ) .entered " )
80+ for _ in 0 ..< 200 where ! FileManager . default . fileExists ( atPath : previousEntered . path ) {
81+ try await Task . sleep ( for : . milliseconds ( 10 ) )
82+ }
83+ try #require ( FileManager . default . fileExists ( atPath : previousEntered . path ) )
84+
85+ let next = Task { await self . runNodeServiceAction ( nextAction , profile : profile ) }
86+ let nextEntered = root . appendingPathComponent ( " \( nextAction ) .entered " )
87+ for _ in 0 ..< 50 where ! FileManager . default . fileExists ( atPath : nextEntered . path ) {
88+ try await Task . sleep ( for : . milliseconds ( 10 ) )
89+ }
90+ #expect ( ! FileManager . default . fileExists ( atPath : nextEntered . path ) )
91+
92+ try Data ( ) . write ( to : release )
93+ #expect ( await previous . value == nil )
94+ #expect ( await next . value == nil )
95+ let observed = try String ( contentsOf : root . appendingPathComponent ( " state " ) , encoding : . utf8 )
96+ #expect ( observed == ( nextAction == " stop " ? " stopped " : " running " ) )
97+ }
98+ }
99+
39100 @ Test func `reads node service ownership command directly from launchd` ( ) throws {
40101 let url = FileManager . default . temporaryDirectory
41102 . appendingPathComponent ( " openclaw-node- \( UUID ( ) . uuidString ) .plist " )
@@ -70,4 +131,17 @@ import Testing
70131 { " service " :{ " loaded " :true, " runtime " :{ " status " : " stopped " }}}
71132 """ ) )
72133 }
134+
135+ private func runNodeServiceAction ( _ action : String , profile : AppProfile ) async -> String ? {
136+ switch action {
137+ case " start " :
138+ await NodeServiceManager . start ( profile : profile )
139+ case " stop " :
140+ await NodeServiceManager . stop ( profile : profile )
141+ case " restart " :
142+ await NodeServiceManager . restart ( profile : profile )
143+ default :
144+ " Unknown node service action "
145+ }
146+ }
73147}
0 commit comments