import { describe, it, expect } from "vitest";
import { Dissected } from "@hamradio/packet";
import { Frame } from "../src/frame";
import { DataType, type UserDefinedPayload } from "../src/frame.types";

describe("Frame.decodeUserDefined", () => {
  it("parses packet type only", () => {
    const data = "CALL>APRS:{01";
    const frame = Frame.fromString(data);
    const decoded = frame.decode() as UserDefinedPayload;
    expect(decoded).not.toBeNull();
    expect(decoded.type).toBe(DataType.UserDefined);
    expect(decoded.userPacketType).toBe("01");
    expect(decoded.data).toBe("");
  });

  it("parses packet type and data and emits sections", () => {
    const data = "CALL>APRS:{TEX Hello world";
    const frame = Frame.fromString(data);
    const res = frame.decode(true) as {
      payload: UserDefinedPayload;
      structure: Dissected;
    };
    expect(res.payload).not.toBeNull();
    expect(res.payload.type).toBe(DataType.UserDefined);
    expect(res.payload.userPacketType).toBe("TEX");
    expect(res.payload.data).toBe("Hello world");

    const raw = res.structure.find((s) => s.name === "user-defined");
    const typeSection = res.structure.find(
      (s) => s.name === "user-packet-type",
    );
    const dataSection = res.structure.find((s) => s.name === "user-data");
    expect(raw).toBeDefined();
    expect(typeSection).toBeDefined();
    expect(dataSection).toBeDefined();
  });
});
