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

describe("Frame decode - Query", () => {
  it("decodes simple query without target", () => {
    const frame = Frame.fromString("SRC>DEST:?APRS");
    const payload = frame.decode() as QueryPayload;
    expect(payload).not.toBeNull();
    expect(payload.type).toBe(DataType.Query);
    expect(payload.queryType).toBe("APRS");
    expect(payload.target).toBeUndefined();
  });

  it("decodes query with target", () => {
    const frame = Frame.fromString("SRC>DEST:?PING N0CALL");
    const payload = frame.decode() as QueryPayload;
    expect(payload).not.toBeNull();
    expect(payload.type).toBe(DataType.Query);
    expect(payload.queryType).toBe("PING");
    expect(payload.target).toBe("N0CALL");
  });

  it("returns structure sections when requested", () => {
    const frame = Frame.fromString("SRC>DEST:?PING N0CALL");
    const result = frame.decode(true) as {
      payload: QueryPayload;
      structure: Dissected;
    };
    expect(result).toHaveProperty("payload");
    expect(result.payload.type).toBe(DataType.Query);
    expect(Array.isArray(result.structure)).toBe(true);
    const names = result.structure.map((s) => s.name);
    expect(names).toContain("query type");
    expect(names).toContain("query target");
  });
});
