11import assert from 'node:assert/strict' ;
22import fs from 'node:fs' ;
3- import { beforeEach , test , vi } from 'vitest' ;
3+ import { afterEach , beforeEach , test , vi } from 'vitest' ;
44import type { DeviceInfo } from '@agent-device/kernel/device' ;
55
66const { runHarmonyHdc } = vi . hoisted ( ( ) => ( { runHarmonyHdc : vi . fn ( ) } ) ) ;
@@ -33,6 +33,10 @@ beforeEach(() => {
3333 scriptHarmonyLayoutDump ( TALL_LAYOUT ) ;
3434} ) ;
3535
36+ afterEach ( ( ) => {
37+ vi . useRealTimers ( ) ;
38+ } ) ;
39+
3640/** Scripts `uitest dumpLayout` + `file recv` so the pulled layout is `layout`. */
3741function scriptHarmonyLayoutDump ( layout : unknown ) : void {
3842 runHarmonyHdc . mockImplementation ( async ( _device : unknown , args : string [ ] ) => {
@@ -56,6 +60,21 @@ test('repeated viewport reads within the TTL trigger a single layout dump', asyn
5660 assert . equal ( dumpLayoutCallCount ( ) , 1 ) ;
5761} ) ;
5862
63+ test ( 'viewport cache expires after two seconds without explicit invalidation' , async ( ) => {
64+ vi . useFakeTimers ( ) ;
65+ vi . setSystemTime ( 0 ) ;
66+
67+ await readHarmonyGestureViewport ( DEVICE_A ) ;
68+ assert . equal ( dumpLayoutCallCount ( ) , 1 ) ;
69+
70+ scriptHarmonyLayoutDump ( WIDE_LAYOUT ) ;
71+ await vi . advanceTimersByTimeAsync ( 2_000 ) ;
72+ const refreshed = await readHarmonyGestureViewport ( DEVICE_A ) ;
73+
74+ assert . deepEqual ( refreshed , { x : 0 , y : 0 , width : 2340 , height : 1080 } ) ;
75+ assert . equal ( dumpLayoutCallCount ( ) , 2 ) ;
76+ } ) ;
77+
5978test ( 'invalidating the viewport cache forces a fresh layout dump' , async ( ) => {
6079 await readHarmonyGestureViewport ( DEVICE_A ) ;
6180 assert . equal ( dumpLayoutCallCount ( ) , 1 ) ;
0 commit comments