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 { baseChannel, inactiveChannel } from "../testing/fixtures";
import { run } from "./getChannel";

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

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

    expect(channel).not.toBeNull();
    expect(channel).toMatchObject({ id: baseChannel.id });
  });

  it("returns inactive channel when requested directly", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([inactiveChannel]);

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

    expect(channel.status).toBe("INACTIVE");
  });

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

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

    expect(channel).toBeNull();
  });
});
