diff --git a/packages/win32-api/test/70.user32.test.ts b/packages/win32-api/test/70.user32.test.ts index 7a609a87..09b569f7 100644 --- a/packages/win32-api/test/70.user32.test.ts +++ b/packages/win32-api/test/70.user32.test.ts @@ -20,7 +20,7 @@ import { } from '../src/index.js' import { calcLpszClassNotepad, calcLpszNotepad } from './config.unittest.js' -import { user32, user32Sync } from './helper.js' +import { assertsHwnd, user32, user32Sync } from './helper.js' const tmpMap = new Map() @@ -39,7 +39,7 @@ describe(fileShortPath(import.meta.url), () => { const hWnd = await user32FindWindowEx(0, 0, calcLpszNotepad, null) assert(hWnd) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window') + assertsHwnd(hWnd) // Change title of the Calculator await user32.SetWindowTextW(hWnd, ucsBufferFrom(title)) @@ -54,19 +54,19 @@ describe(fileShortPath(import.meta.url), () => { point.x = 101 point.y = Math.round(Math.random() * 1000000) - const adress = point.ref().address() - tmpMap.delete(adress) + const address = point.ref().address() + tmpMap.delete(address) - await enumWindows(enumWindowsProc, adress) - console.log({ adress }) + await enumWindows(enumWindowsProc, address) + console.log({ address }) - const point2 = tmpMap.get(adress) + const point2 = tmpMap.get(address) assert(point2, 'point2 should be got') if (point2) { assert(point.x === point2.x) assert(point.y === point2.y) } - tmpMap.delete(adress) + tmpMap.delete(address) child.kill() console.log({ killed: child.killed }) @@ -82,7 +82,7 @@ describe(fileShortPath(import.meta.url), () => { console.log(new Date().toLocaleTimeString()) const hWnd = user32Sync.FindWindowExW(0, 0, calcLpszClassNotepad, null) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window') + assertsHwnd(hWnd) // Change title of the Calculator user32Sync.SetWindowTextW(hWnd, ucsBufferFrom(title)) @@ -100,19 +100,19 @@ describe(fileShortPath(import.meta.url), () => { point.x = 101 point.y = Math.round(Math.random() * 1000000) - const adress = point.ref().address() - tmpMap.delete(adress) + const address = point.ref().address() + tmpMap.delete(address) - await enumWindows(enumWindowsProc, adress) - console.log({ adress }) + await enumWindows(enumWindowsProc, address) + console.log({ address }) - const point2 = tmpMap.get(adress) + const point2 = tmpMap.get(address) assert(point2, 'point2 should be got') if (point2) { assert(point.x === point2.x) assert(point.y === point2.y) } - tmpMap.delete(adress) + tmpMap.delete(address) child.kill() console.log({ killed: child.killed }) diff --git a/packages/win32-api/test/71.find-calc-async.test.ts b/packages/win32-api/test/71.find-calc-async.test.ts index e99d1f41..b4ebeacc 100644 --- a/packages/win32-api/test/71.find-calc-async.test.ts +++ b/packages/win32-api/test/71.find-calc-async.test.ts @@ -9,7 +9,7 @@ import { ucsBufferFrom, ucsBufferToString } from '../src/index.js' import * as UP from '../src/index.user32.js' import { calcLpszWindow } from './config.unittest.js' -import { user32, user32Sync } from './helper.js' +import { assertsHwnd, user32, user32Sync } from './helper.js' describe.skip(fileShortPath(import.meta.url), () => { @@ -23,7 +23,7 @@ describe.skip(fileShortPath(import.meta.url), () => { console.log(new Date().toLocaleTimeString()) const hWnd = await user32FindWindowEx(0, 0, null, calcLpszWindow) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window') + assertsHwnd(hWnd) child.kill() }) @@ -41,7 +41,7 @@ describe.skip(fileShortPath(import.meta.url), () => { assert(false, err.message) } - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window') + assertsHwnd(hWnd) child.kill() done() }) @@ -70,7 +70,7 @@ async function findNSetWinTitleAsync(): Promise { const len = title.length const hWnd = await user32.FindWindowExW(0, 0, null, ucsBufferFrom(calcLpszWindow)) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window') + assertsHwnd(hWnd) const ret = await user32.SetWindowTextW(hWnd, ucsBufferFrom(title)) assert(ret, 'SetWindowTextW() failed') @@ -88,7 +88,7 @@ async function findNSetWinTitleAsyncPartial(): Promise { const len = title.length const hWnd = await u32.FindWindowExW(0, 0, null, ucsBufferFrom(calcLpszWindow)) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window') + assertsHwnd(hWnd) // Change title of the Calculator await u32.SetWindowTextW(hWnd, ucsBufferFrom(title)) diff --git a/packages/win32-api/test/72.find-notepad-async.test.ts b/packages/win32-api/test/72.find-notepad-async.test.ts index 5cfa9902..3c092de9 100644 --- a/packages/win32-api/test/72.find-notepad-async.test.ts +++ b/packages/win32-api/test/72.find-notepad-async.test.ts @@ -9,7 +9,7 @@ import { ucsBufferToString, ucsBufferFrom } from '../src/index.js' import * as UP from '../src/index.user32.js' import { calcLpszClassNotepad, calcLpszNotepad } from './config.unittest.js' -import { destroyWin, user32, user32Sync } from './helper.js' +import { assertsHwnd, destroyWin, user32, user32Sync } from './helper.js' @@ -24,7 +24,7 @@ describe(fileShortPath(import.meta.url), () => { console.log(new Date().toLocaleTimeString()) const hWnd = await user32FindWindowEx(0, 0, calcLpszNotepad, null) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window') + assertsHwnd(hWnd) await destroyWin(hWnd) child.kill() }) @@ -42,7 +42,7 @@ describe(fileShortPath(import.meta.url), () => { assert(false, err.message) } - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no notepad window') + assertsHwnd(hWnd) child.kill() done() }) @@ -74,13 +74,13 @@ async function findNSetWinTitleAsync(): Promise { const size = len + 1 const hWnd = await user32.FindWindowExW(0, 0, calcLpszClassNotepad, null) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window') + assertsHwnd(hWnd) const ret = await user32.SetWindowTextW(hWnd, ucsBufferFrom(title)) assert(ret, 'SetWindowTextW() failed') const buf = Buffer.alloc(size * 2) await user32.GetWindowTextW(hWnd, buf, size) - const str = buf.toString('ucs2').replace(/\0+$/, '') + const str = buf.toString('ucs2').replace(/\0+$/u, '') assert(str === title.trim(), `title should be changed to "${title}", bug got "${str}"`) } @@ -93,7 +93,7 @@ async function findNSetWinTitleAsyncPartial(): Promise { const size = len + 1 const hWnd = await u32.FindWindowExW(0, 0, calcLpszClassNotepad, null) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window') + assertsHwnd(hWnd) // Change title of the Calculator await u32.SetWindowTextW(hWnd, ucsBufferFrom(title)) diff --git a/packages/win32-api/test/gdi32/202.CreateCompatibleBitmap.test.ts b/packages/win32-api/test/gdi32/202.CreateCompatibleBitmap.test.ts index 252c3aed..e928c25e 100644 --- a/packages/win32-api/test/gdi32/202.CreateCompatibleBitmap.test.ts +++ b/packages/win32-api/test/gdi32/202.CreateCompatibleBitmap.test.ts @@ -1,17 +1,8 @@ import assert from 'node:assert/strict' import { fileShortPath } from '@waiting/shared-core' -import ref from 'ref-napi' -import { sleep } from 'zx' -import * as CS from '../../src/index.consts.js' -import { - DModel as M, - DTypes as W, - DStruct as DS, - StructFactory, -} from '../../src/index.js' -import { gdi32 } from '../helper.js' +import { assertsHwnd, gdi32 } from '../helper.js' describe(fileShortPath(import.meta.url), () => { @@ -23,7 +14,7 @@ describe(fileShortPath(import.meta.url), () => { const hBitmap = await gdi32.CreateCompatibleBitmap(hdc, 100, 100) console.log({ hBitmap }) - assert(hBitmap > 0) + assertsHwnd(hBitmap) }) }) }) diff --git a/packages/win32-api/test/helper.ts b/packages/win32-api/test/helper.ts index 5373db3d..2bff6531 100644 --- a/packages/win32-api/test/helper.ts +++ b/packages/win32-api/test/helper.ts @@ -113,7 +113,8 @@ export async function createWindow(wndProc: M.WNDPROC): Promise { ref.NULL, ) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'CreateWindowExW() failed') + assertsHwnd(hWnd) + // @FIXME timeout // await user32.ShowWindow(hWnd, 1) await user32.UpdateWindow(hWnd) @@ -122,22 +123,25 @@ export async function createWindow(wndProc: M.WNDPROC): Promise { } export async function changeTitle(hWnd: M.HANDLE, title: string): Promise { - if (typeof hWnd === 'number' && hWnd > 0 - || Buffer.isBuffer(hWnd) && ! ref.isNull(hWnd) && ref.address(hWnd) - ) { + if (typeof hWnd === 'number' && hWnd > 0) { // Change title of the Calculator const res = await user32.SetWindowTextW(hWnd, Buffer.from(title + '\0', 'ucs2')) - if (! res) { - throw new Error('changeTitle failed') - } - else { + if (res) { const tt = await getTitle(hWnd) return tt } + throw new Error('changeTitle failed') } - else { - return '' + else if (Buffer.isBuffer(hWnd) && ! ref.isNull(hWnd) && ref.address(hWnd)) { + // Change title of the Calculator + const res = await user32.SetWindowTextW(hWnd, Buffer.from(title + '\0', 'ucs2')) + if (res) { + const tt = await getTitle(hWnd) + return tt + } + throw new Error('changeTitle failed') } + return '' } @@ -158,3 +162,16 @@ export async function getTitle(handle: M.HANDLE): Promise { export function destroyWin(hWnd: M.HWND): Promise { return user32.DestroyWindow(hWnd) } + + +export function assertsHwnd(hWnd: M.HWND | string | number | bigint): void { + if (typeof hWnd === 'string') { + assert(hWnd.length > 0, 'found no window') + } + else if (typeof hWnd === 'number' || typeof hWnd === 'bigint') { + assert(hWnd > 0, 'found no window') + } + else { + assert(false, 'found no window') + } +} diff --git a/packages/win32-api/test/user32/201.FindWindowEx.test.ts b/packages/win32-api/test/user32/201.FindWindowEx.test.ts index 6c32dbb0..4093d8bd 100644 --- a/packages/win32-api/test/user32/201.FindWindowEx.test.ts +++ b/packages/win32-api/test/user32/201.FindWindowEx.test.ts @@ -2,18 +2,11 @@ import assert from 'node:assert/strict' import { spawn } from 'node:child_process' import { fileShortPath } from '@waiting/shared-core' -import ref from 'ref-napi' import { sleep } from 'zx' -import * as CS from '../../src/index.consts.js' import { user32FindWindowEx } from '../../src/index.fun.js' -import { - DModel as M, - DTypes as W, - DStruct as DS, - StructFactory, -} from '../../src/index.js' import { calcLpszNotepad } from '../config.unittest.js' +import { assertsHwnd } from '../helper.js' describe(fileShortPath(import.meta.url), () => { @@ -25,7 +18,7 @@ describe(fileShortPath(import.meta.url), () => { const hWnd = await user32FindWindowEx(0, 0, calcLpszNotepad, null) assert(hWnd) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'found no calc window') + assertsHwnd(hWnd) child.kill() }) diff --git a/packages/win32-api/test/user32/54.GetWindowRect.test.ts b/packages/win32-api/test/user32/54.GetWindowRect.test.ts index 6fc2013e..20ee8aaa 100644 --- a/packages/win32-api/test/user32/54.GetWindowRect.test.ts +++ b/packages/win32-api/test/user32/54.GetWindowRect.test.ts @@ -12,7 +12,7 @@ import { StructFactory, } from '../../src/index.js' import { calcLpszWindow } from '../config.unittest.js' -import { user32, destroyWin } from '../helper.js' +import { user32, destroyWin, assertsHwnd } from '../helper.js' describe(fileShortPath(import.meta.url), () => { @@ -23,7 +23,7 @@ describe(fileShortPath(import.meta.url), () => { const rect = StructFactory(DS.RECT) const hWnd = await user32FindWindowEx(0, 0, null, calcLpszWindow) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'invalid hWnd') + assertsHwnd(hWnd) const ret = await user32.GetWindowRect(hWnd, rect.ref()) assert(ret !== 0) diff --git a/packages/win32-api/test/user32/55.SendMessageW.test.ts b/packages/win32-api/test/user32/55.SendMessageW.test.ts index 8625c5f4..3dde56bd 100644 --- a/packages/win32-api/test/user32/55.SendMessageW.test.ts +++ b/packages/win32-api/test/user32/55.SendMessageW.test.ts @@ -14,7 +14,7 @@ import { StructFactory, } from '../../src/index.js' import { calcLpszWindow } from '../config.unittest.js' -import { user32, destroyWin } from '../helper.js' +import { user32, destroyWin, assertsHwnd } from '../helper.js' describe(fileShortPath(import.meta.url), () => { @@ -30,7 +30,7 @@ describe(fileShortPath(import.meta.url), () => { }) const hWnd = await user32FindWindowEx(0, 0, null, calcLpszWindow) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0) + assertsHwnd(hWnd) let ret = await user32.SendMessageW(hWnd, CS.WM_COMMAND, 1000, 3) assert(ret === 0) @@ -44,7 +44,7 @@ describe(fileShortPath(import.meta.url), () => { await sleep(1500) const hWnd = await user32FindWindowEx(0, 0, null, calcLpszWindow) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0) + assertsHwnd(hWnd) const copyData = StructFactory(DS.COPYDATASTRUCT) const msgBuff = Buffer.from('foo\0', 'ucs2') diff --git a/packages/win32-api/test/user32/56.SendMessageW.SETTEXT.test.ts b/packages/win32-api/test/user32/56.SendMessageW.SETTEXT.test.ts index d574024e..f65a0ede 100644 --- a/packages/win32-api/test/user32/56.SendMessageW.SETTEXT.test.ts +++ b/packages/win32-api/test/user32/56.SendMessageW.SETTEXT.test.ts @@ -12,7 +12,7 @@ import { calcLpszNotepad, calcLpszNotepadEdit, } from '../config.unittest.js' -import { user32, destroyWin } from '../helper.js' +import { user32, destroyWin, assertsHwnd } from '../helper.js' describe(fileShortPath(import.meta.url), () => { @@ -22,10 +22,10 @@ describe(fileShortPath(import.meta.url), () => { await sleep(1500) const hWnd = await user32FindWindowEx(0, 0, calcLpszNotepad, null) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0) + assertsHwnd(hWnd) const hWndEdit = await user32FindWindowEx(hWnd, 0, calcLpszNotepadEdit, null) - assert((typeof hWndEdit === 'string' && hWndEdit.length > 0) || hWndEdit > 0) + assertsHwnd(hWndEdit) const txt = 'Hello World.' + Math.random().toString() const msgBuff = ucsBufferFrom(txt) diff --git a/packages/win32-api/test/user32/57.SendMessageW.GETTEXT.test.ts b/packages/win32-api/test/user32/57.SendMessageW.GETTEXT.test.ts index 807cf328..c294f1c9 100644 --- a/packages/win32-api/test/user32/57.SendMessageW.GETTEXT.test.ts +++ b/packages/win32-api/test/user32/57.SendMessageW.GETTEXT.test.ts @@ -12,7 +12,7 @@ import { calcLpszNotepad, calcLpszNotepadEdit, } from '../config.unittest.js' -import { user32, destroyWin } from '../helper.js' +import { user32, destroyWin, assertsHwnd } from '../helper.js' describe(fileShortPath(import.meta.url), () => { @@ -22,10 +22,10 @@ describe(fileShortPath(import.meta.url), () => { await sleep(1500) const hWnd = await user32FindWindowEx(0, 0, calcLpszNotepad, null) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0) + assertsHwnd(hWnd) const hWndEdit = await user32FindWindowEx(hWnd, 0, calcLpszNotepadEdit, null) - assert((typeof hWndEdit === 'string' && hWndEdit.length > 0) || hWndEdit > 0) + assertsHwnd(hWndEdit) const txt = 'Hello World.' + Math.random().toString() const msgBuff = ucsBufferFrom(txt) diff --git a/packages/win32-api/test/user32/60.FlashWindowEx.test.ts b/packages/win32-api/test/user32/60.FlashWindowEx.test.ts index 8226f8da..4da3bed9 100644 --- a/packages/win32-api/test/user32/60.FlashWindowEx.test.ts +++ b/packages/win32-api/test/user32/60.FlashWindowEx.test.ts @@ -15,7 +15,7 @@ import { StructFactory, } from '../../src/index.js' import { calcLpszNotepad } from '../config.unittest.js' -import { user32, destroyWin } from '../helper.js' +import { user32, destroyWin, assertsHwnd } from '../helper.js' describe(fileShortPath(import.meta.url), () => { @@ -26,7 +26,7 @@ describe(fileShortPath(import.meta.url), () => { await sleep(1000) const hWnd = await user32FindWindowEx(0, 0, calcLpszNotepad, null) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0) + assertsHwnd(hWnd) const pfwi = StructFactory(DS.FLASHWINFO) pfwi.cbSize = pfwi.ref().byteLength @@ -47,7 +47,7 @@ describe(fileShortPath(import.meta.url), () => { await sleep(1000) const hWnd = await user32FindWindowEx(0, 0, calcLpszNotepad, null) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0) + assertsHwnd(hWnd) const pfwi = StructFactory(DS.FLASHWINFO) pfwi.cbSize = pfwi.ref().byteLength diff --git a/packages/win32-api/test/user32/61.FlashWindow.test.ts b/packages/win32-api/test/user32/61.FlashWindow.test.ts index 44b6105b..14c3de6d 100644 --- a/packages/win32-api/test/user32/61.FlashWindow.test.ts +++ b/packages/win32-api/test/user32/61.FlashWindow.test.ts @@ -8,7 +8,7 @@ import { sleep } from 'zx' // import * as CS from '../../src/index.consts.js' import { user32FindWindowEx } from '../../src/index.fun.js' import { calcLpszNotepad } from '../config.unittest.js' -import { user32, destroyWin } from '../helper.js' +import { user32, destroyWin, assertsHwnd } from '../helper.js' describe(fileShortPath(import.meta.url), () => { @@ -19,7 +19,7 @@ describe(fileShortPath(import.meta.url), () => { await sleep(1000) const hWnd = await user32FindWindowEx(0, 0, calcLpszNotepad, null) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0) + assertsHwnd(hWnd) const state = await user32.FlashWindow(hWnd, 1) assert(state > 0) @@ -37,7 +37,7 @@ describe(fileShortPath(import.meta.url), () => { await sleep(1000) const hWnd = await user32FindWindowEx(0, 0, calcLpszNotepad, null) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0) + assertsHwnd(hWnd) const state = await user32.FlashWindow(hWnd, 0) assert(state > 0) diff --git a/packages/win32-api/test/user32/63.IsIconic.test.ts b/packages/win32-api/test/user32/63.IsIconic.test.ts index 299b291b..2cf5bb98 100644 --- a/packages/win32-api/test/user32/63.IsIconic.test.ts +++ b/packages/win32-api/test/user32/63.IsIconic.test.ts @@ -6,7 +6,7 @@ import { sleep } from 'zx' import { user32FindWindowEx } from '../../src/index.fun.js' import { calcLpszWindow } from '../config.unittest.js' -import { user32, destroyWin } from '../helper.js' +import { user32, destroyWin, assertsHwnd } from '../helper.js' describe(fileShortPath(import.meta.url), () => { @@ -15,7 +15,7 @@ describe(fileShortPath(import.meta.url), () => { await sleep(1500) const hWnd = await user32FindWindowEx(0, 0, null, calcLpszWindow) - assert((typeof hWnd === 'string' && hWnd.length > 0) || hWnd > 0, 'invalid hWnd') + assertsHwnd(hWnd) assert(! await user32.IsIconic(hWnd)) await user32.ShowWindow(hWnd, 2) // minimize diff --git a/packages/win32-api/test/winspool/503.GetPrinter.test.ts b/packages/win32-api/test/winspool/503.GetPrinter.test.ts index 9233bef6..067d149b 100644 --- a/packages/win32-api/test/winspool/503.GetPrinter.test.ts +++ b/packages/win32-api/test/winspool/503.GetPrinter.test.ts @@ -46,7 +46,7 @@ describe(fileShortPath(import.meta.url), () => { } }) - it.only('Level 4', async () => { + it('Level 4', async () => { const pname = await winspoolGetDefaultPrinter() assert(pname) const hWnd = await winspoolOpenPrinter(pname)