mirror of
https://github.com/hanzoai/extension.git
synced 2026-10-31 21:57:06 +00:00
`pnpm --filter @hanzo/aci build` failed with TS2339: Property 'words'
does not exist on type 'Page'. 09997dba moved aci from tesseract.js 5 to
7. Version 5 returned a flat word list and the block tree by default;
version 7 has no flat list, and returns the block tree only when the
output options ask for it. So findText read `result.data.words`, got
undefined and threw "words is not iterable", and waitForText and
clickText, which call it, failed with it.
findText now asks recognize for blocks and walks blocks, paragraphs and
lines to the words. The test mock answered in the version 5 shape, which
is why nothing caught this; it now answers as version 7 does, with
blocks null unless asked for, and a new test finds the centre of a word.
On the old findText that test fails with the same TypeError.
Co-authored-by: Hanzo Dev <[email protected]>
118 lines
2.9 KiB
TypeScript
118 lines
2.9 KiB
TypeScript
// Mock native dependencies that aren't available in test environment
|
|
import { vi } from 'vitest';
|
|
|
|
// Mock jsautogui
|
|
vi.mock('jsautogui', () => ({
|
|
default: {
|
|
moveTo: vi.fn(),
|
|
click: vi.fn(),
|
|
mouseDown: vi.fn(),
|
|
mouseUp: vi.fn(),
|
|
dragTo: vi.fn(),
|
|
scroll: vi.fn(),
|
|
write: vi.fn(),
|
|
keyTap: vi.fn(),
|
|
keyDown: vi.fn(),
|
|
keyUp: vi.fn(),
|
|
hotkey: vi.fn(),
|
|
screenshot: vi.fn(() => ({
|
|
width: 1920,
|
|
height: 1080,
|
|
data: Buffer.alloc(1920 * 1080 * 4)
|
|
})),
|
|
getScreenSize: vi.fn(() => ({ width: 1920, height: 1080 })),
|
|
locateOnScreen: vi.fn(),
|
|
locateAllOnScreen: vi.fn(() => []),
|
|
pixelMatchesColor: vi.fn(() => true),
|
|
getPixelColor: vi.fn(() => [255, 255, 255])
|
|
}
|
|
}));
|
|
|
|
// Mock @nut-tree-fork/nut-js
|
|
vi.mock('@nut-tree-fork/nut-js', () => ({
|
|
mouse: {
|
|
move: vi.fn(),
|
|
setPosition: vi.fn(),
|
|
leftClick: vi.fn(),
|
|
rightClick: vi.fn(),
|
|
click: vi.fn(),
|
|
doubleClick: vi.fn(),
|
|
drag: vi.fn(),
|
|
scrollUp: vi.fn(),
|
|
scrollDown: vi.fn()
|
|
},
|
|
keyboard: {
|
|
type: vi.fn(),
|
|
pressKey: vi.fn(),
|
|
releaseKey: vi.fn()
|
|
},
|
|
screen: {
|
|
width: vi.fn(() => 1920),
|
|
height: vi.fn(() => 1080),
|
|
grab: vi.fn(() => ({
|
|
toBuffer: vi.fn(() => Buffer.alloc(100))
|
|
})),
|
|
grabRegion: vi.fn(() => ({
|
|
toBuffer: vi.fn(() => Buffer.alloc(100))
|
|
})),
|
|
find: vi.fn(),
|
|
findAll: vi.fn(() => [])
|
|
},
|
|
imageResource: vi.fn((path) => ({ path }))
|
|
}));
|
|
|
|
// Mock robotjs
|
|
vi.mock('robotjs', () => ({
|
|
moveMouse: vi.fn(),
|
|
mouseClick: vi.fn(),
|
|
mouseToggle: vi.fn(),
|
|
dragMouse: vi.fn(),
|
|
scrollMouse: vi.fn(),
|
|
keyTap: vi.fn(),
|
|
keyToggle: vi.fn(),
|
|
typeString: vi.fn(),
|
|
getScreenSize: vi.fn(() => ({ width: 1920, height: 1080 })),
|
|
screen: {
|
|
capture: vi.fn(() => ({
|
|
width: 1920,
|
|
height: 1080,
|
|
image: Buffer.alloc(1920 * 1080 * 4),
|
|
byteWidth: 1920 * 4,
|
|
bitsPerPixel: 32,
|
|
bytesPerPixel: 4
|
|
}))
|
|
}
|
|
}));
|
|
|
|
// Mock sharp
|
|
vi.mock('sharp', () => ({
|
|
default: vi.fn(() => ({
|
|
png: vi.fn().mockReturnThis(),
|
|
jpeg: vi.fn().mockReturnThis(),
|
|
toBuffer: vi.fn(() => Buffer.alloc(100))
|
|
}))
|
|
}));
|
|
|
|
// Mock tesseract.js, in the shape version 7 answers: words sit in
|
|
// blocks -> paragraphs -> lines, and `blocks` is null unless the caller asks
|
|
// for it in the output options.
|
|
vi.mock('tesseract.js', () => {
|
|
const words = [
|
|
{ text: 'Sample', bbox: { x0: 10, y0: 10, x1: 50, y1: 30 } },
|
|
{ text: 'OCR', bbox: { x0: 60, y0: 10, x1: 90, y1: 30 } },
|
|
{ text: 'text', bbox: { x0: 100, y0: 10, x1: 130, y1: 30 } }
|
|
];
|
|
return {
|
|
default: {
|
|
createWorker: vi.fn(async () => ({
|
|
recognize: vi.fn(async (_image: unknown, _options?: unknown, output?: { blocks?: boolean }) => ({
|
|
data: {
|
|
text: 'Sample OCR text',
|
|
blocks: output?.blocks ? [{ paragraphs: [{ lines: [{ words }] }] }] : null
|
|
}
|
|
})),
|
|
terminate: vi.fn()
|
|
}))
|
|
}
|
|
};
|
|
}); |