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

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

describe("Frame.decodeCapabilities", () => {
  it("parses comma separated capabilities", () => {
    const data = "CALL>APRS:<IGATE,MSG_CNT";
    const frame = Frame.fromString(data);
    const decoded = frame.decode() as StationCapabilitiesPayload;
    expect(decoded).not.toBeNull();
    expect(decoded.type).toBe(DataType.StationCapabilities);
    expect(Array.isArray(decoded.capabilities)).toBeTruthy();
    expect(decoded.capabilities).toContain("IGATE");
    expect(decoded.capabilities).toContain("MSG_CNT");
  });

  it("emits structure sections when requested", () => {
    const data = "CALL>APRS:<IGATE MSG_CNT>";
    const frame = Frame.fromString(data);
    const res = frame.decode(true) as {
      payload: Payload | null;
      structure: Dissected;
    };
    expect(res.payload).not.toBeNull();
    if (res.payload && res.payload.type !== DataType.StationCapabilities)
      throw new Error("expected capabilities payload");
    expect(res.structure).toBeDefined();
    const caps = res.structure.find((s) => s.name === "capabilities");
    expect(caps).toBeDefined();
    const capEntry = res.structure.find((s) => s.name === "capability");
    expect(capEntry).toBeDefined();
  });
});
