/**
 * Diagnostics settings tab tests — connection status, fleet info, refresh.
 */

describe("Diagnostics tab", () => {
  before(async () => {
    // Open settings
    const gear = await $(".sidebar-icon");
    await gear.click();
    await browser.pause(500);

    // Click Diagnostics tab (last category)
    const cats = await $$(".cat-btn");
    const diagCat = cats[cats.length - 1];
    if (diagCat) {
      await diagCat.click();
      await browser.pause(300);
    }
  });

  after(async () => {
    await browser.keys("Escape");
    await browser.pause(300);
  });

  it("should render the diagnostics container", async () => {
    const diag = await $(".diagnostics");
    if (await diag.isExisting()) {
      expect(await diag.isDisplayed()).toBe(true);
    }
  });

  it("should show 'Transport Diagnostics' heading", async () => {
    const heading = await $(".diagnostics .sh");
    if (await heading.isExisting()) {
      expect(await heading.getText()).toContain("Transport Diagnostics");
    }
  });

  it("should show PTY daemon connection status", async () => {
    const keys = await $$(".diag-key");
    if (keys.length > 0) {
      const texts = await Promise.all(keys.map((k) => k.getText()));
      expect(texts.some((t) => t.includes("PTY"))).toBe(true);
    }
  });

  it("should show agent fleet section", async () => {
    const labels = await $$(".diag-label");
    if (labels.length > 0) {
      const texts = await Promise.all(labels.map((l) => l.getText()));
      expect(texts.some((t) => t.toLowerCase().includes("agent fleet"))).toBe(true);
    }
  });

  it("should show last refresh timestamp", async () => {
    const footer = await $(".diag-footer");
    if (await footer.isExisting()) {
      expect(await footer.isDisplayed()).toBe(true);
    }
  });

  it("should have a refresh button", async () => {
    const refreshBtn = await $(".refresh-btn");
    if (await refreshBtn.isExisting()) {
      expect(await refreshBtn.isClickable()).toBe(true);
    }
  });
});
