import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { InvalidChannelStatusError } from "../lib/errors.generated";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import { baseChannel, inactiveChannel } from "../testing/fixtures";
import { run } from "./deactivateChannel";

describe("deactivateChannel", () => {
  it("deactivates an active Channel", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseChannel], [inactiveChannel]);

    const result = await mock.withTx((trx) => run(trx, { channelId: baseChannel.id }, commandCtx));

    const value = expectOk(result);
    expect(value.channel.status).toBe("INACTIVE");
  });

  it("throws when Channel is not active", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([inactiveChannel]);

    const result = await mock.withTx((trx) =>
      run(trx, { channelId: inactiveChannel.id }, commandCtx),
    );

    expectErr(result, InvalidChannelStatusError);
  });
});
