Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

30.1 KB
Raw Blame
import '../helpers/safe-fetch-with-auth-to-fetch.js';

/**
 * Tests for deletion modules — confirmation_modal.js and delete_manager.js
 *
 * Focuses on actual workflow behavior: API calls, error handling,
 * success/failure callbacks, preview-driven confirmation messages,
 * and the formatBytes utility. Skips trivial "API shape" assertions.
 */

// delete_manager.js's formatBytes delegates to the shared window.formatBytes;
// load it first so DeleteManager.formatBytes works under test.
import '@js/utils/format-bytes.js';
import '@js/deletion/confirmation_modal.js';

// Preserve the real implementation before the DeleteManager tests replace the
// exported function with an auto-confirm stub.
const realConfirmAndRun = window.DeleteConfirmation.confirmAndRun;

describe('DeleteConfirmation tooltip lookup', () => {
    it('returns the configured tooltip for a known action', () => {
        // We test that lookup works without asserting exact tooltip text
        const tooltip = window.DeleteConfirmation.getTooltip('deleteDocument');
        expect(tooltip).toBeTypeOf('string');
        expect(tooltip.length).toBeGreaterThan(0);
    });

    it('returns a generic fallback for unknown actions', () => {
        expect(window.DeleteConfirmation.getTooltip('unknownAction')).toBe('Delete this item');
    });

    it('marks all destructive actions as dangerous', () => {
        const actions = window.DeleteConfirmation.ACTIONS;
        // Behavioral invariant: deletion actions whose name starts with "delete"
        // and which permanently destroy data must be flagged dangerous.
        expect(actions.deleteDocument.dangerous).toBe(true);
        expect(actions.deleteCollection.dangerous).toBe(true);
        expect(actions.bulkDeleteDocuments.dangerous).toBe(true);
    });

    it('marks recoverable actions as not dangerous', () => {
        const actions = window.DeleteConfirmation.ACTIONS;
        // Removing a PDF blob keeps the text content searchable — recoverable.
        expect(actions.deleteBlob.dangerous).toBe(false);
        expect(actions.removeFromCollection.dangerous).toBe(false);
    });
});

