// packages/marketing-pages/src/jobs/send-welcome-email.job.test.ts
import { describe, it, expect } from "vitest";
import { sendWelcomeEmailJob, sendWelcomeEmailInputSchema } from "@/jobs/send-welcome-email.job";
import { RecordingMailerService } from "@/infrastructure/services/recording-mailer.service";

describe("sendWelcomeEmailJob", () => {
  it("rejects unknown input fields", async () => {
    const mailer = new RecordingMailerService();
    const job = sendWelcomeEmailJob(mailer);
    await expect(job({ unexpectedField: 1 } as never)).rejects.toThrow();
  });

  it("rejects input missing required fields", async () => {
    const mailer = new RecordingMailerService();
    const job = sendWelcomeEmailJob(mailer);
    await expect(job({ userId: "u1" } as never)).rejects.toThrow();
  });

  it("rejects invalid email", async () => {
    const mailer = new RecordingMailerService();
    const job = sendWelcomeEmailJob(mailer);
    await expect(
      job({ userId: "u1", email: "not-an-email" }),
    ).rejects.toThrow();
  });

  it("sends a welcome email via the mailer on valid input", async () => {
    const mailer = new RecordingMailerService();
    const job = sendWelcomeEmailJob(mailer);
    await job({ userId: "u1", email: "u1@example.com" });
    expect(mailer.sent).toEqual([{ userId: "u1", email: "u1@example.com" }]);
  });

  it("references its input schema", () => {
    expect(sendWelcomeEmailInputSchema).toBeDefined();
  });
});
