import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; const useStockItemMock = vi.fn(); const getStockEntryMock = vi.fn(); vi.mock('../../../src/api/stock.js', () => ({ useStockItem: (...args) => useStockItemMock(...args), getStockEntry: (...args) => getStockEntryMock(...args), adjustStockEntry: vi.fn(), lookupItemDetails: vi.fn(), patchStockItem: vi.fn(), listStockEntries: vi.fn(), listGroupedStockEntries: vi.fn(), updateStockItem: vi.fn(), })); vi.mock('../../../src/api/locations.js', () => ({ fetchLocations: vi.fn(async () => ({ flat: [], tree: [] })), })); const { stockDetailPageData } = await import('../../../src/features/stock/stock-detail-page.js'); const { stockListPageData } = await import('../../../src/features/stock/stock-list-page.js'); describe('stock mark-gone behavior', () => { beforeEach(() => { useStockItemMock.mockReset(); getStockEntryMock.mockReset(); globalThis.window = { __loncApp: { navigate: vi.fn(), }, }; }); afterEach(() => { vi.restoreAllMocks(); delete globalThis.window; }); it('stock detail markGone uses /use and shows info for already gone', async () => { useStockItemMock.mockResolvedValueOnce({ status: 'already_gone' }); const addAlert = vi.fn(); const data = stockDetailPageData({ addAlert }); data.entry = { uuid_b64: 'item-1', name: 'Rice' }; await data.markGone(); expect(useStockItemMock).toHaveBeenCalledWith({ addAlert }, 'item-1'); expect(addAlert).toHaveBeenCalledWith({ type: 'info', message: 'Rice was already out of stock.', }); expect(globalThis.window.__loncApp.navigate).toHaveBeenCalledWith('/stock'); }); it('stock list markGone removes entry and uses /use path', async () => { useStockItemMock.mockResolvedValueOnce({ status: 'used' }); const addAlert = vi.fn(); const data = stockListPageData({ addAlert, isConnected: false }); data.entries = [{ id: 1, uuid_b64: 'item-1', name: 'Flour' }]; data.editForms = { 1: { level: 'plenty', quantity: 1 } }; data.editErrors = {}; await data.markGone(data.entries[0]); expect(useStockItemMock).toHaveBeenCalledWith({ addAlert, isConnected: false }, 'item-1'); expect(data.entries).toEqual([]); expect(addAlert).toHaveBeenCalledWith({ type: 'success', message: 'Flour was marked gone and removed from the list.', }); }); });