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

const {
  buildSharedProxyInfo,
  checkSharedProxyGateway,
} = await import("../../src/server/sharedProxy.js");

test("gateway shared proxy info exposes host and socks proxy when running", () => {
  const info = buildSharedProxyInfo({
    appMode: "gateway",
    proxyPort: 8080,
    running: true,
    hostHeader: "192.168.50.111:3456",
  });

  assert.equal(info.available, true);
  assert.deepEqual(info.proxy, {
    host: "192.168.50.111",
    port: 8080,
    protocol: "socks5",
    httpUrl: "http://192.168.50.111:8080",
    socksUrl: "socks5://192.168.50.111:8080",
  });
});

test("client shared proxy check normalizes gateway response into settings patch", async () => {
  const patch = await checkSharedProxyGateway(
    "http://192.168.50.111:3456",
    async (url) => {
      assert.equal(url, "http://192.168.50.111:3456/api/shared-proxy");
      return {
        ok: true,
        status: 200,
        json: async () => ({
          success: true,
          available: true,
          proxy: {
            host: "192.168.50.111",
            port: 8080,
            protocol: "socks5",
          },
        }),
      };
    },
  );

  assert.equal(patch.sharedProxyEnabled, true);
  assert.equal(patch.sharedProxyControlUrl, "http://192.168.50.111:3456");
  assert.deepEqual(patch.sharedProxy, {
    host: "192.168.50.111",
    port: 8080,
    protocol: "socks5",
  });
});
