import { describe, expect, test } from "bun:test";

import type { ResolvedPingTarget } from "../../../../../src/server/checker/runner/icmp/types";

import { buildPingCommand } from "../../../../../src/server/checker/runner/icmp/command";

function makeTarget(overrides?: Partial<ResolvedPingTarget>): ResolvedPingTarget {
  return {
    description: null,
    group: "default",
    icmp: { count: 3, host: "10.0.0.1", packetSize: 56 },
    id: "test",
    intervalMs: 30000,
    name: null,
    timeoutMs: 10000,
    type: "icmp",
    ...overrides,
  };
}

describe("buildPingCommand", () => {
  test("Linux 默认参数", () => {
    const cmd = buildPingCommand(makeTarget(), "linux");
    expect(cmd).toEqual(["ping", "-c", "3", "-s", "56", "-W", "10", "10.0.0.1"]);
  });

  test("Linux 秒向上取整", () => {
    const cmd = buildPingCommand(makeTarget({ timeoutMs: 10500 }), "linux");
    expect(cmd[6]).toBe("11");
  });

  test("Linux timeoutMs < 1000 向上取整为 1", () => {
    const cmd = buildPingCommand(makeTarget({ timeoutMs: 500 }), "linux");
    expect(cmd[6]).toBe("1");
  });

  test("macOS 毫秒", () => {
    const cmd = buildPingCommand(makeTarget(), "darwin");
    expect(cmd).toEqual(["ping", "-c", "3", "-s", "56", "-W", "10000", "10.0.0.1"]);
  });

  test("Windows 格式", () => {
    const cmd = buildPingCommand(makeTarget(), "win32");
    expect(cmd).toEqual(["ping", "-n", "3", "-l", "56", "-w", "10000", "10.0.0.1"]);
  });

  test("自定义 count 和 packetSize", () => {
    const cmd = buildPingCommand(
      makeTarget({ icmp: { count: 5, host: "10.0.0.1", packetSize: 1472 }, timeoutMs: 5000 }),
      "linux",
    );
    expect(cmd).toEqual(["ping", "-c", "5", "-s", "1472", "-W", "5", "10.0.0.1"]);
  });
});
