@@ -99,21 +99,41 @@ test('exposes HarmonyOS deployApp and uninstallApp after fact admission', async
9999 ) . toEqual ( { } ) ;
100100} ) ;
101101
102- const installedDump : HostCommandResult = {
103- stdout : '{"hapModuleInfos":[{"mainElementName":"MainAbility","moduleName":"entry"}]}' ,
102+ /** Answers each call in order; the last answer repeats when a script runs past its end. */
103+ function scriptAnswers ( answers : readonly HostCommandResult [ ] ) {
104+ let next = 0 ;
105+ return async ( ) : Promise < HostCommandResult > => answers [ Math . min ( next ++ , answers . length - 1 ) ] ! ;
106+ }
107+
108+ const bundleInventory = ( entries : string [ ] ) : HostCommandResult => ( {
109+ stdout : [ ...entries , '' ] . join ( '\n' ) ,
110+ stderr : '' ,
111+ exitCode : 0 ,
112+ } ) ;
113+
114+ const uninstallAnswer : HostCommandResult = {
115+ stdout : 'uninstall bundle successfully.' ,
116+ stderr : '' ,
117+ exitCode : 0 ,
118+ } ;
119+
120+ /** `bm dump -a` inventory answers: one bundle name per line, the shared parseHarmonyBundleList shape. */
121+ const installedInventory = bundleInventory ( [ 'com.example.other' , 'com.example.app' ] ) ;
122+ const absentInventory = bundleInventory ( [ 'com.example.other' ] ) ;
123+
124+ /**
125+ * Shapes recorded on physical hardware: bm prints its per-bundle failures into
126+ * stdout while the host exit code stays 0, so this answer is an unknown read,
127+ * never a proof of absence.
128+ */
129+ const unknownInventoryRead : HostCommandResult = {
130+ stdout : 'error: failed to get information and the parameters may be wrong.' ,
104131 stderr : '' ,
105132 exitCode : 0 ,
106133} ;
107134
108135test ( 'uninstall removes an installed HarmonyOS bundle through bm uninstall' , async ( ) => {
109- const uninstallAnswer : HostCommandResult = {
110- stdout : 'uninstall bundle successfully.' ,
111- stderr : '' ,
112- exitCode : 0 ,
113- } ;
114- const run = vi . fn (
115- scriptAnswers ( [ installedDump , uninstallAnswer , absentBundleAnswer , emptyAnswer ] ) ,
116- ) ;
136+ const run = vi . fn ( scriptAnswers ( [ installedInventory , uninstallAnswer , absentInventory ] ) ) ;
117137 const commands = { which : async ( ) => undefined , run } as HostCommandRunner ;
118138 const signal = new AbortController ( ) . signal ;
119139 const operations = createHarmonyAppDeploymentOperations ( { commands, device, signal } ) ;
@@ -122,12 +142,12 @@ test('uninstall removes an installed HarmonyOS bundle through bm uninstall', asy
122142 wasInstalled : true ,
123143 packageName : 'com.example.app' ,
124144 } ) ;
125- expect ( run ) . toHaveBeenCalledTimes ( 4 ) ;
145+ expect ( run ) . toHaveBeenCalledTimes ( 3 ) ;
126146 expect ( run ) . toHaveBeenNthCalledWith (
127147 1 ,
128148 expect . objectContaining ( {
129149 executable : 'hdc' ,
130- args : [ '-t' , device . id , 'shell' , 'bm' , 'dump' , '-n' , 'com.example.app '] ,
150+ args : [ '-t' , device . id , 'shell' , 'bm' , 'dump' , '-a ' ] ,
131151 } ) ,
132152 signal ,
133153 ) ;
@@ -143,24 +163,30 @@ test('uninstall removes an installed HarmonyOS bundle through bm uninstall', asy
143163 3 ,
144164 expect . objectContaining ( {
145165 executable : 'hdc' ,
146- args : [ '-t' , device . id , 'shell' , 'bm' , 'dump' , '-n' , 'com.example.app '] ,
166+ args : [ '-t' , device . id , 'shell' , 'bm' , 'dump' , '-a ' ] ,
147167 } ) ,
148168 signal ,
149169 ) ;
150- expect ( run ) . toHaveBeenNthCalledWith (
151- 4 ,
152- expect . objectContaining ( { args : [ '-t' , device . id , 'shell' , 'true' ] } ) ,
170+ } ) ;
171+
172+ test ( 'uninstall reports an absent bundle without uninstalling' , async ( ) => {
173+ const run = vi . fn ( scriptAnswers ( [ absentInventory ] ) ) ;
174+ const commands = { which : async ( ) => undefined , run } as HostCommandRunner ;
175+ const signal = new AbortController ( ) . signal ;
176+ const operations = createHarmonyAppDeploymentOperations ( { commands, device, signal } ) ;
177+
178+ await expect ( operations . uninstallApp ?.( { app : 'com.example.app' } ) ) . resolves . toEqual ( {
179+ wasInstalled : false ,
180+ } ) ;
181+ expect ( run ) . toHaveBeenCalledTimes ( 1 ) ;
182+ expect ( run ) . not . toHaveBeenCalledWith (
183+ expect . objectContaining ( { args : expect . arrayContaining ( [ 'uninstall' ] ) } ) ,
153184 signal ,
154185 ) ;
155186} ) ;
156187
157- test ( 'uninstall refuses to claim removal when the bundle record survives bm uninstall' , async ( ) => {
158- const lyingUninstallAnswer : HostCommandResult = {
159- stdout : 'uninstall bundle successfully.' ,
160- stderr : '' ,
161- exitCode : 0 ,
162- } ;
163- const run = vi . fn ( scriptAnswers ( [ installedDump , lyingUninstallAnswer , installedDump ] ) ) ;
188+ test ( 'uninstall refuses to claim removal when the inventory still lists the bundle' , async ( ) => {
189+ const run = vi . fn ( scriptAnswers ( [ installedInventory , uninstallAnswer , installedInventory ] ) ) ;
164190 const commands = { which : async ( ) => undefined , run } as HostCommandRunner ;
165191 const signal = new AbortController ( ) . signal ;
166192 const operations = createHarmonyAppDeploymentOperations ( { commands, device, signal } ) ;
@@ -171,109 +197,41 @@ test('uninstall refuses to claim removal when the bundle record survives bm unin
171197 details : { stdout : 'uninstall bundle successfully.' } ,
172198 } ) ;
173199 expect ( run ) . toHaveBeenCalledTimes ( 3 ) ;
174- expect ( run ) . not . toHaveBeenCalledWith (
175- expect . objectContaining ( { args : [ '-t' , device . id , 'shell' , 'true' ] } ) ,
176- signal ,
177- ) ;
178200} ) ;
179201
180- /** Answers each call in order; the last answer repeats when a script runs past its end. */
181- function scriptAnswers ( answers : readonly HostCommandResult [ ] ) {
182- let next = 0 ;
183- return async ( ) : Promise < HostCommandResult > => answers [ Math . min ( next ++ , answers . length - 1 ) ] ! ;
184- }
185-
186- const emptyAnswer : HostCommandResult = { stdout : '' , stderr : '' , exitCode : 0 } ;
187-
188- /**
189- * Shapes recorded on physical hardware: an absent bundle answers through bm with an
190- * error record, while an unreachable target answers through hdc itself and still
191- * exits zero. The second probe call classifies the two.
192- */
193- const absentBundleAnswer : HostCommandResult = {
194- stdout : 'error: failed to get information and the parameters may be wrong.' ,
195- stderr : '' ,
196- exitCode : 0 ,
197- } ;
198- const unreachableAnswer : HostCommandResult = {
199- stdout : '[Fail]Not match target founded, check connect-key please' ,
200- stderr : '' ,
201- exitCode : 0 ,
202- } ;
203-
204- function scriptUninstallCommands (
205- responses : Readonly < { dump : HostCommandResult ; probe ?: HostCommandResult } > ,
206- ) : HostCommandRunner {
207- const run = vi . fn ( async ( request : { executable : string ; args : readonly string [ ] } ) => {
208- if ( request . args . includes ( 'dump' ) ) return responses . dump ;
209- if ( request . args . includes ( 'true' ) ) return responses . probe ?? emptyAnswer ;
210- return emptyAnswer ;
211- } ) ;
212- return { which : async ( ) => undefined , run } as HostCommandRunner ;
213- }
214-
215- test ( 'uninstall reports an absent record as nothing removed once the transport proves alive' , async ( ) => {
216- const commands = scriptUninstallCommands ( { dump : absentBundleAnswer } ) ;
202+ test ( 'uninstall refuses an inventory read it cannot interpret' , async ( ) => {
203+ const run = vi . fn ( scriptAnswers ( [ unknownInventoryRead ] ) ) ;
204+ const commands = { which : async ( ) => undefined , run } as HostCommandRunner ;
217205 const signal = new AbortController ( ) . signal ;
218206 const operations = createHarmonyAppDeploymentOperations ( { commands, device, signal } ) ;
219207
220- await expect ( operations . uninstallApp ?.( { app : 'com.example.app' } ) ) . resolves . toEqual ( {
221- wasInstalled : false ,
208+ await expect ( operations . uninstallApp ?.( { app : 'com.example.app' } ) ) . rejects . toMatchObject ( {
209+ code : 'COMMAND_FAILED' ,
210+ message : 'Could not read the installed bundle inventory' ,
222211 } ) ;
223- expect ( commands . run ) . toHaveBeenCalledTimes ( 2 ) ;
224- expect ( commands . run ) . toHaveBeenNthCalledWith (
225- 2 ,
226- expect . objectContaining ( { args : [ '-t' , device . id , 'shell' , 'true' ] } ) ,
212+ expect ( run ) . toHaveBeenCalledTimes ( 1 ) ;
213+ expect ( run ) . not . toHaveBeenCalledWith (
214+ expect . objectContaining ( { args : expect . arrayContaining ( [ 'uninstall' ] ) } ) ,
227215 signal ,
228216 ) ;
229217} ) ;
230218
231- test . each ( [
232- [
233- 'an unreachable target that hdc answers while exiting zero' ,
234- { dump : unreachableAnswer , probe : unreachableAnswer } ,
235- 2 ,
236- ] ,
237- [
238- 'a dump that exits non-zero on a dead transport' ,
239- {
240- dump : { stdout : '[Fail]Device not connected' , stderr : '' , exitCode : 1 } ,
241- probe : { stdout : '[Fail]Device not connected' , stderr : '' , exitCode : 1 } ,
242- } ,
243- 2 ,
244- ] ,
245- [ 'a dump killed before an answer' , { dump : { stdout : '' , stderr : '' , exitCode : null } } , 1 ] ,
246- ] as const ) (
247- 'uninstall refuses to claim removal for %s' ,
248- async ( _name , responses , expectedCalls ) => {
249- const commands = scriptUninstallCommands ( responses ) ;
250- const signal = new AbortController ( ) . signal ;
251- const operations = createHarmonyAppDeploymentOperations ( { commands, device, signal } ) ;
252-
253- await expect ( operations . uninstallApp ?.( { app : 'com.example.app' } ) ) . rejects . toMatchObject ( {
254- code : 'COMMAND_FAILED' ,
255- } ) ;
256- expect ( commands . run ) . toHaveBeenCalledTimes ( expectedCalls ) ;
257- expect ( commands . run ) . not . toHaveBeenCalledWith (
258- expect . objectContaining ( { args : expect . arrayContaining ( [ 'uninstall' ] ) } ) ,
259- signal ,
260- ) ;
261- } ,
262- ) ;
263-
264- test ( 'uninstall refuses to claim removal when the post-uninstall dump cannot be verified' , async ( ) => {
219+ test ( 'uninstall refuses to claim removal when the inventory probe exits non-zero' , async ( ) => {
265220 const run = vi . fn (
266- scriptAnswers ( [ installedDump , emptyAnswer , absentBundleAnswer , unreachableAnswer ] ) ,
221+ scriptAnswers ( [
222+ { stdout : '[Fail]Device not connected' , stderr : '[Fail]Device not connected' , exitCode : 1 } ,
223+ ] ) ,
267224 ) ;
268225 const commands = { which : async ( ) => undefined , run } as HostCommandRunner ;
269226 const signal = new AbortController ( ) . signal ;
270227 const operations = createHarmonyAppDeploymentOperations ( { commands, device, signal } ) ;
271228
272229 await expect ( operations . uninstallApp ?.( { app : 'com.example.app' } ) ) . rejects . toMatchObject ( {
273230 code : 'COMMAND_FAILED' ,
274- message : 'Could not verify the uninstall of com.example.app' ,
231+ message : 'Could not read the installed bundle inventory' ,
232+ details : { processExitError : true , exitCode : 1 } ,
275233 } ) ;
276- expect ( run ) . toHaveBeenCalledTimes ( 4 ) ;
234+ expect ( run ) . toHaveBeenCalledTimes ( 1 ) ;
277235} ) ;
278236
279237test ( 'uninstall refuses a blank bundle name' , async ( ) => {
0 commit comments