describe('DeleteConfirmation execution', () => {
    function setupModalDom(options = {}) {
        const autoEmitHidden = options.autoEmitHidden ?? true;
        const originalChildren = Array.from(document.body.childNodes);
        const originalBootstrapDescriptor = Object.getOwnPropertyDescriptor(
            globalThis,
            'bootstrap',
        );
        document.body.replaceChildren();

        const modal = document.createElement('div');
        modal.id = 'deleteConfirmModal';
        for (const [tag, id] of [
            ['h2', 'deleteModalTitle'],
            ['p', 'deleteModalMessage'],
            ['div', 'deleteModalDetails'],
            ['ul', 'deleteDetailsList'],
            ['div', 'deleteModalWarning'],
            ['span', 'deleteWarningText'],
        ]) {
            const element = document.createElement(tag);
            element.id = id;
            modal.appendChild(element);
        }
        const confirmButton = document.createElement('button');
        confirmButton.id = 'deleteConfirmBtn';
        const confirmButtonText = document.createElement('span');
        confirmButtonText.id = 'deleteConfirmBtnText';
        confirmButton.appendChild(confirmButtonText);
        modal.appendChild(confirmButton);
        document.body.appendChild(modal);

        const modalInstance = {
            show: vi.fn(),
            hide: vi.fn(() => {
                if (autoEmitHidden) {
                    modal.dispatchEvent(new Event('hidden.bs.modal'));
                }
            }),
        };
        function Modal() {
            return modalInstance;
        }
        Modal.getInstance = vi.fn(() => modalInstance);
        Object.defineProperty(globalThis, 'bootstrap', {
            configurable: true,
            writable: true,
            value: { Modal },
        });

        document.dispatchEvent(new Event('DOMContentLoaded'));

        const cleanup = () => {
            if (originalBootstrapDescriptor) {
                Object.defineProperty(
                    globalThis,
                    'bootstrap',
                    originalBootstrapDescriptor,
                );
            } else {
                Reflect.deleteProperty(globalThis, 'bootstrap');
            }
            document.body.replaceChildren(...originalChildren);
        };

        return { modal, confirmButton, modalInstance, cleanup };
    }

    it('runs a confirmed destructive action exactly once', async () => {
        const { confirmButton, cleanup } = setupModalDom();
        const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
            ok: true,
            json: async () => ({ value: true }),
        });

        try {
            const action = vi.fn(async () => {});
            // DeleteManager passes the same callback both in options.onConfirm
            // and as the explicit action argument.
            const confirmation = realConfirmAndRun(
                { action: 'deleteDocument', onConfirm: action },
                action,
            );
            await new Promise(resolve => setTimeout(resolve, 0));
            confirmButton.click();
            await confirmation;

            expect(action).toHaveBeenCalledTimes(1);
        } finally {
            fetchSpy.mockRestore();
            cleanup();
        }
    });

    it('runs directly exactly once when confirmations are explicitly disabled', async () => {
        const { modalInstance, cleanup } = setupModalDom();
        const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
            ok: true,
            json: async () => ({ value: false }),
        });

        try {
            const action = vi.fn(async () => 'deleted');

            await expect(realConfirmAndRun(
                { action: 'deleteDocument', onConfirm: action },
                action,
            )).resolves.toBe('deleted');

            expect(action).toHaveBeenCalledOnce();
            expect(modalInstance.show).not.toHaveBeenCalled();
        } finally {
            fetchSpy.mockRestore();
            cleanup();
        }
    });

    it.each([
        [
            'a rejected settings lookup',
            () => Promise.reject(new Error('settings unavailable')),
        ],
        [
            'a non-ok settings response',
            () => Promise.resolve({ ok: false }),
        ],
    ])('fails closed after %s', async (_case, settingsResult) => {
        const { modal, modalInstance, cleanup } = setupModalDom({
            autoEmitHidden: false,
        });
        const fetchSpy = vi.spyOn(globalThis, 'fetch')
            .mockImplementation(settingsResult);

        try {
            const action = vi.fn();
            const confirmation = realConfirmAndRun(
                { action: 'deleteDocument' },
                action,
            );

            await vi.waitFor(() => expect(modalInstance.show).toHaveBeenCalledOnce());
            expect(action).not.toHaveBeenCalled();

            modal.dispatchEvent(new Event('hidden.bs.modal'));
            await expect(confirmation).resolves.toBeNull();
            expect(action).not.toHaveBeenCalled();
        } finally {
            fetchSpy.mockRestore();
            cleanup();
        }
    });

    it('consumes confirm callback on rapid double-click and clears cancel callback before modal hides', async () => {
        const { modal, confirmButton, cleanup } = setupModalDom({ autoEmitHidden: false });

        try {
            const onConfirm = vi.fn();
            const onCancel = vi.fn();

            const promise = window.DeleteConfirmation.show({
                action: 'deleteDocument',
                onConfirm,
                onCancel,
            });

            // Rapid double click on confirm button
            confirmButton.click();
            confirmButton.click();

            // Simulate Bootstrap emitting hidden.bs.modal after transition
            modal.dispatchEvent(new Event('hidden.bs.modal'));

            const result = await promise;

            expect(result).toBe(true);
            expect(onConfirm).toHaveBeenCalledTimes(1);
            expect(onCancel).not.toHaveBeenCalled();
        } finally {
            cleanup();
        }
    });

    it('invokes cancel callback and resolves false when modal is hidden without confirmation', async () => {
        const { modal, cleanup } = setupModalDom({ autoEmitHidden: false });

        try {
            const onConfirm = vi.fn();
            const onCancel = vi.fn();

            const promise = window.DeleteConfirmation.show({
                action: 'deleteDocument',
                onConfirm,
                onCancel,
            });

            // Simulate closing the modal without confirming (e.g. backdrop/close click)
            modal.dispatchEvent(new Event('hidden.bs.modal'));

            const result = await promise;

            expect(result).toBe(false);
            expect(onConfirm).not.toHaveBeenCalled();
            expect(onCancel).toHaveBeenCalledTimes(1);
        } finally {
            cleanup();
        }
    });
});

