@@ -4,8 +4,16 @@ import os from "node:os";
44import path from "node:path" ;
55import { afterEach , describe , expect , it } from "vitest" ;
66import {
7+ createPluginInstallRecordMap ,
8+ getPluginInstallRecordMapEntry ,
9+ setPluginInstallRecordMapEntry ,
10+ } from "../../config/plugin-install-record-map.js" ;
11+ import type { PluginInstallRecord } from "../../config/types.plugins.js" ;
12+ import {
13+ preparePostCorePluginInstallRecordsForFreshProcess ,
714 readPostCorePluginInstallRecordsFile ,
815 shouldResumePostCoreUpdateInFreshProcess ,
16+ writePostCorePluginInstallRecordsFile ,
917} from "./update-command-post-core.js" ;
1018
1119const tempDirs : string [ ] = [ ] ;
@@ -35,28 +43,71 @@ describe("readPostCorePluginInstallRecordsFile", () => {
3543 await expect ( readPostCorePluginInstallRecordsFile ( missing ) ) . resolves . toBeUndefined ( ) ;
3644 } ) ;
3745
38- it ( "loads a valid install-records handoff" , async ( ) => {
46+ it ( "loads a prototype-safe install-records handoff with legal special ids " , async ( ) => {
3947 const dir = await withTempDir ( ) ;
4048 const filePath = path . join ( dir , "plugin-install-records.json" ) ;
4149 await fs . writeFile (
4250 filePath ,
43- `${ JSON . stringify ( {
44- demo : {
45- source : "npm" ,
46- spec : "@openclaw/[email protected] " , 47- installPath : "/tmp/demo-plugin" ,
48- } ,
49- } ) } \n`,
51+ '{"demo":{"source":"npm","spec":"@openclaw/[email protected] ","installPath":"/tmp/demo-plugin","futureMetadata":{"retained":true}},"constructor":{"source":"path"},"toString":{"source":"git"},"__proto__":{"source":"archive"}}\n' , 5052 "utf-8" ,
5153 ) ;
5254
53- await expect ( readPostCorePluginInstallRecordsFile ( filePath ) ) . resolves . toEqual ( {
54- demo : {
55- source : "npm" ,
56- spec :
"@openclaw/[email protected] " , 57- installPath : "/tmp/demo-plugin" ,
58- } ,
55+ const records = await readPostCorePluginInstallRecordsFile ( filePath ) ;
56+ if ( ! records ) {
57+ throw new Error ( "Expected plugin install records handoff" ) ;
58+ }
59+ expect ( Object . getPrototypeOf ( records ) ) . toBeNull ( ) ;
60+ expect ( getPluginInstallRecordMapEntry ( records , "demo" ) ) . toEqual ( {
61+ source : "npm" ,
62+ spec :
"@openclaw/[email protected] " , 63+ installPath : "/tmp/demo-plugin" ,
64+ futureMetadata : { retained : true } ,
65+ } ) ;
66+ expect ( getPluginInstallRecordMapEntry ( records , "constructor" ) ) . toEqual ( { source : "path" } ) ;
67+ expect ( getPluginInstallRecordMapEntry ( records , "toString" ) ) . toEqual ( { source : "git" } ) ;
68+ expect ( getPluginInstallRecordMapEntry ( records , "__proto__" ) ) . toEqual ( { source : "archive" } ) ;
69+ } ) ;
70+
71+ it ( "fails closed on structurally invalid handoff records" , async ( ) => {
72+ const dir = await withTempDir ( ) ;
73+ const filePath = path . join ( dir , "plugin-install-records.json" ) ;
74+ await fs . writeFile ( filePath , '{"demo":{"source":"bogus"}}\n' , "utf-8" ) ;
75+
76+ await expect ( readPostCorePluginInstallRecordsFile ( filePath ) ) . rejects . toThrow (
77+ `Invalid plugin install records in handoff file: ${ filePath } ` ,
78+ ) ;
79+ } ) ;
80+
81+ it ( "writes UTF-8 byte order and preserves special ids and passthrough fields" , async ( ) => {
82+ const dir = await withTempDir ( ) ;
83+ const filePath = path . join ( dir , "plugin-install-records.json" ) ;
84+ const records = createPluginInstallRecordMap < PluginInstallRecord > ( ) ;
85+ setPluginInstallRecordMapEntry ( records , "\u{10000}" , { source : "git" } ) ;
86+ setPluginInstallRecordMapEntry ( records , "__proto__" , { source : "archive" } ) ;
87+ setPluginInstallRecordMapEntry ( records , "2" , {
88+ source : "npm" ,
89+ futureMetadata : { retained : true } ,
90+ } as PluginInstallRecord ) ;
91+ setPluginInstallRecordMapEntry ( records , "toString" , { source : "git" } ) ;
92+ setPluginInstallRecordMapEntry ( records , "\uE000" , { source : "path" } ) ;
93+ setPluginInstallRecordMapEntry ( records , "constructor" , { source : "path" } ) ;
94+ setPluginInstallRecordMapEntry ( records , "10" , { source : "path" } ) ;
95+ setPluginInstallRecordMapEntry ( records , "1" , { source : "archive" } ) ;
96+ await writePostCorePluginInstallRecordsFile ( filePath , records ) ;
97+
98+ expect ( await fs . readFile ( filePath , "utf-8" ) ) . toBe (
99+ '{"1":{"source":"archive"},"10":{"source":"path"},"2":{"source":"npm","futureMetadata":{"retained":true}},"__proto__":{"source":"archive"},"constructor":{"source":"path"},"toString":{"source":"git"},"\uE000":{"source":"path"},"\u{10000}":{"source":"git"}}\n' ,
100+ ) ;
101+ const loaded = await readPostCorePluginInstallRecordsFile ( filePath ) ;
102+ if ( ! loaded ) {
103+ throw new Error ( "Expected plugin install records handoff" ) ;
104+ }
105+ expect ( Object . getPrototypeOf ( loaded ) ) . toBeNull ( ) ;
106+ expect ( getPluginInstallRecordMapEntry ( loaded , "2" ) ) . toEqual ( {
107+ source : "npm" ,
108+ futureMetadata : { retained : true } ,
59109 } ) ;
110+ expect ( getPluginInstallRecordMapEntry ( loaded , "__proto__" ) ) . toEqual ( { source : "archive" } ) ;
60111 } ) ;
61112
62113 it ( "fails closed on malformed handoff JSON with a path-labelled error" , async ( ) => {
@@ -93,6 +144,42 @@ describe("readPostCorePluginInstallRecordsFile", () => {
93144 } ) ;
94145} ) ;
95146
147+ describe ( "preparePostCorePluginInstallRecordsForFreshProcess" , ( ) => {
148+ it ( "preserves passthrough fields and untouched record identity across a downgrade handoff" , ( ) => {
149+ const records = createPluginInstallRecordMap < PluginInstallRecord > ( ) ;
150+ const untouched = { source : "path" as const , sourcePath : "/tmp/local" } ;
151+ const constructorRecord = { source : "git" as const } ;
152+ const toStringRecord = { source : "archive" as const } ;
153+ const protoRecord = { source : "path" as const , sourcePath : "/tmp/proto" } ;
154+ setPluginInstallRecordMapEntry ( records , "newer" , {
155+ source : "npm" ,
156+ resolvedVersion : "9999.0.0" ,
157+ resolvedSpec :
"[email protected] " , 158+ futureMetadata : { retained : true } ,
159+ } as PluginInstallRecord ) ;
160+ setPluginInstallRecordMapEntry ( records , "untouched" , untouched ) ;
161+ setPluginInstallRecordMapEntry ( records , "constructor" , constructorRecord ) ;
162+ setPluginInstallRecordMapEntry ( records , "toString" , toStringRecord ) ;
163+ setPluginInstallRecordMapEntry ( records , "__proto__" , protoRecord ) ;
164+
165+ const prepared = preparePostCorePluginInstallRecordsForFreshProcess ( {
166+ records,
167+ targetVersion : "1.0.0" ,
168+ } ) ;
169+
170+ expect ( prepared ) . not . toBe ( records ) ;
171+ expect ( Object . getPrototypeOf ( prepared ) ) . toBeNull ( ) ;
172+ expect ( getPluginInstallRecordMapEntry ( prepared , "untouched" ) ) . toBe ( untouched ) ;
173+ expect ( getPluginInstallRecordMapEntry ( prepared , "constructor" ) ) . toBe ( constructorRecord ) ;
174+ expect ( getPluginInstallRecordMapEntry ( prepared , "toString" ) ) . toBe ( toStringRecord ) ;
175+ expect ( getPluginInstallRecordMapEntry ( prepared , "__proto__" ) ) . toBe ( protoRecord ) ;
176+ expect ( getPluginInstallRecordMapEntry ( prepared , "newer" ) ) . toEqual ( {
177+ source : "npm" ,
178+ futureMetadata : { retained : true } ,
179+ } ) ;
180+ } ) ;
181+ } ) ;
182+
96183describe ( "shouldResumePostCoreUpdateInFreshProcess" , ( ) => {
97184 const unchangedGitResult = {
98185 status : "ok" as const ,
0 commit comments