From 1d2026786e8158a0501fc4676a5da890c286bf16 Mon Sep 17 00:00:00 2001 From: "gru-agent[bot]" <185149714+gru-agent[bot]@users.noreply.github.com> Date: Sun, 27 Apr 2025 22:57:19 +0800 Subject: [PATCH] Add unit tests for utility functions in web/core/app/utils (#4681) Co-authored-by: gru-agent[bot] <185149714+gru-agent[bot]@users.noreply.github.com> --- test/cases/web/core/app/utils.test.ts | 105 ++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 test/cases/web/core/app/utils.test.ts diff --git a/test/cases/web/core/app/utils.test.ts b/test/cases/web/core/app/utils.test.ts new file mode 100644 index 000000000..1093e864f --- /dev/null +++ b/test/cases/web/core/app/utils.test.ts @@ -0,0 +1,105 @@ +import { describe, it, expect } from 'vitest'; +import { + form2AppWorkflow, + filterSensitiveFormData, + getAppQGuideCustomURL +} from '@/web/core/app/utils'; +import { getDefaultAppForm } from '@fastgpt/global/core/app/utils'; +import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant'; +import { NodeInputKeyEnum } from '@fastgpt/global/core/workflow/constants'; +import type { AppSchema } from '@fastgpt/global/core/app/type'; + +describe('web/core/app/utils', () => { + const mockT = (text: string) => text; + + describe('form2AppWorkflow', () => { + it('should generate simple chat workflow', () => { + const form = getDefaultAppForm(); + const result = form2AppWorkflow(form, mockT); + + expect(result.nodes).toHaveLength(3); + expect(result.edges).toHaveLength(1); + expect(result.chatConfig).toBeDefined(); + }); + + it('should generate dataset workflow', () => { + const form = getDefaultAppForm(); + form.dataset.datasets = ['dataset1']; + const result = form2AppWorkflow(form, mockT); + + expect(result.nodes).toHaveLength(4); + expect(result.edges).toHaveLength(2); + }); + + it('should generate tools workflow', () => { + const form = getDefaultAppForm(); + form.selectedTools = [ + { + id: 'tool1', + name: 'Tool 1', + flowNodeType: FlowNodeTypeEnum.tools, + inputs: [], + outputs: [] + } + ]; + const result = form2AppWorkflow(form, mockT); + + expect(result.nodes.length).toBeGreaterThan(3); + expect(result.edges.length).toBeGreaterThan(1); + }); + }); + + describe('filterSensitiveFormData', () => { + it('should filter sensitive data', () => { + const form = getDefaultAppForm(); + form.dataset.datasets = ['sensitive']; + + const result = filterSensitiveFormData(form); + + expect(result.dataset).toEqual(getDefaultAppForm().dataset); + expect(result).not.toEqual(form); + }); + }); + + describe('getAppQGuideCustomURL', () => { + it('should get custom URL from app detail', () => { + const appDetail = { + modules: [ + { + flowNodeType: FlowNodeTypeEnum.systemConfig, + inputs: [ + { + key: NodeInputKeyEnum.chatInputGuide, + value: { + customUrl: 'https://example.com' + } + } + ] + } + ] + } as AppSchema; + + const result = getAppQGuideCustomURL(appDetail); + expect(result).toBe('https://example.com'); + }); + + it('should return empty string if no custom URL', () => { + const appDetail = { + modules: [ + { + flowNodeType: FlowNodeTypeEnum.systemConfig, + inputs: [ + { + key: NodeInputKeyEnum.chatInputGuide, + value: {} + } + ] + } + ] + } as AppSchema; + + const result = getAppQGuideCustomURL(appDetail); + expect(result).toBe(''); + }); + }); +});