File size: 2,866 Bytes
eb3f11e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Vitest harness for daemon lifecycle-core service and runtime dependencies.
import { vi } from "vitest";
import type { GatewayService } from "../../../daemon/service.js";
import type { MockFn } from "../../../test-utils/vitest-mock-fn.js";
import { createCliRuntimeCapture } from "../../test-runtime-capture.js";

const lifecycleRuntimeCapture = createCliRuntimeCapture();
export const lifecycleRuntimeLogs = lifecycleRuntimeCapture.runtimeLogs;
type LifecycleRuntimeHarness = typeof lifecycleRuntimeCapture.defaultRuntime;

type LifecycleServiceHarness = GatewayService & {
  stage: MockFn<GatewayService["stage"]>;
  install: MockFn<GatewayService["install"]>;
  uninstall: MockFn<GatewayService["uninstall"]>;
  start: MockFn<GatewayService["start"]>;
  stop: MockFn<GatewayService["stop"]>;
  isLoaded: MockFn<GatewayService["isLoaded"]>;
  readCommand: MockFn<GatewayService["readCommand"]>;
  readRuntime: MockFn<GatewayService["readRuntime"]>;
  restart: MockFn<GatewayService["restart"]>;
};

export const lifecycleTestRuntime: LifecycleRuntimeHarness = lifecycleRuntimeCapture.defaultRuntime;

export function createGatewayUninstallArgs() {
  return {
    serviceNoun: "Gateway",
    service,
    opts: { json: true as const },
    stopBeforeUninstall: true,
    assertNotLoadedAfterUninstall: true,
  };
}

export function createGatewayServiceRunArgs(checkTokenDrift?: boolean) {
  return {
    serviceNoun: "Gateway",
    service,
    renderStartHints: () => [],
    opts: { json: true as const },
    ...(checkTokenDrift ? { checkTokenDrift } : {}),
  };
}

export const service: LifecycleServiceHarness = {
  label: "TestService",
  loadedText: "loaded",
  notLoadedText: "not loaded",
  stage: vi.fn(),
  install: vi.fn(),
  uninstall: vi.fn(),
  start: vi.fn(),
  stop: vi.fn(),
  isLoaded: vi.fn(),
  readCommand: vi.fn(),
  readRuntime: vi.fn(),
  restart: vi.fn(),
};

export function resetLifecycleRuntimeLogs() {
  lifecycleRuntimeCapture.resetRuntimeCapture();
}

export function resetLifecycleServiceMocks() {
  service.stage.mockReset();
  service.install.mockReset();
  service.uninstall.mockReset();
  service.start.mockReset();
  service.stop.mockReset();
  service.isLoaded.mockReset();
  service.readCommand.mockReset();
  service.readRuntime.mockReset();
  service.restart.mockReset();
  service.isLoaded.mockResolvedValue(true);
  service.readCommand.mockResolvedValue({ programArguments: [], environment: {} });
  service.readRuntime.mockResolvedValue({ status: "stopped" });
  service.stop.mockResolvedValue(undefined);
  service.uninstall.mockResolvedValue(undefined);
  service.start.mockResolvedValue(undefined);
  service.restart.mockResolvedValue({ outcome: "completed" });
}

export function stubEmptyGatewayEnv() {
  vi.unstubAllEnvs();
  vi.stubEnv("OPENCLAW_GATEWAY_TOKEN", "");
  vi.stubEnv("OPENCLAW_GATEWAY_URL", "");
}