import { describe, expect, it } from "vitest";
import { groundTrack, nearestStation, stationPasses, subPoint, targetPasses } from "./passes";

const orbit = { spacecraftId: "56756" };
const centerMs = Date.UTC(2026, 4, 29, 12, 0, 0);

describe("passes", () => {
  it("finds target and station visibility around the sub-satellite point", () => {
    const point = subPoint(orbit, centerMs);
    const range = { fromMs: centerMs - 15 * 60 * 1000, toMs: centerMs + 15 * 60 * 1000 };

    expect(targetPasses(orbit, point, range).length).toBeGreaterThan(0);
    expect(stationPasses(orbit, point, range).length).toBeGreaterThan(0);
  });

  it("selects the nearest station by great-circle distance", () => {
    const point = { lat: 55.75, lon: 37.62 };

    expect(nearestStation(point, [
      { id: "far", lat: -30, lon: -120 },
      { id: "near", lat: 55.8, lon: 37.7 }
    ])?.id).toBe("near");
  });

  it("builds ground track segments in the requested range", () => {
    const segments = groundTrack(orbit, {
      fromMs: centerMs,
      toMs: centerMs + 90 * 60 * 1000
    });

    expect(segments.length).toBeGreaterThan(0);
    expect(segments.every((segment) => segment.length > 1)).toBe(true);
  });
});
