File size: 4,928 Bytes
fcd8223
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import fs from "node:fs";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { reconcileNodePairingOnConnect } from "../gateway/node-connect-reconcile.js";
import { resetPluginLoaderTestStateForTest } from "../plugins/loader.test-fixtures.js";
import { setActivePluginRegistry } from "../plugins/runtime.js";
import { listRegisteredNodeHostCapsAndCommands } from "./plugin-node-host.js";
import {
  getNodeHostPluginRegistry,
  resetNodeHostPluginRegistry,
} from "./plugin-node-host.test-support.js";
import { prepareNodeHostRuntime } from "./runtime.js";

const LINUX_NODE_COMMANDS = [
  "camera.clip",
  "camera.list",
  "camera.snap",
  "location.get",
  "system.notify",
] as const;

function resetPluginState(): void {
  resetPluginLoaderTestStateForTest();
  resetNodeHostPluginRegistry();
}

const tempBundledRoots: string[] = [];

function createLinuxNodeBundledRoot(): string {
  // Keep workspace dependencies resolvable from the copied integration package.
  const root = fs.mkdtempSync(path.resolve(".linux-node-plugin-test-"));
  try {
    // Bundled discovery requires the package itself to stay inside its physical root.
    fs.cpSync(path.resolve("extensions/linux-node"), path.join(root, "linux-node"), {
      recursive: true,
    });
  } catch (error) {
    fs.rmSync(root, { force: true, recursive: true });
    throw error;
  }
  tempBundledRoots.push(root);
  return root;
}

afterEach(() => {
  vi.restoreAllMocks();
  vi.unstubAllEnvs();
  resetPluginState();
  for (const root of tempBundledRoots.splice(0)) {
    fs.rmSync(root, { force: true, recursive: true });
  }
});

describe("linux-node node-host integration", () => {
  it("loads and advertises enabled commands through the node-host runtime", async () => {
    resetPluginState();
    const platformDescriptor = Object.getOwnPropertyDescriptor(process, "platform");
    if (!platformDescriptor) {
      throw new Error("process.platform descriptor unavailable");
    }
    const bundledRoot = createLinuxNodeBundledRoot();
    Object.defineProperty(process, "platform", { ...platformDescriptor, value: "linux" });

    const fakeBinDir = path.resolve(".artifacts", "linux-node-test-bin");
    const originalAccessSync = fs.accessSync.bind(fs);
    vi.spyOn(fs, "accessSync").mockImplementation((candidate, mode) => {
      if (path.dirname(path.resolve(String(candidate))) === fakeBinDir) {
        return;
      }
      return originalAccessSync(candidate, mode);
    });
    vi.stubEnv("PATH", `${fakeBinDir}${path.delimiter}${process.env.PATH ?? ""}`);
    vi.stubEnv("OPENCLAW_BUNDLED_PLUGINS_DIR", bundledRoot);
    vi.stubEnv("OPENCLAW_TEST_TRUST_BUNDLED_PLUGINS_DIR", "1");
    vi.stubEnv("OPENCLAW_DISABLE_BUNDLED_PLUGINS", undefined);

    const config: OpenClawConfig = {
      gateway: {
        nodes: {
          commands: { allow: ["camera.snap", "camera.clip"] },
        },
      },
      nodeHost: { skills: { enabled: false } },
      plugins: {
        allow: ["linux-node"],
        entries: {
          "linux-node": {
            enabled: true,
            config: {
              notify: { enabled: true },
              camera: { enabled: true },
              location: { enabled: true },
            },
          },
        },
      },
    };

    try {
      const prepared = await prepareNodeHostRuntime({ config, env: process.env });
      const registered = listRegisteredNodeHostCapsAndCommands({ config, env: process.env });

      expect(registered.commands, JSON.stringify(getNodeHostPluginRegistry()?.diagnostics)).toEqual(
        LINUX_NODE_COMMANDS,
      );
      expect(registered.caps).toEqual(["camera", "location"]);
      expect(prepared.manifest.commands).toEqual(expect.arrayContaining([...LINUX_NODE_COMMANDS]));

      const requestPairing = vi.fn();
      setActivePluginRegistry(getNodeHostPluginRegistry()!);
      const reconciliation = await reconcileNodePairingOnConnect({
        cfg: config,
        connectParams: {
          minProtocol: 1,
          maxProtocol: 1,
          client: {
            id: "node-host",
            version: "test",
            platform: "linux",
            deviceFamily: "Linux",
            mode: "node",
          },
          caps: registered.caps,
          commands: registered.commands,
        },
        pairedNode: {
          nodeId: "node-host",
          createdAtMs: 1,
          approvedAtMs: 1,
          caps: registered.caps,
          commands: registered.commands,
        },
        requestPairing,
      });

      expect(reconciliation.declaredCommands).toEqual(LINUX_NODE_COMMANDS);
      expect(reconciliation.effectiveCommands).toEqual(LINUX_NODE_COMMANDS);
      expect(requestPairing).not.toHaveBeenCalled();
    } finally {
      Object.defineProperty(process, "platform", platformDescriptor);
    }
  });
});