|
1 | 1 | // Plugin Sdk Surface Report tests cover plugin sdk surface report script behavior. |
2 | 2 | import { spawnSync } from "node:child_process"; |
| 3 | +import { readFileSync } from "node:fs"; |
3 | 4 | import { describe, expect, it } from "vitest"; |
4 | 5 | |
5 | 6 | function runSurfaceReport(env: Record<string, string>) { |
@@ -18,13 +19,32 @@ function readCurrentPublicFunctionExportCount() {
|
18 | 19 | expect(result.status).toBe(0); |
19 | 20 | expect(result.stderr).toBe(""); |
20 | 21 | |
21 | | -const match = /public package SDK entrypoints:[\s\S]*?\n callable exports: (\d+)/u.exec( |
22 | | -result.stdout, |
| 22 | +return parseCurrentPublicCounts(result.stdout).functionExports; |
| 23 | +} |
| 24 | + |
| 25 | +function parseCurrentPublicCounts(stdout: string) { |
| 26 | +const match = /public package SDK entrypoints:[\s\S]*?\n exports: (\d+)\n callable exports: (\d+)/u |
| 27 | +.exec(stdout); |
| 28 | +if (match === null || match[1] === undefined || match[2] === undefined) { |
| 29 | +throw new Error("failed to read current public export counts"); |
| 30 | +} |
| 31 | +return { |
| 32 | +exports: Number(match[1]), |
| 33 | +functionExports: Number(match[2]), |
| 34 | +}; |
| 35 | +} |
| 36 | + |
| 37 | +function readDefaultBudget(envName: string): number { |
| 38 | +const source = readFileSync("scripts/plugin-sdk-surface-report.mjs", "utf8"); |
| 39 | +const match = new RegExp( |
| 40 | +`readBudgetEnv\\("${envName}",\\s*(\\d+)\\)`, |
| 41 | +"u", |
23 | 42 | ); |
24 | | -if (match === null || match[1] === undefined) { |
25 | | -throw new Error("failed to read current public function export count"); |
| 43 | +const result = match.exec(source); |
| 44 | +if (result === null || result[1] === undefined) { |
| 45 | +throw new Error(`failed to read default budget for ${envName}`); |
26 | 46 | } |
27 | | -return Number(match[1]); |
| 47 | +return Number(result[1]); |
28 | 48 | } |
29 | 49 | |
30 | 50 | describe("plugin SDK surface report", () => { |
@@ -95,6 +115,18 @@ describe("plugin SDK surface report", () => {
|
95 | 115 | expect(result.stderr).toBe(""); |
96 | 116 | }); |
97 | 117 | |
| 118 | +it("keeps default public budgets tight to the current source surface", () => { |
| 119 | +const result = runSurfaceReport({}); |
| 120 | +expect(result.status).toBe(0); |
| 121 | +expect(result.stderr).toBe(""); |
| 122 | + |
| 123 | +const counts = parseCurrentPublicCounts(result.stdout); |
| 124 | +expect(readDefaultBudget("OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_EXPORTS")).toBe(counts.exports); |
| 125 | +expect(readDefaultBudget("OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_FUNCTION_EXPORTS")).toBe( |
| 126 | +counts.functionExports, |
| 127 | +); |
| 128 | +}); |
| 129 | + |
98 | 130 | it("keeps generated package declarations out of source surface counts", () => { |
99 | 131 | const budget = readCurrentPublicFunctionExportCount(); |
100 | 132 | const result = runSurfaceReport({ |
|