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

import { resolveClientRoute } from "../../src/web/utils/clientRoute.js";

test("shows gateway route as the active Mac connection", () => {
  const route = resolveClientRoute({
    state: {
      singboxRunning: true,
      proxyPort: 18080,
      clientSettings: {
        sharedProxyEnabled: true,
        sharedProxy: { host: "192.168.50.111", port: 8080, protocol: "socks5" },
      },
    },
  });

  assert.equal(route.mode, "gateway");
  assert.equal(route.status, "connected");
  assert.equal(route.title, "Подключено к gateway");
  assert.equal(route.target, "192.168.50.111:8080");
  assert.deepEqual(route.path, [
    "Mac apps",
    "127.0.0.1:18080",
    "Gateway 192.168.50.111:8080",
    "Internet",
  ]);
});

test("shows local VPN route with selected server", () => {
  const route = resolveClientRoute({
    state: {
      singboxRunning: true,
      proxyPort: 8082,
      selectedTag: "nl-amsterdam",
      clientSettings: {},
    },
    activeServer: { tag: "nl-amsterdam", country: "NL" },
  });

  assert.equal(route.mode, "vpn");
  assert.equal(route.status, "connected");
  assert.equal(route.title, "Подключено через VPN");
  assert.equal(route.target, "nl-amsterdam");
});

test("shows direct route when home mode is enabled", () => {
  const route = resolveClientRoute({
    state: {
      singboxRunning: true,
      proxyPort: 8082,
      clientSettings: { homeBypassEnabled: true },
    },
  });

  assert.equal(route.mode, "direct");
  assert.equal(route.status, "connected");
  assert.equal(route.title, "Подключено напрямую");
  assert.equal(route.target, "без VPN");
});

test("shows configured but stopped route clearly", () => {
  const route = resolveClientRoute({
    state: {
      singboxRunning: false,
      configExists: true,
      proxyPort: 8082,
      selectedTag: "nl-amsterdam",
      clientSettings: {},
    },
  });

  assert.equal(route.mode, "vpn");
  assert.equal(route.status, "stopped");
  assert.equal(route.title, "VPN настроен, но остановлен");
});

test("shows missing setup when nothing is configured", () => {
  const route = resolveClientRoute({
    state: {
      singboxRunning: false,
      configExists: false,
      proxyPort: 8082,
      clientSettings: {},
    },
  });

  assert.equal(route.mode, "none");
  assert.equal(route.status, "empty");
  assert.equal(route.title, "Не подключено");
  assert.equal(route.target, "выберите режим");
});
