@limits/openclaw 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,88 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach } from "vitest";
2
-
3
- const callEnforceMock = vi.fn();
4
- vi.mock("../../src/enforcer.js", () => ({ callEnforce: callEnforceMock }));
5
-
6
- describe("postHook", () => {
7
- let afterHandler: ((...args: unknown[]) => Promise<unknown>) | null = null;
8
-
9
- beforeEach(async () => {
10
- callEnforceMock.mockReset();
11
- afterHandler = null;
12
- const api = {
13
- config: {
14
- plugins: {
15
- entries: {
16
- "@limits/openclaw": {
17
- enabled: true,
18
- config: {
19
- baseUrl: "https://api.test.com",
20
- failMode: "allow",
21
- tokenSource: "event.metadata.apiToken",
22
- },
23
- },
24
- },
25
- },
26
- },
27
- on(event: string, handler: (...args: unknown[]) => unknown) {
28
- if (event === "after_tool_call")
29
- afterHandler = handler as (...args: unknown[]) => Promise<unknown>;
30
- },
31
- };
32
- const { register } = await import("../../src/index.js");
33
- register(api);
34
- });
35
-
36
- async function runPost(
37
- event: unknown,
38
- result?: unknown,
39
- ctx?: unknown
40
- ): Promise<unknown> {
41
- if (!afterHandler) throw new Error("after_tool_call not registered");
42
- if (result !== undefined && ctx !== undefined) return afterHandler(event, result, ctx);
43
- if (result !== undefined) return afterHandler(event, result);
44
- return afterHandler({ ...(event as object), result: undefined }, undefined);
45
- }
46
-
47
- it("ALLOW returns null", async () => {
48
- callEnforceMock.mockResolvedValue({ action: "ALLOW" });
49
- const event = {
50
- toolName: "read_file",
51
- metadata: { apiToken: "sk_ok" },
52
- };
53
- const result = await runPost(event, { content: "data" }, undefined);
54
- expect(result).toBe(null);
55
- });
56
-
57
- it("BLOCK returns blocked result message", async () => {
58
- callEnforceMock.mockResolvedValue({
59
- action: "BLOCK",
60
- reason: "Sensitive output",
61
- });
62
- const event = { toolName: "query_db", metadata: { apiToken: "sk_ok" } };
63
- const result = await runPost(event, { rows: [] }, undefined);
64
- expect(result).toEqual({
65
- result: "[BLOCKED by policy — content withheld]",
66
- });
67
- });
68
-
69
- it("REDACT returns { result: redactedResult }", async () => {
70
- callEnforceMock.mockResolvedValue({
71
- action: "REDACT",
72
- redactedResult: "[REDACTED]",
73
- });
74
- const event = { toolName: "get_user", metadata: { apiToken: "sk_ok" } };
75
- const result = await runPost(event, { email: "a@b.com" }, undefined);
76
- expect(result).toEqual({ result: "[REDACTED]" });
77
- });
78
-
79
- it("REWRITE returns { result: rewrittenResult }", async () => {
80
- callEnforceMock.mockResolvedValue({
81
- action: "REWRITE",
82
- rewrittenResult: { summary: "Safe summary" },
83
- });
84
- const event = { toolName: "run_report", metadata: { apiToken: "sk_ok" } };
85
- const result = await runPost(event, { raw: "long..." }, undefined);
86
- expect(result).toEqual({ result: { summary: "Safe summary" } });
87
- });
88
- });
@@ -1,78 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach } from "vitest";
2
-
3
- const callEnforceMock = vi.fn();
4
- vi.mock("../../src/enforcer.js", () => ({ callEnforce: callEnforceMock }));
5
-
6
- describe("preHook", () => {
7
- let beforeHandler: ((...args: unknown[]) => Promise<unknown>) | null = null;
8
-
9
- beforeEach(async () => {
10
- callEnforceMock.mockReset();
11
- beforeHandler = null;
12
- const api = {
13
- config: {
14
- plugins: {
15
- entries: {
16
- "@limits/openclaw": {
17
- enabled: true,
18
- config: {
19
- baseUrl: "https://api.test.com",
20
- failMode: "allow",
21
- tokenSource: "event.metadata.apiToken",
22
- },
23
- },
24
- },
25
- },
26
- },
27
- on(event: string, handler: (...args: unknown[]) => unknown) {
28
- if (event === "before_tool_call")
29
- beforeHandler = handler as (...args: unknown[]) => Promise<unknown>;
30
- },
31
- };
32
- const { register } = await import("../../src/index.js");
33
- register(api);
34
- });
35
-
36
- async function runPre(event: unknown, ctx?: unknown): Promise<unknown> {
37
- if (!beforeHandler) throw new Error("before_tool_call not registered");
38
- return beforeHandler(event, ctx);
39
- }
40
-
41
- it("ALLOW returns null", async () => {
42
- callEnforceMock.mockResolvedValue({ action: "ALLOW" });
43
- const event = {
44
- toolName: "read_file",
45
- toolParams: {},
46
- metadata: { apiToken: "sk_ok" },
47
- };
48
- const result = await runPre(event, undefined);
49
- expect(result).toBe(null);
50
- });
51
-
52
- it("BLOCK returns { block: true, reason }", async () => {
53
- callEnforceMock.mockResolvedValue({
54
- action: "BLOCK",
55
- reason: "Policy forbids this tool",
56
- });
57
- const event = {
58
- toolName: "stripe_charge",
59
- metadata: { apiToken: "sk_ok" },
60
- };
61
- const result = await runPre(event, undefined);
62
- expect(result).toEqual({ block: true, reason: "Policy forbids this tool" });
63
- });
64
-
65
- it("REWRITE returns { args }", async () => {
66
- callEnforceMock.mockResolvedValue({
67
- action: "REWRITE",
68
- rewriteArgs: { amount: 100, currency: "usd" },
69
- });
70
- const event = {
71
- toolName: "payment",
72
- toolParams: { amount: 500 },
73
- metadata: { apiToken: "sk_ok" },
74
- };
75
- const result = await runPre(event, undefined);
76
- expect(result).toEqual({ args: { amount: 100, currency: "usd" } });
77
- });
78
- });
@@ -1,72 +0,0 @@
1
- import { describe, it, expect, beforeEach, afterEach } from "vitest";
2
- import { extractToken } from "../../src/token.js";
3
-
4
- describe("extractToken", () => {
5
- const origEnv = process.env;
6
-
7
- beforeEach(() => {
8
- process.env = { ...origEnv };
9
- });
10
- afterEach(() => {
11
- process.env = origEnv;
12
- });
13
-
14
- it("reads event.metadata.apiToken", () => {
15
- const event = { metadata: { apiToken: "sk_test_123" } };
16
- expect(extractToken("event.metadata.apiToken", event, undefined)).toBe(
17
- "sk_test_123"
18
- );
19
- });
20
-
21
- it("reads ctx.auth.token", () => {
22
- const ctx = { auth: { token: "bearer_abc" } };
23
- expect(extractToken("ctx.auth.token", ctx, ctx)).toBe("bearer_abc");
24
- });
25
-
26
- it("reads env.LIMITS_API_TOKEN", () => {
27
- process.env.LIMITS_API_TOKEN = "env_secret";
28
- expect(
29
- extractToken("env.LIMITS_API_TOKEN", undefined, undefined)
30
- ).toBe("env_secret");
31
- });
32
-
33
- it("returns undefined for missing event path", () => {
34
- const event = {};
35
- expect(extractToken("event.metadata.apiToken", event, undefined)).toBe(
36
- undefined
37
- );
38
- });
39
-
40
- it("returns undefined for missing ctx path", () => {
41
- const ctx = {};
42
- expect(extractToken("ctx.auth.token", undefined, ctx)).toBe(undefined);
43
- });
44
-
45
- it("returns undefined for missing env key", () => {
46
- expect(
47
- extractToken("env.NONEXISTENT_KEY_XYZ", undefined, undefined)
48
- ).toBe(undefined);
49
- });
50
-
51
- it("returns undefined for non-string value at path", () => {
52
- const event = { metadata: { apiToken: 42 } };
53
- expect(extractToken("event.metadata.apiToken", event, undefined)).toBe(
54
- undefined
55
- );
56
- });
57
-
58
- it("returns undefined for malformed source (single segment)", () => {
59
- expect(extractToken("event", {}, undefined)).toBe(undefined);
60
- });
61
-
62
- it("returns undefined for empty string", () => {
63
- expect(extractToken("", {}, undefined)).toBe(undefined);
64
- });
65
-
66
- it("trims tokenSource", () => {
67
- const event = { metadata: { apiToken: "trimmed" } };
68
- expect(
69
- extractToken(" event.metadata.apiToken ", event, undefined)
70
- ).toBe("trimmed");
71
- });
72
- });
package/tsconfig.json DELETED
@@ -1,20 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "NodeNext",
5
- "moduleResolution": "NodeNext",
6
- "lib": ["ES2022"],
7
- "types": ["node"],
8
- "strict": true,
9
- "esModuleInterop": true,
10
- "skipLibCheck": true,
11
- "forceConsistentCasingInFileNames": true,
12
- "resolveJsonModule": true,
13
- "noUnusedLocals": true,
14
- "noUnusedParameters": true,
15
- "noImplicitReturns": true,
16
- "outDir": "dist"
17
- },
18
- "include": ["src/**/*.ts", "test/**/*.ts"],
19
- "exclude": ["node_modules"]
20
- }
package/vitest.config.ts DELETED
@@ -1,15 +0,0 @@
1
- import { defineConfig } from "vitest/config";
2
- import tsconfig from "./tsconfig.json" with { type: "json" };
3
-
4
- export default defineConfig({
5
- test: {
6
- globals: false,
7
- environment: "node",
8
- },
9
- resolve: {
10
- extensions: [".ts"],
11
- },
12
- esbuild: {
13
- target: (tsconfig.compilerOptions as { target?: string }).target ?? "ES2022",
14
- },
15
- });