File size: 3,060 Bytes
3201ca6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { renderHook } from "@testing-library/react";
import { useAgentNotification } from "#/hooks/use-agent-notification";
import { AgentState } from "#/types/agent-state";

// Mock useSettings to control the sound notification setting
vi.mock("#/hooks/query/use-settings", () => ({
  useSettings: vi.fn().mockReturnValue({
    data: { enable_sound_notifications: true },
  }),
}));

// Mock Audio
const mockPlay = vi.fn().mockResolvedValue(undefined);
const mockAudio = {
  play: mockPlay,
  currentTime: 0,
  volume: 0.5,
};

class MockAudio {
  play = mockPlay;
  currentTime = 0;
  volume = 0.5;
  constructor() {
    Object.assign(this, mockAudio);
    return mockAudio as unknown as MockAudio;
  }
}
vi.stubGlobal("Audio", MockAudio);

describe("useAgentNotification", () => {
  beforeEach(() => {
    vi.useFakeTimers();
    vi.clearAllMocks();
  });

  afterEach(() => {
    vi.useRealTimers();
  });

  it("plays notification sound when agent reaches FINISHED state and sound is enabled", () => {
    const { rerender } = renderHook(
      ({ state }) => useAgentNotification(state),
      { initialProps: { state: AgentState.RUNNING } },
    );

    rerender({ state: AgentState.FINISHED });

    expect(mockPlay).toHaveBeenCalledTimes(1);
  });

  it("plays notification sound when agent reaches AWAITING_USER_INPUT state", () => {
    const { rerender } = renderHook(
      ({ state }) => useAgentNotification(state),
      { initialProps: { state: AgentState.RUNNING } },
    );

    rerender({ state: AgentState.AWAITING_USER_INPUT });

    expect(mockPlay).toHaveBeenCalledTimes(1);
  });

  it("plays notification sound when agent reaches AWAITING_USER_CONFIRMATION state", () => {
    const { rerender } = renderHook(
      ({ state }) => useAgentNotification(state),
      { initialProps: { state: AgentState.RUNNING } },
    );

    rerender({ state: AgentState.AWAITING_USER_CONFIRMATION });

    expect(mockPlay).toHaveBeenCalledTimes(1);
  });

  it("does not play sound when sound notifications are disabled", async () => {
    const { useSettings } = await import("#/hooks/query/use-settings");
    vi.mocked(useSettings).mockReturnValue({
      data: { enable_sound_notifications: false },
    } as ReturnType<typeof useSettings>);

    const { rerender } = renderHook(
      ({ state }) => useAgentNotification(state),
      { initialProps: { state: AgentState.RUNNING } },
    );

    rerender({ state: AgentState.FINISHED });

    expect(mockPlay).not.toHaveBeenCalled();

    // Restore
    vi.mocked(useSettings).mockReturnValue({
      data: { enable_sound_notifications: true },
    } as ReturnType<typeof useSettings>);
  });

  it("does not trigger for non-notification states like RUNNING", () => {
    const { rerender } = renderHook(
      ({ state }) => useAgentNotification(state),
      { initialProps: { state: AgentState.LOADING } },
    );

    rerender({ state: AgentState.RUNNING });

    expect(mockPlay).not.toHaveBeenCalled();
  });
});