Files
RuneStone/browser-runtime/test/subscriptions.test.mjs
T

30 lines
1.5 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtemp, rm } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { Filters } from '../src/filters.mjs';
import { loadLists } from '../src/subscriptions.mjs';
import { fetchList } from '../src/downloads.mjs';
test('custom lists download once, work offline and stop blocking when removed', async () => {
const directory = await mkdtemp(path.join(os.tmpdir(), 'runestone-lists-'));
const filters = new Filters();
try {
let downloads = 0;
const addresses = ['https://filters.example.com/custom.txt'];
const options = { directory, download: true, fetch: async () => { downloads++; return '||custom-ad.fixture.test^'; } };
await filters.load('', addresses, options);
const request = { url: 'https://custom-ad.fixture.test/a.js', sourceUrl: 'https://fixture.test/', type: 'script' };
assert.equal(await filters.match(request), true);
await filters.load('', addresses, { directory });
assert.equal(await filters.match(request), true);
assert.equal(downloads, 1);
await filters.load('', []);
assert.equal(await filters.match(request), false);
await assert.rejects(loadLists(['https://filters.example.com/missing.txt'], { directory }), /missing/);
await assert.rejects(fetchList('https://127.0.0.1/list.txt'), /private network/);
await assert.rejects(fetchList('file:///secret.txt'), /HTTP/);
} finally { await filters.close(); await rm(directory, { recursive: true, force: true }); }
});