import * as fs from "node:fs/promises";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Physical Android inventories use Android Devices", WEB, async ({t3, app, screen}) => {
  const mode = `${process.env.ANDROID_HOME}/mode`;
  await fs.writeFile(mode, "phone");
  const project = await t3.project("Device labels");
  const thread = await t3.thread({project, title:"Android inventory", messages:["Hello"]});
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel", {exact:true}).tap();
  await screen.getByRole("button", "Device M", {exact:true}).tap();
  await screen.getByRole("switch", "Enable device hub").tap();
  await screen.getByRole("button", "Continue", {exact:true}).tap();
  await screen.getByRole("button", "Continue", {exact:true}).tap();
  await screen.getByRole("button", "Done", {exact:true}).tap();
  await screen.getByText("Samsung SM-X710", {exact:true}).waitFor();
  await app.screenshot("phone-only");
  const phoneHeading = await screen.getByRole("heading", /^Android (Devices|Emulators)$/).textContent();
  await fs.writeFile(mode, "mixed");
  await screen.getByRole("button", "Refresh devices", {exact:true}).tap();
  await screen.getByText("Pixel_9_API_35", {exact:true}).waitFor();
  await app.screenshot("mixed-inventory");
  const mixedHeading = await screen.getByRole("heading", /^Android (Devices|Emulators)$/).textContent();
  await fs.writeFile(mode, "emulator");
  await screen.getByRole("button", "Refresh devices", {exact:true}).tap();
  await screen.getByText("Samsung SM-X710", {exact:true}).waitFor({state:"hidden"});
  await app.screenshot("emulator-only");
  await expect(screen.getByRole("heading", "Android Emulators", {exact:true})).toBeVisible();
  expect(phoneHeading).toBe("Android Devices");
  expect(mixedHeading).toBe("Android Devices");
});
