mirror of
https://github.com/langgenius/dify.git
synced 2026-10-04 13:47:55 +08:00
fix(cli): make app-info cache resilient to corrupt entries + route errors through structured envelope (WTA-257) (#37852)
This commit is contained in:
1 parent
2112115962
commit
32dc9ff2d9
5 files changed
+144
-28
No files matched your search
@@ -1,13 +1,14 @@
|
||||
import type { DifyMock } from '@test/fixtures/dify-mock/server'
|
||||
import { mkdtemp, rm } from 'node:fs/promises'
|
||||
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { startMock } from '@test/fixtures/dify-mock/server'
|
||||
import { testHttpClient } from '@test/fixtures/http-client'
|
||||
import yaml from 'js-yaml'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { loadAppInfoCache } from '@/cache/app-info'
|
||||
import { ENV_CACHE_DIR } from '@/store/dir'
|
||||
import { CACHE_APP_INFO, getCache } from '@/store/manager'
|
||||
import { CACHE_APP_INFO, cachePath, getCache } from '@/store/manager'
|
||||
import { FieldInfo, FieldParameters } from '@/types/app-meta'
|
||||
import { AppMetaClient } from './app-meta.js'
|
||||
import { AppsClient } from './apps.js'
|
||||
@@ -97,4 +98,40 @@ describe('AppMetaClient', () => {
|
||||
await client.get('app-1', [FieldInfo])
|
||||
expect(spy).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('corrupt cache entry refetches; valid sibling stays cached; no throw', async () => {
|
||||
const path = cachePath(dir, CACHE_APP_INFO)
|
||||
const apps = new AppsClient(testHttpClient(mock.url, 'dfoa_test'))
|
||||
|
||||
// Seed a real, production-serialized entry by fetching app-1 once (this
|
||||
// calls cache.set → serialize, so we never hand-write the on-disk shape).
|
||||
const seed = await loadAppInfoCache({ store: getCache(CACHE_APP_INFO) })
|
||||
await new AppMetaClient({ apps, host: mock.url, cache: seed }).get('app-1', [FieldInfo])
|
||||
|
||||
// Reuse that serialized entry as a valid sibling; corrupt the app-1 slot.
|
||||
const file = yaml.load(await readFile(path, 'utf8')) as { entries: Record<string, unknown> }
|
||||
const validEntry = file.entries[`${mock.url}::app-1`]
|
||||
await writeFile(
|
||||
path,
|
||||
yaml.dump({ entries: {
|
||||
[`${mock.url}::app-1`]: 'corrupted-string',
|
||||
[`${mock.url}::sibling`]: validEntry,
|
||||
} }),
|
||||
'utf8',
|
||||
)
|
||||
|
||||
// Reload: app-1 dropped, sibling kept.
|
||||
const cache = await loadAppInfoCache({ store: getCache(CACHE_APP_INFO) })
|
||||
const spy = vi.spyOn(apps, 'describe')
|
||||
const client = new AppMetaClient({ apps, host: mock.url, cache })
|
||||
|
||||
// app-1 corrupt → dropped → miss → refetched from the mock
|
||||
const a = await client.get('app-1', [FieldInfo])
|
||||
expect(a.info?.id).toBe('app-1')
|
||||
expect(spy).toHaveBeenCalledTimes(1)
|
||||
|
||||
// sibling is the real serialized entry → served from cache, no network
|
||||
await client.get('sibling', [FieldInfo])
|
||||
expect(spy).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user