// Now load delete_manager and stub its dependencies before importing
let DM;

beforeAll(async () => {
    window.api = {
        fetchWithErrorHandling: vi.fn(),
        getCsrfToken: () => 'test-token',
    };
    window.showToast = vi.fn();

    // Auto-confirm: bypass the modal and just run the action immediately.
    // This lets us test the actual API workflow without the modal UI.
    window.DeleteConfirmation.confirmAndRun = vi.fn(async (opts, action) => {
        await action();
    });

    await import('@js/deletion/delete_manager.js');
    DM = window.DeleteManager;
});

beforeEach(() => {
    window.api.fetchWithErrorHandling.mockReset();
    window.showToast.mockReset();
    window.DeleteConfirmation.confirmAndRun.mockClear();
});

// Flush pending browser work used by notification callbacks.
const flushPromises = () => new Promise(resolve => setTimeout(resolve, 0));

describe('DeleteManager.formatBytes', () => {
    it('formats 0 bytes', () => {
        expect(DM.formatBytes(0)).toBe('0 Bytes');
    });

    it('formats bytes < 1024', () => {
        expect(DM.formatBytes(500)).toBe('500 Bytes');
    });

    it('formats kilobytes with decimal precision', () => {
        expect(DM.formatBytes(1024)).toBe('1 KB');
        expect(DM.formatBytes(1536)).toBe('1.5 KB');
    });

    it('formats megabytes', () => {
        expect(DM.formatBytes(1048576)).toBe('1 MB');
    });

    it('formats gigabytes', () => {
        expect(DM.formatBytes(1073741824)).toBe('1 GB');
    });

    it('chooses the right unit for boundary values', () => {
        expect(DM.formatBytes(1023)).toContain('Bytes');
        expect(DM.formatBytes(1024)).toContain('KB');
        expect(DM.formatBytes(1024 * 1024 - 1)).toContain('KB');
        expect(DM.formatBytes(1024 * 1024)).toContain('MB');
    });
});

describe('DeleteManager.deleteDocument', () => {
    it('calls DELETE on the document endpoint', async () => {
        // First call: preview lookup (GET). Second call: actual DELETE.
        window.api.fetchWithErrorHandling
            .mockResolvedValueOnce({ success: true, title: 'My Doc', has_blob: false, has_text: false })
            .mockResolvedValueOnce({ success: true });

        await DM.deleteDocument('doc-123');

        // Verify a DELETE was made to the right endpoint
        const deleteCall = window.api.fetchWithErrorHandling.mock.calls.find(
            ([_url, opts]) => opts && opts.method === 'DELETE'
        );
        expect(deleteCall).toBeDefined();
        expect(deleteCall[0]).toContain('doc-123');
    });

    it('skips the preview when skipConfirm is true', async () => {
        window.api.fetchWithErrorHandling.mockResolvedValueOnce({ success: true });

        await DM.deleteDocument('doc-456', { skipConfirm: true });
        await flushPromises();

        // Only one call (the DELETE), no preview GET
        expect(window.api.fetchWithErrorHandling).toHaveBeenCalledTimes(1);
        expect(window.api.fetchWithErrorHandling.mock.calls[0][1].method).toBe('DELETE');
    });

    it('owns the skipped-confirmation request until its callback completes', async () => {
        let finishDelete;
        window.api.fetchWithErrorHandling.mockImplementationOnce(() =>
            new Promise(resolve => {
                finishDelete = resolve;
            }));
        const onSuccess = vi.fn();

        const deletion = DM.deleteDocument('doc-owned', {
            skipConfirm: true,
            onSuccess,
        });
        await Promise.resolve();
        expect(onSuccess).not.toHaveBeenCalled();

        finishDelete({ success: true });
        await deletion;

        expect(onSuccess).toHaveBeenCalledWith({ success: true });
    });

    it('calls onSuccess callback with API result on success', async () => {
        const onSuccess = vi.fn();
        const apiResult = { success: true, deleted_id: 'doc-789' };
        window.api.fetchWithErrorHandling.mockResolvedValueOnce(apiResult);

        await DM.deleteDocument('doc-789', { skipConfirm: true, onSuccess });
        await flushPromises();

        expect(onSuccess).toHaveBeenCalledWith(apiResult);
    });

    it('calls onError callback when API returns success: false', async () => {
        const onError = vi.fn();
        window.api.fetchWithErrorHandling.mockResolvedValueOnce({
            success: false,
            error: 'Document is locked',
        });

        await DM.deleteDocument('doc-locked', { skipConfirm: true, onError });
        await flushPromises();

        expect(onError).toHaveBeenCalled();
        const err = onError.mock.calls[0][0];
        expect(err.message).toContain('Document is locked');
    });

    it('calls onError when fetch throws', async () => {
        const onError = vi.fn();
        window.api.fetchWithErrorHandling.mockRejectedValueOnce(new Error('Network down'));

        await DM.deleteDocument('doc-net', { skipConfirm: true, onError });
        await flushPromises();

        expect(onError).toHaveBeenCalled();
    });

    it('does not call onSuccess when API call fails', async () => {
        const onSuccess = vi.fn();
        window.api.fetchWithErrorHandling.mockResolvedValueOnce({ success: false, error: 'fail' });

        await DM.deleteDocument('doc-x', { skipConfirm: true, onSuccess, onError: () => {} });

        expect(onSuccess).not.toHaveBeenCalled();
    });
});

