Skip to content

Commit 503bdc5

Browse files
authored
feat(web-integration): let plan before locate in cache file (#965)
1 parent ea13294 commit 503bdc5

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

packages/web-integration/src/common/task-cache.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,20 @@ export class TaskCache {
229229
mkdirSync(dir, { recursive: true });
230230
debug('created cache directory: %s', dir);
231231
}
232-
const yamlData = yaml.dump(this.cache);
232+
233+
// Sort caches to ensure plan entries come before locate entries for better readability
234+
const sortedCaches = [...this.cache.caches].sort((a, b) => {
235+
if (a.type === 'plan' && b.type === 'locate') return -1;
236+
if (a.type === 'locate' && b.type === 'plan') return 1;
237+
return 0;
238+
});
239+
240+
const cacheToWrite = {
241+
...this.cache,
242+
caches: sortedCaches,
243+
};
244+
245+
const yamlData = yaml.dump(cacheToWrite);
233246
writeFileSync(this.cacheFilePath, yamlData);
234247
debug('cache flushed to file: %s', this.cacheFilePath);
235248
} catch (err) {

packages/web-integration/tests/unit-test/task-cache.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { cacheFileExt } from '@/common/task-cache';
99
import { getMidsceneRunSubDir } from '@midscene/shared/common';
1010
import { uuid } from '@midscene/shared/utils';
11+
import yaml from 'js-yaml';
1112
import {
1213
afterAll,
1314
afterEach,
@@ -361,6 +362,65 @@ describe(
361362

362363
expect(existsSync(cache.cacheFilePath!)).toBe(true);
363364
});
365+
366+
it('should sort caches with plan entries before locate entries when writing to disk', () => {
367+
const cacheId = uuid();
368+
const cache = new TaskCache(cacheId, true);
369+
370+
// Add caches in mixed order: locate, plan, locate, plan
371+
cache.appendCache({
372+
type: 'locate',
373+
prompt: 'locate-prompt-1',
374+
xpaths: ['xpath-1'],
375+
});
376+
377+
cache.appendCache({
378+
type: 'plan',
379+
prompt: 'plan-prompt-1',
380+
yamlWorkflow: 'workflow-1',
381+
});
382+
383+
cache.appendCache({
384+
type: 'locate',
385+
prompt: 'locate-prompt-2',
386+
xpaths: ['xpath-2'],
387+
});
388+
389+
cache.appendCache({
390+
type: 'plan',
391+
prompt: 'plan-prompt-2',
392+
yamlWorkflow: 'workflow-2',
393+
});
394+
395+
// In memory, caches should maintain insertion order
396+
expect(cache.cache.caches[0].type).toBe('locate');
397+
expect(cache.cache.caches[1].type).toBe('plan');
398+
expect(cache.cache.caches[2].type).toBe('locate');
399+
expect(cache.cache.caches[3].type).toBe('plan');
400+
401+
// Read the file content to verify disk ordering
402+
const fileContent = readFileSync(cache.cacheFilePath!, 'utf-8');
403+
const parsedContent = yaml.load(fileContent) as any;
404+
405+
// On disk, all plan entries should come before all locate entries
406+
const diskCaches = parsedContent.caches;
407+
expect(diskCaches[0].type).toBe('plan');
408+
expect(diskCaches[0].prompt).toBe('plan-prompt-1');
409+
expect(diskCaches[1].type).toBe('plan');
410+
expect(diskCaches[1].prompt).toBe('plan-prompt-2');
411+
expect(diskCaches[2].type).toBe('locate');
412+
expect(diskCaches[2].prompt).toBe('locate-prompt-1');
413+
expect(diskCaches[3].type).toBe('locate');
414+
expect(diskCaches[3].prompt).toBe('locate-prompt-2');
415+
416+
// Verify that plan entries maintain their relative order
417+
expect(diskCaches[0].yamlWorkflow).toBe('workflow-1');
418+
expect(diskCaches[1].yamlWorkflow).toBe('workflow-2');
419+
420+
// Verify that locate entries maintain their relative order
421+
expect(diskCaches[2].xpaths).toEqual(['xpath-1']);
422+
expect(diskCaches[3].xpaths).toEqual(['xpath-2']);
423+
});
364424
},
365425
{ timeout: 20000 },
366426
);

0 commit comments

Comments
 (0)