File size: 1,333 Bytes
fc93158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { beforeEach, describe, expect, it, vi } from "vitest";

const runCommandWithTimeout = vi.hoisted(() => vi.fn());

vi.mock("../process/exec.js", () => ({
  runCommandWithTimeout: (...args: unknown[]) => runCommandWithTimeout(...args),
}));

const { execSchtasks } = await import("./schtasks-exec.js");

beforeEach(() => {
  runCommandWithTimeout.mockReset();
});

describe("execSchtasks", () => {
  it("runs schtasks with bounded timeouts", async () => {
    runCommandWithTimeout.mockResolvedValue({
      stdout: "ok",
      stderr: "",
      code: 0,
      signal: null,
      killed: false,
      termination: "exit",
    });

    await expect(execSchtasks(["/Query"])).resolves.toEqual({
      stdout: "ok",
      stderr: "",
      code: 0,
    });
    expect(runCommandWithTimeout).toHaveBeenCalledWith(["schtasks", "/Query"], {
      timeoutMs: 15_000,
      noOutputTimeoutMs: 5_000,
    });
  });

  it("maps a timeout into a non-zero schtasks result", async () => {
    runCommandWithTimeout.mockResolvedValue({
      stdout: "",
      stderr: "",
      code: null,
      signal: "SIGTERM",
      killed: true,
      termination: "timeout",
    });

    await expect(execSchtasks(["/Create"])).resolves.toEqual({
      stdout: "",
      stderr: "schtasks timed out after 15000ms",
      code: 124,
    });
  });
});