Files

1 line
7.9 KiB
JSON

{"repo": "brhiza/sydf.cc", "files": {"src/stores/settings.test.ts": {"eb1e969a309786760eb4fe5c86598a4262a3c52c": {"t": 1785746519.041357, "n": 2746, "h": "4fb056b0429265b8", "c": "// @vitest-environment jsdom\n\nimport { createPinia, setActivePinia } from 'pinia';\nimport { beforeEach, describe, expect, it, vi } from 'vitest';\nimport { useSettingsStore } from './settings';\n\ndescribe('settings store', () => {\n beforeEach(() => {\n vi.restoreAllMocks();\n setActivePinia(createPinia());\n localStorage.clear();\n });\n\n it('加载异常本地设置时应保持默认内置 API 通道', () => {\n localStorage.setItem(\n 'sydf-settings',\n JSON.stringify({\n customApiKey: 123,\n customApiEndpoint: {},\n useCustomApi: 'true',\n selectedModel: false,\n availableModels: 'bad-models',\n })\n );\n\n const store = useSettingsStore();\n\n expect(store.settings.useCustomApi).toBe(false);\n expect(store.settings.customApiKey).toBe('');\n expect(store.settings.customApiEndpoint).toBe('');\n expect(store.settings.selectedModel).toBe('');\n expect(store.settings.availableModels).toEqual([]);\n });\n\n it('更新设置时应忽略异常字段并保留可用默认值', () => {\n const store = useSettingsStore();\n\n store.updateSettings({\n customApiKey: 123,\n customApiEndpoint: {},\n useCustomApi: 'true',\n selectedModel: false,\n availableModels: ['deepseek-chat', 123],\n } as unknown as Parameters<typeof store.updateSettings>[0]);\n\n expect(store.settings.useCustomApi).toBe(false);\n expect(store.settings.customApiKey).toBe('');\n expect(store.settings.customApiEndpoint).toBe('');\n expect(store.settings.selectedModel).toBe('');\n expect(store.settings.availableModels).toEqual(['deepseek-chat']);\n });\n\n it('获取模型列表时应拒绝非本机 HTTP 端点且不发送密钥', async () => {\n const fetchMock = vi.fn();\n vi.stubGlobal('fetch', fetchMock);\n const store = useSettingsStore();\n\n const models = await store.fetchAvailableModels('http://api.example.com/v1', 'sk-xxx');\n\n expect(models).toEqual([]);\n expect(store.error).toContain('API 端点必须是 HTTPS 地址');\n expect(fetchMock).not.toHaveBeenCalled();\n });\n\n it('获取模型列表时应使用清理后的安全模型地址', async () => {\n const fetchMock = vi.fn(\n async () =>\n new Response(\n JSON.stringify({\n data: [{ id: 'gpt-4o' }],\n }),\n {\n status: 200,\n headers: { 'Content-Type': 'application/json' },\n }\n )\n );\n vi.stubGlobal('fetch', fetchMock);\n const store = useSettingsStore();\n\n const models = await store.fetchAvailableModels(\n ' https://api.example.com/v1/chat/completions ',\n 'sk-xxx'\n );\n\n expect(models).toEqual(['gpt-4o']);\n expect(fetchMock).toHaveBeenCalledWith('https://api.example.com/v1/models', {\n method: 'GET',\n headers: {\n Authorization: 'Bearer sk-xxx',\n 'Content-Type': 'application/json',\n },\n });\n });\n});\n"}, "fe7a995898a29b4fcebe2ddca7653f4c75aee6c1": {"t": 1785765218.7745402, "n": 2746, "h": "4fb056b0429265b8", "c": "// @vitest-environment jsdom\n\nimport { createPinia, setActivePinia } from 'pinia';\nimport { beforeEach, describe, expect, it, vi } from 'vitest';\nimport { useSettingsStore } from './settings';\n\ndescribe('settings store', () => {\n beforeEach(() => {\n vi.restoreAllMocks();\n setActivePinia(createPinia());\n localStorage.clear();\n });\n\n it('加载异常本地设置时应保持默认内置 API 通道', () => {\n localStorage.setItem(\n 'sydf-settings',\n JSON.stringify({\n customApiKey: 123,\n customApiEndpoint: {},\n useCustomApi: 'true',\n selectedModel: false,\n availableModels: 'bad-models',\n })\n );\n\n const store = useSettingsStore();\n\n expect(store.settings.useCustomApi).toBe(false);\n expect(store.settings.customApiKey).toBe('');\n expect(store.settings.customApiEndpoint).toBe('');\n expect(store.settings.selectedModel).toBe('');\n expect(store.settings.availableModels).toEqual([]);\n });\n\n it('更新设置时应忽略异常字段并保留可用默认值', () => {\n const store = useSettingsStore();\n\n store.updateSettings({\n customApiKey: 123,\n customApiEndpoint: {},\n useCustomApi: 'true',\n selectedModel: false,\n availableModels: ['deepseek-chat', 123],\n } as unknown as Parameters<typeof store.updateSettings>[0]);\n\n expect(store.settings.useCustomApi).toBe(false);\n expect(store.settings.customApiKey).toBe('');\n expect(store.settings.customApiEndpoint).toBe('');\n expect(store.settings.selectedModel).toBe('');\n expect(store.settings.availableModels).toEqual(['deepseek-chat']);\n });\n\n it('获取模型列表时应拒绝非本机 HTTP 端点且不发送密钥', async () => {\n const fetchMock = vi.fn();\n vi.stubGlobal('fetch', fetchMock);\n const store = useSettingsStore();\n\n const models = await store.fetchAvailableModels('http://api.example.com/v1', 'sk-xxx');\n\n expect(models).toEqual([]);\n expect(store.error).toContain('API 端点必须是 HTTPS 地址');\n expect(fetchMock).not.toHaveBeenCalled();\n });\n\n it('获取模型列表时应使用清理后的安全模型地址', async () => {\n const fetchMock = vi.fn(\n async () =>\n new Response(\n JSON.stringify({\n data: [{ id: 'gpt-4o' }],\n }),\n {\n status: 200,\n headers: { 'Content-Type': 'application/json' },\n }\n )\n );\n vi.stubGlobal('fetch', fetchMock);\n const store = useSettingsStore();\n\n const models = await store.fetchAvailableModels(\n ' https://api.example.com/v1/chat/completions ',\n 'sk-xxx'\n );\n\n expect(models).toEqual(['gpt-4o']);\n expect(fetchMock).toHaveBeenCalledWith('https://api.example.com/v1/models', {\n method: 'GET',\n headers: {\n Authorization: 'Bearer sk-xxx',\n 'Content-Type': 'application/json',\n },\n });\n });\n});\n"}, "3b6d2761d292be40ec69009469c3b3b2106adcf0": {"t": 1785765220.278436, "n": 1527, "h": "e6d6ef749132aed2", "c": "// @vitest-environment jsdom\n\nimport { createPinia, setActivePinia } from 'pinia';\nimport { beforeEach, describe, expect, it } from 'vitest';\nimport { useSettingsStore } from './settings';\n\ndescribe('settings store', () => {\n beforeEach(() => {\n setActivePinia(createPinia());\n localStorage.clear();\n });\n\n it('加载异常本地设置时应保持默认内置 API 通道', () => {\n localStorage.setItem(\n 'sydf-settings',\n JSON.stringify({\n customApiKey: 123,\n customApiEndpoint: {},\n useCustomApi: 'true',\n selectedModel: false,\n availableModels: 'bad-models',\n })\n );\n\n const store = useSettingsStore();\n\n expect(store.settings.useCustomApi).toBe(false);\n expect(store.settings.customApiKey).toBe('');\n expect(store.settings.customApiEndpoint).toBe('');\n expect(store.settings.selectedModel).toBe('');\n expect(store.settings.availableModels).toEqual([]);\n });\n\n it('更新设置时应忽略异常字段并保留可用默认值', () => {\n const store = useSettingsStore();\n\n store.updateSettings({\n customApiKey: 123,\n customApiEndpoint: {},\n useCustomApi: 'true',\n selectedModel: false,\n availableModels: ['deepseek-chat', 123],\n } as unknown as Parameters<typeof store.updateSettings>[0]);\n\n expect(store.settings.useCustomApi).toBe(false);\n expect(store.settings.customApiKey).toBe('');\n expect(store.settings.customApiEndpoint).toBe('');\n expect(store.settings.selectedModel).toBe('');\n expect(store.settings.availableModels).toEqual(['deepseek-chat']);\n });\n});\n"}}}}