📄 src/downloader/test/progress.test.ts
D-OPEN SOVEREIGN
1import { describe, it, expect, vi, afterEach } from 'vitest';
2import { emitProgress, emitDone } from '../client/progress';
3
4describe('progress — no window (worker/node context)', () => {
5 it('emitProgress is a no-op when window is undefined', () => {
6 expect(typeof window).toBe('undefined');
7 expect(() => emitProgress('tx1', 5, 10)).not.toThrow();
8 });
9
10 it('emitDone is a no-op when window is undefined', () => {
11 expect(() => emitDone('tx1', 'pdf')).not.toThrow();
12 });
13});
14
15describe('progress — with window (main thread)', () => {
16 afterEach(() => {
17 // @ts-expect-error test-only global cleanup
18 delete globalThis.window;
19 });
20
21 it('emitProgress dispatches an ARWEAVE_DOWNLOAD_PROGRESS CustomEvent with txId/loaded/total', () => {
22 const dispatchEvent = vi.fn();
23 // @ts-expect-error minimal window stub for this test
24 globalThis.window = { dispatchEvent };
25
26 emitProgress('tx1', 5, 10);
27
28 expect(dispatchEvent).toHaveBeenCalledTimes(1);
29 const evt = dispatchEvent.mock.calls[0][0] as CustomEvent;
30 expect(evt.type).toBe('ARWEAVE_DOWNLOAD_PROGRESS');
31 expect(evt.detail).toEqual({ txId: 'tx1', loaded: 5, total: 10 });
32 expect(evt.bubbles).toBe(true);
33 });
34
35 it('emitDone dispatches an ARWEAVE_DOWNLOAD_DONE CustomEvent with txId/mime', () => {
36 const dispatchEvent = vi.fn();
37 // @ts-expect-error minimal window stub for this test
38 globalThis.window = { dispatchEvent };
39
40 emitDone('tx2', 'preview');
41
42 const evt = dispatchEvent.mock.calls[0][0] as CustomEvent;
43 expect(evt.type).toBe('ARWEAVE_DOWNLOAD_DONE');
44 expect(evt.detail).toEqual({ txId: 'tx2', mime: 'preview' });
45 });
46});
47