2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2018 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-02-22 18:21:53 +00:00
|
|
|
// NOTE: Temporarily allow `then` until we convert the entire file to `async` / `await`:
|
|
|
|
/* eslint-disable more/no-then */
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as fse from 'fs-extra';
|
|
|
|
import * as os from 'os';
|
|
|
|
import * as path from 'path';
|
2022-01-19 19:19:08 +00:00
|
|
|
import { assert } from 'chai';
|
2018-01-08 21:19:25 +00:00
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
import {
|
2018-01-08 21:19:25 +00:00
|
|
|
eliminateOutOfDateFiles,
|
|
|
|
eliminateOldEntries,
|
|
|
|
isLineAfterDate,
|
|
|
|
fetchLog,
|
2021-07-30 16:43:16 +00:00
|
|
|
fetchLogs,
|
2021-01-27 21:13:33 +00:00
|
|
|
} from '../logging/main_process_logging';
|
2018-01-08 21:19:25 +00:00
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
describe('logging', () => {
|
|
|
|
const fakeLogEntry = ({
|
|
|
|
level = 30,
|
|
|
|
msg = 'hello world',
|
|
|
|
time = new Date().toISOString(),
|
|
|
|
}: {
|
|
|
|
level?: number;
|
|
|
|
msg?: string;
|
|
|
|
time?: string;
|
|
|
|
}): Record<string, unknown> => ({
|
|
|
|
level,
|
|
|
|
msg,
|
|
|
|
time,
|
|
|
|
});
|
2018-01-08 21:19:25 +00:00
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
const fakeLogLine = (...args: Parameters<typeof fakeLogEntry>): string =>
|
|
|
|
JSON.stringify(fakeLogEntry(...args));
|
|
|
|
|
|
|
|
let tmpDir: string;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
tmpDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'signal-test-'));
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
afterEach(async () => {
|
|
|
|
await fse.remove(tmpDir);
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#isLineAfterDate', () => {
|
|
|
|
it('returns false if falsy', () => {
|
|
|
|
const actual = isLineAfterDate('', new Date());
|
2022-01-19 19:19:08 +00:00
|
|
|
assert.isFalse(actual);
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
it('returns false if invalid JSON', () => {
|
|
|
|
const actual = isLineAfterDate('{{}', new Date());
|
2022-01-19 19:19:08 +00:00
|
|
|
assert.isFalse(actual);
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
it('returns false if date is invalid', () => {
|
|
|
|
const line = JSON.stringify({ time: '2018-01-04T19:17:05.014Z' });
|
|
|
|
const actual = isLineAfterDate(line, new Date('try6'));
|
2022-01-19 19:19:08 +00:00
|
|
|
assert.isFalse(actual);
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
it('returns false if log time is invalid', () => {
|
|
|
|
const line = JSON.stringify({ time: 'try7' });
|
|
|
|
const date = new Date('2018-01-04T19:17:00.000Z');
|
|
|
|
const actual = isLineAfterDate(line, date);
|
2022-01-19 19:19:08 +00:00
|
|
|
assert.isFalse(actual);
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
it('returns false if date before provided date', () => {
|
|
|
|
const line = JSON.stringify({ time: '2018-01-04T19:17:00.000Z' });
|
|
|
|
const date = new Date('2018-01-04T19:17:05.014Z');
|
|
|
|
const actual = isLineAfterDate(line, date);
|
2022-01-19 19:19:08 +00:00
|
|
|
assert.isFalse(actual);
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
it('returns true if date is after provided date', () => {
|
|
|
|
const line = JSON.stringify({ time: '2018-01-04T19:17:05.014Z' });
|
|
|
|
const date = new Date('2018-01-04T19:17:00.000Z');
|
|
|
|
const actual = isLineAfterDate(line, date);
|
2022-01-19 19:19:08 +00:00
|
|
|
assert.isTrue(actual);
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#eliminateOutOfDateFiles', () => {
|
|
|
|
it('deletes an empty file', () => {
|
|
|
|
const date = new Date();
|
|
|
|
const log = '\n';
|
2021-01-27 21:13:33 +00:00
|
|
|
const target = path.join(tmpDir, 'log.log');
|
2018-01-08 21:19:25 +00:00
|
|
|
fs.writeFileSync(target, log);
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
return eliminateOutOfDateFiles(tmpDir, date).then(() => {
|
2022-01-19 19:19:08 +00:00
|
|
|
assert.isFalse(fs.existsSync(target));
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
it('deletes a file with invalid JSON lines', () => {
|
|
|
|
const date = new Date();
|
|
|
|
const log = '{{}\n';
|
2021-01-27 21:13:33 +00:00
|
|
|
const target = path.join(tmpDir, 'log.log');
|
2018-01-08 21:19:25 +00:00
|
|
|
fs.writeFileSync(target, log);
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
return eliminateOutOfDateFiles(tmpDir, date).then(() => {
|
2022-01-19 19:19:08 +00:00
|
|
|
assert.isFalse(fs.existsSync(target));
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
it('deletes a file with all dates before provided date', () => {
|
|
|
|
const date = new Date('2018-01-04T19:17:05.014Z');
|
|
|
|
const contents = [
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:00.014Z' }),
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:01.014Z' }),
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:02.014Z' }),
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:03.014Z' }),
|
|
|
|
].join('\n');
|
2021-01-27 21:13:33 +00:00
|
|
|
const target = path.join(tmpDir, 'log.log');
|
2018-01-08 21:19:25 +00:00
|
|
|
fs.writeFileSync(target, contents);
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
return eliminateOutOfDateFiles(tmpDir, date).then(() => {
|
2022-01-19 19:19:08 +00:00
|
|
|
assert.isFalse(fs.existsSync(target));
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
it('keeps a file with first line date before provided date', () => {
|
|
|
|
const date = new Date('2018-01-04T19:16:00.000Z');
|
|
|
|
const contents = [
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:00.014Z' }),
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:01.014Z' }),
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:02.014Z' }),
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:03.014Z' }),
|
|
|
|
].join('\n');
|
2021-01-27 21:13:33 +00:00
|
|
|
const target = path.join(tmpDir, 'log.log');
|
2018-01-08 21:19:25 +00:00
|
|
|
fs.writeFileSync(target, contents);
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
return eliminateOutOfDateFiles(tmpDir, date).then(() => {
|
2022-01-19 19:19:08 +00:00
|
|
|
assert.isTrue(fs.existsSync(target));
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
it('keeps a file with last line date before provided date', () => {
|
|
|
|
const date = new Date('2018-01-04T19:17:01.000Z');
|
|
|
|
const contents = [
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:00.014Z' }),
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:01.014Z' }),
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:02.014Z' }),
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:03.014Z' }),
|
|
|
|
].join('\n');
|
2021-01-27 21:13:33 +00:00
|
|
|
const target = path.join(tmpDir, 'log.log');
|
2018-01-08 21:19:25 +00:00
|
|
|
fs.writeFileSync(target, contents);
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
return eliminateOutOfDateFiles(tmpDir, date).then(() => {
|
2022-01-19 19:19:08 +00:00
|
|
|
assert.isTrue(fs.existsSync(target));
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#eliminateOldEntries', () => {
|
|
|
|
it('eliminates all non-parsing entries', () => {
|
|
|
|
const date = new Date('2018-01-04T19:17:01.000Z');
|
|
|
|
const contents = [
|
|
|
|
'random line',
|
2021-01-27 21:13:33 +00:00
|
|
|
fakeLogLine({ time: '2018-01-04T19:17:01.014Z' }),
|
|
|
|
fakeLogLine({ time: '2018-01-04T19:17:02.014Z' }),
|
|
|
|
fakeLogLine({ time: '2018-01-04T19:17:03.014Z' }),
|
2018-01-08 21:19:25 +00:00
|
|
|
].join('\n');
|
|
|
|
const expected = [
|
2021-01-27 21:13:33 +00:00
|
|
|
fakeLogEntry({ time: '2018-01-04T19:17:01.014Z' }),
|
|
|
|
fakeLogEntry({ time: '2018-01-04T19:17:02.014Z' }),
|
|
|
|
fakeLogEntry({ time: '2018-01-04T19:17:03.014Z' }),
|
|
|
|
];
|
2018-01-08 21:19:25 +00:00
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
const target = path.join(tmpDir, 'log.log');
|
2018-04-27 21:25:04 +00:00
|
|
|
const files = [
|
|
|
|
{
|
|
|
|
path: target,
|
|
|
|
},
|
|
|
|
];
|
2018-01-08 21:19:25 +00:00
|
|
|
|
|
|
|
fs.writeFileSync(target, contents);
|
|
|
|
|
|
|
|
return eliminateOldEntries(files, date).then(() => {
|
2021-01-27 21:13:33 +00:00
|
|
|
const actualEntries = fs
|
|
|
|
.readFileSync(target, 'utf8')
|
|
|
|
.split('\n')
|
|
|
|
.map(line => line.trim())
|
|
|
|
.filter(Boolean)
|
|
|
|
.map(line => JSON.parse(line));
|
2022-01-19 19:19:08 +00:00
|
|
|
assert.deepStrictEqual(actualEntries, expected);
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
it('preserves all lines if before target date', () => {
|
|
|
|
const date = new Date('2018-01-04T19:17:03.000Z');
|
|
|
|
const contents = [
|
|
|
|
'random line',
|
2021-01-27 21:13:33 +00:00
|
|
|
fakeLogLine({ time: '2018-01-04T19:17:01.014Z' }),
|
|
|
|
fakeLogLine({ time: '2018-01-04T19:17:02.014Z' }),
|
|
|
|
fakeLogLine({ time: '2018-01-04T19:17:03.014Z' }),
|
2018-01-08 21:19:25 +00:00
|
|
|
].join('\n');
|
2021-01-27 21:13:33 +00:00
|
|
|
const expected = fakeLogEntry({ time: '2018-01-04T19:17:03.014Z' });
|
2018-01-08 21:19:25 +00:00
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
const target = path.join(tmpDir, 'log.log');
|
2018-04-27 21:25:04 +00:00
|
|
|
const files = [
|
|
|
|
{
|
|
|
|
path: target,
|
|
|
|
},
|
|
|
|
];
|
2018-01-08 21:19:25 +00:00
|
|
|
|
|
|
|
fs.writeFileSync(target, contents);
|
|
|
|
|
|
|
|
return eliminateOldEntries(files, date).then(() => {
|
2021-01-27 21:13:33 +00:00
|
|
|
// There should only be 1 line, so we can parse it safely.
|
2022-01-19 19:19:08 +00:00
|
|
|
assert.deepStrictEqual(
|
|
|
|
JSON.parse(fs.readFileSync(target, 'utf8')),
|
2021-01-27 21:13:33 +00:00
|
|
|
expected
|
|
|
|
);
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#fetchLog', () => {
|
|
|
|
it('returns error if file does not exist', () => {
|
|
|
|
const target = 'random_file';
|
2018-04-27 21:25:04 +00:00
|
|
|
return fetchLog(target).then(
|
|
|
|
() => {
|
|
|
|
throw new Error('Expected an error!');
|
|
|
|
},
|
|
|
|
error => {
|
2022-01-19 19:19:08 +00:00
|
|
|
assert.match(error.message, /random_file/);
|
2018-04-27 21:25:04 +00:00
|
|
|
}
|
|
|
|
);
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
it('returns empty array if file has no valid JSON lines', () => {
|
|
|
|
const contents = 'line 1\nline2\n';
|
2021-01-27 21:13:33 +00:00
|
|
|
const target = path.join(tmpDir, 'test.log');
|
2018-01-08 21:19:25 +00:00
|
|
|
|
|
|
|
fs.writeFileSync(target, contents);
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
return fetchLog(target).then(result => {
|
2022-01-19 19:19:08 +00:00
|
|
|
assert.isEmpty(result);
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
it('returns just three fields in each returned line', () => {
|
|
|
|
const contents = [
|
|
|
|
JSON.stringify({
|
|
|
|
one: 1,
|
|
|
|
two: 2,
|
2021-01-27 21:13:33 +00:00
|
|
|
level: 30,
|
|
|
|
time: '2020-04-20T06:09:08.000Z',
|
|
|
|
msg: 'message 1',
|
2018-01-08 21:19:25 +00:00
|
|
|
}),
|
|
|
|
JSON.stringify({
|
|
|
|
one: 1,
|
|
|
|
two: 2,
|
2021-01-27 21:13:33 +00:00
|
|
|
level: 40,
|
|
|
|
time: '2021-04-20T06:09:08.000Z',
|
|
|
|
msg: 'message 2',
|
2018-01-08 21:19:25 +00:00
|
|
|
}),
|
|
|
|
'',
|
|
|
|
].join('\n');
|
2018-04-27 21:25:04 +00:00
|
|
|
const expected = [
|
|
|
|
{
|
2021-01-27 21:13:33 +00:00
|
|
|
level: 30,
|
|
|
|
time: '2020-04-20T06:09:08.000Z',
|
|
|
|
msg: 'message 1',
|
2018-04-27 21:25:04 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-27 21:13:33 +00:00
|
|
|
level: 40,
|
|
|
|
time: '2021-04-20T06:09:08.000Z',
|
|
|
|
msg: 'message 2',
|
2018-04-27 21:25:04 +00:00
|
|
|
},
|
|
|
|
];
|
2018-01-08 21:19:25 +00:00
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
const target = path.join(tmpDir, 'test.log');
|
2018-01-08 21:19:25 +00:00
|
|
|
|
|
|
|
fs.writeFileSync(target, contents);
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
return fetchLog(target).then(result => {
|
2022-01-19 19:19:08 +00:00
|
|
|
assert.deepStrictEqual(result, expected);
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-07-30 16:43:16 +00:00
|
|
|
describe('#fetchLogs', () => {
|
2018-01-08 21:19:25 +00:00
|
|
|
it('returns single entry if no files', () => {
|
2021-07-30 16:43:16 +00:00
|
|
|
return fetchLogs(tmpDir).then(results => {
|
2022-01-19 19:19:08 +00:00
|
|
|
assert.lengthOf(results, 1);
|
|
|
|
assert.match(results[0]?.msg || '', /Loaded this list/);
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
it('returns sorted entries from all files', () => {
|
|
|
|
const first = [
|
2021-01-27 21:13:33 +00:00
|
|
|
fakeLogLine({ msg: '2', time: '2018-01-04T19:17:05.014Z' }),
|
2018-01-08 21:19:25 +00:00
|
|
|
'',
|
|
|
|
].join('\n');
|
|
|
|
const second = [
|
2021-01-27 21:13:33 +00:00
|
|
|
fakeLogLine({ msg: '1', time: '2018-01-04T19:17:00.014Z' }),
|
|
|
|
fakeLogLine({ msg: '3', time: '2018-01-04T19:18:00.014Z' }),
|
2018-01-08 21:19:25 +00:00
|
|
|
'',
|
|
|
|
].join('\n');
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
fs.writeFileSync(path.join(tmpDir, 'first.log'), first);
|
|
|
|
fs.writeFileSync(path.join(tmpDir, 'second.log'), second);
|
2018-01-08 21:19:25 +00:00
|
|
|
|
2021-07-30 16:43:16 +00:00
|
|
|
return fetchLogs(tmpDir).then(results => {
|
2022-01-19 19:19:08 +00:00
|
|
|
assert.lengthOf(results, 4);
|
|
|
|
assert.strictEqual(results[0]?.msg, '1');
|
|
|
|
assert.strictEqual(results[1]?.msg, '2');
|
|
|
|
assert.strictEqual(results[2]?.msg, '3');
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|