Files
ytplayer/frontend/downloads.test.js
2026-10-03 14:47:57 +08:00

18 lines
1.6 KiB
JavaScript

const { test } = require('node:test');
const assert = require('node:assert/strict');
const { breakdown, bytes } = require('./downloads.js');
test('storage counts media once and shared playlist files within each playlist', () => {
const result = breakdown([{ id: 'a', size: 100, kind: 'video' }, { id: 'a', size: 20, kind: 'eq' }, { id: 'b', size: 30, kind: 'audio' }], [{ id: 'one', videos: [{ id: 'a' }, { id: 'a' }] }, { id: 'two', videos: [{ id: 'a' }, { id: 'b' }] }]);
assert.equal(result.total, 150); assert.deepEqual(result.kinds, { video: 100, audio: 30, eq: 20 }); assert.deepEqual(result.playlists.map(p => p.size), [120, 150]);
});
test('storage ignores invalid and negative sizes', () => { assert.equal(breakdown([{ id: 'a', size: -10 }, { id: 'b', size: 'bad' }], []).total, 0); assert.equal(bytes(1073741824), '1.0 GB'); });
test('pausing a worker terminates it once and ignores later completion', async () => {
const vm = require('node:vm'), fs = require('node:fs'); let worker;
class Worker { constructor() { worker = this; this.terminated = 0; } postMessage() {} terminate() { this.terminated++; } }
const window = {};
vm.runInNewContext(fs.readFileSync(require.resolve('./opfs.js'), 'utf8'), { window, navigator: {}, Worker, URL, AbortController });
const controller = new AbortController(); const result = window.OPFS.downloadVideo('video', '/download', { signal: controller.signal });
controller.abort(); assert.equal((await result).paused, true); assert.equal(worker.terminated, 1);
worker.onmessage({ data: { type: 'done' } }); assert.equal(worker.terminated, 1);
});