@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
22import { mkdir , mkdtemp , stat , writeFile } from "node:fs/promises" ;
33import os from "node:os" ;
44import path from "node:path" ;
5+ import { pathToFileURL } from "node:url" ;
56import { test } from "node:test" ;
67import { createMockSdkPackage } from "../src/advanced.js" ;
78
@@ -19,3 +20,42 @@ test("mock SDK ignores subpaths that would escape the plugin-sdk package", async
1920
2021 await assert . rejects ( stat ( path . join ( rootDir , "node_modules" , "openclaw" , "escape.js" ) ) , { code : "ENOENT" } ) ;
2122} ) ;
23+
24+ test ( "mock SDK preserves the isRecord predicate contract" , async ( ) => {
25+ const rootDir = await mkdtemp ( path . join ( os . tmpdir ( ) , "plugin-inspector-sdk-mock-" ) ) ;
26+ const pluginRoot = path . join ( rootDir , "plugin" ) ;
27+ await mkdir ( pluginRoot , { recursive : true } ) ;
28+ await writeFile (
29+ path . join ( pluginRoot , "index.js" ) ,
30+ [
31+ 'import { asNullableRecord, asOptionalRecord, asRecord, isRecord, readStringField } from "openclaw/plugin-sdk/string-coerce-runtime";' ,
32+ "export { asNullableRecord, asOptionalRecord, asRecord, isRecord, readStringField };" ,
33+ "" ,
34+ ] . join ( "\n" ) ,
35+ "utf8" ,
36+ ) ;
37+
38+ await createMockSdkPackage ( rootDir , { pluginRoot } ) ;
39+
40+ const mockModule = await import (
41+ pathToFileURL ( path . join ( rootDir , "node_modules" , "openclaw" , "plugin-sdk" , "string-coerce-runtime.js" ) ) . href
42+ ) ;
43+ const record = { count : 42 , value : "ok" } ;
44+ const array = [ "value" ] ;
45+ assert . equal ( mockModule . isRecord ( record ) , true ) ;
46+ assert . equal ( mockModule . isRecord ( [ ] ) , false ) ;
47+ assert . equal ( mockModule . isRecord ( "value" ) , false ) ;
48+ assert . equal ( mockModule . isRecord ( 42 ) , false ) ;
49+ assert . equal ( mockModule . isRecord ( null ) , false ) ;
50+ assert . equal ( mockModule . isRecord ( undefined ) , false ) ;
51+ assert . equal ( mockModule . asRecord ( record ) , record ) ;
52+ assert . equal ( mockModule . asRecord ( array ) , array ) ;
53+ assert . deepEqual ( mockModule . asRecord ( null ) , { } ) ;
54+ assert . equal ( mockModule . asOptionalRecord ( record ) , record ) ;
55+ assert . equal ( mockModule . asOptionalRecord ( array ) , undefined ) ;
56+ assert . equal ( mockModule . asNullableRecord ( record ) , record ) ;
57+ assert . equal ( mockModule . asNullableRecord ( array ) , null ) ;
58+ assert . equal ( mockModule . readStringField ( record , "value" ) , "ok" ) ;
59+ assert . equal ( mockModule . readStringField ( record , "count" ) , undefined ) ;
60+ assert . equal ( mockModule . readStringField ( undefined , "value" ) , undefined ) ;
61+ } ) ;
0 commit comments