@@ -21,7 +21,7 @@ var TestConfig = function() {
2121 "color" : {
2222 "status" : "http://localhost:8080/color/status" ,
2323 "url" : "http://localhost:8080/color/set/%s" ,
24- "brightness" : true
24+ "brightness" : false
2525 } ,
2626 "brightness" : {
2727 "status" : "http://localhost:8080/brightness/status" ,
@@ -200,6 +200,80 @@ describe('Get power state', function () {
200200
201201} ) ;
202202
203+ // -----------------------------------------------------------------------------
204+ describe ( 'Set brightness' , function ( ) {
205+
206+ beforeEach ( function ( ) {
207+ // 1. Arrange
208+ this . testConfig = new TestConfig ( ) ;
209+
210+ // This will also make sure to reset the embedded Sinon stubs.
211+ this . homebridgeStub = new ( require ( './homebridge.stub.js' ) ) ( this . testConfig ) ;
212+ sut ( this . homebridgeStub ) ;
213+
214+ this . homebridgeStub . accessory . _httpRequest = sinon . stub ( ) ;
215+ this . homebridgeCallback = sinon . stub ( ) ;
216+
217+ } ) ;
218+
219+
220+ it ( 'sends HTTP GET request with correct URL' , function ( ) {
221+ // 1. Arrange
222+ var url = this . testConfig . brightness . url . replace ( '%s' , 100 ) ;
223+
224+ // 2. Act
225+ this . homebridgeStub . accessory . setBrightness ( 100 , this . homebridgeCallback ) ;
226+
227+ // 3. Assert
228+ expect ( this . homebridgeStub . accessory . _httpRequest . firstCall . args [ 0 ] ) . equals ( url ) ;
229+ expect ( this . homebridgeStub . accessory . _httpRequest . firstCall . args [ 1 ] ) . to . be . empty ; // Body empty.
230+ expect ( this . homebridgeStub . accessory . _httpRequest . firstCall . args [ 2 ] ) . equals ( 'GET' ) ;
231+ } ) ;
232+
233+ it ( 'replies to Homebridge on valid HTTP GET device response "100"' , function ( ) {
234+ // 2. Act
235+ // Trigger filling of callback
236+ this . homebridgeStub . accessory . setBrightness ( 100 , this . homebridgeCallback ) ;
237+ // Call collected HTTP response callback to simulate device response.
238+ this . homebridgeStub . accessory . _httpRequest . firstCall . callback ( undefined , { statusCode : 200 } , '100' ) ;
239+
240+ // 3. Assert
241+ expect ( this . homebridgeCallback . calledOnce ) . to . be . true ;
242+ expect ( this . homebridgeStub . logger . firstCall . args ) . deep . equals ( [ 'setBrightness() successfully set to %s %' , 100 ] ) ;
243+ } ) ;
244+
245+ it ( 'sets RGB instead of brightness to Homebridge when config.color.brightness equals true' , function ( ) {
246+ // 1. Arrange
247+ this . homebridgeStub . accessory . color . brightness = true ;
248+
249+ // 2. Act
250+ // Trigger filling of callback
251+ this . homebridgeStub . accessory . setBrightness ( 100 , this . homebridgeCallback ) ;
252+ // Call collected HTTP response callback to simulate device response.
253+ this . homebridgeStub . accessory . _httpRequest . firstCall . callback ( undefined , { statusCode : 200 } , '100' ) ;
254+
255+ // 3. Assert
256+ expect ( this . homebridgeCallback . calledOnce ) . to . be . true ; // But now inside _setRGB.
257+ expect ( this . homebridgeStub . logger . firstCall . args [ 0 ] ) . deep . equals ( '_setRGB converting H:%s S:%s B:%s to RGB:%s ...' ) ;
258+ expect ( this . homebridgeStub . logger . secondCall . args ) . deep . equals ( [ '... _setRGB() successfully set to #%s' , 'FFFFFF' ] ) ;
259+ } ) ;
260+
261+ it ( 'replies an Error object with message "Received HTTP error code 500." to Homebridge on HTTP GET device response status code 500' , function ( ) {
262+ // 1. Arrange
263+ const HTTP_ERROR_STATUS_CODE = 500 ;
264+
265+ // 2. Act
266+ // Trigger filling of callback
267+ this . homebridgeStub . accessory . setBrightness ( 100 , this . homebridgeCallback ) ;
268+ // Call collected HTTP response callback to simulate device response.
269+ this . homebridgeStub . accessory . _httpRequest . firstCall . callback ( undefined , { statusCode : HTTP_ERROR_STATUS_CODE } , 'Dummy error' ) ;
270+
271+ // 3. Assert
272+ expect ( this . homebridgeCallback . firstCall . args [ 0 ] ) . to . be . instanceOf ( Error ) . and . have . property ( 'message' , 'Received HTTP error code ' + HTTP_ERROR_STATUS_CODE + ': "Dummy error"' ) ;
273+ expect ( this . homebridgeStub . logger . firstCall . args ) . deep . equals ( [ 'setBrightness() returned HTTP error code: %s: "%s"' , HTTP_ERROR_STATUS_CODE , 'Dummy error' ] ) ;
274+ } ) ;
275+
276+ } ) ;
203277
204278// -----------------------------------------------------------------------------
205279describe ( 'Get services' , function ( ) {
0 commit comments