2025-02-21 09:56:09 +08:00
|
|
|
import { writeFileSync } from 'node:fs';
|
2025-03-24 09:50:27 +08:00
|
|
|
import Insight, {
|
|
|
|
type Rect,
|
2025-02-21 09:56:09 +08:00
|
|
|
MIDSCENE_MODEL_NAME,
|
|
|
|
getAIConfig,
|
|
|
|
} from '@midscene/core';
|
2025-03-17 19:19:54 +08:00
|
|
|
import { vlLocateMode } from '@midscene/core/env';
|
2025-02-21 09:56:09 +08:00
|
|
|
import { sleep } from '@midscene/core/utils';
|
|
|
|
import { saveBase64Image } from '@midscene/shared/img';
|
|
|
|
import dotenv from 'dotenv';
|
2025-03-24 09:50:27 +08:00
|
|
|
import { afterEach, expect, test } from 'vitest';
|
2025-03-07 17:20:18 +08:00
|
|
|
import { TestResultCollector } from '../src/test-analyzer';
|
2025-03-24 09:50:27 +08:00
|
|
|
import { annotateRects, buildContext, getCases } from './util';
|
2025-02-21 09:56:09 +08:00
|
|
|
|
|
|
|
dotenv.config({
|
|
|
|
debug: true,
|
|
|
|
override: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const testSources = [
|
|
|
|
'antd-carousel',
|
|
|
|
'todo',
|
|
|
|
'online_order',
|
|
|
|
'online_order_list',
|
|
|
|
'taobao',
|
|
|
|
'aweme-login',
|
|
|
|
'aweme-play',
|
|
|
|
];
|
|
|
|
|
2025-03-17 19:19:54 +08:00
|
|
|
const positionModeTag = vlLocateMode() ? 'by_coordinates' : 'by_element';
|
2025-02-21 09:56:09 +08:00
|
|
|
const resultCollector = new TestResultCollector(
|
|
|
|
positionModeTag,
|
|
|
|
getAIConfig(MIDSCENE_MODEL_NAME) || 'unspecified',
|
|
|
|
);
|
|
|
|
|
2025-02-21 16:15:51 +08:00
|
|
|
let failCaseThreshold = 0;
|
2025-03-17 19:19:54 +08:00
|
|
|
if (process.env.CI && !vlLocateMode()) {
|
2025-02-21 16:15:51 +08:00
|
|
|
failCaseThreshold = 3;
|
|
|
|
}
|
|
|
|
|
2025-03-17 19:19:54 +08:00
|
|
|
if (process.env.MIDSCENE_EVALUATION_EXPECT_VL) {
|
|
|
|
expect(vlLocateMode()).toBeTruthy();
|
|
|
|
}
|
|
|
|
|
2025-03-24 09:50:27 +08:00
|
|
|
afterEach(async () => {
|
|
|
|
await resultCollector.printSummary();
|
2025-02-21 09:56:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
testSources.forEach((source) => {
|
|
|
|
test(
|
|
|
|
`${source}: locate element`,
|
|
|
|
async () => {
|
|
|
|
const { path: aiDataPath, content: cases } = await getCases(
|
|
|
|
source,
|
|
|
|
'inspect',
|
|
|
|
);
|
|
|
|
|
|
|
|
const annotations: Array<{
|
|
|
|
indexId: number;
|
2025-03-24 09:50:27 +08:00
|
|
|
rect: Rect;
|
2025-02-21 09:56:09 +08:00
|
|
|
}> = [];
|
|
|
|
for (const [index, testCase] of cases.testCases.entries()) {
|
|
|
|
const context = await buildContext(source);
|
|
|
|
|
|
|
|
const prompt = testCase.prompt;
|
|
|
|
const startTime = Date.now();
|
2025-03-24 09:50:27 +08:00
|
|
|
|
|
|
|
const insight = new Insight(context);
|
|
|
|
|
|
|
|
const result = await insight.locate({
|
|
|
|
prompt,
|
2025-04-08 17:56:15 +08:00
|
|
|
deepThink: testCase.deepThink,
|
2025-02-21 09:56:09 +08:00
|
|
|
});
|
2025-03-24 09:50:27 +08:00
|
|
|
const { element, rect } = result;
|
2025-02-21 09:56:09 +08:00
|
|
|
|
|
|
|
if (process.env.UPDATE_ANSWER_DATA) {
|
2025-03-24 09:50:27 +08:00
|
|
|
// const { elementById } = context;
|
2025-02-21 09:56:09 +08:00
|
|
|
|
2025-03-24 09:50:27 +08:00
|
|
|
if (rect) {
|
2025-02-21 09:56:09 +08:00
|
|
|
const indexId = index + 1;
|
2025-03-24 09:50:27 +08:00
|
|
|
testCase.response_rect = rect;
|
2025-02-21 09:56:09 +08:00
|
|
|
testCase.annotation_index_id = indexId;
|
|
|
|
annotations.push({
|
|
|
|
indexId,
|
2025-03-24 09:50:27 +08:00
|
|
|
rect,
|
2025-02-21 09:56:09 +08:00
|
|
|
});
|
2025-03-24 09:50:27 +08:00
|
|
|
|
|
|
|
// // biome-ignore lint/performance/noDelete: <explanation>
|
|
|
|
// delete (testCase as any).response_bbox;
|
|
|
|
// // biome-ignore lint/performance/noDelete: <explanation>
|
|
|
|
// delete (testCase as any).response;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (element) {
|
|
|
|
testCase.response_element = {
|
|
|
|
id: element.id,
|
|
|
|
indexId: element.indexId,
|
|
|
|
};
|
2025-02-21 09:56:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// write testCase to file
|
|
|
|
writeFileSync(aiDataPath, JSON.stringify(cases, null, 2));
|
|
|
|
}
|
|
|
|
if (annotations.length > 0) {
|
2025-03-24 09:50:27 +08:00
|
|
|
const markedImage = await annotateRects(
|
2025-02-21 09:56:09 +08:00
|
|
|
context.screenshotBase64,
|
2025-03-24 09:50:27 +08:00
|
|
|
annotations.map((item) => item.rect),
|
2025-02-21 09:56:09 +08:00
|
|
|
);
|
|
|
|
await saveBase64Image({
|
|
|
|
base64Data: markedImage,
|
|
|
|
outputPath: `${aiDataPath}-coordinates-annotated.png`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
resultCollector.addResult(
|
|
|
|
source,
|
|
|
|
testCase,
|
|
|
|
result,
|
|
|
|
Date.now() - startTime,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-03-24 09:50:27 +08:00
|
|
|
await resultCollector.analyze(source, failCaseThreshold);
|
2025-02-21 09:56:09 +08:00
|
|
|
await sleep(3 * 1000);
|
|
|
|
},
|
2025-02-24 16:53:06 +08:00
|
|
|
360 * 1000,
|
2025-02-21 09:56:09 +08:00
|
|
|
);
|
|
|
|
});
|