Files
dify/web/utils/app-redirection.spec.ts

109 lines
4.5 KiB
TypeScript

/**
* Test suite for app redirection utility functions
* Tests navigation path generation based on user permissions and app modes
*/
import { AppModeEnum } from '@/types/app'
import { AppACLPermission } from '@/utils/permission'
import { getRedirection, getRedirectionPath } from './app-redirection'
describe('app-redirection', () => {
/**
* Tests getRedirectionPath which determines the correct path based on:
* - App ACL layout access permissions
* - App mode (workflow, advanced-chat, chat, completion, agent-chat)
*/
describe('getRedirectionPath', () => {
it('returns overview path when app ACL cannot access layout', () => {
const app = { id: 'app-123', mode: AppModeEnum.CHAT, permission_keys: [] }
const result = getRedirectionPath(app)
expect(result).toBe('/app/app-123/overview')
})
it('returns workflow path for workflow mode when app ACL can access layout', () => {
const app = { id: 'app-123', mode: AppModeEnum.WORKFLOW, permission_keys: [AppACLPermission.ViewLayout] }
const result = getRedirectionPath(app)
expect(result).toBe('/app/app-123/workflow')
})
it('returns workflow path for advanced-chat mode when app ACL can access layout', () => {
const app = { id: 'app-123', mode: AppModeEnum.ADVANCED_CHAT, permission_keys: [AppACLPermission.TestAndRun] }
const result = getRedirectionPath(app)
expect(result).toBe('/app/app-123/workflow')
})
it('returns configuration path for chat mode when app ACL can access layout', () => {
const app = { id: 'app-123', mode: AppModeEnum.CHAT, permission_keys: [AppACLPermission.Edit] }
const result = getRedirectionPath(app)
expect(result).toBe('/app/app-123/configuration')
})
it('returns configuration path for completion mode when app ACL can access layout', () => {
const app = { id: 'app-123', mode: AppModeEnum.COMPLETION, permission_keys: [AppACLPermission.ViewLayout] }
const result = getRedirectionPath(app)
expect(result).toBe('/app/app-123/configuration')
})
it('returns configuration path for agent-chat mode when app ACL can access layout', () => {
const app = { id: 'app-456', mode: AppModeEnum.AGENT_CHAT, permission_keys: [AppACLPermission.ViewLayout] }
const result = getRedirectionPath(app)
expect(result).toBe('/app/app-456/configuration')
})
it('handles different app IDs', () => {
const app1 = { id: 'abc-123', mode: AppModeEnum.CHAT, permission_keys: [] }
const app2 = { id: 'xyz-789', mode: AppModeEnum.WORKFLOW, permission_keys: [AppACLPermission.ViewLayout] }
expect(getRedirectionPath(app1)).toBe('/app/abc-123/overview')
expect(getRedirectionPath(app2)).toBe('/app/xyz-789/workflow')
})
})
/**
* Tests getRedirection which combines path generation with a redirect callback
*/
describe('getRedirection', () => {
/**
* Tests that the redirection function is called with the correct path
*/
it('calls redirection function with overview path when app ACL cannot access layout', () => {
const app = { id: 'app-123', mode: AppModeEnum.CHAT, permission_keys: [] }
const mockRedirect = vi.fn()
getRedirection(app, mockRedirect)
expect(mockRedirect).toHaveBeenCalledWith('/app/app-123/overview')
expect(mockRedirect).toHaveBeenCalledTimes(1)
})
it('calls redirection function with workflow path when app ACL can access layout', () => {
const app = { id: 'app-123', mode: AppModeEnum.WORKFLOW, permission_keys: [AppACLPermission.ViewLayout] }
const mockRedirect = vi.fn()
getRedirection(app, mockRedirect)
expect(mockRedirect).toHaveBeenCalledWith('/app/app-123/workflow')
expect(mockRedirect).toHaveBeenCalledTimes(1)
})
it('calls redirection function with configuration path for chat mode with layout access', () => {
const app = { id: 'app-123', mode: AppModeEnum.CHAT, permission_keys: [AppACLPermission.ViewLayout] }
const mockRedirect = vi.fn()
getRedirection(app, mockRedirect)
expect(mockRedirect).toHaveBeenCalledWith('/app/app-123/configuration')
expect(mockRedirect).toHaveBeenCalledTimes(1)
})
it('works with different redirection functions', () => {
const app = { id: 'app-123', mode: AppModeEnum.WORKFLOW, permission_keys: [AppACLPermission.ViewLayout] }
const paths: string[] = []
const customRedirect = (path: string) => paths.push(path)
getRedirection(app, customRedirect)
expect(paths).toEqual(['/app/app-123/workflow'])
})
})
})