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 { baseListing, retiredListing } from "../testing/fixtures";
import { run } from "./getListing";

describe("getListing", () => {
  it("returns listing when found by id", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseListing]);

    const result = await run(mock.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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([retiredListing]);

    const result = await run(mock.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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

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

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