import { describe, it } from "vitest";
import { expect } from "vitest";

import { Frame } from "../src/frame";
import {
  DataType,
  TelemetryBitSensePayload,
  TelemetryCoefficientsPayload,
  TelemetryDataPayload,
  TelemetryParameterPayload,
  TelemetryUnitPayload
} from "../src/frame.types";

describe("Frame decode - Telemetry", () => {
  it("decodes telemetry data payload", () => {
    const frame = Frame.fromString("SRC>DEST:T#1 10,20,30,40,50 7");
    const payload = frame.decode() as TelemetryDataPayload;
    expect(payload).not.toBeNull();
    expect(payload.type).toBe(DataType.TelemetryData);
    expect(payload.variant).toBe("data");
    expect(payload.sequence).toBe(1);
    expect(Array.isArray(payload.analog)).toBe(true);
    expect(payload.analog.length).toBe(5);
    expect(payload.digital).toBe(7);
  });

  it("decodes telemetry parameters list", () => {
    const frame = Frame.fromString("SRC>DEST:TPARAM Temp,Hum,Wind");
    const payload = frame.decode() as TelemetryParameterPayload;
    expect(payload).not.toBeNull();
    expect(payload.type).toBe(DataType.TelemetryData);
    expect(payload.variant).toBe("parameters");
    expect(Array.isArray(payload.names)).toBe(true);
    expect(payload.names).toEqual(["Temp", "Hum", "Wind"]);
  });

  it("decodes telemetry units list", () => {
    const frame = Frame.fromString("SRC>DEST:TUNIT C,% ,mph");
    const payload = frame.decode() as TelemetryUnitPayload;
    expect(payload).not.toBeNull();
    expect(payload.type).toBe(DataType.TelemetryData);
    expect(payload.variant).toBe("unit");
    expect(payload.units).toEqual(["C", "%", "mph"]);
  });

  it("decodes telemetry coefficients", () => {
    const frame = Frame.fromString("SRC>DEST:TCOEFF A:1,2 B:3,4 C:5,6");
    const payload = frame.decode() as TelemetryCoefficientsPayload;
    expect(payload).not.toBeNull();
    expect(payload.type).toBe(DataType.TelemetryData);
    expect(payload.variant).toBe("coefficients");
    expect(payload.coefficients.a).toEqual([1, 2]);
    expect(payload.coefficients.b).toEqual([3, 4]);
    expect(payload.coefficients.c).toEqual([5, 6]);
  });

  it("decodes telemetry bitsense with project", () => {
    const frame = Frame.fromString("SRC>DEST:TBITS 255 ProjectX");
    const payload = frame.decode() as TelemetryBitSensePayload;
    expect(payload).not.toBeNull();
    expect(payload.type).toBe(DataType.TelemetryData);
    expect(payload.variant).toBe("bitsense");
    expect(payload.sense).toBe(255);
    expect(payload.projectName).toBe("ProjectX");
  });
});
