|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import assert from 'node:assert'; |
| 8 | +import {describe, it} from 'node:test'; |
| 9 | + |
| 10 | +import { |
| 11 | + ConsoleFormatter, |
| 12 | + GroupedConsoleFormatter, |
| 13 | +} from '../../src/formatters/ConsoleFormatter.js'; |
| 14 | +import type {ConsoleMessage} from '../../src/third_party/index.js'; |
| 15 | + |
| 16 | +const createMockMessage = ( |
| 17 | + type: string, |
| 18 | + text: string, |
| 19 | + argsCount = 0, |
| 20 | +): ConsoleMessage => { |
| 21 | + const args = Array.from({length: argsCount}, () => ({ |
| 22 | + jsonValue: async () => 'val', |
| 23 | + remoteObject: () => ({type: 'string'}), |
| 24 | + })); |
| 25 | + return { |
| 26 | + type: () => type, |
| 27 | + text: () => text, |
| 28 | + args: () => args, |
| 29 | + } as unknown as ConsoleMessage; |
| 30 | +}; |
| 31 | + |
| 32 | +const makeFormatter = (id: number, type: string, text: string, argsCount = 0) => |
| 33 | + ConsoleFormatter.from(createMockMessage(type, text, argsCount), {id}); |
| 34 | + |
| 35 | +describe('ConsoleFormatter grouping', () => { |
| 36 | + describe('groupConsecutive', () => { |
| 37 | + it('groups identical consecutive messages', async () => { |
| 38 | + const msgs = await Promise.all([ |
| 39 | + makeFormatter(1, 'log', 'hello'), |
| 40 | + makeFormatter(2, 'log', 'hello'), |
| 41 | + makeFormatter(3, 'log', 'hello'), |
| 42 | + ]); |
| 43 | + const grouped = ConsoleFormatter.groupConsecutive(msgs); |
| 44 | + assert.strictEqual(grouped.length, 1); |
| 45 | + assert.ok(grouped[0] instanceof GroupedConsoleFormatter); |
| 46 | + assert.ok(grouped[0].toString().includes('[3 times]')); |
| 47 | + }); |
| 48 | + |
| 49 | + it('does not group different messages', async () => { |
| 50 | + const msgs = await Promise.all([ |
| 51 | + makeFormatter(1, 'log', 'aaa'), |
| 52 | + makeFormatter(2, 'log', 'bbb'), |
| 53 | + makeFormatter(3, 'log', 'ccc'), |
| 54 | + ]); |
| 55 | + const grouped = ConsoleFormatter.groupConsecutive(msgs); |
| 56 | + assert.strictEqual(grouped.length, 3); |
| 57 | + for (const g of grouped) { |
| 58 | + assert.ok(!(g instanceof GroupedConsoleFormatter)); |
| 59 | + assert.ok(!g.toString().includes('times')); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + it('groups A,A,B,A,A correctly', async () => { |
| 64 | + const msgs = await Promise.all([ |
| 65 | + makeFormatter(1, 'log', 'A'), |
| 66 | + makeFormatter(2, 'log', 'A'), |
| 67 | + makeFormatter(3, 'log', 'B'), |
| 68 | + makeFormatter(4, 'log', 'A'), |
| 69 | + makeFormatter(5, 'log', 'A'), |
| 70 | + ]); |
| 71 | + const grouped = ConsoleFormatter.groupConsecutive(msgs); |
| 72 | + assert.strictEqual(grouped.length, 3); |
| 73 | + assert.ok(grouped[0] instanceof GroupedConsoleFormatter); |
| 74 | + assert.ok(grouped[0].toString().includes('[2 times]')); |
| 75 | + assert.ok(!(grouped[1] instanceof GroupedConsoleFormatter)); |
| 76 | + assert.ok(grouped[2] instanceof GroupedConsoleFormatter); |
| 77 | + assert.ok(grouped[2].toString().includes('[2 times]')); |
| 78 | + }); |
| 79 | + |
| 80 | + it('does not group messages with different types', async () => { |
| 81 | + const msgs = await Promise.all([ |
| 82 | + makeFormatter(1, 'log', 'hello'), |
| 83 | + makeFormatter(2, 'error', 'hello'), |
| 84 | + ]); |
| 85 | + const grouped = ConsoleFormatter.groupConsecutive(msgs); |
| 86 | + assert.strictEqual(grouped.length, 2); |
| 87 | + }); |
| 88 | + |
| 89 | + it('does not group messages with different argsCount', async () => { |
| 90 | + const msgs = await Promise.all([ |
| 91 | + makeFormatter(1, 'log', 'hello', 1), |
| 92 | + makeFormatter(2, 'log', 'hello', 2), |
| 93 | + ]); |
| 94 | + const grouped = ConsoleFormatter.groupConsecutive(msgs); |
| 95 | + assert.strictEqual(grouped.length, 2); |
| 96 | + }); |
| 97 | + |
| 98 | + it('returns empty array for empty input', () => { |
| 99 | + const grouped = ConsoleFormatter.groupConsecutive([]); |
| 100 | + assert.strictEqual(grouped.length, 0); |
| 101 | + }); |
| 102 | + |
| 103 | + it('handles single message', async () => { |
| 104 | + const msgs = await Promise.all([makeFormatter(1, 'log', 'solo')]); |
| 105 | + const grouped = ConsoleFormatter.groupConsecutive(msgs); |
| 106 | + assert.strictEqual(grouped.length, 1); |
| 107 | + assert.ok(!(grouped[0] instanceof GroupedConsoleFormatter)); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('GroupedConsoleFormatter output', () => { |
| 112 | + it('toString includes count suffix', async () => { |
| 113 | + const msgs = await Promise.all([ |
| 114 | + makeFormatter(1, 'log', 'hello'), |
| 115 | + makeFormatter(2, 'log', 'hello'), |
| 116 | + makeFormatter(3, 'log', 'hello'), |
| 117 | + makeFormatter(4, 'log', 'hello'), |
| 118 | + makeFormatter(5, 'log', 'hello'), |
| 119 | + ]); |
| 120 | + const grouped = ConsoleFormatter.groupConsecutive(msgs); |
| 121 | + assert.strictEqual(grouped.length, 1); |
| 122 | + const str = grouped[0].toString(); |
| 123 | + assert.ok(str.includes('[5 times]'), `expected [5 times] in: ${str}`); |
| 124 | + assert.ok(str.includes('msgid=1'), `expected msgid=1 in: ${str}`); |
| 125 | + }); |
| 126 | + |
| 127 | + it('toJSON includes count field', async () => { |
| 128 | + const msgs = await Promise.all([ |
| 129 | + makeFormatter(1, 'log', 'hello'), |
| 130 | + makeFormatter(2, 'log', 'hello'), |
| 131 | + makeFormatter(3, 'log', 'hello'), |
| 132 | + ]); |
| 133 | + const grouped = ConsoleFormatter.groupConsecutive(msgs); |
| 134 | + assert.strictEqual(grouped.length, 1); |
| 135 | + const json = (grouped[0] as GroupedConsoleFormatter).toJSON(); |
| 136 | + assert.strictEqual(json.count, 3); |
| 137 | + assert.strictEqual(json.id, 1); |
| 138 | + }); |
| 139 | + }); |
| 140 | +}); |
0 commit comments