|
| 1 | +// Copyright (c) 2026, The Robot Web Tools Contributors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +const assert = require('assert'); |
| 18 | +const sinon = require('sinon'); |
| 19 | +const Logging = require('../lib/logging.js'); |
| 20 | +const LoggingService = require('../lib/logging_service.js'); |
| 21 | + |
| 22 | +const LOGGING_SEVERITY = { |
| 23 | + UNSET: 0, |
| 24 | + DEBUG: 10, |
| 25 | + INFO: 20, |
| 26 | +}; |
| 27 | + |
| 28 | +describe('LoggingService test suite', function () { |
| 29 | + let sandbox; |
| 30 | + |
| 31 | + beforeEach(function () { |
| 32 | + sandbox = sinon.createSandbox(); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(function () { |
| 36 | + sandbox.restore(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('starts get_logger_levels and set_logger_levels services', function () { |
| 40 | + const node = { |
| 41 | + name: () => 'logger_node', |
| 42 | + createService: sandbox.stub(), |
| 43 | + }; |
| 44 | + const service = new LoggingService(node); |
| 45 | + |
| 46 | + service.start(); |
| 47 | + service.start(); |
| 48 | + |
| 49 | + assert.strictEqual(service.isStarted(), true); |
| 50 | + assert.strictEqual(node.createService.callCount, 2); |
| 51 | + assert.deepStrictEqual(node.createService.firstCall.args.slice(0, 2), [ |
| 52 | + 'rcl_interfaces/srv/GetLoggerLevels', |
| 53 | + 'logger_node/get_logger_levels', |
| 54 | + ]); |
| 55 | + assert.deepStrictEqual(node.createService.secondCall.args.slice(0, 2), [ |
| 56 | + 'rcl_interfaces/srv/SetLoggerLevels', |
| 57 | + 'logger_node/set_logger_levels', |
| 58 | + ]); |
| 59 | + }); |
| 60 | + |
| 61 | + it('returns logger levels and maps lookup failures to UNSET', function () { |
| 62 | + sandbox.stub(Logging, 'getLogger').callsFake((name) => { |
| 63 | + if (name === 'missing_logger') { |
| 64 | + throw new Error('logger not found'); |
| 65 | + } |
| 66 | + return { loggerEffectiveLevel: LOGGING_SEVERITY.INFO }; |
| 67 | + }); |
| 68 | + const service = new LoggingService({}); |
| 69 | + const response = { |
| 70 | + template: { levels: [] }, |
| 71 | + send: sandbox.spy(), |
| 72 | + }; |
| 73 | + |
| 74 | + service._handleGetLoggerLevels( |
| 75 | + { names: ['existing_logger', 'missing_logger'] }, |
| 76 | + response |
| 77 | + ); |
| 78 | + |
| 79 | + assert.strictEqual(response.send.calledOnce, true); |
| 80 | + assert.deepStrictEqual(response.send.firstCall.args[0].levels, [ |
| 81 | + { name: 'existing_logger', level: LOGGING_SEVERITY.INFO }, |
| 82 | + { name: 'missing_logger', level: LOGGING_SEVERITY.UNSET }, |
| 83 | + ]); |
| 84 | + }); |
| 85 | + |
| 86 | + it('sets logger levels and reports per-level failures', function () { |
| 87 | + sandbox.stub(Logging, 'getLogger').callsFake((name) => ({ |
| 88 | + setLoggerLevel(level) { |
| 89 | + if (name === 'bad_logger') { |
| 90 | + throw new Error(`failed to set ${level}`); |
| 91 | + } |
| 92 | + }, |
| 93 | + })); |
| 94 | + const service = new LoggingService({}); |
| 95 | + const response = { |
| 96 | + template: { results: [] }, |
| 97 | + send: sandbox.spy(), |
| 98 | + }; |
| 99 | + |
| 100 | + service._handleSetLoggerLevels( |
| 101 | + { |
| 102 | + levels: [ |
| 103 | + { name: 'good_logger', level: LOGGING_SEVERITY.DEBUG }, |
| 104 | + { name: 'bad_logger', level: 999 }, |
| 105 | + ], |
| 106 | + }, |
| 107 | + response |
| 108 | + ); |
| 109 | + |
| 110 | + assert.strictEqual(response.send.calledOnce, true); |
| 111 | + assert.deepStrictEqual(response.send.firstCall.args[0].results, [ |
| 112 | + { successful: true, reason: '' }, |
| 113 | + { successful: false, reason: 'failed to set 999' }, |
| 114 | + ]); |
| 115 | + }); |
| 116 | +}); |
0 commit comments