import { Dissected } from "@hamradio/packet";
import { describe, expect, it } from "vitest";

import { Frame } from "../src/frame";
import { DataType, type RawGPSPayload } from "../src/frame.types";

describe("Raw GPS decoding", () => {
  it("decodes simple NMEA sentence as raw-gps payload", () => {
    const sentence = "GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A";
    const frameStr = `SRC>DEST:$${sentence}`;

    const f = Frame.parse(frameStr);
    const payload = f.decode(false) as RawGPSPayload | null;

    expect(payload).not.toBeNull();
    expect(payload?.type).toBe(DataType.RawGPS);
    expect(payload?.sentence).toBe(sentence);
    expect(payload?.position).toBeDefined();
    expect(typeof payload?.position?.latitude).toBe("number");
    expect(typeof payload?.position?.longitude).toBe("number");
  });

  it("returns structure when requested", () => {
    const sentence = "GPGGA,092750.000,5321.6802,N,00630.3372,W,1,08,1.0,73.0,M,0.0,M,,*6A";
    const frameStr = `SRC>DEST:$${sentence}`;

    const f = Frame.parse(frameStr);
    const result = f.decode(true) as {
      payload: RawGPSPayload | null;
      structure: Dissected;
    };

    expect(result.payload).not.toBeNull();
    expect(result.payload?.type).toBe(DataType.RawGPS);
    expect(result.payload?.sentence).toBe(sentence);
    expect(result.payload?.position).toBeDefined();
    expect(typeof result.payload?.position?.latitude).toBe("number");
    expect(typeof result.payload?.position?.longitude).toBe("number");
    expect(result.structure).toBeDefined();
    const rawSection = result.structure.find((s) => s.name === "raw-gps");
    expect(rawSection).toBeDefined();
    const posSection = result.structure.find((s) => s.name === "raw-gps-position");
    expect(posSection).toBeDefined();
  });
});
