| // Copyright 2022 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| import * as constant from '../src/constants'; |
| import { describe, it } from 'mocha'; |
| import { ffxEventForTest, malformedLogForTest, symbolizedDataForTest } from './util'; |
| import { FfxLogData, LogPayload } from '../src/log_data'; |
| import { LogRow } from '../components/log_row'; |
| import { messageForEvent } from '../src/format_log_text'; |
| |
| /** |
| * Creates a fake log data to be used in tests. |
| * |
| * @param payload payload to use for the fake log data. |
| * @returns a log data to use for testing. |
| */ |
| function logDataForTest(payload: LogPayload): FfxLogData { |
| return { |
| data: { |
| // eslint-disable-next-line @typescript-eslint/naming-convention |
| TargetLog: { |
| moniker: 'core/foo', |
| metadata: { |
| errors: [], |
| // eslint-disable-next-line @typescript-eslint/naming-convention |
| component_url: 'fuchsia-pkg://fuchsia.com/foo#metafoo.cm', |
| timestamp: 12345678900, |
| severity: 'Info', |
| tags: ['bar', 'baz'], |
| pid: 123, |
| tid: 456, |
| file: 'foo/main.rs', |
| line: 25, |
| }, |
| payload, |
| }, |
| }, |
| timestamp: 12345, |
| version: 1 |
| }; |
| }; |
| |
| describe('LogRow', () => { |
| describe('#constructor', () => { |
| it('adds title to to non-message cells', () => { |
| const logRow = new LogRow(logDataForTest({ |
| root: { |
| message: { |
| value: 'hello vscode', |
| }, |
| keys: null, |
| printf: null, |
| } |
| }), false).element; |
| const testFields = ['000012.35', '123', '456', 'core/foo', 'bar,baz', 'Info', 'hello vscode']; |
| let field: keyof typeof testFields; |
| for (field in testFields) { |
| if (testFields[field] === 'hello vscode') { |
| return; |
| } |
| const logField = logRow.children[field] as HTMLElement; |
| logField.title.should.equal(testFields[field]); |
| } |
| }); |
| |
| it('formats target logs', () => { |
| const logRow = new LogRow(logDataForTest({ |
| root: { |
| message: { |
| value: 'hello vscode', |
| }, |
| keys: null, |
| printf: null, |
| } |
| }), false).element; |
| const testFields = ['000012.35', '123', '456', 'core/foo', 'bar,baz', 'Info', 'hello vscode']; |
| let field: keyof typeof testFields; |
| for (field in testFields) { |
| const logField = logRow.children[field] as HTMLElement; |
| logField.innerText.should.equal(testFields[field]); |
| } |
| }); |
| |
| it('formats structured logs', () => { |
| const logRow = new LogRow(logDataForTest({ |
| root: { |
| message: { |
| value: 'hello vscode', |
| }, |
| keys: { |
| os: 'fuchsia', |
| number: 1, |
| }, |
| printf: null, |
| } |
| }), false).element; |
| const testFields = |
| ['000012.35', '123', '456', 'core/foo', 'bar,baz', |
| 'Info', 'hello vscodeos=fuchsia number=1']; |
| let field: keyof typeof testFields; |
| for (field in testFields) { |
| const logField = logRow.children[field] as HTMLElement; |
| logField.innerText.should.equal(testFields[field]); |
| } |
| }); |
| |
| it('formats printf logs', () => { |
| const logRow = new LogRow(logDataForTest({ |
| root: { |
| message: null, |
| keys: null, |
| printf: { |
| format: '%s is #%d', |
| args: ['Fuchsia', 1] |
| }, |
| } |
| }), false).element; |
| const testFields = |
| ['000012.35', '123', '456', 'core/foo', 'bar,baz', 'Info', '%s is #%d args=[Fuchsia, 1]']; |
| let field: keyof typeof testFields; |
| for (field in testFields) { |
| const logField = logRow.children[field] as HTMLElement; |
| logField.innerText.should.equal(testFields[field]); |
| } |
| }); |
| |
| it('formats symbolized log', () => { |
| const logRow = new LogRow(symbolizedDataForTest({ |
| root: { |
| message: { |
| value: 'this is ignored', |
| }, |
| keys: null, |
| printf: null, |
| } |
| }), false).element; |
| const testFields = |
| ['000000.12', '123', '456', 'core/foo', 'my_tag', 'Info', 'symbolized']; |
| let field: keyof typeof testFields; |
| for (field in testFields) { |
| const logField = logRow.children[field] as HTMLElement; |
| logField.innerText.should.equal(testFields[field]); |
| } |
| }); |
| |
| it('formats logging started events', () => { |
| let logRow = new LogRow(ffxEventForTest('LoggingStarted'), false).element; |
| let moniker = constant.FFX_MONIKER; |
| let msg = messageForEvent('LoggingStarted', false); |
| let monikerEl = logRow.children[3] as HTMLElement; |
| let msgEl = logRow.children[6] as HTMLElement; |
| |
| monikerEl.innerText.should.equal(moniker); |
| msgEl.innerText.should.equal(msg); |
| |
| logRow = new LogRow(ffxEventForTest('LoggingStarted'), true).element; |
| msg = messageForEvent('LoggingStarted', true); |
| monikerEl = logRow.children[3] as HTMLElement; |
| msgEl = logRow.children[6] as HTMLElement; |
| |
| monikerEl.innerText.should.equal(moniker); |
| msgEl.innerText.should.equal(msg); |
| }); |
| |
| it('formats target disconnected events', () => { |
| const logRow = new LogRow(ffxEventForTest('TargetDisconnected'), false).element; |
| let moniker = constant.FFX_MONIKER; |
| let msg = messageForEvent('TargetDisconnected', true); |
| let monikerEl = logRow.children[3] as HTMLElement; |
| let msgEl = logRow.children[6] as HTMLElement; |
| |
| monikerEl.innerText.should.equal(moniker); |
| msgEl.innerText.should.equal(msg); |
| }); |
| |
| it('formats malformed log', () => { |
| const logRow = new LogRow(malformedLogForTest('hello world'), false).element; |
| let msg = 'Malformed target log: hello world'; |
| let msgEl = logRow.children[6] as HTMLElement; |
| msgEl.innerText.should.equal(msg); |
| }); |
| |
| }); |
| }); |