11// Covers bundling rules encoded in the root tsdown config.
22import { readFileSync } from "node:fs" ;
3+ import path from "node:path" ;
34import { bundledPluginRoot } from "openclaw/plugin-sdk/test-fixtures" ;
45import { describe , expect , it } from "vitest" ;
5- import tsdownConfig from "../../tsdown.config.ts" ;
6+ import tsdownConfig , {
7+ createStateSchemaInlinePlugin ,
8+ STATE_SCHEMA_INLINE_PLUGIN_NAME ,
9+ } from "../../tsdown.config.ts" ;
10+ import { OPENCLAW_AGENT_SCHEMA_SQL } from "../state/openclaw-agent-schema.js" ;
11+ import { OPENCLAW_STATE_SCHEMA_SQL } from "../state/openclaw-state-schema.js" ;
612
713type TsdownConfigEntry = {
814 deps ?: {
@@ -12,6 +18,7 @@ type TsdownConfigEntry = {
1218 entry ?: Record < string , string > | string [ ] ;
1319 inputOptions ?: TsdownInputOptions ;
1420 outDir ?: string ;
21+ plugins ?: Array < { name ?: string } > ;
1522} ;
1623
1724type TsdownLog = {
@@ -87,6 +94,53 @@ function readAgentAuthDiscoverySource(): string {
8794}
8895
8996describe ( "tsdown config" , ( ) => {
97+ it . each ( [
98+ {
99+ exportName : "OPENCLAW_STATE_SCHEMA_SQL" ,
100+ modulePath : "src/state/openclaw-state-schema.ts" ,
101+ schemaPath : "src/state/openclaw-state-schema.sql" ,
102+ sourceValue : OPENCLAW_STATE_SCHEMA_SQL ,
103+ } ,
104+ {
105+ exportName : "OPENCLAW_AGENT_SCHEMA_SQL" ,
106+ modulePath : "src/state/openclaw-agent-schema.ts" ,
107+ schemaPath : "src/state/openclaw-agent-schema.sql" ,
108+ sourceValue : OPENCLAW_AGENT_SCHEMA_SQL ,
109+ } ,
110+ ] ) ( "inlines canonical schema bytes for $modulePath" , ( schema ) => {
111+ const rootDir = process . cwd ( ) ;
112+ const watchedPaths : string [ ] = [ ] ;
113+ const plugin = createStateSchemaInlinePlugin ( rootDir ) ;
114+ const result = plugin . load . call (
115+ { addWatchFile : ( filePath : string ) => watchedPaths . push ( filePath ) } ,
116+ path . resolve ( rootDir , schema . modulePath ) ,
117+ ) ;
118+ const schemaPath = path . resolve ( rootDir , schema . schemaPath ) ;
119+ const canonicalSql = readFileSync ( schemaPath , "utf8" ) ;
120+
121+ expect ( result ) . not . toBeNull ( ) ;
122+ const match = result ?. code . match (
123+ new RegExp ( `^export const ${ schema . exportName } = (.*);\\n$` , "su" ) ,
124+ ) ;
125+ expect ( match ?. [ 1 ] ) . toBeDefined ( ) ;
126+ expect ( JSON . parse ( match ?. [ 1 ] ?? "null" ) ) . toBe ( canonicalSql ) ;
127+ expect ( schema . sourceValue ) . toBe ( canonicalSql ) ;
128+ expect ( watchedPaths ) . toEqual ( [ schemaPath ] ) ;
129+ } ) ;
130+
131+ it ( "installs schema inlining only on the unified runtime graph" , ( ) => {
132+ const unifiedGraph = requireUnifiedDistGraph ( ) ;
133+ const inlinePlugins = asConfigArray ( tsdownConfig ) . flatMap (
134+ ( config ) =>
135+ config . plugins ?. filter ( ( plugin ) => plugin . name === STATE_SCHEMA_INLINE_PLUGIN_NAME ) ?? [ ] ,
136+ ) ;
137+
138+ expect ( unifiedGraph . plugins ) . toContainEqual (
139+ expect . objectContaining ( { name : STATE_SCHEMA_INLINE_PLUGIN_NAME } ) ,
140+ ) ;
141+ expect ( inlinePlugins ) . toHaveLength ( 1 ) ;
142+ } ) ;
143+
90144 it ( "keeps core, plugin runtime, plugin-sdk, bundled root plugins, and bundled hooks in one dist graph" , ( ) => {
91145 const distGraph = requireUnifiedDistGraph ( ) ;
92146
0 commit comments