2024-08-04 08:28:19 +08:00
|
|
|
import { type LocateTask, type PlanTask, TaskCache } from '@/common/task-cache';
|
|
|
|
import type { WebUIContext } from '@/common/utils';
|
|
|
|
import type { WebElementInfo } from '@/web-element';
|
|
|
|
import type { AIElementParseResponse } from '@midscene/core';
|
|
|
|
import { beforeEach, describe, expect, it } from 'vitest';
|
2024-08-01 15:46:40 +08:00
|
|
|
|
|
|
|
describe('TaskCache', () => {
|
|
|
|
let taskCache: TaskCache;
|
|
|
|
let formalPageContext: WebUIContext;
|
|
|
|
let pageContext: LocateTask['pageContext'];
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
taskCache = new TaskCache();
|
|
|
|
pageContext = {
|
|
|
|
url: 'https://example.com',
|
|
|
|
size: { width: 1024, height: 768 },
|
|
|
|
};
|
|
|
|
formalPageContext = {
|
|
|
|
...pageContext,
|
|
|
|
screenshotBase64: '',
|
|
|
|
content: [{ id: 'element1' } as WebElementInfo], // 示例页面内容
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false if no cache is available', async () => {
|
2024-08-04 08:28:19 +08:00
|
|
|
const result = taskCache.readCache(
|
|
|
|
formalPageContext,
|
|
|
|
'plan',
|
|
|
|
'test prompt',
|
|
|
|
);
|
2024-08-01 15:46:40 +08:00
|
|
|
expect(result).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false if the prompt does not match', async () => {
|
|
|
|
taskCache.cache = {
|
2024-08-04 08:28:19 +08:00
|
|
|
aiTasks: [
|
|
|
|
{
|
|
|
|
type: 'plan',
|
|
|
|
prompt: 'different prompt',
|
|
|
|
pageContext,
|
|
|
|
response: { plans: [] },
|
|
|
|
},
|
|
|
|
],
|
2024-08-01 15:46:40 +08:00
|
|
|
};
|
2024-08-04 08:28:19 +08:00
|
|
|
const result = taskCache.readCache(
|
|
|
|
formalPageContext,
|
|
|
|
'plan',
|
|
|
|
'test prompt',
|
|
|
|
);
|
2024-08-01 15:46:40 +08:00
|
|
|
expect(result).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false if the element cannot be found in the new context', async () => {
|
|
|
|
taskCache.cache = {
|
|
|
|
aiTasks: [
|
|
|
|
{
|
|
|
|
type: 'locate',
|
|
|
|
prompt: 'test prompt',
|
|
|
|
pageContext,
|
2024-08-04 08:28:19 +08:00
|
|
|
response: {
|
|
|
|
elements: [{ id: 'element3' }],
|
|
|
|
} as AIElementParseResponse,
|
2024-08-01 15:46:40 +08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
2024-08-04 08:28:19 +08:00
|
|
|
const result = taskCache.readCache(
|
|
|
|
formalPageContext,
|
|
|
|
'locate',
|
|
|
|
'test prompt',
|
|
|
|
);
|
2024-08-01 15:46:40 +08:00
|
|
|
expect(result).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return cached response if the conditions match', async () => {
|
2024-08-04 08:28:19 +08:00
|
|
|
const cachedResponse = {
|
|
|
|
plans: [{ type: 'Locate', thought: '', param: {} }],
|
|
|
|
} as PlanTask['response'];
|
2024-08-01 15:46:40 +08:00
|
|
|
taskCache.cache = {
|
2024-08-04 08:28:19 +08:00
|
|
|
aiTasks: [
|
|
|
|
{
|
|
|
|
type: 'plan',
|
|
|
|
prompt: 'test prompt',
|
|
|
|
pageContext,
|
|
|
|
response: cachedResponse,
|
|
|
|
},
|
|
|
|
],
|
2024-08-01 15:46:40 +08:00
|
|
|
};
|
2024-08-04 08:28:19 +08:00
|
|
|
const result = taskCache.readCache(
|
|
|
|
formalPageContext,
|
|
|
|
'plan',
|
|
|
|
'test prompt',
|
|
|
|
);
|
2024-08-01 15:46:40 +08:00
|
|
|
expect(result).toEqual(cachedResponse);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should save cache correctly', () => {
|
|
|
|
const newCache: PlanTask = {
|
|
|
|
type: 'plan',
|
|
|
|
prompt: 'new prompt',
|
|
|
|
pageContext,
|
|
|
|
response: { plans: [{ type: 'Locate', thought: '', param: {} }] },
|
|
|
|
};
|
|
|
|
taskCache.saveCache(newCache);
|
|
|
|
expect(taskCache.newCache.aiTasks).toContain(newCache);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should check page context equality correctly', () => {
|
|
|
|
const isEqual = taskCache.pageContextEqual(pageContext, formalPageContext);
|
|
|
|
expect(isEqual).toBe(true);
|
|
|
|
|
2024-08-04 08:28:19 +08:00
|
|
|
const differentContext = {
|
|
|
|
...formalPageContext,
|
|
|
|
size: { width: 800, height: 600 },
|
|
|
|
};
|
|
|
|
const isNotEqual = taskCache.pageContextEqual(
|
|
|
|
pageContext,
|
|
|
|
differentContext,
|
|
|
|
);
|
2024-08-01 15:46:40 +08:00
|
|
|
expect(isNotEqual).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate task cache correctly', () => {
|
|
|
|
const generatedCache = taskCache.generateTaskCache();
|
|
|
|
expect(generatedCache).toEqual(taskCache.newCache);
|
|
|
|
});
|
|
|
|
});
|