describe('DeleteManager.deleteDocumentBlob', () => {
    it('calls DELETE on the document blob endpoint', async () => {
        window.api.fetchWithErrorHandling.mockResolvedValueOnce({ success: true, bytes_freed: 2048 });

        await DM.deleteDocumentBlob('doc-1', { skipConfirm: true });
        await flushPromises();

        const call = window.api.fetchWithErrorHandling.mock.calls[0];
        expect(call[0]).toContain('doc-1');
        expect(call[0]).toContain('blob');
        expect(call[1].method).toBe('DELETE');
    });

    it('passes bytes_freed to onSuccess', async () => {
        const onSuccess = vi.fn();
        window.api.fetchWithErrorHandling.mockResolvedValueOnce({
            success: true,
            bytes_freed: 5000,
        });

        await DM.deleteDocumentBlob('doc-1', { skipConfirm: true, onSuccess });
        await flushPromises();

        expect(onSuccess).toHaveBeenCalledWith(expect.objectContaining({ bytes_freed: 5000 }));
    });
});

describe('DeleteManager.removeFromCollection', () => {
    it('calls DELETE on the collection-document endpoint with both IDs', async () => {
        window.api.fetchWithErrorHandling.mockResolvedValueOnce({
            success: true,
            document_deleted: false,
        });

        await DM.removeFromCollection('doc-1', 'coll-2', { skipConfirm: true });
        await flushPromises();

        const call = window.api.fetchWithErrorHandling.mock.calls[0];
        expect(call[0]).toContain('doc-1');
        expect(call[0]).toContain('coll-2');
        expect(call[1].method).toBe('DELETE');
    });

    it('handles document_deleted: true (last collection removal)', async () => {
        const onSuccess = vi.fn();
        window.api.fetchWithErrorHandling.mockResolvedValueOnce({
            success: true,
            document_deleted: true,
        });

        await DM.removeFromCollection('doc-1', 'coll-2', { skipConfirm: true, onSuccess });
        await flushPromises();

        expect(onSuccess).toHaveBeenCalledWith(expect.objectContaining({ document_deleted: true }));
    });
});

describe('DeleteManager.deleteCollection', () => {
    it('calls DELETE on the collection endpoint', async () => {
        window.api.fetchWithErrorHandling.mockResolvedValueOnce({
            success: true,
            documents_unlinked: 5,
            chunks_deleted: 100,
        });

        await DM.deleteCollection('coll-99', { skipConfirm: true });
        await flushPromises();

        const call = window.api.fetchWithErrorHandling.mock.calls[0];
        expect(call[0]).toContain('coll-99');
        expect(call[1].method).toBe('DELETE');
    });

    it('reports failure via onError', async () => {
        const onError = vi.fn();
        window.api.fetchWithErrorHandling.mockResolvedValueOnce({
            success: false,
            error: 'Collection in use',
        });

        await DM.deleteCollection('coll-used', { skipConfirm: true, onError });
        await flushPromises();

        expect(onError).toHaveBeenCalled();
    });
});

