import { ATTACHMENT_STALLED_DOWNLOAD_THRESHOLD_MS, shouldResetStalledAttachmentDownload } from './attachment-autodownload.rules'; const NOW_MS = 1_750_000_000_000; describe('attachment autodownload rules', () => { it('does not reset an actively transferring download with recent chunk progress', () => { // Regression: the pending-request marker is deleted on the first received // chunk, so an in-flight multi-chunk transfer has receivedBytes > 0 and no // pending request - it must NOT be treated as stalled while chunks flow. expect(shouldResetStalledAttachmentDownload({ available: false, receivedBytes: 128, lastUpdateMs: NOW_MS - 100 }, false, NOW_MS)).toBe(false); }); it('resets partial downloads with no progress past the stall threshold', () => { expect(shouldResetStalledAttachmentDownload({ available: false, receivedBytes: 128, lastUpdateMs: NOW_MS - ATTACHMENT_STALLED_DOWNLOAD_THRESHOLD_MS - 1 }, false, NOW_MS)).toBe(true); }); it('treats partial downloads without a progress timestamp as stalled', () => { expect(shouldResetStalledAttachmentDownload({ available: false, receivedBytes: 128 }, false, NOW_MS)).toBe(true); }); it('never resets downloads with a pending request or already available', () => { expect(shouldResetStalledAttachmentDownload({ available: false, receivedBytes: 128, lastUpdateMs: NOW_MS - ATTACHMENT_STALLED_DOWNLOAD_THRESHOLD_MS - 1 }, true, NOW_MS)).toBe(false); expect(shouldResetStalledAttachmentDownload({ available: true, receivedBytes: 128, lastUpdateMs: NOW_MS - ATTACHMENT_STALLED_DOWNLOAD_THRESHOLD_MS - 1 }, false, NOW_MS)).toBe(false); }); it('never resets downloads that have not received any bytes', () => { expect(shouldResetStalledAttachmentDownload({ available: false, receivedBytes: 0 }, false, NOW_MS)).toBe(false); }); });