Skip to content

Commit 2d32c61

Browse files
committed
test(core): enhance agent cache fallback test with improved mock configurations
1 parent 193f29e commit 2d32c61

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

packages/core/tests/unit-test/agent-cache-fallback.test.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import { Agent } from '@/agent';
2-
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';
2+
import {
3+
MIDSCENE_MODEL_NAME,
4+
MIDSCENE_MODEL_API_KEY,
5+
MIDSCENE_MODEL_BASE_URL,
6+
MIDSCENE_MODEL_FAMILY,
7+
} from '@midscene/shared/env';
8+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
9+
10+
const defaultModelConfig = {
11+
[MIDSCENE_MODEL_NAME]: 'qwen2.5-vl-max',
12+
[MIDSCENE_MODEL_API_KEY]: 'test-key',
13+
[MIDSCENE_MODEL_BASE_URL]: 'https://api.sample.com/v1',
14+
[MIDSCENE_MODEL_FAMILY]: 'qwen2.5-vl' as const,
15+
};
316

417
const createMockInterface = () =>
518
({
@@ -12,6 +25,7 @@ describe('Agent cache fallback', () => {
1225

1326
beforeEach(() => {
1427
vi.mock('openai');
28+
Object.assign(process.env, defaultModelConfig);
1529
agent = new Agent(createMockInterface());
1630
});
1731

@@ -23,30 +37,43 @@ describe('Agent cache fallback', () => {
2337
// Mock cache with yaml workflow
2438
const mockCache = {
2539
cacheContent: {
26-
yamlWorkflow: 'invalid-yaml-content',
40+
yamlWorkflow: 'tasks:\n - name: test\n flow: invalid',
2741
},
2842
};
2943

3044
// Mock taskCache to return cache match
45+
const matchPlanCacheSpy = vi.fn().mockReturnValue(mockCache);
3146
agent.taskCache = {
3247
isCacheResultUsed: true,
33-
findCache: vi.fn().mockResolvedValue(mockCache),
48+
matchPlanCache: matchPlanCacheSpy,
3449
} as any;
3550

3651
// Mock runYaml to throw error (simulating cache execution failure)
37-
const runYamlSpy = vi.spyOn(agent, 'runYaml').mockRejectedValue(new Error('YAML execution failed'));
52+
const runYamlSpy = vi
53+
.spyOn(agent, 'runYaml')
54+
.mockRejectedValue(new Error('YAML execution failed'));
3855

39-
// Mock taskExecutor methods
56+
// Mock taskExecutor methods - make loadYamlFlowAsPlanning async
4057
agent.taskExecutor = {
41-
loadYamlFlowAsPlanning: vi.fn(),
42-
action: vi.fn().mockResolvedValue({ output: { result: { success: true } } }),
58+
loadYamlFlowAsPlanning: vi.fn().mockResolvedValue(undefined),
59+
action: vi
60+
.fn()
61+
.mockResolvedValue({ output: { result: { success: true } } }),
62+
} as any;
63+
64+
// Mock model config manager to return non-vlm-ui-tars config
65+
agent.modelConfigManager = {
66+
getModelConfig: vi.fn().mockReturnValue({ vlMode: 'normal' }),
4367
} as any;
4468

69+
// Mock resolveReplanningCycleLimit
70+
vi.spyOn(agent as any, 'resolveReplanningCycleLimit').mockReturnValue(3);
71+
4572
const result = await agent.aiAct('test task');
4673

4774
// Verify runYaml was called and failed
48-
expect(runYamlSpy).toHaveBeenCalledWith('invalid-yaml-content');
49-
75+
expect(runYamlSpy).toHaveBeenCalledWith('tasks:\n - name: test\n flow: invalid');
76+
5077
// Verify fallback to normal execution
5178
expect(agent.taskExecutor.action).toHaveBeenCalled();
5279
});

0 commit comments

Comments
 (0)