describe('DeleteManager.deleteCollectionIndex', () => {
    it('calls DELETE on the index endpoint', async () => {
        window.api.fetchWithErrorHandling.mockResolvedValueOnce({
            success: true,
            chunks_deleted: 42,
        });

        await DM.deleteCollectionIndex('coll-1', { skipConfirm: true });
        await flushPromises();

        const call = window.api.fetchWithErrorHandling.mock.calls[0];
        expect(call[0]).toContain('coll-1');
        expect(call[0]).toContain('index');
        expect(call[1].method).toBe('DELETE');
    });
});

describe('DeleteManager.bulkDeleteDocuments', () => {
    it('sends document IDs in the request body', async () => {
        window.api.fetchWithErrorHandling.mockResolvedValueOnce({
            success: true,
            deleted: 3,
            total: 3,
            total_bytes_freed: 1024,
        });

        await DM.bulkDeleteDocuments(['a', 'b', 'c'], { skipConfirm: true });
        await flushPromises();

        const call = window.api.fetchWithErrorHandling.mock.calls[0];
        expect(call[1].method).toBe('DELETE');
        const body = JSON.parse(call[1].body);
        expect(body.document_ids).toEqual(['a', 'b', 'c']);
    });

    it('reports partial-success counts via onSuccess', async () => {
        const onSuccess = vi.fn();
        window.api.fetchWithErrorHandling.mockResolvedValueOnce({
            success: true,
            deleted: 2,
            total: 3,
            total_bytes_freed: 2048,
        });

        await DM.bulkDeleteDocuments(['a', 'b', 'c'], { skipConfirm: true, onSuccess });
        await flushPromises();

        expect(onSuccess).toHaveBeenCalledWith(
            expect.objectContaining({ deleted: 2, total: 3 })
        );
    });
});

describe('DeleteManager.bulkDeleteBlobs', () => {
    it('sends document IDs in the request body', async () => {
        window.api.fetchWithErrorHandling.mockResolvedValueOnce({
            success: true,
            deleted: 2,
            total_bytes_freed: 1024,
        });

        await DM.bulkDeleteBlobs(['x', 'y'], { skipConfirm: true });
        await flushPromises();

        const call = window.api.fetchWithErrorHandling.mock.calls[0];
        const body = JSON.parse(call[1].body);
        expect(body.document_ids).toEqual(['x', 'y']);
    });
});

describe('DeleteManager.bulkRemoveFromCollection', () => {
    it('sends collection ID in URL and document IDs in body', async () => {
        window.api.fetchWithErrorHandling.mockResolvedValueOnce({
            success: true,
            unlinked: 3,
            deleted: 1,
        });

        await DM.bulkRemoveFromCollection(['a', 'b', 'c'], 'coll-7', { skipConfirm: true });
        await flushPromises();

        const call = window.api.fetchWithErrorHandling.mock.calls[0];
        expect(call[0]).toContain('coll-7');
        expect(call[1].method).toBe('DELETE');
        const body = JSON.parse(call[1].body);
        expect(body.document_ids).toEqual(['a', 'b', 'c']);
    });
});

