11// Plugin Sdk Surface Report tests cover plugin sdk surface report script behavior.
22import { spawnSync } from "node:child_process" ;
3+ import { readFileSync } from "node:fs" ;
34import { describe , expect , it } from "vitest" ;
45
56function runSurfaceReport ( env : Record < string , string > ) {
@@ -18,13 +19,32 @@ function readCurrentPublicFunctionExportCount() {
1819 expect ( result . status ) . toBe ( 0 ) ;
1920 expect ( result . stderr ) . toBe ( "" ) ;
2021
21- const match = / p u b l i c p a c k a g e S D K e n t r y p o i n t s : [ \s \S ] * ?\n c a l l a b l e e x p o r t s : ( \d + ) / u. exec (
22- result . stdout ,
22+ return parseCurrentPublicCounts ( result . stdout ) . functionExports ;
23+ }
24+
25+ function parseCurrentPublicCounts ( stdout : string ) {
26+ const match = / p u b l i c p a c k a g e S D K e n t r y p o i n t s : [ \s \S ] * ?\n e x p o r t s : ( \d + ) \n c a l l a b l e e x p o r t s : ( \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" ,
2342 ) ;
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 } ` ) ;
2646 }
27- return Number ( match [ 1 ] ) ;
47+ return Number ( result [ 1 ] ) ;
2848}
2949
3050describe ( "plugin SDK surface report" , ( ) => {
@@ -95,6 +115,18 @@ describe("plugin SDK surface report", () => {
95115 expect ( result . stderr ) . toBe ( "" ) ;
96116 } ) ;
97117
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+
98130 it ( "keeps generated package declarations out of source surface counts" , ( ) => {
99131 const budget = readCurrentPublicFunctionExportCount ( ) ;
100132 const result = runSurfaceReport ( {
0 commit comments