import test from "node:test";
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";

test("settings explains and confirms PC data reset with the shared app dialog", async () => {
  const [settings, dialog, api] = await Promise.all([
    readFile(new URL("./SettingsWindow.jsx", import.meta.url), "utf8"),
    readFile(new URL("./PcDataResetDialog.jsx", import.meta.url), "utf8"),
    readFile(new URL("./pc-data-reset.js", import.meta.url), "utf8")
  ]);

  assert.match(settings, /settings\.resetPcData/);
  assert.match(settings, /<PcDataResetDialog/);
  assert.match(dialog, /<ActionDialog/);
  assert.match(dialog, /PC_DATA_RESET_CONFIRMATION/);
  assert.match(dialog, /dismissDisabled=\{scheduled\}/);
  assert.match(api, /delete_code_remote_manager_data/);
  assert.match(api, /body: \{ confirmation \}/);
  assert.match(dialog, /settings\.resetPcKeepsCodex/);
  assert.match(dialog, /settings\.resetPcKeepsAccounts/);
});

test("settings groups destructive data resets and gives every main row a consistent icon", async () => {
  const [settings, legal, icons, css] = await Promise.all([
    readFile(new URL("./SettingsWindow.jsx", import.meta.url), "utf8"),
    readFile(new URL("./LegalSupportSection.jsx", import.meta.url), "utf8"),
    readFile(new URL("../../shared/ActionIcons.jsx", import.meta.url), "utf8"),
    readFile(new URL("./context.css", import.meta.url), "utf8")
  ]);

  assert.match(settings, /className="settings-section settings-data-reset"[\s\S]*settings\.resetThisDevice[\s\S]*settings\.resetPcData[\s\S]*<\/section>/);
  assert.equal((settings.match(/settings-data-reset"/g) ?? []).length, 1);
  assert.doesNotMatch(settings, /settings-data-reset-list/);
  for (const icon of ["AccountsSettingsIcon", "ChatsSettingsIcon", "NotificationsSettingsIcon", "LanguageSettingsIcon", "ThemeSettingsIcon", "DeviceResetIcon", "PcDataResetIcon"]) {
    assert.match(settings, new RegExp(`<${icon} className="settings-row-icon" \\/>`));
    assert.match(icons, new RegExp(`export function ${icon}\\(`));
  }
  assert.match(legal, /<LegalSupportIcon className="settings-row-icon" \/>/);
  assert.match(icons, /export function LegalSupportIcon\(/);
  assert.match(css, /\.settings-row-icon\{[^}]*width:20px[^}]*height:20px[^}]*stroke-width:2/s);
});

test("Windows uninstall still preserves retained configuration and user data", async () => {
  const installer = await readFile(new URL("../../../../../windows-installer/build-installer.ps1", import.meta.url), "utf8");
  assert.doesNotMatch(installer, /\[UninstallDelete\]/);
  assert.doesNotMatch(installer, /config\.local\.json.*(?:delete|remove)/i);
});
