|
| 1 | +import { readFileSync } from "node:fs"; |
| 2 | +import { dirname, resolve } from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | + |
| 6 | +const ROOT_DIR = resolve(dirname(fileURLToPath(import.meta.url)), ".."); |
| 7 | + |
| 8 | +type GuardedSource = { |
| 9 | + path: string; |
| 10 | + forbiddenPatterns: RegExp[]; |
| 11 | +}; |
| 12 | + |
| 13 | +const SAME_CHANNEL_SDK_GUARDS: GuardedSource[] = [ |
| 14 | + { |
| 15 | + path: "extensions/discord/src/plugin-shared.ts", |
| 16 | + forbiddenPatterns: [/openclaw\/plugin-sdk\/discord/, /plugin-sdk-internal\/discord/], |
| 17 | + }, |
| 18 | + { |
| 19 | + path: "extensions/discord/src/shared.ts", |
| 20 | + forbiddenPatterns: [/openclaw\/plugin-sdk\/discord/, /plugin-sdk-internal\/discord/], |
| 21 | + }, |
| 22 | + { |
| 23 | + path: "extensions/slack/src/shared.ts", |
| 24 | + forbiddenPatterns: [/openclaw\/plugin-sdk\/slack/, /plugin-sdk-internal\/slack/], |
| 25 | + }, |
| 26 | + { |
| 27 | + path: "extensions/telegram/src/shared.ts", |
| 28 | + forbiddenPatterns: [/openclaw\/plugin-sdk\/telegram/, /plugin-sdk-internal\/telegram/], |
| 29 | + }, |
| 30 | + { |
| 31 | + path: "extensions/imessage/src/shared.ts", |
| 32 | + forbiddenPatterns: [/openclaw\/plugin-sdk\/imessage/, /plugin-sdk-internal\/imessage/], |
| 33 | + }, |
| 34 | + { |
| 35 | + path: "extensions/whatsapp/src/shared.ts", |
| 36 | + forbiddenPatterns: [/openclaw\/plugin-sdk\/whatsapp/, /plugin-sdk-internal\/whatsapp/], |
| 37 | + }, |
| 38 | + { |
| 39 | + path: "extensions/signal/src/shared.ts", |
| 40 | + forbiddenPatterns: [/openclaw\/plugin-sdk\/signal/, /plugin-sdk-internal\/signal/], |
| 41 | + }, |
| 42 | +]; |
| 43 | + |
| 44 | +const SETUP_BARREL_GUARDS: GuardedSource[] = [ |
| 45 | + { |
| 46 | + path: "extensions/signal/src/setup-core.ts", |
| 47 | + forbiddenPatterns: [/\bformatCliCommand\b/, /\bformatDocsLink\b/], |
| 48 | + }, |
| 49 | + { |
| 50 | + path: "extensions/signal/src/setup-surface.ts", |
| 51 | + forbiddenPatterns: [ |
| 52 | + /\bdetectBinary\b/, |
| 53 | + /\binstallSignalCli\b/, |
| 54 | + /\bformatCliCommand\b/, |
| 55 | + /\bformatDocsLink\b/, |
| 56 | + ], |
| 57 | + }, |
| 58 | + { |
| 59 | + path: "extensions/slack/src/setup-core.ts", |
| 60 | + forbiddenPatterns: [/\bformatDocsLink\b/], |
| 61 | + }, |
| 62 | + { |
| 63 | + path: "extensions/slack/src/setup-surface.ts", |
| 64 | + forbiddenPatterns: [/\bformatDocsLink\b/], |
| 65 | + }, |
| 66 | + { |
| 67 | + path: "extensions/discord/src/setup-core.ts", |
| 68 | + forbiddenPatterns: [/\bformatDocsLink\b/], |
| 69 | + }, |
| 70 | + { |
| 71 | + path: "extensions/discord/src/setup-surface.ts", |
| 72 | + forbiddenPatterns: [/\bformatDocsLink\b/], |
| 73 | + }, |
| 74 | + { |
| 75 | + path: "extensions/imessage/src/setup-core.ts", |
| 76 | + forbiddenPatterns: [/\bformatDocsLink\b/], |
| 77 | + }, |
| 78 | + { |
| 79 | + path: "extensions/imessage/src/setup-surface.ts", |
| 80 | + forbiddenPatterns: [/\bdetectBinary\b/, /\bformatDocsLink\b/], |
| 81 | + }, |
| 82 | + { |
| 83 | + path: "extensions/telegram/src/setup-core.ts", |
| 84 | + forbiddenPatterns: [/\bformatCliCommand\b/, /\bformatDocsLink\b/], |
| 85 | + }, |
| 86 | + { |
| 87 | + path: "extensions/whatsapp/src/setup-surface.ts", |
| 88 | + forbiddenPatterns: [/\bformatCliCommand\b/, /\bformatDocsLink\b/], |
| 89 | + }, |
| 90 | +]; |
| 91 | + |
| 92 | +function readSource(path: string): string { |
| 93 | + return readFileSync(resolve(ROOT_DIR, "..", path), "utf8"); |
| 94 | +} |
| 95 | + |
| 96 | +function readSetupBarrelImportBlock(path: string): string { |
| 97 | + const lines = readSource(path).split("\n"); |
| 98 | + const targetLineIndex = lines.findIndex((line) => |
| 99 | + /from\s*"[^"]*plugin-sdk(?:-internal)?\/setup(?:\.js)?";/.test(line), |
| 100 | + ); |
| 101 | + if (targetLineIndex === -1) { |
| 102 | + return ""; |
| 103 | + } |
| 104 | + let startLineIndex = targetLineIndex; |
| 105 | + while (startLineIndex >= 0 && !lines[startLineIndex].includes("import")) { |
| 106 | + startLineIndex -= 1; |
| 107 | + } |
| 108 | + return lines.slice(startLineIndex, targetLineIndex + 1).join("\n"); |
| 109 | +} |
| 110 | + |
| 111 | +describe("channel import guardrails", () => { |
| 112 | + it("keeps channel helper modules off their own SDK barrels", () => { |
| 113 | + for (const source of SAME_CHANNEL_SDK_GUARDS) { |
| 114 | + const text = readSource(source.path); |
| 115 | + for (const pattern of source.forbiddenPatterns) { |
| 116 | + expect(text, `${source.path} should not match ${pattern}`).not.toMatch(pattern); |
| 117 | + } |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + it("keeps setup barrels limited to setup primitives", () => { |
| 122 | + for (const source of SETUP_BARREL_GUARDS) { |
| 123 | + const importBlock = readSetupBarrelImportBlock(source.path); |
| 124 | + for (const pattern of source.forbiddenPatterns) { |
| 125 | + expect(importBlock, `${source.path} setup import should not match ${pattern}`).not.toMatch( |
| 126 | + pattern, |
| 127 | + ); |
| 128 | + } |
| 129 | + } |
| 130 | + }); |
| 131 | +}); |
0 commit comments