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

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

    const result = await run(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 { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(inactiveChannel);

    const result = await run(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 { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(undefined);

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

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