dify/cli/src/workspace/resolver.test.ts
Stephen Zhou a84c2d36a3
style: format with vp fmt (#38803)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-07-12 15:57:46 +00:00

39 lines
1.4 KiB
TypeScript

import type { ActiveContext } from '@/auth/hosts'
import { describe, expect, it } from 'vitest'
import { resolveWorkspaceId } from './resolver'
function active(workspaceId?: string): ActiveContext {
return {
host: 'h',
email: 'e',
ctx: {
account: { id: '', email: 'e', name: '' },
workspace: workspaceId ? { id: workspaceId, name: 'W', role: 'owner' } : undefined,
},
}
}
const UUID_FLAG = 'aaaaaaaa-0000-0000-0000-000000000001'
const UUID_ENV = 'aaaaaaaa-0000-0000-0000-000000000002'
const UUID_CTX = 'aaaaaaaa-0000-0000-0000-000000000003'
describe('resolveWorkspaceId', () => {
it('prefers the flag', () => {
expect(resolveWorkspaceId({ flag: UUID_FLAG, env: UUID_ENV, active: active(UUID_CTX) })).toBe(
UUID_FLAG,
)
})
it('falls back to env over active workspace', () => {
expect(resolveWorkspaceId({ env: UUID_ENV, active: active(UUID_CTX) })).toBe(UUID_ENV)
})
it('falls back to active workspace when no flag or env', () => {
expect(resolveWorkspaceId({ active: active(UUID_CTX) })).toBe(UUID_CTX)
})
it('throws when active workspace ID is not a valid UUID', () => {
expect(() => resolveWorkspaceId({ active: active('ws-ctx') })).toThrow(/stored workspace ID/)
})
it('throws when no workspace is selected (no implicit default)', () => {
expect(() => resolveWorkspaceId({ active: active(undefined) })).toThrow(/no workspace selected/)
})
})