import { test, expect } from '@playwright/test';
import { TARGETS, url } from '../targets';

// Core assertion the user asked for: once ingress-nginx is up and the app is
// ready, NAME-APP.<domain> must be VIEWABLE in Safari's engine — not just return
// a status code. We navigate, assert a real 2xx/3xx (not nginx 404/503 default
// backend), and assert the app's own UI actually rendered.

// Only apps without a dedicated flow spec (auth logins live in their own file).
const smokeTargets = TARGETS.filter((t) => !t.dedicated);

for (const t of smokeTargets) {
  test(`${t.name} is viewable at ${url(t)}`, async ({ page }) => {
    const resp = await page.goto(url(t), { waitUntil: 'domcontentloaded' });

    expect(resp, 'no response from ingress').toBeTruthy();
    const status = resp!.status();
    expect(status, `unexpected HTTP status ${status}`).toBeLessThan(400);

    // Not the nginx default backend / error page.
    await expect(page.locator('body')).not.toContainText(/default backend - 404|503 Service Temporarily/i);

    // App's own UI rendered.
    const r = t.ready;
    if (r.selector) {
      await expect(page.locator(r.selector).first()).toBeVisible();
    } else if (r.role && r.name) {
      await expect(page.getByRole(r.role as any, { name: r.name }).first()).toBeVisible();
    } else if (r.text) {
      await expect(page.getByText(r.text).first()).toBeVisible();
    }
  });
}
