Path: blob/main/extensions/copilot/src/platform/log/test/common/loggerHelpers.ts
13405 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { ILogTarget, LogLevel } from '../../common/logService';67export type TestLogMessage = {8level: LogLevel;9message: string;10};1112export class TestLogTarget implements ILogTarget {13private readonly _messages: TestLogMessage[] = [];1415logIt(level: LogLevel, messageString: string): void {16this._messages.push({ level, message: messageString });17}1819public hasMessage(level: LogLevel, message: string) {20return this._messages.some(21m =>22m.level === level && m.message === message23);24}2526public assertHasMessage(level: LogLevel, message: string) {27if (!this.hasMessage(level, message)) {28throw new Error(29`Expected message not found: ${LogLevel[level]} ${JSON.stringify(30message31)}. Actual messages: ${this._messages32.map(m => '\n- ' + LogLevel[m.level] + ': ' + JSON.stringify(m.message))33.join('')}`34);35}36}3738/**39* Checks for a logged message matching a given regex. Emulates40* OutputChannelLog for conversion of log message to string.41*/42hasMessageMatching(level: LogLevel, test: RegExp) {43return this._messages.some(m => m.level === level && test.test(m.message));44}4546public isEmpty() {47return this._messages.length === 0;48}49}505152