File size: 2,235 Bytes
4440aec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Plugin node-capability route matching and surface listing tests.
 */
import { describe, expect, it } from "vitest";
import { createEmptyPluginRegistry } from "../../../plugins/registry-empty.js";
import type { PluginRegistry } from "../../../plugins/registry.js";
import { resolvePluginRoutePathContext } from "./path-context.js";
import {
  findMatchingPluginNodeCapabilityRoute,
  listPluginNodeCapabilities,
} from "./route-capability.js";

describe("plugin node capability route metadata", () => {
  it("lists one capability per surface with the shortest ttl", () => {
    const registry: PluginRegistry = {
      ...createEmptyPluginRegistry(),
      httpRoutes: [
        {
          pluginId: "one",
          path: "/one",
          auth: "plugin",
          match: "exact",
          handler: async () => false,
          nodeCapability: { surface: "canvas" },
        },
        {
          pluginId: "two",
          path: "/two",
          auth: "plugin",
          match: "exact",
          handler: async () => false,
          nodeCapability: { surface: "canvas", ttlMs: 100 },
        },
        {
          pluginId: "files",
          path: "/files",
          auth: "plugin",
          match: "exact",
          handler: async () => false,
          nodeCapability: { surface: "files", ttlMs: 200 },
        },
      ],
    };

    expect(listPluginNodeCapabilities(registry)).toEqual([
      { surface: "canvas", ttlMs: 100, scopeKey: "two:canvas" },
      { surface: "files", ttlMs: 200, scopeKey: "files:files" },
    ]);
  });

  it("adds plugin ownership to matched capability route metadata", () => {
    const registry: PluginRegistry = {
      ...createEmptyPluginRegistry(),
      httpRoutes: [
        {
          pluginId: "canvas-plugin",
          path: "/__openclaw__/canvas/ws",
          auth: "plugin",
          match: "exact",
          handler: async () => false,
          nodeCapability: { surface: "canvas" },
        },
      ],
    };

    expect(
      findMatchingPluginNodeCapabilityRoute(
        registry,
        resolvePluginRoutePathContext("/__openclaw__/canvas/ws"),
      )?.nodeCapability,
    ).toEqual({ surface: "canvas", scopeKey: "canvas-plugin:canvas" });
  });
});