import { ok } from "@tailor-platform/erp-kit/core"; import { vi } from "vitest"; import type { InventoryCommands, InventoryQueries, ItemManagementQueries, SalesCommands, } from "../module"; export type Item = NonNullable< Awaited>["value"]["item"] >; export function mockItemManagementQueries( item: Item | null, ): Pick { return { getItem: () => Promise.resolve({ ok: true, value: { item } }), }; } export type StorageLocation = NonNullable< Awaited>["value"]["storageLocation"] >; export type PostInventoryLedgerResult = Awaited< ReturnType >; export type InventoryLedgerEntry = Extract< PostInventoryLedgerResult, { ok: true } >["value"]["inventoryLedgerEntries"][number]; export const mockInventoryQueries = ( overrides: { storageLocation?: StorageLocation | null; } = {}, ): Pick => ({ getStorageLocation: () => Promise.resolve( overrides.storageLocation ? ok({ storageLocation: overrides.storageLocation, stockLevels: [] }) : ok({ storageLocation: null, stockLevels: [] }), ), }); export const mockInventoryCommands = ( overrides: { postInventoryLedgerResult?: PostInventoryLedgerResult } = {}, ) => ({ // Echoes each movement line back as a ledger entry so tests can assert pass-through. postInventoryLedger: vi.fn((_db, input) => Promise.resolve( overrides.postInventoryLedgerResult ?? ok({ inventoryLedgerEntries: input.lines.map((line, index): InventoryLedgerEntry => ({ id: `ledger-${index + 1}`, sourceType: input.sourceType, sourceId: input.sourceId, sourceLineId: line.sourceLineId ?? null, // Persisted only on IN rows; these mocks echo OUT lines. orderDocumentType: null, orderDocumentId: null, orderDocumentLineId: null, direction: line.direction, action: line.action, itemId: line.itemId, storageLocationId: line.storageLocationId, stockType: line.stockType, quantity: line.quantity, executedAt: new Date("2024-01-10T00:00:00.000Z"), effectiveDate: input.effectiveDate, createdAt: new Date("2024-01-10T00:00:00.000Z"), })), }), ), ), }); export const mockSalesCommands = () => ({ recalculateSalesOrderFulfillmentStatus: vi.fn< SalesCommands["recalculateSalesOrderFulfillmentStatus"] >(() => Promise.resolve(ok({ salesOrderIds: [] }))), });