import assert from "node:assert/strict";
import test from "node:test";

import {
  developmentRuntimeEnvironment,
  installedRuntimeCleanupScript
} from "./development-runtime-lib.mjs";

test("development runtime uses the repository config and configured Tailscale hosts", () => {
  const environment = developmentRuntimeEnvironment({
    allowedOrigins: [
      "https://desktop.example.ts.net",
      "http://127.0.0.1:43111"
    ]
  }, "C:\\Project\\Code-Remote-Manager\\config.local.json", {
    KEEP_ME: "yes",
    CODE_REMOTE_MANAGER_PWA_ALLOWED_HOSTS: "manual.example.ts.net"
  });

  assert.equal(environment.KEEP_ME, "yes");
  assert.equal(environment.CODE_REMOTE_MANAGER_CONFIG_PATH, "C:\\Project\\Code-Remote-Manager\\config.local.json");
  assert.equal(environment.CODE_REMOTE_MANAGER_PWA_ALLOWED_HOSTS, "manual.example.ts.net,desktop.example.ts.net");
  assert.equal(environment.NODE_ENV, "development");
  assert.equal(environment.CODE_REMOTE_MANAGER_DEVELOPER_PRO, "1");
  assert.equal(environment.VITE_CODE_REMOTE_MANAGER_RUNTIME, "development");
});

test("development cleanup targets only the installed Windows host and preserves development startup tasks", () => {
  const script = installedRuntimeCleanupScript();

  assert.match(script, /Code Remote Manager Host/);
  assert.match(script, /CodeRemoteManager[\\/]safe-runtime/);
  assert.equal(script.includes(String.raw`safe-runtime[\\/]control[\\/]safe-runtime\.mjs`), true);
  assert.match(script, /taskkill\.exe[\s\S]*?\/pid[\s\S]*?\/t[\s\S]*?\/f/i);
  assert.doesNotMatch(script, /Code-Remote-Manager Backend/);
  assert.doesNotMatch(script, /Code-Remote-Manager PWA/);
  assert.doesNotMatch(script, /Disable-ScheduledTask/);
  assert.match(script, /Programs[\\/]Code Remote Manager/);
  assert.equal(script.includes(String.raw`host[\\/]host\.mjs`), true);
  assert.equal(script.includes(String.raw`app[\\/]backend[\\/]src[\\/]index\.js`), true);
  assert.doesNotMatch(script, /Stop-Process[^\r\n]*(?:-Name\s+node|Get-Process\s+-Name\s+node)/i);
});
