@@ -2,6 +2,7 @@ import fs from "node:fs/promises";
22import path from "node:path" ;
33import * as tar from "tar" ;
44import { describe , expect , it , vi } from "vitest" ;
5+ import { runCommandWithTimeout } from "../../process/exec.js" ;
56import { withTestDir } from "../../test-helpers/temp-dir.js" ;
67import {
78 createWorkerBundleProducer ,
@@ -246,6 +247,76 @@ describe("worker bundle producer", () => {
246247 } ) ;
247248 } ) ;
248249
250+ it ( "installs a source bundle when the AI workspace import is bundled" , async ( ) => {
251+ await withTestDir ( { prefix : "openclaw-worker-bundle-npm-install-" } , async ( root ) => {
252+ const repoRoot = path . resolve ( import . meta. dirname , "../../.." ) ;
253+ const aiManifest = JSON . parse (
254+ await fs . readFile ( path . join ( repoRoot , "packages/ai/package.json" ) , "utf8" ) ,
255+ ) as {
256+ dependencies ?: Record < string , string > ;
257+ devDependencies ?: Record < string , string > ;
258+ } ;
259+ const dependencyFields = ( [ "dependencies" , "devDependencies" ] as const ) . filter (
260+ ( field ) => aiManifest [ field ] ?. [ "@openclaw/normalization-core" ] !== undefined ,
261+ ) ;
262+ if ( dependencyFields . length !== 1 ) {
263+ throw new Error (
264+ "@openclaw/ai must classify normalization-core in exactly one dependency field" ,
265+ ) ;
266+ }
267+ const dependencyField = dependencyFields [ 0 ] ! ;
268+ const normalizationCoreSpec = aiManifest [ dependencyField ] ?. [ "@openclaw/normalization-core" ] ;
269+ if ( ! normalizationCoreSpec ?. startsWith ( "workspace:" ) ) {
270+ throw new Error ( "@openclaw/ai must use a workspace normalization-core dependency" ) ;
271+ }
272+ const packageRoot = path . join ( root , "package" ) ;
273+ await writeFixture ( packageRoot , [ [ "dist/entry.js" , 'import "@openclaw/ai";\nexport {};\n' ] ] ) ;
274+ await fs . writeFile (
275+ path . join ( packageRoot , "package.json" ) ,
276+ `${ JSON . stringify ( {
277+ name : "openclaw" ,
278+ version : "1.2.3" ,
279+ type : "module" ,
280+ files : [ "dist/" ] ,
281+ dependencies : { "@openclaw/ai" : "workspace:*" } ,
282+ } ) } \n`,
283+ "utf8" ,
284+ ) ;
285+ const vendorSource = path . join ( packageRoot , "node_modules/@openclaw/ai" ) ;
286+ await fs . mkdir ( path . join ( vendorSource , "dist" ) , { recursive : true } ) ;
287+ await fs . writeFile (
288+ path . join ( vendorSource , "package.json" ) ,
289+ `${ JSON . stringify ( {
290+ name : "@openclaw/ai" ,
291+ version : "1.2.3" ,
292+ type : "module" ,
293+ main : "./dist/index.js" ,
294+ [ dependencyField ] : { "@openclaw/normalization-core" : normalizationCoreSpec } ,
295+ } ) } \n`,
296+ "utf8" ,
297+ ) ;
298+ await fs . writeFile ( path . join ( vendorSource , "dist/index.js" ) , "export {};\n" , "utf8" ) ;
299+
300+ const bundle = await createWorkerBundleProducer ( {
301+ packageRoot,
302+ cacheDir : path . join ( root , "cache" ) ,
303+ } ) . prepare ( ) ;
304+ const extractRoot = path . join ( root , "extract" ) ;
305+ await fs . mkdir ( extractRoot ) ;
306+ await tar . extract ( { file : bundle . tarballPath , cwd : extractRoot } ) ;
307+
308+ const install = await runCommandWithTimeout (
309+ [ "npm" , "install" , "--ignore-scripts" , "--omit=dev" , "--no-audit" , "--no-fund" ] ,
310+ {
311+ cwd : extractRoot ,
312+ env : { NPM_CONFIG_CACHE : path . join ( root , "npm-cache" ) } ,
313+ timeoutMs : 30_000 ,
314+ } ,
315+ ) ;
316+ expect ( install . code , install . stderr ) . toBe ( 0 ) ;
317+ } ) ;
318+ } ) ;
319+
249320 it ( "fails closed when a dist-referenced workspace package is not installed" , async ( ) => {
250321 await withTestDir ( { prefix : "openclaw-worker-bundle-vendor-missing-" } , async ( root ) => {
251322 const packageRoot = path . join ( root , "package" ) ;
0 commit comments