describe('DeleteManager destructive-operation failures', () => {
    it.each([
        [
            'blob deletion',
            options => DM.deleteDocumentBlob('doc-failure', options),
            'Failed to remove PDF: ',
        ],
        [
            'single collection removal',
            options => DM.removeFromCollection('doc-failure', 'collection-failure', options),
            'Failed to remove: ',
        ],
        [
            'collection index deletion',
            options => DM.deleteCollectionIndex('collection-failure', options),
            'Failed to delete index: ',
        ],
        [
            'bulk document deletion',
            options => DM.bulkDeleteDocuments(['doc-failure'], options),
            'Bulk delete failed: ',
        ],
        [
            'bulk blob deletion',
            options => DM.bulkDeleteBlobs(['doc-failure'], options),
            'Failed to remove PDFs: ',
        ],
        [
            'bulk collection removal',
            options => DM.bulkRemoveFromCollection(
                ['doc-failure'],
                'collection-failure',
                options,
            ),
            'Failed to remove documents: ',
        ],
    ])('reports %s failure without calling success', async (
        _operation,
        invoke,
        notificationPrefix,
    ) => {
        const onSuccess = vi.fn();
        const onError = vi.fn();
        window.api.fetchWithErrorHandling.mockResolvedValueOnce({
            success: false,
            error: 'Backend rejected the destructive operation',
        });

        await invoke({ skipConfirm: true, onSuccess, onError });

        expect(onSuccess).not.toHaveBeenCalled();
        expect(onError).toHaveBeenCalledOnce();
        expect(onError.mock.calls[0][0].message)
            .toBe('Backend rejected the destructive operation');
        expect(window.showToast).toHaveBeenLastCalledWith(
            `${notificationPrefix}Backend rejected the destructive operation`,
            'error',
        );
    });

    it('distinguishes unlink-only bulk removal from permanent deletion', async () => {
        window.api.fetchWithErrorHandling.mockResolvedValueOnce({
            success: true,
            unlinked: 2,
            deleted: 0,
        });

        await DM.bulkRemoveFromCollection(
            ['doc-one', 'doc-two'],
            'collection-one',
            { skipConfirm: true },
        );

        expect(window.showToast).toHaveBeenLastCalledWith(
            'Removed 2 document(s) from collection',
            'success',
        );
    });
});

describe('DeleteManager confirmation flow', () => {
    it('skipConfirm: true bypasses DeleteConfirmation.confirmAndRun', async () => {
        window.api.fetchWithErrorHandling.mockResolvedValueOnce({ success: true });

        await DM.deleteDocument('doc-1', { skipConfirm: true });
        await flushPromises();

        expect(window.DeleteConfirmation.confirmAndRun).not.toHaveBeenCalled();
    });

    it('skipConfirm: false routes through confirmation', async () => {
        // The first call is the preview lookup
        window.api.fetchWithErrorHandling
            .mockResolvedValueOnce({ success: true, title: 'Doc' })
            .mockResolvedValueOnce({ success: true });

        await DM.deleteDocument('doc-2');

        expect(window.DeleteConfirmation.confirmAndRun).toHaveBeenCalled();
    });

    it('preview failure does not block the deletion (warning only)', async () => {
        // Preview throws, but the deletion should still proceed via confirmAndRun
        window.api.fetchWithErrorHandling
            .mockRejectedValueOnce(new Error('preview failed'))
            .mockResolvedValueOnce({ success: true });

        await DM.deleteDocument('doc-3');

        // confirmAndRun was still called → user still gets the dialog
        expect(window.DeleteConfirmation.confirmAndRun).toHaveBeenCalled();
    });

    it('builds a complete document-impact preview before confirmation', async () => {
        window.api.fetchWithErrorHandling
            .mockResolvedValueOnce({
                success: true,
                title: 'Migration source',
                has_blob: true,
                blob_size: 2048,
                has_text: true,
                chunks_count: 12,
                collections_count: 3,
            })
            .mockResolvedValueOnce({ success: true });

        await DM.deleteDocument('doc-preview');

        const [options] = window.DeleteConfirmation.confirmAndRun.mock.calls[0];
        expect(options.title).toBe('Delete "Migration source"?');
        expect(options.details).toEqual([
            'PDF file (2 KB)',
            'Extracted text content',
            '12 RAG index chunks',
            'Links to 3 collection(s)',
        ]);
    });

    it('builds a collection-impact preview from the migrated endpoint', async () => {
        window.api.fetchWithErrorHandling
            .mockResolvedValueOnce({
                success: true,
                name: 'Owned collection',
                documents_count: 4,
                chunks_count: 25,
                folders_count: 2,
            })
            .mockResolvedValueOnce({
                success: true,
                documents_unlinked: 4,
                chunks_deleted: 25,
            });

        await DM.deleteCollection('collection-preview');

        expect(window.api.fetchWithErrorHandling).toHaveBeenNthCalledWith(
            1,
            '/library/api/collections/collection-preview/preview',
            { method: 'GET' },
        );
        const [options] = window.DeleteConfirmation.confirmAndRun.mock.calls[0];
        expect(options.title).toBe('Delete "Owned collection"?');
        expect(options.details).toEqual([
            '4 document(s) will be unlinked',
            '25 RAG index chunks',
            '2 linked folder(s)',
        ]);
    });

    it('requests a bulk-delete impact preview before destructive confirmation', async () => {
        window.api.fetchWithErrorHandling
            .mockResolvedValueOnce({
                success: true,
                documents_with_blobs: 2,
                total_blob_size: 4096,
                total_chunks: 18,
            })
            .mockResolvedValueOnce({
                success: true,
                deleted: 2,
                total: 2,
                total_bytes_freed: 4096,
            });

        await DM.bulkDeleteDocuments(['first', 'second']);

        expect(window.api.fetchWithErrorHandling).toHaveBeenNthCalledWith(
            1,
            '/library/api/documents/preview',
            {
                method: 'POST',
                body: JSON.stringify({
                    document_ids: ['first', 'second'],
                    operation: 'delete',
                }),
            },
        );
        const [options] = window.DeleteConfirmation.confirmAndRun.mock.calls[0];
        expect(options.details).toEqual([
            '2 PDF file(s) (4 KB)',
            '18 RAG index chunks',
        ]);
    });

    it('requests blob-only impact without claiming extracted text is removed', async () => {
        window.api.fetchWithErrorHandling
            .mockResolvedValueOnce({
                success: true,
                documents_with_blobs: 3,
                total_blob_size: 6144,
            })
            .mockResolvedValueOnce({
                success: true,
                deleted: 3,
                total_bytes_freed: 6144,
            });

        await DM.bulkDeleteBlobs(['one', 'two', 'three']);

        expect(window.api.fetchWithErrorHandling).toHaveBeenNthCalledWith(
            1,
            '/library/api/documents/preview',
            {
                method: 'POST',
                body: JSON.stringify({
                    document_ids: ['one', 'two', 'three'],
                    operation: 'delete_blobs',
                }),
            },
        );
        const [options] = window.DeleteConfirmation.confirmAndRun.mock.calls[0];
        expect(options.details).toEqual([
            '3 PDF file(s)',
            '6 KB will be freed',
        ]);
    });
});

