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