import { test as base } from "@playwright/test";
import { startApp, startFakeSmtp, smtpEnvFor, type FakeSmtp, type RunningApp } from "../helpers/harness";

/**
 * Browser tests run against the real standalone bundle with a local SMTP server
 * behind it. Nothing external is required, so the suite is runnable offline and
 * on a fresh clone without credentials of any kind.
 */
export const test = base.extend<{ smtpInbox: FakeSmtp }, { smtp: FakeSmtp; app: RunningApp }>({
  smtp: [
    async ({}, use) => {
      const smtp = await startFakeSmtp();
      await use(smtp);
      await smtp.close();
    },
    { scope: "worker" },
  ],

  app: [
    async ({ smtp }, use) => {
      // basePath is left off the URL: the specs prefix paths with BASE_PATH.
      const app = await startApp({ env: smtpEnvFor(smtp.port), basePath: "" });
      await use(app);
      await app.close();
    },
    { scope: "worker" },
  ],

  baseURL: async ({ app }, use) => {
    await use(app.baseUrl);
  },

  smtpInbox: async ({ smtp }, use) => {
    smtp.messages.length = 0;
    smtp.rejectAuth = false;
    await use(smtp);
  },
});

export { expect } from "@playwright/test";
