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

test("Capacitor config bundles the production PWA for a platform-neutral app", async () => {
  const config = JSON.parse(await readFile(new URL("./capacitor.config.json", import.meta.url), "utf8"));

  assert.equal(config.appId, "com.makecdev.coderemotemanager");
  assert.equal(config.appName, "Code Remote Manager");
  assert.equal(config.webDir, "dist");
  assert.equal(config.server.androidScheme, "https");
  assert.equal("url" in config.server, false);
});

test("PWA package exposes repeatable Capacitor Android commands", async () => {
  const pkg = JSON.parse(await readFile(new URL("./package.json", import.meta.url), "utf8"));

  assert.equal(pkg.scripts.build, "vite build");
  assert.equal(pkg.scripts["android:sync"], "npm run build && cap sync android");
  assert.equal(pkg.scripts["android:open"], "cap open android");
  assert.ok(pkg.dependencies["@capacitor/core"]);
  assert.ok(pkg.dependencies["@capacitor/android"]);
  assert.ok(pkg.dependencies["@capacitor/barcode-scanner"]);
  assert.ok(pkg.devDependencies["@capacitor/cli"]);
});

test("Android native input declares its supported SDK, capabilities, and network security policy", async () => {
  const [variables, manifest] = await Promise.all([
    readFile(new URL("./android/variables.gradle", import.meta.url), "utf8"),
    readFile(new URL("./android/app/src/main/AndroidManifest.xml", import.meta.url), "utf8")
  ]);

  assert.match(variables, /minSdkVersion = 26/);
  assert.match(manifest, /<uses-permission android:name="android\.permission\.CAMERA" \/>/);
  assert.match(manifest, /<uses-permission android:name="android\.permission\.RECORD_AUDIO" \/>/);
  assert.match(manifest, /android:allowBackup="false"/);
  assert.match(manifest, /android:usesCleartextTraffic="false"/);
  assert.doesNotMatch(manifest, /android:debuggable="true"/);
});
