← back to Govarbitrage

src/engines/costs.test.ts

79 lines

import { describe, expect, it } from "vitest";
import { computeCosts } from "./costs";
import type { CostInput } from "./types";

const base: CostInput = {
  source: "GOVDEALS",
  winningBid: 500,
  quantity: 1,
  weightLbs: 40,
  condition: "USED_GOOD",
  expectedSalePrice: 9000,
  daysUntilSold: 30,
};

describe("computeCosts", () => {
  it("applies the source buyer premium", () => {
    const c = computeCosts(base);
    // GovDeals premium is 10% of the winning bid.
    expect(c.buyerPremium).toBeCloseTo(50, 2);
  });

  it("ships parcel under the freight threshold and freight above it", () => {
    const light = computeCosts({ ...base, weightLbs: 40 });
    expect(light.freight).toBe(0);
    expect(light.shipping).toBeGreaterThan(0);

    const heavy = computeCosts({ ...base, weightLbs: 600 });
    expect(heavy.freight).toBeGreaterThan(0);
    expect(heavy.shipping).toBe(0);
  });

  it("computes a positive net profit for a strong flip", () => {
    const c = computeCosts(base);
    expect(c.expectedNetProfit).toBeGreaterThan(0);
    expect(c.roi).toBeGreaterThan(0);
    expect(c.totalInvestment).toBeGreaterThan(base.winningBid);
  });

  it("recommends a max bid that back-solves to the target ROI", () => {
    const c = computeCosts(base);
    // Bidding exactly the recommended max should yield ~40% ROI.
    const atMax = computeCosts({ ...base, winningBid: c.recommendedMaxBid });
    expect(atMax.roi).toBeCloseTo(0.4, 1);
  });

  it("zeroes shipping/freight but adds pickup labor for local pickup", () => {
    const c = computeCosts({ ...base, weightLbs: 600, localPickup: true });
    expect(c.shipping).toBe(0);
    expect(c.freight).toBe(0);
    expect(c.pickupLabor).toBeGreaterThan(0);
  });

  it("stays coherent for high-quantity lots (qty > 1)", () => {
    // Quantity-scaled lot: expectedSalePrice is the lot total; costs must remain
    // internally consistent (positive investment, sane max bid below returns).
    const lot = computeCosts({
      ...base,
      quantity: 20,
      weightLbs: 120,
      expectedSalePrice: 26000,
    });
    expect(lot.testing).toBeCloseTo(20 * 15, 2); // testing scales per unit
    expect(lot.totalInvestment).toBeGreaterThan(0);
    expect(lot.recommendedMaxBid).toBeGreaterThan(0);
    expect(lot.recommendedMaxBid).toBeLessThan(lot.expectedReturns);
    // Bidding the recommended max still back-solves to the target ROI at qty>1.
    const atMax = computeCosts({ ...base, quantity: 20, weightLbs: 120, expectedSalePrice: 26000, winningBid: lot.recommendedMaxBid });
    expect(atMax.roi).toBeCloseTo(0.4, 1);
  });

  it("applies purchase sales tax when a rate is provided", () => {
    const taxed = computeCosts({ ...base, purchaseTaxRate: 0.0725 });
    const untaxed = computeCosts({ ...base, purchaseTaxRate: 0 });
    expect(taxed.salesTax).toBeGreaterThan(0);
    expect(untaxed.salesTax).toBe(0);
    expect(taxed.totalInvestment).toBeGreaterThan(untaxed.totalInvestment);
  });
});