blob: e919813e75482aa63a44e4c505f41ff9010a7398 [file] [log] [blame]
// 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 chai from 'chai'; // not esm
import * as constant from '../src/constants';
import { CURRENT_VERSION, State } from '../src/state';
import { FakeWebviewAPi } from './util';
import { copyHeaderConst } from '../src/fields';
import { AndExpression, Filter, OrExpression } from '../src/filter';
before(() => {
chai.should();
});
describe('LoggingState', function () {
let vscode: FakeWebviewAPi;
let testFields = copyHeaderConst();
this.beforeEach(() => {
vscode = new FakeWebviewAPi();
testFields.timestamp.displayName = 'Uptime';
vscode.setState({
filter: 'moniker:foo',
version: CURRENT_VERSION,
fields: testFields,
wrappingLogs: false,
});
});
describe('#constructor', () => {
it('creates a new empty state when nothing was persisted', () => {
const state = new State(new FakeWebviewAPi());
state.currentFilter.should.deep.equal(new OrExpression([]));
state.currentFilterText.should.equal('');
state.currentFields.should.deep.equal(constant.LOGS_HEADERS);
state.shouldWrapLogs.should.be.true;
});
it('creates a new state from the persisted data', () => {
const state = new State(vscode);
state.currentFilterText.should.deep.equal('moniker:foo');
state.currentFields.should.deep.equal(testFields);
state.shouldWrapLogs.should.be.false;
});
it('creates a new default state when the persisted data is invalid', () => {
// @ts-ignore: for testing purposes simulate the case when vscode carries invalid data. In
// this case invalid data is when the selectedFilters object is missing.
vscode.setState({});
const state = new State(vscode);
state.currentFilterText.should.deep.equal('');
state.currentFilter.should.deep.equal(new OrExpression([]));
state.currentFields.should.deep.equal(constant.LOGS_HEADERS);
state.shouldWrapLogs.should.be.true;
});
it('checks the version when loading the state and loads an empty one if wrong', () => {
vscode.setState({
version: CURRENT_VERSION + 100,
filter: 'core',
fields: testFields,
wrappingLogs: true,
});
const state = new State(vscode);
state.currentFilterText.should.deep.equal('');
state.currentFilter.should.deep.equal(new OrExpression([]));
state.currentFields.should.deep.equal(constant.LOGS_HEADERS);
state.shouldWrapLogs.should.be.true;
});
});
describe('#registerFilters', () => {
it('updates the filters when new filters are received and notifies', () => {
const state = new State(vscode);
const filter = new AndExpression([
new Filter({
category: 'moniker',
operator: 'contains',
subCategory: undefined,
value: 'bar',
}),
new Filter({
category: 'severity',
operator: 'contains',
subCategory: undefined,
value: 'info',
}),
]);
let gotFilters = '';
state.addEventListener('filterChange', (event) => {
gotFilters = (event as CustomEvent).detail.filter;
});
let filterText = 'moniker:bar severity:info';
state.registerFilter(filter, filterText);
gotFilters.should.deep.equal(filter);
state.currentFilter.should.deep.equal(filter);
state.currentFilterText.should.deep.equal(filterText);
vscode.getState()!.should.deep.equal({
version: CURRENT_VERSION,
filter: filterText,
fields: testFields,
wrappingLogs: false,
});
});
});
describe('#reset', () => {
it('reloads from storage', () => {
const state = new State(vscode);
state.currentFilter.should.deep.equal(new Filter({
category: 'moniker',
operator: 'contains',
subCategory: undefined,
value: 'foo',
}));
state.currentFilterText.should.deep.equal('moniker:foo');
let testFields = constant.LOGS_HEADERS;
testFields.message.displayName = 'MSG';
vscode.setState({
filter: 'severity:info',
version: CURRENT_VERSION,
fields: testFields,
wrappingLogs: true,
});
state.reset();
state.currentFilter.should.deep.equal(new Filter({
category: 'severity',
operator: 'contains',
subCategory: undefined,
value: 'info',
})
);
state.currentFilterText.should.deep.equal('severity:info');
state.shouldWrapLogs.should.be.true;
});
});
});