import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { expectOk } from "../testing/commandTestUtils";
import { baseStorageLocation, baseStockLevel } from "../testing/fixtures";
import { run } from "./getStorageLocation";

describe("getStorageLocation", () => {
  it("returns storage location when found", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseStorageLocation], [baseStockLevel]);

    const result = await run(mock.db, { id: baseStorageLocation.id });
    const { storageLocation } = expectOk<{ storageLocation: unknown }>(result as never);

    expect(storageLocation).not.toBeNull();
    expect(storageLocation).toMatchObject({ id: baseStorageLocation.id });
  });

  it("returns null when not found", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await run(mock.db, { id: "nonexistent-id" });
    const { storageLocation } = expectOk<{ storageLocation: unknown }>(result as never);

    expect(storageLocation).toBeNull();
  });

  it("includes associated stock levels", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseStorageLocation], [baseStockLevel]);

    const result = await run(mock.db, { id: baseStorageLocation.id });
    const { stockLevels } = expectOk<{ stockLevels: unknown[] }>(result as never);

    expect(stockLevels).toHaveLength(1);
  });
});
