🐛 fix(vscode): 设置写入 globalStorage 文件,更新不再丢提交 profile
This commit is contained in:
1 parent
3a912fce3e
commit
b2d0d17584
3 files changed
+164
-14
No files matched your search
@@ -0,0 +1,91 @@
|
||||
import type { ExtensionContext, Memento, Uri } from 'vscode'
|
||||
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs'
|
||||
import * as os from 'node:os'
|
||||
import * as path from 'node:path'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { DEFAULT_COMMIT_PROFILE_NAME } from '../cc/commitProfile'
|
||||
import { getEffectiveCommitProfilePath, getSettings, persistSeededProfiles, saveSettings, SETTINGS_KEY } from './store'
|
||||
|
||||
const tempDirs: string[] = []
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
while (tempDirs.length > 0) {
|
||||
const dir = tempDirs.pop()
|
||||
if (dir)
|
||||
rmSync(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
function tempDir(): string {
|
||||
const dir = mkdtempSync(path.join(os.tmpdir(), 'settings-store-'))
|
||||
tempDirs.push(dir)
|
||||
return dir
|
||||
}
|
||||
|
||||
function fakeCtx(dir: string, memento: Record<string, unknown> = {}): ExtensionContext {
|
||||
return {
|
||||
extensionPath: path.resolve(__dirname, '../..'),
|
||||
globalStorageUri: { fsPath: dir } as Uri,
|
||||
globalState: {
|
||||
get: <T>(key: string): T | undefined => memento[key] as T | undefined,
|
||||
update: async (key: string, value: unknown) => {
|
||||
memento[key] = value
|
||||
},
|
||||
keys: () => Object.keys(memento),
|
||||
} as unknown as Memento,
|
||||
} as ExtensionContext
|
||||
}
|
||||
|
||||
describe('saveSettings 落盘', () => {
|
||||
it('写入 globalStorage 的 settings.json,清空 memento 后仍能读到 commitProfilePath', async () => {
|
||||
const dir = tempDir()
|
||||
const ctx = fakeCtx(dir)
|
||||
await saveSettings(ctx, { commitProfilePath: '/p/deepseek-v4-pro.json' })
|
||||
|
||||
const file = path.join(dir, 'settings.json')
|
||||
expect(existsSync(file)).toBe(true)
|
||||
expect(JSON.parse(readFileSync(file, 'utf8')).commitProfilePath).toBe('/p/deepseek-v4-pro.json')
|
||||
|
||||
const reloaded = fakeCtx(dir, {})
|
||||
expect(getSettings(reloaded).commitProfilePath).toBe('/p/deepseek-v4-pro.json')
|
||||
expect(await getEffectiveCommitProfilePath(reloaded)).toBe('/p/deepseek-v4-pro.json')
|
||||
})
|
||||
})
|
||||
|
||||
describe('persistSeededProfiles', () => {
|
||||
it('memento 里有旧设置、磁盘无文件时,把旧设置迁到 settings.json', async () => {
|
||||
const dir = tempDir()
|
||||
const ctx = fakeCtx(dir, {
|
||||
[SETTINGS_KEY]: { commitProfilePath: '/old/from-memento.json', webhookPort: 9000 },
|
||||
})
|
||||
await persistSeededProfiles(ctx)
|
||||
|
||||
const reloaded = fakeCtx(dir, {})
|
||||
expect(getSettings(reloaded).commitProfilePath).toBe('/old/from-memento.json')
|
||||
expect(getSettings(reloaded).webhookPort).toBe(9000)
|
||||
})
|
||||
|
||||
it('key 缺失且本机有 deepseek-v4-pro 时,把 seed 写进 settings.json', async () => {
|
||||
const dir = tempDir()
|
||||
const ctx = fakeCtx(dir)
|
||||
vi.spyOn(await import('../cc/profiles.js'), 'listClaudeProfiles').mockResolvedValue([
|
||||
{ name: DEFAULT_COMMIT_PROFILE_NAME, path: '/p/deepseek-v4-pro.json' },
|
||||
])
|
||||
await persistSeededProfiles(ctx)
|
||||
|
||||
const reloaded = fakeCtx(dir, {})
|
||||
expect(getSettings(reloaded).commitProfilePath).toBe('/p/deepseek-v4-pro.json')
|
||||
})
|
||||
|
||||
it('用户主动存了空串,不再被 seed 覆盖', async () => {
|
||||
const dir = tempDir()
|
||||
const ctx = fakeCtx(dir)
|
||||
await saveSettings(ctx, { commitProfilePath: '' })
|
||||
vi.spyOn(await import('../cc/profiles.js'), 'listClaudeProfiles').mockResolvedValue([
|
||||
{ name: DEFAULT_COMMIT_PROFILE_NAME, path: '/p/deepseek-v4-pro.json' },
|
||||
])
|
||||
await persistSeededProfiles(ctx)
|
||||
expect(getSettings(ctx).commitProfilePath).toBe('')
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user