import { describe, it, expect } from "vitest";
import { renderStub } from "./stub-templates";

describe("renderStub", () => {
  it("command: generates input interface and run function", () => {
    const result = renderStub("command", "createItem");
    expect(result).toContain("export interface CreateItemInput");
    expect(result).toContain(
      "export async function run(db: Transaction, input: CreateItemInput, ctx: CommandContext)",
    );
    expect(result).toContain("return ok({})");
  });

  it("commandTest: generates test with createMockDb", () => {
    const result = renderStub("commandTest", "createItem");
    expect(result).toContain('describe("createItem - test scenario"');
    expect(result).toContain("createMockDb<Transaction>()");
    expect(result).toContain("CreateItemInput");
  });

  it("query: generates ReadonlyDB-based stub", () => {
    const result = renderStub("query", "getProduct");
    expect(result).toContain("export interface GetProductInput");
    expect(result).toContain("ReadonlyDB<DB>");
  });

  it("queryTest: generates test with DB type", () => {
    const result = renderStub("queryTest", "getProduct");
    expect(result).toContain('describe("getProduct"');
    expect(result).toContain("createMockDb<DB>()");
    expect(result).toContain("type GetProductInput");
  });

  it("db: generates model type definition", () => {
    const result = renderStub("db", "item");
    expect(result).toContain("export const item = db");
    expect(result).toContain('.type("Item"');
    expect(result).toContain("unsafeAllowAllTypePermission");
  });

  it("resolver: generates createResolver stub with SDK imports", () => {
    const result = renderStub("resolver", "createItem");
    expect(result).toContain("createResolver");
    expect(result).toContain('name: "createItem"');
    expect(result).toContain('operation: "mutation"');
    expect(result).toContain('getDB("main-db")');
  });

  it("storyTest: generates integration test with graphql client and test cases", () => {
    const result = renderStub("storyTest", "admin--create-item", [
      "creates item successfully",
      "rejects duplicate SKU",
    ]);
    expect(result).toContain('describe("admin--create-item"');
    expect(result).toContain("createGraphQLClient");
    expect(result).toContain('it("creates item successfully"');
    expect(result).toContain('it("rejects duplicate SKU"');
  });
});
