import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import { baseListing, retiredListing } from "../testing/fixtures";
import { expectOk } from "../testing/commandTestUtils";
import { run } from "./getListing";

describe("getListing", () => {
  it("returns listing when found by id", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(baseListing);

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

    expect(listing).not.toBeNull();
    expect(listing).toMatchObject({ id: baseListing.id });
  });

  it("returns retired listing when requested directly", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(retiredListing);

    const result = await run(db, { id: retiredListing.id });
    const { listing } = expectOk<{ listing: { status: string } }>(result as never);

    expect(listing.status).toBe("RETIRED");
  });

  it("returns null when listing is not found", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(undefined);

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

    expect(listing).toBeNull();
  });
});
