added test cases

This commit is contained in:
Boki 2026-02-12 13:08:19 -05:00
parent c1892230b7
commit 93e2234c4e
9 changed files with 320 additions and 141 deletions

View file

@ -5,7 +5,8 @@ import { sleep } from '../../util/sleep.js';
import { GRID_LAYOUTS } from '../../game/GridReader.js';
import type { Bot } from '../../bot/Bot.js';
import type { Server } from '../Server.js';
import type { OcrEngine, OcrPreprocess } from '../../game/OcrDaemon.js';
import type { OcrEngine, OcrPreprocess, DiffOcrParams } from '../../game/OcrDaemon.js';
import type { OcrSettings } from '../../game/ScreenReader.js';
export function debugRoutes(bot: Bot, server: Server): Router {
const router = Router();
@ -15,39 +16,23 @@ export function debugRoutes(bot: Bot, server: Server): Router {
return false;
};
// --- Sync: OCR engine/preprocess selection ---
// --- Sync: OCR settings ---
router.get('/ocr-engine', (req, res) => {
router.get('/ocr-settings', (req, res) => {
if (notReady(req, res)) return;
res.json({ ok: true, engine: bot.screenReader.debugOcrEngine });
res.json({ ok: true, ...bot.screenReader.settings });
});
router.post('/ocr-engine', (req, res) => {
router.post('/ocr-settings', (req, res) => {
if (notReady(req, res)) return;
const { engine } = req.body as { engine: string };
if (!['tesseract', 'easyocr', 'paddleocr'].includes(engine)) {
res.status(400).json({ error: 'Invalid engine. Must be tesseract, easyocr, or paddleocr.' });
return;
}
bot.screenReader.debugOcrEngine = engine as OcrEngine;
server.broadcastLog('info', `OCR engine set to: ${engine}`);
res.json({ ok: true });
});
router.get('/ocr-preprocess', (req, res) => {
if (notReady(req, res)) return;
res.json({ ok: true, preprocess: bot.screenReader.debugPreprocess });
});
router.post('/ocr-preprocess', (req, res) => {
if (notReady(req, res)) return;
const { preprocess } = req.body as { preprocess: string };
if (!['none', 'bgsub', 'tophat'].includes(preprocess)) {
res.status(400).json({ error: 'Invalid preprocess. Must be none, bgsub, or tophat.' });
return;
}
bot.screenReader.debugPreprocess = preprocess as OcrPreprocess;
server.broadcastLog('info', `OCR preprocess set to: ${preprocess}`);
const body = req.body as Partial<OcrSettings>;
const s = bot.screenReader.settings;
if (body.engine && ['tesseract', 'easyocr', 'paddleocr'].includes(body.engine)) s.engine = body.engine;
if (body.screenPreprocess && ['none', 'bgsub', 'tophat'].includes(body.screenPreprocess)) s.screenPreprocess = body.screenPreprocess;
if (body.tooltipPreprocess && ['none', 'bgsub', 'tophat'].includes(body.tooltipPreprocess)) s.tooltipPreprocess = body.tooltipPreprocess;
if (body.tooltipParams != null) s.tooltipParams = body.tooltipParams;
if (body.saveDebugImages != null) s.saveDebugImages = body.saveDebugImages;
server.broadcastLog('info', `OCR settings updated: engine=${s.engine} screen=${s.screenPreprocess} tooltip=${s.tooltipPreprocess}`);
res.json({ ok: true });
});
@ -68,8 +53,8 @@ export function debugRoutes(bot: Bot, server: Server): Router {
router.post('/ocr', (req, res) => {
if (notReady(req, res)) return;
res.json({ ok: true });
bot.screenReader.debugReadFullScreen().then(text => {
server.broadcastLog('info', `OCR [${bot.screenReader.debugOcrEngine}] (${text.length} chars): ${text.substring(0, 200)}`);
bot.screenReader.readFullScreen().then(text => {
server.broadcastLog('info', `OCR [${bot.screenReader.settings.engine}] (${text.length} chars): ${text.substring(0, 200)}`);
server.broadcastDebug('ocr', { text });
}).catch(err => {
logger.error({ err }, 'Debug OCR failed');
@ -82,11 +67,11 @@ export function debugRoutes(bot: Bot, server: Server): Router {
const { text } = req.body as { text: string };
if (!text) { res.status(400).json({ error: 'Missing text parameter' }); return; }
res.json({ ok: true });
bot.screenReader.debugFindTextOnScreen(text).then(pos => {
bot.screenReader.findTextOnScreen(text).then(pos => {
if (pos) {
server.broadcastLog('info', `Found "${text}" at (${pos.x}, ${pos.y}) [${bot.screenReader.debugOcrEngine}]`);
server.broadcastLog('info', `Found "${text}" at (${pos.x}, ${pos.y}) [${bot.screenReader.settings.engine}]`);
} else {
server.broadcastLog('warn', `"${text}" not found on screen [${bot.screenReader.debugOcrEngine}]`);
server.broadcastLog('warn', `"${text}" not found on screen [${bot.screenReader.settings.engine}]`);
}
server.broadcastDebug('find-text', { searchText: text, found: !!pos, position: pos });
}).catch(err => {
@ -101,14 +86,14 @@ export function debugRoutes(bot: Bot, server: Server): Router {
if (!text) { res.status(400).json({ error: 'Missing text parameter' }); return; }
res.json({ ok: true });
(async () => {
const pos = await bot.screenReader.debugFindTextOnScreen(text, !!fuzzy);
const pos = await bot.screenReader.findTextOnScreen(text, !!fuzzy);
if (pos) {
await bot.gameController.focusGame();
await bot.gameController.leftClickAt(pos.x, pos.y);
server.broadcastLog('info', `Found "${text}" at (${pos.x}, ${pos.y}) and clicked [${bot.screenReader.debugOcrEngine}]`);
server.broadcastLog('info', `Found "${text}" at (${pos.x}, ${pos.y}) and clicked [${bot.screenReader.settings.engine}]`);
server.broadcastDebug('find-and-click', { searchText: text, found: true, position: pos });
} else {
server.broadcastLog('warn', `"${text}" not found on screen [${bot.screenReader.debugOcrEngine}]`);
server.broadcastLog('warn', `"${text}" not found on screen [${bot.screenReader.settings.engine}]`);
server.broadcastDebug('find-and-click', { searchText: text, found: false, position: null });
}
})().catch(err => {
@ -234,7 +219,8 @@ export function debugRoutes(bot: Bot, server: Server): Router {
];
await bot.gameController.focusGame();
await mkdir('items', { recursive: true });
const saveImages = bot.screenReader.settings.saveDebugImages;
if (saveImages) await mkdir('items', { recursive: true });
const tooltips: Array<{ row: number; col: number; label: string; text: string }> = [];
const ts = Date.now();
const reg = result.layout.region;
@ -245,7 +231,7 @@ export function debugRoutes(bot: Bot, server: Server): Router {
bot.gameController.moveMouseInstant(reg.x + reg.width + 50, reg.y + reg.height / 2);
await sleep(50);
await bot.screenReader.snapshot();
await bot.screenReader.saveScreenshot(`items/${ts}_snapshot.png`);
if (saveImages) await bot.screenReader.saveScreenshot(`items/${ts}_snapshot.png`);
await sleep(200);
for (const cell of hoverCells) {
@ -257,7 +243,7 @@ export function debugRoutes(bot: Bot, server: Server): Router {
await sleep(50);
const afterMove = performance.now();
const imgPath = `items/${ts}_${cell.row}-${cell.col}.png`;
const imgPath = saveImages ? `items/${ts}_${cell.row}-${cell.col}.png` : undefined;
const diff = await bot.screenReader.diffOcr(imgPath);
const afterOcr = performance.now();
const text = diff.text.trim();