/** * MSW (Mock Service Worker) Handlers * Provides mock API responses for testing * * Usage in tests: * import { server } from '@/tests/mocks/server' * * beforeEach(() => server.listen()) * afterEach(() => server.close()) * * it('fetches orders', async () => { * const response = await fetch('/api/orders') * expect(response.status).toBe(200) * }) */ import { http, HttpResponse } from 'msw' const API_BASE = process.env.VITE_API_BASE_URL || 'http://localhost:3000/api' /** * Mock Handlers * Intercept HTTP requests and return predefined responses */ export const handlers = [ // ===== ORDERS (OMS) ===== http.get(`${API_BASE}/orders`, () => { return HttpResponse.json({ data: [ { orderId: 'ORD-001', orderNo: 'ORD-001', customerId: 'CUST-001', orderDate: '2026-08-12', totalAmount: 100000, status: 'DRAFT', createdBy: 'user@example.com', createdAt: '2026-08-12T10:00:00Z' }, { orderId: 'ORD-002', orderNo: 'ORD-002', customerId: 'CUST-002', orderDate: '2026-08-11', totalAmount: 250000, status: 'CONFIRMED', createdBy: 'user@example.com', createdAt: '2026-08-11T09:00:00Z' } ], totalCount: 2 }) }), http.get(`${API_BASE}/orders/:orderId`, ({ params }) => { return HttpResponse.json({ orderId: params.orderId, orderNo: `ORD-${String(params.orderId).padStart(3, '0')}`, customerId: 'CUST-001', orderDate: '2026-08-12', totalAmount: 100000, status: 'DRAFT', createdBy: 'user@example.com', createdAt: '2026-08-12T10:00:00Z' }) }), http.post(`${API_BASE}/orders`, async ({ request }) => { const body = await request.json() as any return HttpResponse.json( { orderId: 'ORD-NEW', orderNo: 'ORD-NEW', customerId: body.customerId, orderDate: new Date().toISOString().split('T')[0], totalAmount: body.totalAmount || 0, status: 'DRAFT', createdBy: 'user@example.com', createdAt: new Date().toISOString() }, { status: 201 } ) }), http.put(`${API_BASE}/orders/:orderId`, async ({ params, request }) => { const body = await request.json() as any return HttpResponse.json({ orderId: params.orderId, orderNo: `ORD-${String(params.orderId).padStart(3, '0')}`, customerId: body.customerId, orderDate: body.orderDate, totalAmount: body.totalAmount, status: body.status, createdBy: 'user@example.com', createdAt: '2026-08-12T10:00:00Z', modifiedBy: 'user@example.com', modifiedAt: new Date().toISOString() }) }), http.delete(`${API_BASE}/orders/:orderId`, () => { return HttpResponse.json( { message: 'Order deleted successfully' }, { status: 204 } ) }), // ===== INVENTORY (WMS) ===== http.get(`${API_BASE}/inventory`, () => { return HttpResponse.json({ data: [ { inventoryId: 'INV-001', warehouseId: 'WH-SEOUL', productId: 'PROD-001', qtyOnHand: 1000, qtyReserved: 100, qtyAvailable: 900, status: 'ACTIVE' } ] }) }), http.patch(`${API_BASE}/inventory/:inventoryId`, async ({ params, request }) => { const body = await request.json() as any return HttpResponse.json({ inventoryId: params.inventoryId, warehouseId: 'WH-SEOUL', productId: 'PROD-001', qtyOnHand: body.qtyOnHand, qtyReserved: body.qtyReserved, qtyAvailable: (body.qtyOnHand - body.qtyReserved), status: 'ACTIVE', modifiedAt: new Date().toISOString() }) }), // ===== PRODUCTS (ERP) ===== http.get(`${API_BASE}/products`, () => { return HttpResponse.json({ data: [ { productId: 'PROD-001', sku: 'SKU-001', productName: 'Product A', categoryId: 'CAT-001', status: 'ACTIVE', createdAt: '2026-08-01T00:00:00Z' } ] }) }), http.get(`${API_BASE}/products/:productId`, ({ params }) => { return HttpResponse.json({ productId: params.productId, sku: `SKU-${params.productId}`, productName: 'Product Sample', categoryId: 'CAT-001', status: 'ACTIVE', createdAt: '2026-08-01T00:00:00Z' }) }), http.post(`${API_BASE}/products`, async ({ request }) => { const body = await request.json() as any return HttpResponse.json( { productId: 'PROD-NEW', sku: body.sku, productName: body.productName, categoryId: body.categoryId, status: 'ACTIVE', createdAt: new Date().toISOString() }, { status: 201 } ) }), // ===== ERROR HANDLER ===== http.all('*', ({ request }) => { console.warn(`Unhandled request: ${request.method} ${request.url}`) return HttpResponse.json( { message: 'Not Found', path: request.url }, { status: 404 } ) }) ]