File size: 6,093 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Covers plugin activation context construction and lazy boundaries.
import { afterEach, describe, expect, it, vi } from "vitest";
import {
  createPluginMetadataSnapshot,
  makeRegistry,
} from "../config/plugin-auto-enable.test-helpers.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { setCurrentPluginMetadataSnapshot } from "./current-plugin-metadata.test-support.js";
import type { PluginDiscoveryResult } from "./discovery.js";
import { clearPluginMetadataLifecycleCaches } from "./plugin-metadata-lifecycle.js";

const applyPluginAutoEnableMock = vi.hoisted(() =>
  vi.fn((params: { config?: OpenClawConfig }) => ({
    config: params.config,
    changes: [],
    autoEnabledReasons: {},
  })),
);
const withBundledPluginEnablementCompatMock = vi.hoisted(() =>
  vi.fn((params: { config?: OpenClawConfig }) => params.config),
);

vi.mock("../config/plugin-auto-enable.js", () => ({
  applyPluginAutoEnable: applyPluginAutoEnableMock,
}));
vi.mock("./bundled-compat.js", () => ({
  withBundledPluginEnablementCompat: withBundledPluginEnablementCompatMock,
}));

import {
  resolveBundledCompatActivationInputs,
  withActivatedPluginIds,
} from "./activation-context.js";

afterEach(() => {
  clearPluginMetadataLifecycleCaches();
  applyPluginAutoEnableMock.mockClear();
  withBundledPluginEnablementCompatMock.mockClear();
});

describe("withActivatedPluginIds", () => {
  it("keeps omitted plugin ids outside restrictive allowlists", () => {
    expect(
      withActivatedPluginIds({
        config: {
          plugins: {
            allow: ["memory-core"],
            deny: ["blocked"],
            entries: {
              disabled: { enabled: false },
            },
          },
        },
        pluginIds: ["openai", "blocked", "disabled"],
      }),
    ).toEqual({
      plugins: {
        allow: ["memory-core"],
        deny: ["blocked"],
        entries: {
          disabled: { enabled: false },
        },
      },
    });
  });
});

describe("plugin activation inputs", () => {
  it("passes the current manifest registry into activation auto-enable", () => {
    const manifestRegistry = makeRegistry([{ id: "openai", channels: [], providers: ["openai"] }]);
    const workspaceDir = "/tmp/openclaw-activation-workspace";
    setCurrentPluginMetadataSnapshot(
      createPluginMetadataSnapshot({
        config: {},
        manifestRegistry,
        workspaceDir,
      }),
      {
        config: {},
        workspaceDir,
      },
    );

    resolveBundledCompatActivationInputs({
      rawConfig: { plugins: { allow: ["openai"] } },
      workspaceDir,
      applyAutoEnable: true,
      resolveBundledPluginIds: () => [],
    });

    expect(applyPluginAutoEnableMock).toHaveBeenCalledWith({
      config: { plugins: { allow: ["openai"] } },
      env: process.env,
      manifestRegistry,
    });
  });

  it("uses the caller's exact metadata generation across lifecycle replacement", () => {
    const firstManifestRegistry = makeRegistry([
      { id: "first", channels: [], providers: ["first"] },
    ]);
    const secondManifestRegistry = makeRegistry([
      { id: "second", channels: [], providers: ["second"] },
    ]);
    const firstDiscovery = { plugins: [], diagnostics: [] } as unknown as PluginDiscoveryResult;
    const secondDiscovery = { plugins: [], diagnostics: [] } as unknown as PluginDiscoveryResult;

    for (const [manifestRegistry, discovery] of [
      [firstManifestRegistry, firstDiscovery],
      [secondManifestRegistry, secondDiscovery],
    ] as const) {
      resolveBundledCompatActivationInputs({
        rawConfig: { plugins: { allow: [manifestRegistry.plugins[0]!.id] } },
        manifestRegistry,
        discovery,
        applyAutoEnable: true,
        resolveBundledPluginIds: () => [],
      });
    }

    expect(applyPluginAutoEnableMock).toHaveBeenNthCalledWith(1, {
      config: { plugins: { allow: ["first"] } },
      env: process.env,
      manifestRegistry: firstManifestRegistry,
      discovery: firstDiscovery,
    });
    expect(applyPluginAutoEnableMock).toHaveBeenNthCalledWith(2, {
      config: { plugins: { allow: ["second"] } },
      env: process.env,
      manifestRegistry: secondManifestRegistry,
      discovery: secondDiscovery,
    });
  });

  it("applies bundled enablement once after canonical auto-enable", () => {
    const rawConfig = { plugins: { allow: ["openai"] } } satisfies OpenClawConfig;
    const autoEnabledConfig = {
      plugins: { allow: ["openai"], entries: { openai: { enabled: true } } },
    } satisfies OpenClawConfig;
    const compatConfig = {
      plugins: {
        allow: ["openai", "anthropic"],
        entries: { openai: { enabled: true }, anthropic: { enabled: true } },
      },
    } satisfies OpenClawConfig;
    const resolveBundledPluginIds = vi.fn(() => ["anthropic"]);
    applyPluginAutoEnableMock.mockReturnValueOnce({
      config: autoEnabledConfig,
      changes: [],
      autoEnabledReasons: { openai: ["configured"] },
    });
    withBundledPluginEnablementCompatMock.mockReturnValueOnce(compatConfig);

    const activation = resolveBundledCompatActivationInputs({
      rawConfig,
      env: process.env,
      workspaceDir: "/tmp/openclaw-activation-workspace",
      onlyPluginIds: ["anthropic"],
      applyAutoEnable: true,
      resolveBundledPluginIds,
    });

    expect(resolveBundledPluginIds).toHaveBeenCalledWith({
      config: autoEnabledConfig,
      workspaceDir: "/tmp/openclaw-activation-workspace",
      env: process.env,
      onlyPluginIds: ["anthropic"],
    });
    expect(withBundledPluginEnablementCompatMock).toHaveBeenCalledOnce();
    expect(withBundledPluginEnablementCompatMock).toHaveBeenCalledWith({
      config: autoEnabledConfig,
      pluginIds: ["anthropic"],
      env: process.env,
    });
    expect(activation.config).toBe(compatConfig);
    expect(activation.normalized.entries.anthropic?.enabled).toBe(true);
    expect(activation.activationSourceConfig).toBe(rawConfig);
    expect(activation.autoEnabledReasons).toEqual({ openai: ["configured"] });
  });
});