11import { beforeAll , describe , test } from 'vitest' ;
22import assert from 'node:assert/strict' ;
3+ import fs from 'node:fs' ;
34import path from 'node:path' ;
45import { fileURLToPath } from 'node:url' ;
6+ import { PNG } from 'pngjs' ;
57import { runCmd } from '@agent-device/host-kit/command' ;
68import { AppError } from '@agent-device/kernel/errors' ;
79import { CONTACT_SHEET_UNSUPPORTED_HOST_REASON } from '../contact-sheet-report.ts' ;
810import { assertContactSheetHostSupport } from '../contact-sheet-frames.ts' ;
911import { getRecordingOverlaySupportWarning } from '../overlay.ts' ;
12+ import { mkdtempForTest } from '../../tmp-dir.fixtures.ts' ;
1013
1114const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
1215const recordingScriptsDir = path . resolve (
@@ -24,8 +27,9 @@ type TypecheckOutcome = { exitCode: number; stderr: string; source: string };
2427// gates in platform-apple keep their native compiles out of test-case wall time the same way.
2528// One beforeAll covers all three so the SDK probe and the two shared-source compiles are paid once
2629// per file rather than once per case.
27- describe . skipIf ( process . platform !== 'darwin' ) ( 'recording Swift scripts typecheck ' , ( ) => {
30+ describe . skipIf ( process . platform !== 'darwin' ) ( 'recording Swift scripts' , ( ) => {
2831 let outcomes : TypecheckOutcome [ ] = [ ] ;
32+ let exports : OverlayExportOutcome [ ] = [ ] ;
2933
3034 beforeAll (
3135 async ( ) => {
@@ -58,6 +62,7 @@ describe.skipIf(process.platform !== 'darwin')('recording Swift scripts typechec
5862 return { source, exitCode : result . exitCode , stderr : result . stderr } ;
5963 } ) ,
6064 ) ;
65+ exports = await exerciseOverlayExports ( swiftCompilerPath , swiftSdkPath , sharedSupport ) ;
6166 } ,
6267 SWIFT_TYPECHECK_TIMEOUT_MS * 3 + 30_000 ,
6368 ) ;
@@ -73,8 +78,143 @@ describe.skipIf(process.platform !== 'darwin')('recording Swift scripts typechec
7378 test ( 'recording frames Swift script typechecks' , ( ) => {
7479 assertTypechecked ( outcomes , 'recording-frames.swift' ) ;
7580 } ) ;
81+
82+ for ( const transform of [ 'identity' , 'rot90-negative-origin' , 'rot90-translated' ] ) {
83+ test ( `overlay keeps upright content and touches for ${ transform } ` , ( ) => {
84+ const outcome = exports . find ( ( candidate ) => candidate . transform === transform ) ;
85+ assert . ok ( outcome ) ;
86+ assert . equal ( outcome . exitCode , 0 , outcome . stderr ) ;
87+ assert . deepEqual ( outcome . outputSize , outcome . sourceSize ) ;
88+ assert . ok ( outcome . sourceLuma ! > 100 ) ;
89+ assert . ok ( outcome . outputLuma ! > 100 , JSON . stringify ( outcome ) ) ;
90+ assert . equal ( outcome . sourceBluePixels , 0 , 'the fixture must not contain overlay colors' ) ;
91+ assert . ok ( outcome . outputBluePixels ! > 180 , 'the touch overlay must actually draw' ) ;
92+ assert . ok ( outcome . markerDistance ! < 3 , 'the asymmetric source marker must stay upright' ) ;
93+ } ) ;
94+ }
7695} ) ;
7796
97+ type OverlayExportOutcome = {
98+ transform : string ;
99+ exitCode : number ;
100+ stderr : string ;
101+ sourceSize ?: [ number , number ] ;
102+ outputSize ?: [ number , number ] ;
103+ sourceLuma ?: number ;
104+ outputLuma ?: number ;
105+ sourceBluePixels ?: number ;
106+ outputBluePixels ?: number ;
107+ markerDistance ?: number ;
108+ } ;
109+
110+ async function exerciseOverlayExports ( compiler : string , sdk : string , sharedSupport : string ) {
111+ const dir = await mkdtempForTest ( 'recording-overlay-export-' ) ;
112+ const overlay = path . join ( dir , 'overlay' ) ;
113+ const fixture = path . join ( dir , 'fixture' ) ;
114+ const inspect = path . join ( dir , 'inspect' ) ;
115+ await Promise . all (
116+ [
117+ [ overlay , path . join ( recordingScriptsDir , 'recording-overlay.swift' ) , sharedSupport ] ,
118+ [ fixture , path . join ( recordingTestSupportDir , 'recording-overlay-fixture.swift' ) ] ,
119+ [ inspect , path . join ( recordingTestSupportDir , 'recording-inspect.swift' ) ] ,
120+ ] . map ( async ( [ output , ...sources ] ) => {
121+ await runCmd ( compiler , [ '-sdk' , sdk , ...sources , '-o' , output ! ] , {
122+ timeoutMs : SWIFT_TYPECHECK_TIMEOUT_MS ,
123+ } ) ;
124+ } ) ,
125+ ) ;
126+ const results : OverlayExportOutcome [ ] = [ ] ;
127+ for ( const transform of [ 'identity' , 'rot90-negative-origin' , 'rot90-translated' ] ) {
128+ const source = path . join ( dir , `${ transform } .mp4` ) ;
129+ const output = path . join ( dir , `${ transform } -overlay.mp4` ) ;
130+ const events = path . join ( dir , `${ transform } .json` ) ;
131+ const [ width , height ] = transform === 'identity' ? [ 480 , 640 ] : [ 640 , 480 ] ;
132+ fs . writeFileSync (
133+ events ,
134+ JSON . stringify ( {
135+ events : [
136+ {
137+ kind : 'tap' ,
138+ tMs : 150 ,
139+ x : width ! / 2 ,
140+ y : height ! / 2 ,
141+ referenceWidth : width ,
142+ referenceHeight : height ,
143+ } ,
144+ ] ,
145+ } ) ,
146+ ) ;
147+ await runCmd ( fixture , [ source , transform ] , { timeoutMs : 15_000 } ) ;
148+ const exported = await runCmd (
149+ overlay ,
150+ [ '--input' , source , '--output' , output , '--events' , events ] ,
151+ { allowFailure : true , timeoutMs : 15_000 } ,
152+ ) ;
153+ const outcome : OverlayExportOutcome = {
154+ transform,
155+ exitCode : exported . exitCode ,
156+ stderr : exported . stderr ,
157+ } ;
158+ if ( exported . exitCode === 0 ) {
159+ const inspected = [ ] ;
160+ for ( const [ label , video ] of [
161+ [ 'source' , source ] ,
162+ [ 'output' , output ] ,
163+ ] ) {
164+ const inspectDir = path . join ( dir , `${ transform } -${ label } ` ) ;
165+ await runCmd ( inspect , [ '--input' , video ! , '--events' , events , '--output-dir' , inspectDir ] , {
166+ timeoutMs : 15_000 ,
167+ } ) ;
168+ const manifest = JSON . parse (
169+ fs . readFileSync ( path . join ( inspectDir , 'manifest.json' ) , 'utf8' ) ,
170+ ) ;
171+ inspected . push ( {
172+ size : [ manifest . renderWidth , manifest . renderHeight ] as [ number , number ] ,
173+ pixels : inspectExportPixels ( manifest . items [ 0 ] . fullFramePath ) ,
174+ } ) ;
175+ }
176+ const [ raw , composited ] = inspected ;
177+ outcome . sourceSize = raw ! . size ;
178+ outcome . outputSize = composited ! . size ;
179+ outcome . sourceLuma = raw ! . pixels . luma ;
180+ outcome . outputLuma = composited ! . pixels . luma ;
181+ outcome . sourceBluePixels = raw ! . pixels . bluePixels ;
182+ outcome . outputBluePixels = composited ! . pixels . bluePixels ;
183+ outcome . markerDistance = Math . hypot (
184+ raw ! . pixels . markerX - composited ! . pixels . markerX ,
185+ raw ! . pixels . markerY - composited ! . pixels . markerY ,
186+ ) ;
187+ }
188+ results . push ( outcome ) ;
189+ }
190+ return results ;
191+ }
192+
193+ function inspectExportPixels ( framePath : string ) {
194+ const png = PNG . sync . read ( fs . readFileSync ( framePath ) ) ;
195+ let luma = 0 ;
196+ let bluePixels = 0 ;
197+ let markerPixels = 0 ;
198+ let markerX = 0 ;
199+ let markerY = 0 ;
200+ for ( let i = 0 ; i < png . width * png . height ; i ++ ) {
201+ const [ r , g , b ] = png . data . subarray ( i * 4 , i * 4 + 3 ) ;
202+ luma += 0.299 * r ! + 0.587 * g ! + 0.114 * b ! ;
203+ if ( b ! >= 180 && g ! >= 160 && r ! <= 235 && b ! >= g ! && b ! - r ! >= 25 ) bluePixels ++ ;
204+ if ( r ! > 170 && g ! < 100 && b ! < 100 ) {
205+ markerPixels ++ ;
206+ markerX += i % png . width ;
207+ markerY += Math . floor ( i / png . width ) ;
208+ }
209+ }
210+ return {
211+ luma : luma / ( png . width * png . height ) ,
212+ bluePixels,
213+ markerX : markerX / markerPixels ,
214+ markerY : markerY / markerPixels ,
215+ } ;
216+ }
217+
78218function assertTypechecked ( outcomes : readonly TypecheckOutcome [ ] , basename : string ) : void {
79219 const outcome = outcomes . find ( ( entry ) => path . basename ( entry . source ) === basename ) ;
80220 assert . ok ( outcome , `${ basename } was never typechecked` ) ;
0 commit comments