import { Test } from "@nestjs/testing";

import { {{ module_class_name }}Module } from "../../src/modules/{{ module_slug }}/passwordless.module";
import { {{ module_class_name }}Service } from "../../src/modules/{{ module_slug }}/passwordless.service";
import { {{ module_class_name }}Controller } from "../../src/modules/{{ module_slug }}/passwordless.controller";
import { {{ module_class_name }}HealthController } from "../../src/health/passwordless.health";

const IDENTIFIER = "user@example.com";

describe("{{ module_class_name }}Module", () => {
  it("registers controllers and service", async () => {
    const moduleRef = await Test.createTestingModule({
      imports: [{{ module_class_name }}Module],
    }).compile();

    expect(moduleRef.get({{ module_class_name }}Service)).toBeDefined();
    expect(moduleRef.get({{ module_class_name }}Controller)).toBeDefined();
    expect(moduleRef.get({{ module_class_name }}HealthController)).toBeDefined();
  });

  it("issues and verifies passwordless codes", async () => {
    const moduleRef = await Test.createTestingModule({
      imports: [{{ module_class_name }}Module],
    }).compile();

    const service = moduleRef.get({{ module_class_name }}Service);
    const issued = service.issueCode(IDENTIFIER, "email");

    expect(issued.tokenId).toBeDefined();
    expect(issued.deliveryMethod).toBe("email");

    const verified = service.verifyCode(IDENTIFIER, issued.code, "email");
    expect(verified.tokenId).toBe(issued.tokenId);
    expect(verified.identifier).toBe(IDENTIFIER);
  });

  it("issues magic links with composed URLs", async () => {
    const moduleRef = await Test.createTestingModule({
      imports: [{{ module_class_name }}Module],
    }).compile();

    const service = moduleRef.get({{ module_class_name }}Service);
    const issued = service.issueMagicLink(IDENTIFIER, "email");

    expect(issued.tokenId).toBeDefined();
    expect(issued.url).toMatch(/^https:\/\//u);
    expect(issued.expiresAt).toBeGreaterThan(Date.now() / 1000);
  });

  it("reports health metadata", async () => {
    const moduleRef = await Test.createTestingModule({
      imports: [{{ module_class_name }}Module],
    }).compile();

    const controller = moduleRef.get({{ module_class_name }}HealthController);
    const payload = controller.getModuleHealth();

    expect(payload.status).toBe("ok");
    expect(Array.isArray(payload.metadata["deliveryMethods"])).toBe(true);
  });
});
