import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { execFile } from "child_process";
import { SoundNotificationService } from "./soundNotification.js";
vi.mock("child_process", async (importOriginal) => {
  const actual = await importOriginal();
  return {
    ...actual,
    execFile: vi.fn((_file, _args, _options, callback) => {
      const cb = typeof _options === "function" ? _options : callback;
      if (cb) cb(null, "", "");
    })
  };
});
const mockedExecFile = vi.mocked(execFile);
const TEST_CONFIG = {
  enabled: true,
  onStartup: false,
  onApprovalRequired: true,
  onTaskComplete: true
};
describe("SoundNotificationService", () => {
  let originalStdoutWrite;
  let writeSpy;
  beforeEach(() => {
    originalStdoutWrite = process.stdout.write;
    writeSpy = vi.fn();
    process.stdout.write = writeSpy;
  });
  afterEach(() => {
    process.stdout.write = originalStdoutWrite;
  });
  describe("constructor", () => {
    it("should accept full config", () => {
      const service = new SoundNotificationService(TEST_CONFIG);
      expect(service.getConfig()).toEqual(TEST_CONFIG);
    });
    it("should allow disabling specific sounds", () => {
      const config = {
        enabled: true,
        onStartup: false,
        onApprovalRequired: false,
        onTaskComplete: true
      };
      const service = new SoundNotificationService(config);
      expect(service.getConfig()).toEqual(config);
    });
  });
  describe("setConfig", () => {
    it("should update config", () => {
      const service = new SoundNotificationService({ ...TEST_CONFIG, enabled: false });
      service.setConfig({ enabled: true });
      expect(service.getConfig().enabled).toBe(true);
    });
  });
  describe("playApprovalSound", () => {
    it("should not play when sounds disabled", () => {
      const service = new SoundNotificationService({ ...TEST_CONFIG, enabled: false });
      service.playApprovalSound();
      expect(writeSpy).not.toHaveBeenCalled();
    });
    it("should not play when approval sounds disabled", () => {
      const service = new SoundNotificationService({
        ...TEST_CONFIG,
        onApprovalRequired: false
      });
      service.playApprovalSound();
      expect(writeSpy).not.toHaveBeenCalled();
    });
    it("should attempt to play sound when enabled", () => {
      const service = new SoundNotificationService(TEST_CONFIG);
      mockedExecFile.mockClear();
      writeSpy.mockClear();
      service.playApprovalSound();
      const soundAttempted = mockedExecFile.mock.calls.length > 0 || writeSpy.mock.calls.length > 0;
      expect(soundAttempted).toBe(true);
    });
  });
  describe("playStartupSound", () => {
    it("should not play when startup sounds disabled", () => {
      const service = new SoundNotificationService({
        ...TEST_CONFIG,
        onStartup: false
      });
      mockedExecFile.mockClear();
      writeSpy.mockClear();
      service.playStartupSound();
      expect(writeSpy).not.toHaveBeenCalled();
      expect(mockedExecFile).not.toHaveBeenCalled();
    });
    it("should attempt to play sound when enabled", () => {
      const service = new SoundNotificationService({
        ...TEST_CONFIG,
        onStartup: true
      });
      mockedExecFile.mockClear();
      writeSpy.mockClear();
      service.playStartupSound();
      const soundAttempted = mockedExecFile.mock.calls.length > 0 || writeSpy.mock.calls.length > 0;
      expect(soundAttempted).toBe(true);
    });
  });
  describe("playCompleteSound", () => {
    it("should not play when sounds disabled", () => {
      const service = new SoundNotificationService({ ...TEST_CONFIG, enabled: false });
      service.playCompleteSound();
      expect(writeSpy).not.toHaveBeenCalled();
    });
    it("should not play when complete sounds disabled", () => {
      const service = new SoundNotificationService({
        ...TEST_CONFIG,
        onTaskComplete: false
      });
      service.playCompleteSound();
      expect(writeSpy).not.toHaveBeenCalled();
    });
    it("should attempt to play sound when enabled", () => {
      const service = new SoundNotificationService(TEST_CONFIG);
      mockedExecFile.mockClear();
      writeSpy.mockClear();
      service.playCompleteSound();
      const soundAttempted = mockedExecFile.mock.calls.length > 0 || writeSpy.mock.calls.length > 0;
      expect(soundAttempted).toBe(true);
    });
  });
});
