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

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

describe("Frame decode - Weather", () => {
  it("parses weather with timestamp, wind, temp, rain, humidity and pressure", () => {
    const data = "SRC>DEST:_120345z180/10g15t072r000p025P050h50b10132";
    const frame = Frame.fromString(data);
    const payload = frame.decode() as WeatherPayload;
    expect(payload).not.toBeNull();
    expect(payload.type).toBe(DataType.WeatherReportNoPosition);
    expect(payload.timestamp).toBeDefined();
    expect(payload.windDirection).toBe(180);
    expect(payload.windSpeed).toBe(10);
    expect(payload.windGust).toBe(15);
    expect(payload.temperature).toBe(72);
    expect(payload.rainLast24Hours).toBe(25);
    expect(payload.rainSinceMidnight).toBe(50);
    expect(payload.humidity).toBe(50);
    expect(payload.pressure).toBe(10132);
  });

  it("emits structure when requested", () => {
    const data = "SRC>DEST:_120345z180/10g15t072r000p025P050h50b10132";
    const frame = Frame.fromString(data);
    const res = frame.decode(true) as {
      payload: WeatherPayload;
      structure: Dissected;
    };
    expect(res.payload).not.toBeNull();
    expect(Array.isArray(res.structure)).toBe(true);
    const names = res.structure.map((s) => s.name);
    expect(names).toContain("timestamp");
    expect(names).toContain("weather");
  });
});
