File size: 2,336 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";

vi.mock("../request", () => ({
  request: vi.fn(),
}));

import { commandsApi } from "./commands";
import { request } from "../request";

// Silence console.log from sendApprovalCommand implementation.
vi.spyOn(console, "log").mockImplementation(() => {});

describe("commandsApi", () => {
  beforeEach(() => {
    vi.mocked(request).mockResolvedValue(undefined);
  });

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

  it("checkCommand returns boolean is_control_command (true)", async () => {
    vi.mocked(request).mockResolvedValue({
      is_control_command: true,
      command_token: "/approve",
    });
    const result = await commandsApi.checkCommand("/approve");
    expect(request).toHaveBeenCalledWith("/commands/check", {
      method: "POST",
      body: JSON.stringify({ text: "/approve" }),
    });
    expect(result).toBe(true);
  });

  it("checkCommand returns false for non-control text", async () => {
    vi.mocked(request).mockResolvedValue({
      is_control_command: false,
      command_token: null,
    });
    const result = await commandsApi.checkCommand("hello there");
    expect(result).toBe(false);
  });

  it("sendApprovalCommand approve sends reason when provided", async () => {
    const resp = { success: true, message: "ok" };
    vi.mocked(request).mockResolvedValue(resp);
    const result = await commandsApi.sendApprovalCommand(
      "approve",
      "req-1",
      "sess-1",
      "looks good",
    );
    expect(request).toHaveBeenCalledWith("/approval/approve", {
      method: "POST",
      body: JSON.stringify({
        request_id: "req-1",
        session_id: "sess-1",
        reason: "looks good",
      }),
    });
    expect(result).toEqual(resp);
  });

  it("sendApprovalCommand deny omits reason (undefined)", async () => {
    const resp = { success: true, message: "denied" };
    vi.mocked(request).mockResolvedValue(resp);
    const result = await commandsApi.sendApprovalCommand(
      "deny",
      "req-2",
      "sess-2",
    );
    expect(request).toHaveBeenCalledWith("/approval/deny", {
      method: "POST",
      body: JSON.stringify({
        request_id: "req-2",
        session_id: "sess-2",
        reason: undefined,
      }),
    });
    expect(result).toEqual(resp);
  });
});