11import { readFileSync , statSync } from 'node:fs'
22import { createServer } from 'node:http'
3- import { join } from 'node:path'
3+ import path from 'node:path'
44
55import puppeteer from 'puppeteer'
66import { afterAll , beforeAll , describe , expect , it } from 'vitest'
77
88import type { IncomingMessage , ServerResponse } from 'node:http'
9- import type { Browser , Page } from 'puppeteer'
9+ import type { Browser , ConsoleMessage , Page } from 'puppeteer'
1010
1111function createStaticServer (
1212 port : number ,
@@ -32,10 +32,10 @@ function createStaticServer(
3232 return
3333 }
3434
35- const filePath = join (
35+ const filePath = path . join (
3636 process . cwd ( ) ,
3737 'test/testData/test1' ,
38- req . url === '/' ? 'myTrix.ix' : req . url ! ,
38+ req . url === '/' ? 'myTrix.ix' : ( req . url ?? '/' ) ,
3939 )
4040
4141 try {
@@ -78,13 +78,12 @@ function createStaticServer(
7878 return createServer ( handler )
7979}
8080
81- function createAppServer ( port : number ) : ReturnType < typeof createServer > {
82- const handler = ( req : IncomingMessage , res : ServerResponse ) => {
83- res . setHeader ( 'Access-Control-Allow-Origin' , '*' )
81+ function appHandler ( req : IncomingMessage , res : ServerResponse ) {
82+ res . setHeader ( 'Access-Control-Allow-Origin' , '*' )
8483
85- if ( req . url === '/' ) {
86- res . writeHead ( 200 , { 'Content-Type' : 'text/html' } )
87- res . end ( `<!DOCTYPE html>
84+ if ( req . url === '/' ) {
85+ res . writeHead ( 200 , { 'Content-Type' : 'text/html' } )
86+ res . end ( `<!DOCTYPE html>
8887<html>
8988<head>
9089 <script type="importmap">
@@ -105,32 +104,33 @@ function createAppServer(port: number): ReturnType<typeof createServer> {
105104 </script>
106105</body>
107106</html>` )
108- return
109- }
107+ return
108+ }
110109
111- const filePath = join ( process . cwd ( ) , req . url ! )
112- try {
113- const stat = statSync ( filePath )
114- if ( stat . isFile ( ) ) {
115- const content = readFileSync ( filePath )
116- const ext = filePath . split ( '.' ) . pop ( )
117- const contentType =
118- ext === 'js' ? 'application/javascript' : 'application/octet-stream'
119- res . writeHead ( 200 , {
120- 'Content-Type' : contentType ,
121- 'Content-Length' : content . length ,
122- } )
123- res . end ( content )
124- return
125- }
126- } catch {
127- // fall through to 404
110+ const filePath = path . join ( process . cwd ( ) , req . url ?? '/' )
111+ try {
112+ const stat = statSync ( filePath )
113+ if ( stat . isFile ( ) ) {
114+ const content = readFileSync ( filePath )
115+ const ext = filePath . split ( '.' ) . pop ( )
116+ const contentType =
117+ ext === 'js' ? 'application/javascript' : 'application/octet-stream'
118+ res . writeHead ( 200 , {
119+ 'Content-Type' : contentType ,
120+ 'Content-Length' : content . length ,
121+ } )
122+ res . end ( content )
123+ return
128124 }
129- res . writeHead ( 404 )
130- res . end ( 'Not found' )
125+ } catch {
126+ // fall through to 404
131127 }
128+ res . writeHead ( 404 )
129+ res . end ( 'Not found' )
130+ }
132131
133- return createServer ( handler )
132+ function createAppServer ( ) {
133+ return createServer ( appHandler )
134134}
135135
136136describe ( 'Browser tests with Puppeteer' , ( ) => {
@@ -146,7 +146,7 @@ describe('Browser tests with Puppeteer', () => {
146146 beforeAll ( async ( ) => {
147147 corsServer = createStaticServer ( corsPort , true )
148148 noCorsServer = createStaticServer ( noCorsPort , false )
149- appServer = createAppServer ( appPort )
149+ appServer = createAppServer ( )
150150
151151 await Promise . all ( [
152152 new Promise < void > ( resolve => corsServer . listen ( corsPort , resolve ) ) ,
@@ -161,7 +161,7 @@ describe('Browser tests with Puppeteer', () => {
161161 page = await browser . newPage ( )
162162
163163 const errors : string [ ] = [ ]
164- page . on ( 'pageerror' , ( err : any ) => errors . push ( err . message ) )
164+ page . on ( 'pageerror' , ( err : Error ) => errors . push ( err . message ) )
165165 page . on ( 'console' , msg => {
166166 if ( msg . type ( ) === 'error' ) {
167167 errors . push ( msg . text ( ) )
@@ -181,21 +181,22 @@ describe('Browser tests with Puppeteer', () => {
181181 } , 30000 )
182182
183183 afterAll ( async ( ) => {
184- await browser ? .close ( )
184+ await browser . close ( )
185185 await Promise . all ( [
186186 new Promise < void > ( resolve => corsServer . close ( ( ) => { resolve ( ) } ) ) ,
187187 new Promise < void > ( resolve => noCorsServer . close ( ( ) => { resolve ( ) } ) ) ,
188188 new Promise < void > ( resolve => appServer . close ( ( ) => { resolve ( ) } ) ) ,
189189 ] )
190190 } )
191191
192+ /* eslint-disable @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any */
192193 it ( 'searches via HTTP with CORS enabled server' , async ( ) => {
193194 const results = await page . evaluate ( async ( port : number ) => {
194195 const trix = new ( globalThis as any ) . Trix (
195196 new ( globalThis as any ) . RemoteFile ( `http://localhost:${ port } /myTrix.ixx` ) ,
196197 new ( globalThis as any ) . RemoteFile ( `http://localhost:${ port } /myTrix.ix` ) ,
197198 )
198- return trix . search ( 'for' )
199+ return await trix . search ( 'for' )
199200 } , corsPort )
200201
201202 expect ( results . length ) . toBeGreaterThan ( 0 )
@@ -204,7 +205,7 @@ describe('Browser tests with Puppeteer', () => {
204205
205206 it ( 'fails to search via HTTP without CORS (browser enforces CORS)' , async ( ) => {
206207 const consoleMessages : string [ ] = [ ]
207- const consoleHandler = ( msg : any ) => {
208+ const consoleHandler = ( msg : ConsoleMessage ) => {
208209 consoleMessages . push ( msg . text ( ) )
209210 }
210211 page . on ( 'console' , consoleHandler )
@@ -216,12 +217,13 @@ describe('Browser tests with Puppeteer', () => {
216217 )
217218 try {
218219 await trix . search ( 'for' )
219- return { success : true , error : null , errorName : null }
220- } catch ( error : any ) {
220+ return { success : true , error : undefined , errorName : undefined }
221+ } catch ( error : unknown ) {
222+ const name = error instanceof Error ? error . name : undefined
221223 return {
222224 success : false ,
223225 error : String ( error ) ,
224- errorName : error ?. name || null ,
226+ errorName : name ,
225227 }
226228 }
227229 } , noCorsPort )
@@ -246,7 +248,7 @@ describe('Browser tests with Puppeteer', () => {
246248 new ( globalThis as any ) . RemoteFile ( `http://localhost:${ port } /myTrix.ixx` ) ,
247249 new ( globalThis as any ) . RemoteFile ( `http://localhost:${ port } /myTrix.ix` ) ,
248250 )
249- return trix . search ( 'this' )
251+ return await trix . search ( 'this' )
250252 } , corsPort )
251253
252254 expect ( results ) . toMatchSnapshot ( )
@@ -258,9 +260,10 @@ describe('Browser tests with Puppeteer', () => {
258260 new ( globalThis as any ) . RemoteFile ( `http://localhost:${ port } /myTrix.ixx` ) ,
259261 new ( globalThis as any ) . RemoteFile ( `http://localhost:${ port } /myTrix.ix` ) ,
260262 )
261- return trix . search ( 'zzz' )
263+ return await trix . search ( 'zzz' )
262264 } , corsPort )
263265
264266 expect ( results ) . toEqual ( [ ] )
265267 } )
268+ /* eslint-enable @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any */
266269} )
0 commit comments