|
| 1 | +// Copyright IBM Corp. and LoopBack contributors 2026. All Rights Reserved. |
| 2 | +// Node module: @loopback/core |
| 3 | +// This file is licensed under the MIT License. |
| 4 | +// License text available at https://opensource.org/licenses/MIT |
| 5 | + |
| 6 | +import {BindingKey} from '@loopback/context'; |
| 7 | +import {expect} from '@loopback/testlab'; |
| 8 | +import {CoreBindings, CoreTags} from '../..'; |
| 9 | + |
| 10 | +describe('CoreBindings', () => { |
| 11 | + describe('APPLICATION_INSTANCE', () => { |
| 12 | + it('has correct key', () => { |
| 13 | + expect(CoreBindings.APPLICATION_INSTANCE.key).to.equal( |
| 14 | + 'application.instance', |
| 15 | + ); |
| 16 | + }); |
| 17 | + |
| 18 | + it('is a BindingKey', () => { |
| 19 | + expect(CoreBindings.APPLICATION_INSTANCE).to.be.instanceOf(BindingKey); |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + describe('APPLICATION_CONFIG', () => { |
| 24 | + it('has correct key', () => { |
| 25 | + expect(CoreBindings.APPLICATION_CONFIG.key).to.equal( |
| 26 | + 'application.config', |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + it('is a BindingKey', () => { |
| 31 | + expect(CoreBindings.APPLICATION_CONFIG).to.be.instanceOf(BindingKey); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('APPLICATION_METADATA', () => { |
| 36 | + it('has correct key', () => { |
| 37 | + expect(CoreBindings.APPLICATION_METADATA.key).to.equal( |
| 38 | + 'application.metadata', |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + it('is a BindingKey', () => { |
| 43 | + expect(CoreBindings.APPLICATION_METADATA).to.be.instanceOf(BindingKey); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('SERVERS', () => { |
| 48 | + it('has correct value', () => { |
| 49 | + expect(CoreBindings.SERVERS).to.equal('servers'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('is a string', () => { |
| 53 | + expect(CoreBindings.SERVERS).to.be.a.String(); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('COMPONENTS', () => { |
| 58 | + it('has correct value', () => { |
| 59 | + expect(CoreBindings.COMPONENTS).to.equal('components'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('is a string', () => { |
| 63 | + expect(CoreBindings.COMPONENTS).to.be.a.String(); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('CONTROLLERS', () => { |
| 68 | + it('has correct value', () => { |
| 69 | + expect(CoreBindings.CONTROLLERS).to.equal('controllers'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('is a string', () => { |
| 73 | + expect(CoreBindings.CONTROLLERS).to.be.a.String(); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('CONTROLLER_CLASS', () => { |
| 78 | + it('has correct key', () => { |
| 79 | + expect(CoreBindings.CONTROLLER_CLASS.key).to.equal( |
| 80 | + 'controller.current.ctor', |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it('is a BindingKey', () => { |
| 85 | + expect(CoreBindings.CONTROLLER_CLASS).to.be.instanceOf(BindingKey); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('CONTROLLER_METHOD_NAME', () => { |
| 90 | + it('has correct key', () => { |
| 91 | + expect(CoreBindings.CONTROLLER_METHOD_NAME.key).to.equal( |
| 92 | + 'controller.current.operation', |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it('is a BindingKey', () => { |
| 97 | + expect(CoreBindings.CONTROLLER_METHOD_NAME).to.be.instanceOf(BindingKey); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('CONTROLLER_METHOD_META', () => { |
| 102 | + it('has correct value', () => { |
| 103 | + expect(CoreBindings.CONTROLLER_METHOD_META).to.equal( |
| 104 | + 'controller.method.meta', |
| 105 | + ); |
| 106 | + }); |
| 107 | + |
| 108 | + it('is a string', () => { |
| 109 | + expect(CoreBindings.CONTROLLER_METHOD_META).to.be.a.String(); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('CONTROLLER_CURRENT', () => { |
| 114 | + it('has correct key', () => { |
| 115 | + expect(CoreBindings.CONTROLLER_CURRENT.key).to.equal( |
| 116 | + 'controller.current', |
| 117 | + ); |
| 118 | + }); |
| 119 | + |
| 120 | + it('is a BindingKey', () => { |
| 121 | + expect(CoreBindings.CONTROLLER_CURRENT).to.be.instanceOf(BindingKey); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + describe('LIFE_CYCLE_OBSERVERS', () => { |
| 126 | + it('has correct value', () => { |
| 127 | + expect(CoreBindings.LIFE_CYCLE_OBSERVERS).to.equal('lifeCycleObservers'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('is a string', () => { |
| 131 | + expect(CoreBindings.LIFE_CYCLE_OBSERVERS).to.be.a.String(); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('LIFE_CYCLE_OBSERVER_REGISTRY', () => { |
| 136 | + it('has correct key', () => { |
| 137 | + expect(CoreBindings.LIFE_CYCLE_OBSERVER_REGISTRY.key).to.equal( |
| 138 | + 'lifeCycleObserver.registry', |
| 139 | + ); |
| 140 | + }); |
| 141 | + |
| 142 | + it('is a BindingKey', () => { |
| 143 | + expect(CoreBindings.LIFE_CYCLE_OBSERVER_REGISTRY).to.be.instanceOf( |
| 144 | + BindingKey, |
| 145 | + ); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + describe('LIFE_CYCLE_OBSERVER_OPTIONS', () => { |
| 150 | + it('has correct key', () => { |
| 151 | + expect(CoreBindings.LIFE_CYCLE_OBSERVER_OPTIONS.key).to.equal( |
| 152 | + 'lifeCycleObserver.options', |
| 153 | + ); |
| 154 | + }); |
| 155 | + |
| 156 | + it('is a BindingKey', () => { |
| 157 | + expect(CoreBindings.LIFE_CYCLE_OBSERVER_OPTIONS).to.be.instanceOf( |
| 158 | + BindingKey, |
| 159 | + ); |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + describe('namespace consistency', () => { |
| 164 | + it('all binding keys are unique', () => { |
| 165 | + const keys = [ |
| 166 | + CoreBindings.APPLICATION_INSTANCE.key, |
| 167 | + CoreBindings.APPLICATION_CONFIG.key, |
| 168 | + CoreBindings.APPLICATION_METADATA.key, |
| 169 | + CoreBindings.CONTROLLER_CLASS.key, |
| 170 | + CoreBindings.CONTROLLER_METHOD_NAME.key, |
| 171 | + CoreBindings.CONTROLLER_CURRENT.key, |
| 172 | + CoreBindings.LIFE_CYCLE_OBSERVER_REGISTRY.key, |
| 173 | + CoreBindings.LIFE_CYCLE_OBSERVER_OPTIONS.key, |
| 174 | + ]; |
| 175 | + |
| 176 | + const uniqueKeys = new Set(keys); |
| 177 | + expect(uniqueKeys.size).to.equal(keys.length); |
| 178 | + }); |
| 179 | + |
| 180 | + it('all string constants are unique', () => { |
| 181 | + const constants = [ |
| 182 | + CoreBindings.SERVERS, |
| 183 | + CoreBindings.COMPONENTS, |
| 184 | + CoreBindings.CONTROLLERS, |
| 185 | + CoreBindings.CONTROLLER_METHOD_META, |
| 186 | + CoreBindings.LIFE_CYCLE_OBSERVERS, |
| 187 | + ]; |
| 188 | + |
| 189 | + const uniqueConstants = new Set(constants); |
| 190 | + expect(uniqueConstants.size).to.equal(constants.length); |
| 191 | + }); |
| 192 | + }); |
| 193 | +}); |
| 194 | + |
| 195 | +describe('CoreTags', () => { |
| 196 | + describe('COMPONENT', () => { |
| 197 | + it('has correct value', () => { |
| 198 | + expect(CoreTags.COMPONENT).to.equal('component'); |
| 199 | + }); |
| 200 | + |
| 201 | + it('is a string', () => { |
| 202 | + expect(CoreTags.COMPONENT).to.be.a.String(); |
| 203 | + }); |
| 204 | + }); |
| 205 | + |
| 206 | + describe('SERVER', () => { |
| 207 | + it('has correct value', () => { |
| 208 | + expect(CoreTags.SERVER).to.equal('server'); |
| 209 | + }); |
| 210 | + |
| 211 | + it('is a string', () => { |
| 212 | + expect(CoreTags.SERVER).to.be.a.String(); |
| 213 | + }); |
| 214 | + }); |
| 215 | + |
| 216 | + describe('CONTROLLER', () => { |
| 217 | + it('has correct value', () => { |
| 218 | + expect(CoreTags.CONTROLLER).to.equal('controller'); |
| 219 | + }); |
| 220 | + |
| 221 | + it('is a string', () => { |
| 222 | + expect(CoreTags.CONTROLLER).to.be.a.String(); |
| 223 | + }); |
| 224 | + }); |
| 225 | + |
| 226 | + describe('SERVICE', () => { |
| 227 | + it('has correct value', () => { |
| 228 | + expect(CoreTags.SERVICE).to.equal('service'); |
| 229 | + }); |
| 230 | + |
| 231 | + it('is a string', () => { |
| 232 | + expect(CoreTags.SERVICE).to.be.a.String(); |
| 233 | + }); |
| 234 | + }); |
| 235 | + |
| 236 | + describe('SERVICE_INTERFACE', () => { |
| 237 | + it('has correct value', () => { |
| 238 | + expect(CoreTags.SERVICE_INTERFACE).to.equal('serviceInterface'); |
| 239 | + }); |
| 240 | + |
| 241 | + it('is a string', () => { |
| 242 | + expect(CoreTags.SERVICE_INTERFACE).to.be.a.String(); |
| 243 | + }); |
| 244 | + }); |
| 245 | + |
| 246 | + describe('LIFE_CYCLE_OBSERVER', () => { |
| 247 | + it('has correct value', () => { |
| 248 | + expect(CoreTags.LIFE_CYCLE_OBSERVER).to.equal('lifeCycleObserver'); |
| 249 | + }); |
| 250 | + |
| 251 | + it('is a string', () => { |
| 252 | + expect(CoreTags.LIFE_CYCLE_OBSERVER).to.be.a.String(); |
| 253 | + }); |
| 254 | + }); |
| 255 | + |
| 256 | + describe('LIFE_CYCLE_OBSERVER_GROUP', () => { |
| 257 | + it('has correct value', () => { |
| 258 | + expect(CoreTags.LIFE_CYCLE_OBSERVER_GROUP).to.equal( |
| 259 | + 'lifeCycleObserverGroup', |
| 260 | + ); |
| 261 | + }); |
| 262 | + |
| 263 | + it('is a string', () => { |
| 264 | + expect(CoreTags.LIFE_CYCLE_OBSERVER_GROUP).to.be.a.String(); |
| 265 | + }); |
| 266 | + }); |
| 267 | + |
| 268 | + describe('EXTENSION_FOR', () => { |
| 269 | + it('has correct value', () => { |
| 270 | + expect(CoreTags.EXTENSION_FOR).to.equal('extensionFor'); |
| 271 | + }); |
| 272 | + |
| 273 | + it('is a string', () => { |
| 274 | + expect(CoreTags.EXTENSION_FOR).to.be.a.String(); |
| 275 | + }); |
| 276 | + }); |
| 277 | + |
| 278 | + describe('EXTENSION_POINT', () => { |
| 279 | + it('has correct value', () => { |
| 280 | + expect(CoreTags.EXTENSION_POINT).to.equal('extensionPoint'); |
| 281 | + }); |
| 282 | + |
| 283 | + it('is a string', () => { |
| 284 | + expect(CoreTags.EXTENSION_POINT).to.be.a.String(); |
| 285 | + }); |
| 286 | + }); |
| 287 | + |
| 288 | + describe('tag uniqueness', () => { |
| 289 | + it('all tags are unique', () => { |
| 290 | + const tags = [ |
| 291 | + CoreTags.COMPONENT, |
| 292 | + CoreTags.SERVER, |
| 293 | + CoreTags.CONTROLLER, |
| 294 | + CoreTags.SERVICE, |
| 295 | + CoreTags.SERVICE_INTERFACE, |
| 296 | + CoreTags.LIFE_CYCLE_OBSERVER, |
| 297 | + CoreTags.LIFE_CYCLE_OBSERVER_GROUP, |
| 298 | + CoreTags.EXTENSION_FOR, |
| 299 | + CoreTags.EXTENSION_POINT, |
| 300 | + ]; |
| 301 | + |
| 302 | + const uniqueTags = new Set(tags); |
| 303 | + expect(uniqueTags.size).to.equal(tags.length); |
| 304 | + }); |
| 305 | + }); |
| 306 | + |
| 307 | + describe('tag naming conventions', () => { |
| 308 | + it('uses camelCase for tag names', () => { |
| 309 | + const tags = [ |
| 310 | + CoreTags.COMPONENT, |
| 311 | + CoreTags.SERVER, |
| 312 | + CoreTags.CONTROLLER, |
| 313 | + CoreTags.SERVICE, |
| 314 | + CoreTags.SERVICE_INTERFACE, |
| 315 | + CoreTags.LIFE_CYCLE_OBSERVER, |
| 316 | + CoreTags.LIFE_CYCLE_OBSERVER_GROUP, |
| 317 | + CoreTags.EXTENSION_FOR, |
| 318 | + CoreTags.EXTENSION_POINT, |
| 319 | + ]; |
| 320 | + |
| 321 | + for (const tag of tags) { |
| 322 | + // Check that tag doesn't contain spaces or special characters |
| 323 | + expect(tag).to.match(/^[a-zA-Z][a-zA-Z0-9]*$/); |
| 324 | + } |
| 325 | + }); |
| 326 | + }); |
| 327 | +}); |
| 328 | + |
| 329 | +describe('Keys and Tags Integration', () => { |
| 330 | + it('CoreBindings and CoreTags are separate namespaces', () => { |
| 331 | + // Ensure there's no overlap between binding keys and tags |
| 332 | + const bindingKeys = [ |
| 333 | + CoreBindings.APPLICATION_INSTANCE.key, |
| 334 | + CoreBindings.APPLICATION_CONFIG.key, |
| 335 | + CoreBindings.APPLICATION_METADATA.key, |
| 336 | + CoreBindings.SERVERS, |
| 337 | + CoreBindings.COMPONENTS, |
| 338 | + CoreBindings.CONTROLLERS, |
| 339 | + CoreBindings.CONTROLLER_CLASS.key, |
| 340 | + CoreBindings.CONTROLLER_METHOD_NAME.key, |
| 341 | + CoreBindings.CONTROLLER_METHOD_META, |
| 342 | + CoreBindings.CONTROLLER_CURRENT.key, |
| 343 | + CoreBindings.LIFE_CYCLE_OBSERVERS, |
| 344 | + CoreBindings.LIFE_CYCLE_OBSERVER_REGISTRY.key, |
| 345 | + CoreBindings.LIFE_CYCLE_OBSERVER_OPTIONS.key, |
| 346 | + ]; |
| 347 | + |
| 348 | + const tags = [ |
| 349 | + CoreTags.COMPONENT, |
| 350 | + CoreTags.SERVER, |
| 351 | + CoreTags.CONTROLLER, |
| 352 | + CoreTags.SERVICE, |
| 353 | + CoreTags.SERVICE_INTERFACE, |
| 354 | + CoreTags.LIFE_CYCLE_OBSERVER, |
| 355 | + CoreTags.LIFE_CYCLE_OBSERVER_GROUP, |
| 356 | + CoreTags.EXTENSION_FOR, |
| 357 | + CoreTags.EXTENSION_POINT, |
| 358 | + ]; |
| 359 | + |
| 360 | + // Tags and binding keys should be distinct |
| 361 | + const allValues = [...bindingKeys, ...tags]; |
| 362 | + const uniqueValues = new Set(allValues); |
| 363 | + expect(uniqueValues.size).to.equal(allValues.length); |
| 364 | + }); |
| 365 | + |
| 366 | + it('related bindings and tags use consistent naming', () => { |
| 367 | + // Check that related concepts use similar naming |
| 368 | + expect(CoreBindings.SERVERS).to.equal('servers'); |
| 369 | + expect(CoreTags.SERVER).to.equal('server'); |
| 370 | + |
| 371 | + expect(CoreBindings.COMPONENTS).to.equal('components'); |
| 372 | + expect(CoreTags.COMPONENT).to.equal('component'); |
| 373 | + |
| 374 | + expect(CoreBindings.CONTROLLERS).to.equal('controllers'); |
| 375 | + expect(CoreTags.CONTROLLER).to.equal('controller'); |
| 376 | + |
| 377 | + expect(CoreBindings.LIFE_CYCLE_OBSERVERS).to.equal('lifeCycleObservers'); |
| 378 | + expect(CoreTags.LIFE_CYCLE_OBSERVER).to.equal('lifeCycleObserver'); |
| 379 | + }); |
| 380 | +}); |
| 381 | + |
| 382 | +// Made with Bob |
0 commit comments