Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(logger): pin the whole level and node budget boundaries on the line
Yudhi Armyndharis1e4b53b00e23
diff --git a/src/common/services/logger.service.spec.ts b/src/common/services/logger.service.spec.tsindex 4cc65f331..d13d862b6 100644--- a/src/common/services/logger.service.spec.ts+++ b/src/common/services/logger.service.spec.ts@@ -159,6 +159,41 @@ describe('LoggerService', () => {expect(items[0]).toEqual({ id: 0 });expect(items[1499]).toBe('[Truncated]');});++ // Where the first five levels end and the node budget runs out, as they reach the line.+ describe('the boundaries of the whole levels and of the node budget', () => {+ const bare = (length: number): object[] => Array.from({ length }, () => ({}));+ const wide = (keys: number): Record<string, number> =>+ Object.fromEntries(Array.from({ length: keys }, (_, key) => [`k${key}`, key]));+ const at = (line: unknown, ...path: string[]): unknown =>+ path.reduce<unknown>((node, key) => (node as Record<string, unknown>)[key], line);+ const line = (metadata: unknown): unknown => {+ consoleSpy.mockClear();+ logger.log('cfg', metadata as never);+ return getLogOutput(consoleSpy);+ };++ it('copies exactly 1000 objects below the fifth level and writes the next as [Truncated]', () => {+ const rows = at(line({ a: { b: { c: { rows: bare(1001) } } } }), 'a', 'b', 'c', 'rows') as unknown[];++ expect(rows.slice(0, 1000)).toEqual(bare(1000));+ expect(rows[1000]).toBe('[Truncated]');+ });++ it('copies an object of the fifth level whole, whatever its number of keys', () => {+ const copy = at(line({ a: { b: { c: { wide: wide(1500) } } } }), 'a', 'b', 'c', 'wide') as object;++ expect(Object.keys(copy)).toHaveLength(1500);+ });++ it('copies an object of the sixth level that fits in what is left of the budget, and not a wider one', () => {+ const fits = at(line({ a: { b: { c: { d: { wide: wide(999) } } } } }), 'a', 'b', 'c', 'd', 'wide') as object;+ const wider = at(line({ a: { b: { c: { d: { wide: wide(1000) } } } } }), 'a', 'b', 'c', 'd', 'wide');++ expect(Object.keys(fits)).toHaveLength(999);+ expect(wider).toBe('[Truncated]');+ });+ });});// Plugin ctx.logger spreads its caller's metadata in verbatim, so the metadata is whatever the plugin holds.diff --git a/src/engine/adapters/baileys-logger.spec.ts b/src/engine/adapters/baileys-logger.spec.tsindex 0723881ae..85b6d408b 100644--- a/src/engine/adapters/baileys-logger.spec.ts+++ b/src/engine/adapters/baileys-logger.spec.ts@@ -123,6 +123,42 @@ describe('createBaileysLogger writes any record the library hands it', () => {expect(items[1499]).toEqual({ id: 1499, tags: [1499] });});+ describe('the boundaries of the whole levels and of the node budget', () => {+ const bare = (length: number): object[] => Array.from({ length }, () => ({}));+ const wide = (keys: number): Record<string, number> =>+ Object.fromEntries(Array.from({ length: keys }, (_, key) => [`k${key}`, key]));+ const at = (line: unknown, ...path: string[]): unknown =>+ path.reduce<unknown>((node, key) => (node as Record<string, unknown>)[key], line);++ it('copies exactly 1000 objects below the fifth level and writes the next as [Truncated]', () => {+ const rows = at(write({ a: { b: { c: { rows: bare(1001) } } } }, 'x'), 'a', 'b', 'c', 'rows') as unknown[];++ expect(rows.slice(0, 1000)).toEqual(bare(1000));+ expect(rows[1000]).toBe('[Truncated]');+ });++ it('copies an object of the fifth level whole, whatever its number of keys', () => {+ const copy = at(write({ a: { b: { c: { wide: wide(1500) } } } }, 'x'), 'a', 'b', 'c', 'wide') as object;++ expect(Object.keys(copy)).toHaveLength(1500);+ });++ it('copies an object of the sixth level that fits in what is left of the budget, and not a wider one', () => {+ const fits = at(+ write({ a: { b: { c: { d: { wide: wide(999) } } } } }, 'x'),+ 'a',+ 'b',+ 'c',+ 'd',+ 'wide',+ ) as object;+ const wider = at(write({ a: { b: { c: { d: { wide: wide(1000) } } } } }, 'x'), 'a', 'b', 'c', 'd', 'wide');++ expect(Object.keys(fits)).toHaveLength(999);+ expect(wider).toBe('[Truncated]');+ });+ });+it('replaces a credential nested deeper than five levels', () => {const line = JSON.stringify(write({ record: nest(8, { password: 'value-deep', visible: 'v' }) }, 'x'));