2025-01-16 14:37:35 +08:00
|
|
|
import { readFileSync } from 'node:fs';
|
|
|
|
import path from 'node:path';
|
|
|
|
import { describe } from 'node:test';
|
2025-02-10 16:36:12 +08:00
|
|
|
import { AiAssert } from '@midscene/core';
|
|
|
|
import { buildContext } from '@midscene/core/evaluation';
|
2025-01-16 14:37:35 +08:00
|
|
|
import { afterAll, expect, test } from 'vitest';
|
2025-02-10 16:36:12 +08:00
|
|
|
import { type InspectAiTestCase, repeatFile } from './util';
|
|
|
|
|
2025-01-16 14:37:35 +08:00
|
|
|
import 'dotenv/config';
|
2025-02-07 14:55:52 +08:00
|
|
|
import dotenv from 'dotenv';
|
|
|
|
|
|
|
|
dotenv.config({
|
|
|
|
debug: true,
|
|
|
|
override: true,
|
|
|
|
});
|
2025-01-16 14:37:35 +08:00
|
|
|
|
2025-02-10 16:36:12 +08:00
|
|
|
const testSources = ['online_order'];
|
2025-01-16 14:37:35 +08:00
|
|
|
|
|
|
|
describe('ai inspect element', () => {
|
|
|
|
const testResult: {
|
|
|
|
path: string;
|
|
|
|
result: {
|
|
|
|
score: number;
|
|
|
|
averageTime: string;
|
|
|
|
successCount: number;
|
|
|
|
failCount: number;
|
|
|
|
};
|
|
|
|
}[] = [];
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
console.table(
|
|
|
|
testResult.map((r) => {
|
|
|
|
return {
|
|
|
|
path: r.path,
|
|
|
|
...r.result,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2025-02-07 14:55:52 +08:00
|
|
|
repeatFile(testSources, 1, (source, repeatIndex) => {
|
2025-02-10 16:36:12 +08:00
|
|
|
const aiDataPath = path.join(
|
|
|
|
__dirname,
|
|
|
|
`../page-cases/assertion/${source}.json`,
|
|
|
|
);
|
2025-01-16 14:37:35 +08:00
|
|
|
const aiData = JSON.parse(
|
|
|
|
readFileSync(aiDataPath, 'utf-8'),
|
|
|
|
) as InspectAiTestCase;
|
|
|
|
|
|
|
|
aiData.testCases.forEach((testCase, index) => {
|
|
|
|
const prompt = testCase.prompt;
|
|
|
|
test(
|
|
|
|
`${source}-${repeatIndex}: assertion-${prompt.slice(0, 30)}...`,
|
|
|
|
async () => {
|
2025-02-10 16:36:12 +08:00
|
|
|
const { context } = await buildContext(
|
|
|
|
path.join(__dirname, '../page-data/', aiData.testDataPath),
|
2025-01-16 14:37:35 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
const { prompt, expected } = testCase;
|
|
|
|
const result = await AiAssert({
|
|
|
|
assertion: prompt,
|
|
|
|
context,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(typeof result?.content?.pass).toBe('boolean');
|
|
|
|
if (result?.content?.pass !== expected) {
|
|
|
|
throw new Error(
|
|
|
|
`assertion failed: ${prompt} expected: ${expected}, actual: ${result?.content?.pass}, thought: ${result?.content?.thought}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('assertion passed, thought:', result?.content?.thought);
|
|
|
|
},
|
|
|
|
{
|
2025-01-30 14:14:14 +08:00
|
|
|
timeout: 3 * 60 * 1000,
|
2025-01-16 14:37:35 +08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|