2025-03-17 19:19:54 +08:00
|
|
|
import { writeFileSync } from 'node:fs';
|
2025-04-02 19:26:56 +08:00
|
|
|
import { MIDSCENE_MODEL_NAME, type Rect, getAIConfig } from '@midscene/core';
|
2025-03-17 19:19:54 +08:00
|
|
|
import { AiLocateSection } from '@midscene/core/ai-model';
|
|
|
|
import { sleep } from '@midscene/core/utils';
|
2025-04-24 22:54:52 +08:00
|
|
|
import { vlLocateMode } from '@midscene/shared/env';
|
2025-03-17 19:19:54 +08:00
|
|
|
import { saveBase64Image } from '@midscene/shared/img';
|
|
|
|
import dotenv from 'dotenv';
|
|
|
|
import { afterAll, expect, test } from 'vitest';
|
2025-04-02 19:26:56 +08:00
|
|
|
import { TestResultCollector } from '../src/test-analyzer';
|
|
|
|
import { annotateRects, buildContext, getCases } from './util';
|
2025-03-17 19:19:54 +08:00
|
|
|
|
|
|
|
dotenv.config({
|
|
|
|
debug: true,
|
|
|
|
override: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const testSources = ['antd-tooltip'];
|
|
|
|
|
|
|
|
const resultCollector = new TestResultCollector(
|
|
|
|
'section-locator',
|
|
|
|
getAIConfig(MIDSCENE_MODEL_NAME) || 'unspecified',
|
|
|
|
);
|
|
|
|
|
|
|
|
let failCaseThreshold = 0;
|
|
|
|
if (process.env.CI && !vlLocateMode()) {
|
|
|
|
failCaseThreshold = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
afterAll(async () => {
|
2025-04-02 19:26:56 +08:00
|
|
|
await resultCollector.printSummary();
|
2025-03-17 19:19:54 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
testSources.forEach((source) => {
|
|
|
|
test(
|
|
|
|
`${source}: locate section`,
|
|
|
|
async () => {
|
|
|
|
const { path: aiDataPath, content: cases } = await getCases(
|
|
|
|
source,
|
|
|
|
'section-locator',
|
|
|
|
);
|
|
|
|
|
|
|
|
const annotations: Array<{
|
|
|
|
indexId: number;
|
2025-04-02 19:26:56 +08:00
|
|
|
rect: Rect;
|
2025-03-17 19:19:54 +08:00
|
|
|
}> = [];
|
|
|
|
for (const [index, testCase] of cases.testCases.entries()) {
|
|
|
|
const context = await buildContext(source);
|
|
|
|
const prompt = testCase.prompt;
|
|
|
|
const startTime = Date.now();
|
|
|
|
const result = await AiLocateSection({
|
|
|
|
context,
|
|
|
|
sectionDescription: prompt,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (process.env.UPDATE_ANSWER_DATA) {
|
2025-04-02 19:26:56 +08:00
|
|
|
const { rect } = result;
|
2025-03-17 19:19:54 +08:00
|
|
|
|
2025-04-02 19:26:56 +08:00
|
|
|
if (rect) {
|
2025-03-17 19:19:54 +08:00
|
|
|
const indexId = index + 1;
|
2025-04-02 19:26:56 +08:00
|
|
|
testCase.response_rect = rect;
|
2025-03-17 19:19:54 +08:00
|
|
|
testCase.annotation_index_id = indexId;
|
|
|
|
annotations.push({
|
|
|
|
indexId,
|
2025-04-02 19:26:56 +08:00
|
|
|
rect,
|
2025-03-17 19:19:54 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// write testCase to file
|
|
|
|
writeFileSync(aiDataPath, JSON.stringify(cases, null, 2));
|
|
|
|
}
|
|
|
|
if (annotations.length > 0) {
|
2025-04-02 19:26:56 +08:00
|
|
|
const markedImage = await annotateRects(
|
2025-03-17 19:19:54 +08:00
|
|
|
context.screenshotBase64,
|
2025-04-02 19:26:56 +08:00
|
|
|
annotations.map((item) => item.rect),
|
2025-05-20 15:52:03 +08:00
|
|
|
prompt,
|
2025-03-17 19:19:54 +08:00
|
|
|
);
|
|
|
|
await saveBase64Image({
|
|
|
|
base64Data: markedImage,
|
|
|
|
outputPath: `${aiDataPath}-coordinates-annotated.png`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
resultCollector.addResult(
|
|
|
|
source,
|
|
|
|
testCase,
|
|
|
|
result,
|
|
|
|
Date.now() - startTime,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-04-02 19:26:56 +08:00
|
|
|
await resultCollector.printSummary();
|
|
|
|
await resultCollector.analyze(source, failCaseThreshold);
|
2025-03-17 19:19:54 +08:00
|
|
|
await sleep(3 * 1000);
|
|
|
|
},
|
|
|
|
360 * 1000,
|
|
|
|
);
|
|
|
|
});
|