|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 4 | +import { SchemaRegistry, REGISTRY_LOG_LEVELS } from './registry'; |
| 5 | + |
| 6 | +/** |
| 7 | + * #3420 — the registry's expected-but-noisy housekeeping (re-registering an |
| 8 | + * owned object, overwriting a package manifest on a rebuild / HMR / multi-project |
| 9 | + * seed-replay) must NOT reach a stock `os dev` boot log. It used to be emitted |
| 10 | + * via `console.warn` (always on) and looked like an error, though it is a normal |
| 11 | + * path. It is now emitted at `debug`, so it stays out of the default `info` level |
| 12 | + * but `OS_REGISTRY_LOG=debug` (or `{ logLevel: 'debug' }`) makes it discoverable. |
| 13 | + * |
| 14 | + * This is the regression guard: if either line ever regresses back to `warn` |
| 15 | + * (or to the always-on `log`/`console.log`), the "silent at info" assertions |
| 16 | + * fail — keeping the official examples' boot log clean. |
| 17 | + */ |
| 18 | +describe('SchemaRegistry log-level gating (#3420)', () => { |
| 19 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 20 | + const debug = vi.spyOn(console, 'debug').mockImplementation(() => {}); |
| 21 | + beforeEach(() => { warn.mockClear(); debug.mockClear(); }); |
| 22 | + afterEach(() => { delete process.env.OS_REGISTRY_LOG; }); |
| 23 | + |
| 24 | + const reRegisterSameOwner = (r: SchemaRegistry) => { |
| 25 | + r.registerObject({ name: 'sys_thing', fields: {} } as any, 'com.acme.app', 'sys', 'own'); |
| 26 | + r.registerObject({ name: 'sys_thing', fields: {} } as any, 'com.acme.app', 'sys', 'own'); |
| 27 | + }; |
| 28 | + |
| 29 | + it('at the default (info) level, re-registering an owned object is silent — no warn, no debug', () => { |
| 30 | + const r = new SchemaRegistry({ multiTenant: false }); |
| 31 | + reRegisterSameOwner(r); |
| 32 | + expect(warn).not.toHaveBeenCalledWith(expect.stringContaining('Re-registering owned object')); |
| 33 | + expect(debug).not.toHaveBeenCalledWith(expect.stringContaining('Re-registering owned object')); |
| 34 | + }); |
| 35 | + |
| 36 | + it('at debug level, the re-register line is emitted via console.debug (never console.warn)', () => { |
| 37 | + const r = new SchemaRegistry({ multiTenant: false, logLevel: 'debug' }); |
| 38 | + reRegisterSameOwner(r); |
| 39 | + expect(debug).toHaveBeenCalledWith(expect.stringContaining('Re-registering owned object: sys_thing')); |
| 40 | + expect(warn).not.toHaveBeenCalledWith(expect.stringContaining('Re-registering owned object')); |
| 41 | + }); |
| 42 | + |
| 43 | + it('overwriting a package manifest is debug-gated the same way', () => { |
| 44 | + const manifest = { id: 'com.test', name: 'Test', namespace: 'test', version: '1.0.0' } as any; |
| 45 | + |
| 46 | + const info = new SchemaRegistry({ multiTenant: false }); |
| 47 | + info.installPackage(manifest); |
| 48 | + info.installPackage(manifest); // same-package reload → "Overwriting package" |
| 49 | + expect(warn).not.toHaveBeenCalledWith(expect.stringContaining('Overwriting package')); |
| 50 | + expect(debug).not.toHaveBeenCalledWith(expect.stringContaining('Overwriting package')); |
| 51 | + |
| 52 | + debug.mockClear(); |
| 53 | + const dbg = new SchemaRegistry({ multiTenant: false, logLevel: 'debug' }); |
| 54 | + dbg.installPackage(manifest); |
| 55 | + dbg.installPackage(manifest); |
| 56 | + expect(debug).toHaveBeenCalledWith(expect.stringContaining('Overwriting package: com.test')); |
| 57 | + }); |
| 58 | + |
| 59 | + it('resolves the level from OS_REGISTRY_LOG when no explicit option is given', () => { |
| 60 | + process.env.OS_REGISTRY_LOG = 'debug'; |
| 61 | + expect(new SchemaRegistry({ multiTenant: false }).logLevel).toBe('debug'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('falls back to info for an unrecognized OS_REGISTRY_LOG value', () => { |
| 65 | + process.env.OS_REGISTRY_LOG = 'chatty'; |
| 66 | + expect(new SchemaRegistry({ multiTenant: false }).logLevel).toBe('info'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('an explicit logLevel option wins over the env var', () => { |
| 70 | + process.env.OS_REGISTRY_LOG = 'debug'; |
| 71 | + expect(new SchemaRegistry({ multiTenant: false, logLevel: 'silent' }).logLevel).toBe('silent'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('exposes the full level vocabulary for validation', () => { |
| 75 | + expect(REGISTRY_LOG_LEVELS).toEqual(['debug', 'info', 'warn', 'error', 'silent']); |
| 76 | + }); |
| 77 | +}); |
0 commit comments