mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-09-30 17:40:53 -07:00
69 lines
2.1 KiB
TypeScript
69 lines
2.1 KiB
TypeScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import path from 'node:path';
|
|
import { EventEmitter } from 'node:events';
|
|
import { ignoreClosedPipeErrors, resolveDefaultLogFilePath, setLogRotation } from './logger';
|
|
import { localDateKey } from './shared/log-files';
|
|
|
|
test('resolveDefaultLogFilePath uses APPDATA on windows', () => {
|
|
const today = localDateKey(new Date());
|
|
const resolved = resolveDefaultLogFilePath({
|
|
platform: 'win32',
|
|
homeDir: 'C:\\Users\\tester',
|
|
appDataDir: 'C:\\Users\\tester\\AppData\\Roaming',
|
|
});
|
|
|
|
assert.equal(
|
|
path.normalize(resolved),
|
|
path.normalize(
|
|
path.join('C:\\Users\\tester\\AppData\\Roaming', 'SubMiner', 'logs', `app-${today}.log`),
|
|
),
|
|
);
|
|
});
|
|
|
|
test('resolveDefaultLogFilePath uses .config on linux', () => {
|
|
const today = localDateKey(new Date());
|
|
const resolved = resolveDefaultLogFilePath({
|
|
platform: 'linux',
|
|
homeDir: '/home/tester',
|
|
});
|
|
|
|
assert.equal(
|
|
resolved,
|
|
path.join('/home/tester', '.config', 'SubMiner', 'logs', `app-${today}.log`),
|
|
);
|
|
});
|
|
|
|
test('setLogRotation accepts numeric retention days', () => {
|
|
const previous = process.env.SUBMINER_LOG_ROTATION;
|
|
const today = localDateKey(new Date());
|
|
setLogRotation(14);
|
|
try {
|
|
const resolved = resolveDefaultLogFilePath({
|
|
platform: 'linux',
|
|
homeDir: '/home/tester',
|
|
});
|
|
|
|
assert.equal(
|
|
resolved,
|
|
path.join('/home/tester', '.config', 'SubMiner', 'logs', `app-${today}.log`),
|
|
);
|
|
assert.equal(process.env.SUBMINER_LOG_ROTATION, '14');
|
|
} finally {
|
|
if (previous == null) {
|
|
delete process.env.SUBMINER_LOG_ROTATION;
|
|
} else {
|
|
process.env.SUBMINER_LOG_ROTATION = previous;
|
|
}
|
|
}
|
|
});
|
|
|
|
test('ignoreClosedPipeErrors swallows broken-pipe errors but rethrows others', () => {
|
|
const stream = new EventEmitter();
|
|
ignoreClosedPipeErrors(stream);
|
|
const closedPipe = Object.assign(new Error('write EPIPE'), { code: 'EPIPE' });
|
|
assert.doesNotThrow(() => stream.emit('error', closedPipe));
|
|
const other = Object.assign(new Error('boom'), { code: 'EACCES' });
|
|
assert.throws(() => stream.emit('error', other), /boom/);
|
|
});
|