11import Ajv from 'ajv' ;
22import {
33 ApplyRequestData ,
4+ ImportRequestData ,
5+ ImportResponseData ,
46 InitializeResponseData ,
57 IpcMessageSchema ,
68 MessageCmd ,
@@ -15,6 +17,7 @@ import {
1517 ValidateResponseData
1618} from 'codify-schemas' ;
1719import { ChildProcess , SpawnOptions , fork , spawn } from 'node:child_process' ;
20+ import inspector from 'node:inspector'
1821import path from 'node:path' ;
1922
2023import { CodifyTestUtils } from './test-utils.js' ;
@@ -25,6 +28,7 @@ const ajv = new Ajv.default({
2528const ipcMessageValidator = ajv . compile ( IpcMessageSchema ) ;
2629const sudoRequestValidator = ajv . compile ( SudoRequestDataSchema ) ;
2730
31+
2832export class PluginTester {
2933 childProcess : ChildProcess
3034
@@ -38,6 +42,9 @@ export class PluginTester {
3842 throw new Error ( 'A fully qualified path must be supplied to PluginTester' ) ;
3943 }
4044
45+ console . log ( 'Node Inspector:' )
46+ console . log ( inspector . url ( ) ) ;
47+
4148 this . childProcess = fork (
4249 pluginPath ,
4350 [ ] ,
@@ -52,7 +59,19 @@ export class PluginTester {
5259 this . handleSudoRequests ( this . childProcess ) ;
5360 }
5461
55- async fullTest ( configs : ResourceConfig [ ] , skipUninstall = false , assertPlans ?: ( plans : PlanResponseData [ ] ) => void ) : Promise < void > {
62+ async fullTest (
63+ configs : ResourceConfig [ ] ,
64+ {
65+ skipUninstall = false ,
66+ ...options
67+ } : {
68+ skipUninstall : boolean ,
69+ validatePlan ?: ( plans : PlanResponseData [ ] ) => Promise < void > | void
70+ validateApply ?: ( plans : PlanResponseData [ ] ) => Promise < void > | void ,
71+ validateDestroy ?: ( plans : PlanResponseData [ ] ) => Promise < void > | void ,
72+ validateImport ?: ( importResults : ( ImportResponseData [ 'result' ] [ 0 ] ) [ ] ) => Promise < void > | void ,
73+ } ) : Promise < void > {
74+
5675 const initializeResult = await this . initialize ( ) ;
5776
5877 const unsupportedConfigs = configs . filter ( ( c ) =>
@@ -78,8 +97,8 @@ export class PluginTester {
7897 } ) ) ;
7998 }
8099
81- if ( assertPlans ) {
82- assertPlans ( plans ) ;
100+ if ( options . validatePlan ) {
101+ await options . validatePlan ( plans ) ;
83102 }
84103
85104 for ( const plan of plans ) {
@@ -105,12 +124,40 @@ ${JSON.stringify(unsuccessfulPlans, null, 2)}`
105124 )
106125 }
107126
127+ if ( options . validateApply ) {
128+ await options . validateApply ( plans ) ;
129+ }
130+
131+ const importResults = [ ] ;
132+ const unsuccessfulImports = [ ] ;
133+ for ( const config of configs ) {
134+ const importResult = await this . import ( { config } )
135+ importResults . push ( importResult ) ;
136+
137+ if ( importResult . result . length !== 1 ||
138+ Object . entries ( config ) . some ( ( [ k , v ] ) => importResult . result [ 0 ] [ k ] !== v )
139+ ) {
140+ unsuccessfulImports . push ( importResult ) ;
141+ }
142+ }
143+
144+ if ( unsuccessfulImports . length > 0 ) {
145+ throw new Error ( `The following imports were not successful. The imports differed from the original.
146+ ${ JSON . stringify ( unsuccessfulImports , null , 2 ) } `) ;
147+ }
148+
149+ if ( options . validateImport ) {
150+ await options . validateImport ( importResults . map ( ( r ) => r . result [ 0 ] ) ) ;
151+ }
152+
108153 if ( ! skipUninstall ) {
109- await this . uninstall ( configs . toReversed ( ) ) ;
154+ await this . uninstall ( configs . toReversed ( ) , options ) ;
110155 }
111156 }
112157
113- async uninstall ( configs : ResourceConfig [ ] ) {
158+ async uninstall ( configs : ResourceConfig [ ] , options : {
159+ validateDestroy ?: ( plans : PlanResponseData [ ] ) => Promise < void > | void
160+ } ) {
114161 const plans = [ ] ;
115162
116163 for ( const config of configs ) {
@@ -145,6 +192,10 @@ ${JSON.stringify(validationPlan, null, 2)}
145192 ` ) ;
146193 }
147194 }
195+
196+ if ( options . validateDestroy ) {
197+ await options . validateDestroy ( plans ) ;
198+ }
148199 }
149200
150201 async initialize ( ) : Promise < InitializeResponseData > {
@@ -175,6 +226,13 @@ ${JSON.stringify(validationPlan, null, 2)}
175226 } ) ;
176227 }
177228
229+ async import ( data : ImportRequestData ) : Promise < ImportResponseData > {
230+ return CodifyTestUtils . sendMessageAndAwaitResponse ( this . childProcess , {
231+ cmd : 'import' ,
232+ data,
233+ } ) ;
234+ }
235+
178236 kill ( ) {
179237 this . childProcess . kill ( ) ;
180238 }
0 commit comments