describe('DeleteManager notification fallback', () => {
    let originalShowToast;

    beforeEach(() => {
        originalShowToast = window.showToast;
        delete window.showToast;
        window.LdrAlertHelpers = {
            mapAlertType: vi.fn(type => (
                type === 'error' ? 'danger' : type
            )),
        };
        window.escapeHtml = value => String(value)
            .replaceAll('&', '&amp;')
            .replaceAll('<', '&lt;')
            .replaceAll('>', '&gt;');
    });

    afterEach(() => {
        window.showToast = originalShowToast;
        delete window.bootstrap;
        delete window.LdrAlertHelpers;
        delete window.escapeHtml;
        document.querySelector('.ldr-toast-container')?.remove();
    });

    it('escapes a backend message in the Bootstrap toast and removes it when hidden', () => {
        const show = vi.fn();
        window.bootstrap = {
            Toast: vi.fn(function Toast() {
                return { show };
            }),
        };
        const payload = '<img src=x onerror="window.pwned=true">';

        DM.showNotification('error', payload);

        const toast = document.querySelector('.ldr-toast-container .toast');
        expect(toast.classList.contains('bg-danger')).toBe(true);
        expect(toast.querySelector('.toast-body').textContent).toBe(payload);
        expect(toast.querySelector('img')).toBeNull();
        expect(window.pwned).toBeUndefined();
        expect(show).toHaveBeenCalledOnce();

        toast.dispatchEvent(new Event('hidden.bs.toast'));
        expect(toast.isConnected).toBe(false);
    });

    it('logs a bounded fallback when no toast implementation is loaded', () => {
        const log = vi.spyOn(SafeLogger, 'log').mockImplementation(() => {});

        DM.showNotification('warning', 'Migration warning');

        expect(log).toHaveBeenCalledWith('[WARNING] Migration warning');
        log.mockRestore();
    });
});