|
8 | 8 | import { cacheFileExt } from '@/common/task-cache';
|
9 | 9 | import { getMidsceneRunSubDir } from '@midscene/shared/common';
|
10 | 10 | import { uuid } from '@midscene/shared/utils';
|
| 11 | +import yaml from 'js-yaml'; |
11 | 12 | import {
|
12 | 13 | afterAll,
|
13 | 14 | afterEach,
|
@@ -361,6 +362,65 @@ describe(
|
361 | 362 |
|
362 | 363 | expect(existsSync(cache.cacheFilePath!)).toBe(true);
|
363 | 364 | });
|
| 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 | + }); |
364 | 424 | },
|
365 | 425 | { timeout: 20000 },
|
366 | 426 | );
|
|
0 commit comments