import { Address } from "../src/address";
import { describe, it, expect } from "vitest";

describe("Address C/H and reserved bits", () => {
  it("encodes and decodes C/H and reserved", () => {
    const addr = new Address("NOCALL", 7, true, 0b10);
    const bytes = addr.toBytes();
    expect((bytes[6] & 0x80) !== 0).toBe(true); // C/H
    expect(((bytes[6] >> 5) & 0b11)).toBe(0b10); // reserved
    expect(((bytes[6] >> 1) & 0x0f)).toBe(7); // SSID
    const decoded = Address.fromBytes(bytes);
    expect(decoded.callsign).toBe("NOCALL");
    expect(decoded.ssid).toBe(7);
    expect(decoded.getCH()).toBe(true);
    expect(decoded.getReserved()).toBe(0b10);
  });

  it("sets and detects last address extension", () => {
    const addr = new Address("TEST", 1, false, 0b11);
    const bytes = addr.toBytes(true);
    expect(Address.isLastAddress(bytes)).toBe(true);
    expect((bytes[6] & 0x01)).toBe(1);
    const notLast = addr.toBytes(false);
    expect(Address.isLastAddress(notLast)).toBe(false);
  });
});
