import { describe, expect, it } from "vitest";
import { bluetoothActionSchema, parseBluetoothctlStatus, parseBluetoothStatus } from "../src/bluetooth.js";

describe("Bluetooth controls", () => {
  it("accepts only fixed radio actions", () => {
    expect(bluetoothActionSchema.safeParse("on").success).toBe(true);
    expect(bluetoothActionSchema.safeParse("off").success).toBe(true);
    expect(bluetoothActionSchema.safeParse("discoverable").success).toBe(true);
    expect(bluetoothActionSchema.safeParse("scan --all").success).toBe(false);
  });

  it("validates status returned by the privileged helper", () => {
    expect(parseBluetoothStatus(JSON.stringify({
      supported: true,
      powered: true,
      discoverable: false,
      pairable: true,
      message: "Bluetooth is on."
    }))).toMatchObject({ supported: true, powered: true, discoverable: false });

    expect(() => parseBluetoothStatus('{"powered":"yes"}')).toThrow();
  });

  it("reads the adapter state from BlueZ without privileged polling", () => {
    const status = parseBluetoothctlStatus(`Controller AA:BB:CC:DD:EE:FF Pi [default]\n\tPowered: yes\n\tDiscoverable: yes\n\tPairable: yes\n`);
    expect(status).toMatchObject({ supported: true, powered: true, discoverable: true, pairable: true });
    expect(parseBluetoothctlStatus("")).toMatchObject({ supported: false, powered: false });
  });
});
