import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import { InvalidChannelStatusError } from "../lib/errors.generated";
import { baseChannel, inactiveChannel } from "../testing/fixtures";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import { run } from "./deactivateChannel";

describe("deactivateChannel", () => {
  it("deactivates an active Channel", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseChannel);
    spies.update.mockReturnValueOnce(inactiveChannel);

    const result = await run(db, { channelId: baseChannel.id }, commandCtx);

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

  it("throws when Channel is not active", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(inactiveChannel);

    const result = await run(db, { channelId: inactiveChannel.id }, commandCtx);

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