Update Tests & Make TypeScript happy

This commit is contained in:
SukkaW 2024-10-02 21:33:59 +08:00
parent 9735d05956
commit d1041f0e59
7 changed files with 31 additions and 12 deletions

View File

@ -7,7 +7,7 @@ import { SOURCE_DIR } from '../constants/dir';
const file = path.join(SOURCE_DIR, 'domainset/cdn.conf'); const file = path.join(SOURCE_DIR, 'domainset/cdn.conf');
group('read file by line', () => { group(() => {
bench('readFileByLine', () => processLineFromReadline(readFileByLine(file))); bench('readFileByLine', () => processLineFromReadline(readFileByLine(file)));
bench('fsp.readFile', () => fsp.readFile(file, 'utf-8').then((content) => content.split('\n').filter(processLine))); bench('fsp.readFile', () => fsp.readFile(file, 'utf-8').then((content) => content.split('\n').filter(processLine)));
}); });

View File

@ -11,7 +11,7 @@ describe('parse', () => {
const MUTABLE_PARSE_LINE_RESULT: [string, ParseType] = ['', 1000]; const MUTABLE_PARSE_LINE_RESULT: [string, ParseType] = ['', 1000];
it('||top.mail.ru^$badfilter', () => { it('||top.mail.ru^$badfilter', () => {
console.log(parse('||top.mail.ru^$badfilter', MUTABLE_PARSE_LINE_RESULT)); console.log(parse('||top.mail.ru^$badfilter', MUTABLE_PARSE_LINE_RESULT, false));
}); });
}); });

View File

@ -15,6 +15,8 @@ export class IPListOutput extends RuleOutput<Preprocessed> {
super(span, id); super(span, id);
} }
mitmSgmodule = undefined;
protected preprocess() { protected preprocess() {
const results: string[] = []; const results: string[] = [];
appendArrayInPlace( appendArrayInPlace(

View File

@ -6,16 +6,16 @@ import { bench, group, run } from 'mitata';
(async () => { (async () => {
const data = await processLineFromReadline(await fetchRemoteTextByLine('https://osint.digitalside.it/Threat-Intel/lists/latestdomains.txt')); const data = await processLineFromReadline(await fetchRemoteTextByLine('https://osint.digitalside.it/Threat-Intel/lists/latestdomains.txt'));
group('setAddFromArray', () => { group(() => {
bench('run', () => { bench('setAddFromArray', () => {
const set = new Set(['1', '2', '1', '3', 'skk.moe']); const set = new Set(['1', '2', '1', '3', 'skk.moe']);
for (let i = 0, len = data.length; i < len; i++) { for (let i = 0, len = data.length; i < len; i++) {
set.add(data[i]); set.add(data[i]);
} }
}); });
}); });
group('setAddFromArray', () => { group(() => {
bench('run', () => { bench('', () => {
const set = new Set(['1', '2', '1', '3', 'skk.moe']); const set = new Set(['1', '2', '1', '3', 'skk.moe']);
// eslint-disable-next-line @typescript-eslint/unbound-method -- thisArg is passed // eslint-disable-next-line @typescript-eslint/unbound-method -- thisArg is passed
data.forEach(set.add, set); data.forEach(set.add, set);

View File

@ -7,8 +7,8 @@ import { bench, group, run } from 'mitata';
(async () => { (async () => {
const data = await processLineFromReadline(await fetchRemoteTextByLine('https://osint.digitalside.it/Threat-Intel/lists/latestdomains.txt')); const data = await processLineFromReadline(await fetchRemoteTextByLine('https://osint.digitalside.it/Threat-Intel/lists/latestdomains.txt'));
group('sortDomains', () => { group(() => {
bench('run', () => sortDomains(data)); bench('sortDomains', () => sortDomains(data));
}); });
run(); run();

View File

@ -18,7 +18,7 @@ import * as tldtsExperimental from 'tldts-experimental';
}; };
(['getDomain', 'getPublicSuffix', 'getSubdomain', 'parse'] as const).forEach(methodName => { (['getDomain', 'getPublicSuffix', 'getSubdomain', 'parse'] as const).forEach(methodName => {
group(methodName, () => { group(() => {
bench('tldts', () => { bench('tldts', () => {
for (let i = 0, len = data.length; i < len; i++) { for (let i = 0, len = data.length; i < len; i++) {
tldts[methodName](data[i], tldtsOpt); tldts[methodName](data[i], tldtsOpt);

View File

@ -35,7 +35,7 @@ describe('hostname to tokens', () => {
describe('Trie', () => { describe('Trie', () => {
it('should be possible to add domains to a Trie.', () => { it('should be possible to add domains to a Trie.', () => {
const trie = createTrie(); const trie = createTrie(null, false);
trie.add('a.skk.moe'); trie.add('a.skk.moe');
trie.add('skk.moe'); trie.add('skk.moe');
@ -52,7 +52,7 @@ describe('Trie', () => {
}); });
it('adding the same item several times should not increase size.', () => { it('adding the same item several times should not increase size.', () => {
const trie = createTrie(); const trie = createTrie(null, false);
trie.add('skk.moe'); trie.add('skk.moe');
trie.add('blog.skk.moe'); trie.add('blog.skk.moe');
@ -63,7 +63,7 @@ describe('Trie', () => {
}); });
it('should be possible to set the null sequence.', () => { it('should be possible to set the null sequence.', () => {
let trie = createTrie(); let trie = createTrie(null, false);
trie.add(''); trie.add('');
expect(trie.has('')).to.equal(true); expect(trie.has('')).to.equal(true);
@ -123,6 +123,23 @@ describe('Trie', () => {
expect(trie.find('')).to.deep.equal(['example.org', 'example.com', 'cdn.example.com', 'blog.example.com']); expect(trie.find('')).to.deep.equal(['example.org', 'example.com', 'cdn.example.com', 'blog.example.com']);
}); });
it('should be possible to retrieve items matching the given prefix even with a smol trie.', () => {
const trie = createTrie(null, true);
trie.add('.example.com');
trie.add('example.com');
trie.add('blog.example.com');
trie.add('cdn.example.com');
trie.add('example.org');
expect(trie.find('example.com')).to.deep.equal(['.example.com']);
expect(trie.find('com')).to.deep.equal(['.example.com']);
expect(trie.find('.example.com')).to.deep.equal(['.example.com']);
expect(trie.find('org')).to.deep.equal(['example.org']);
expect(trie.find('example.net')).to.deep.equal([]);
expect(trie.find('')).to.deep.equal(['example.org', '.example.com']);
});
it('should be possible to create a trie from an arbitrary iterable.', () => { it('should be possible to create a trie from an arbitrary iterable.', () => {
let trie = createTrie(['skk.moe', 'blog.skk.moe']); let trie = createTrie(['skk.moe', 'blog.skk.moe']);