import { describe, expect, it } from 'vitest'
import { postAdjustment } from './post-adjustment.js'
import {
  AVG_ITEM_ID,
  LOCATION_A,
  NOW,
  TENANT_ID,
  freshDb,
  seedItem,
  seedLocation,
} from './test-helpers.js'

describe('postAdjustment', () => {
  it('posts signed adjustments through postMovement', async () => {
    const db = await freshDb()
    await seedLocation(db, LOCATION_A)
    await seedItem(db, AVG_ITEM_ID, 'weighted_average')

    const increase = await postAdjustment(db, {
      tenantId: TENANT_ID,
      itemId: AVG_ITEM_ID,
      locationId: LOCATION_A,
      qtyDelta: 4,
      unitCost: 6,
      reason: 'cycle-count-open',
      occurredAt: NOW,
    })
    const decrease = await postAdjustment(db, {
      tenantId: TENANT_ID,
      itemId: AVG_ITEM_ID,
      locationId: LOCATION_A,
      qtyDelta: -1,
      reason: 'cycle-count-close',
      occurredAt: NOW,
    })

    expect(Number(increase.movement.qtyDelta)).toBe(4)
    expect(Number(decrease.movement.qtyDelta)).toBe(-1)
    expect(Number(decrease.movement.cogsAmount)).toBe(6)
  })
})
