Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

25.9 KB
Raw Blame
import type { AccountWithRoleResponse } from '@dify/contracts/api/console/workspaces/types.gen'
import type { AccessPolicyWithBindings, ResourceUserAccessSetting } from '@/models/access-control'
import { act, screen, waitFor, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { renderWithConsoleQuery as render } from '@/test/console/query-data'
import AccessRulesEditor from '../index'

const mockMembers = vi.hoisted(() => ({
  accounts: [] as AccountWithRoleResponse[] | null,
}))
const mockMembersQuery = vi.hoisted(() => vi.fn())

const createRule = (resourceType: 'app' | 'dataset'): AccessPolicyWithBindings => ({
  policy: {
    id: `${resourceType}-policy-id`,
    tenant_id: 'tenant-id',
    resource_type: resourceType,
    policy_key: `${resourceType}-policy-key`,
    name: `${resourceType} policy`,
    description: `${resourceType} policy description`,
    permission_keys: [],
    is_builtin: false,
    category: 'global_custom',
    created_at: '2026-05-22T00:00:00Z',
    updated_at: '2026-05-22T00:00:00Z',
  },
  roles: [],
  accounts: [],
})

const createUserAccessSetting = (
  resourceType: 'app' | 'dataset' = 'app',
): ResourceUserAccessSetting => ({
  account: {
    account_id: 'account-1',
    account_name: 'Evan',
    email: '[email protected]',
  },
  roles: [
    {
      id: 'role-1',
      type: 'app',
      category: 'global_custom',
      name: 'Maintainer',
      is_builtin: false,
      permission_keys: [],
      role_tag: '',
    },
  ],
  access_policies: [
    {
      id: `${resourceType}-policy-id`,
      tenant_id: 'tenant-id',
      resource_type: resourceType,
      policy_key: `${resourceType}-policy-key`,
      name: 'Manage',
      description: 'Can manage this app',
      permission_keys: [],
      is_builtin: false,
      category: 'global_custom',
    },
  ],
})

const createDefaultUserAccessSetting = (): ResourceUserAccessSetting => ({
  ...createUserAccessSetting(),
  access_policies: [],
})

const createMember = (overrides: Partial<AccountWithRoleResponse> = {}): AccountWithRoleResponse =>
  ({
    id: 'account-2',
    name: 'Mia',
    email: '[email protected]',
    avatar: '',
    avatar_url: '',
    status: 'active',
    role: 'normal',
    roles: [],
    ...overrides,
  }) as AccountWithRoleResponse

describe('AccessRulesEditor', () => {
  beforeEach(() => {
    vi.clearAllMocks()
    mockMembers.accounts = []
    mockMembersQuery.mockImplementation(() => Promise.resolve({ accounts: mockMembers.accounts }))
  })

  it('should render loading state before empty or row content', () => {
    render(
      <AccessRulesEditor
        rules={[]}
        userAccessSettings={[]}
        isLoadingRules
        isLoadingUserAccessSettings={false}
        automaticIncludeWorkspaceMembers={false}
        isUpdatingAutomaticIncludeWorkspaceMembers={false}
        updatingAccountId={null}
      />,
    )

    expect(screen.getByRole('progressbar', { name: 'common.loading' })).toBeInTheDocument()
    expect(screen.queryByText('permission.accessRule.noUserAccessSettings')).not.toBeInTheDocument()
  })

  it('should disable automatic inclusion before its value is available', () => {
    render(
      <AccessRulesEditor
        rules={[]}
        userAccessSettings={[]}
        isLoadingRules={false}
        isLoadingUserAccessSettings={false}
        isUpdatingAutomaticIncludeWorkspaceMembers={false}
        updatingAccountId={null}
      />,
    )

    expect(
      screen.getByText('permission.accessRule.automaticallyIncludeWorkspaceMembersDescription'),
    ).toBeInTheDocument()
    const automaticInclusionSwitch = screen.getByRole('switch', {
      name: 'permission.accessRule.automaticallyIncludeWorkspaceMembers',
    })
    expect(automaticInclusionSwitch).toHaveAttribute('aria-disabled', 'true')
    expect(automaticInclusionSwitch).toHaveAttribute('aria-checked', 'false')
  })

  it('should toggle automatic inclusion and update account exceptions', async () => {
    const user = userEvent.setup()
    const onAutomaticIncludeWorkspaceMembersChange = vi.fn()
    const onUserAccessPoliciesChange = vi.fn()
    const onRemoveAccessPolicyMemberBinding = vi.fn()

    render(
      <AccessRulesEditor
        rules={[createRule('app')]}
        userAccessSettings={[createUserAccessSetting()]}
        isLoadingRules={false}
        isLoadingUserAccessSettings={false}
        automaticIncludeWorkspaceMembers={false}
        isUpdatingAutomaticIncludeWorkspaceMembers={false}
        updatingAccountId={null}
        onAutomaticIncludeWorkspaceMembersChange={onAutomaticIncludeWorkspaceMembersChange}
        onUserAccessPoliciesChange={onUserAccessPoliciesChange}
        onRemoveAccessPolicyMemberBinding={onRemoveAccessPolicyMemberBinding}
      />,
    )

    expect(screen.getByText('permission.accessRule.allowedMembers')).toBeInTheDocument()
    expect(screen.getByText('Evan')).toBeInTheDocument()
    expect(screen.getByText('[email protected]')).toBeInTheDocument()
    expect(screen.queryByText('Maintainer')).not.toBeInTheDocument()
    expect(screen.getAllByText('Manage').length).toBeGreaterThan(0)

    await user.click(
      screen.getByRole('switch', {
        name: 'permission.accessRule.automaticallyIncludeWorkspaceMembers',
      }),
    )
    expect(onAutomaticIncludeWorkspaceMembersChange).toHaveBeenCalledWith(true)

    await user.click(screen.getByRole('button', { name: 'common.operation.remove' }))
    expect(onRemoveAccessPolicyMemberBinding).toHaveBeenCalledWith('account-1', 'app-policy-id')
  })

  it('should render and remove the default access policy', async () => {
    const user = userEvent.setup()
    const onRemoveAccessPolicyMemberBinding = vi.fn()

    render(
      <AccessRulesEditor
        rules={[createRule('app')]}
        userAccessSettings={[createDefaultUserAccessSetting()]}
        isLoadingRules={false}
        isLoadingUserAccessSettings={false}
        automaticIncludeWorkspaceMembers={false}
        isUpdatingAutomaticIncludeWorkspaceMembers={false}
        updatingAccountId={null}
        onRemoveAccessPolicyMemberBinding={onRemoveAccessPolicyMemberBinding}
      />,
    )

    expect(screen.getByText('permission.accessRule.defaultPermission')).toBeInTheDocument()
    await user.click(screen.getByRole('button', { name: 'common.operation.remove' }))
    expect(onRemoveAccessPolicyMemberBinding).toHaveBeenCalledWith('account-1', 'default')
  })

  it('should disable membership changes while automatic inclusion is enabled', () => {
    render(
      <AccessRulesEditor
        rules={[createRule('app')]}
        userAccessSettings={[createUserAccessSetting()]}
        isLoadingRules={false}
        isLoadingUserAccessSettings={false}
        automaticIncludeWorkspaceMembers
        isUpdatingAutomaticIncludeWorkspaceMembers={false}
        existingAccountIds={['account-1']}
        updatingAccountId={null}
        onUserAccessPoliciesChange={vi.fn()}
        onRemoveAccessPolicyMemberBinding={vi.fn()}
        onAddAccessSubject={vi.fn()}
      />,
    )

    expect(screen.getByRole('button', { name: 'common.operation.add' })).toBeDisabled()
    expect(screen.getByRole('checkbox', { name: 'common.operation.selectAll' })).toHaveAttribute(
      'aria-disabled',
      'true',
    )
    expect(screen.getByRole('checkbox', { name: 'Evan' })).toHaveAttribute('aria-disabled', 'true')
    expect(screen.getByRole('button', { name: 'common.operation.remove' })).toBeDisabled()
    expect(
      screen.getByLabelText(/permission\.accessRule\.exceptionPermissionFor/),
    ).not.toBeDisabled()
  })

  it('should mark maintainer rows and prevent editing them', async () => {
    const user = userEvent.setup()
    const onUserAccessPoliciesChange = vi.fn()
    const onRemoveAccessPolicyMemberBinding = vi.fn()

    render(
      <AccessRulesEditor
        rules={[createRule('app')]}
        userAccessSettings={[createUserAccessSetting()]}
        isLoadingRules={false}
        isLoadingUserAccessSettings={false}
        automaticIncludeWorkspaceMembers={false}
        isUpdatingAutomaticIncludeWorkspaceMembers={false}
        updatingAccountId={null}
        maintainerId="account-1"
        onUserAccessPoliciesChange={onUserAccessPoliciesChange}
        onRemoveAccessPolicyMemberBinding={onRemoveAccessPolicyMemberBinding}
      />,
    )

    expect(screen.getByText('permission.accessRule.maintainer')).toBeInTheDocument()
    expect(screen.getByRole('checkbox', { name: 'Evan' })).toHaveAttribute('aria-disabled', 'true')
    expect(screen.getByLabelText(/permission\.accessRule\.exceptionPermissionFor/)).toBeDisabled()

    const removeButton = screen.getByRole('button', { name: 'common.operation.remove' })
    expect(removeButton).toBeDisabled()

    await user.click(removeButton)
    expect(onUserAccessPoliciesChange).not.toHaveBeenCalled()
    expect(onRemoveAccessPolicyMemberBinding).not.toHaveBeenCalled()
  })

  it.each(['app', 'dataset'] as const)(
    'should prevent editing or removing the workspace owner from %s access',
    async (resourceType) => {
      const user = userEvent.setup()
      mockMembers.accounts = [
        createMember({
          id: 'workspace-owner',
          name: 'Workspace Owner',
          email: '[email protected]',
        }),
        createMember(),
      ]
      const ownerSetting: ResourceUserAccessSetting = {
        ...createUserAccessSetting(resourceType),
        account: {
          account_id: 'workspace-owner',
          account_name: 'Workspace Owner',
          email: '[email protected]',
        },
        roles: [
          {
            id: 'owner-role',
            type: 'workspace',
            category: 'global_system_default',
            name: 'Owner',
            is_builtin: true,
            permission_keys: [],
            role_tag: 'owner',
          },
        ],
      }
      const memberSetting: ResourceUserAccessSetting = {
        ...createDefaultUserAccessSetting(),
        account: {
          account_id: 'account-2',
          account_name: 'Mia',
          email: '[email protected]',
        },
      }
      const onUserAccessPoliciesChange = vi.fn()
      const onRemoveAccessPolicyMemberBinding = vi.fn()
      const onBatchRemoveAccessPolicyMemberBindings = vi.fn().mockResolvedValue(undefined)
      const onAddAccessSubject = vi.fn()

      render(
        <AccessRulesEditor
          rules={[createRule(resourceType)]}
          userAccessSettings={[ownerSetting, memberSetting]}
          isLoadingRules={false}
          isLoadingUserAccessSettings={false}
          automaticIncludeWorkspaceMembers={false}
          isUpdatingAutomaticIncludeWorkspaceMembers={false}
          existingAccountIds={[]}
          updatingAccountId={null}
          onUserAccessPoliciesChange={onUserAccessPoliciesChange}
          onRemoveAccessPolicyMemberBinding={onRemoveAccessPolicyMemberBinding}
          onBatchRemoveAccessPolicyMemberBindings={onBatchRemoveAccessPolicyMemberBindings}
          onAddAccessSubject={onAddAccessSubject}
        />,
      )

      const ownerRow = screen.getByRole('row', { name: /Workspace Owner/ })
      expect(within(ownerRow).getByText('permission.accessRule.workspaceOwner')).toBeInTheDocument()
      expect(within(ownerRow).getByRole('checkbox', { name: 'Workspace Owner' })).toHaveAttribute(
        'aria-disabled',
        'true',
      )
      expect(
        within(ownerRow).getByLabelText(/permission\.accessRule\.exceptionPermissionFor/),
      ).toBeDisabled()
      expect(
        within(ownerRow).getByRole('button', { name: 'common.operation.remove' }),
      ).toBeDisabled()

      await user.click(screen.getByRole('checkbox', { name: 'common.operation.selectAll' }))
      expect(screen.getByRole('checkbox', { name: 'Workspace Owner' })).not.toBeChecked()
      expect(screen.getByRole('checkbox', { name: 'Mia' })).toBeChecked()

      await user.click(screen.getByRole('button', { name: 'common.operation.delete' }))
      const dialog = screen.getByRole('alertdialog', {
        name: 'permission.accessRule.batchRemoveTitle',
      })
      await user.click(within(dialog).getByRole('button', { name: 'common.operation.sure' }))

      expect(onBatchRemoveAccessPolicyMemberBindings).toHaveBeenCalledWith([
        { accessPolicyId: 'default', accountIds: ['account-2'] },
      ])
      expect(onUserAccessPoliciesChange).not.toHaveBeenCalled()
      expect(onRemoveAccessPolicyMemberBinding).not.toHaveBeenCalled()

      await user.click(screen.getByRole('button', { name: 'common.operation.add' }))
      const addDialog = await screen.findByRole('dialog', {
        name: 'permission.accessRule.addMembersTitle',
      })
      expect(within(addDialog).getByText('Workspace Owner')).toBeInTheDocument()
      const ownerItem = within(addDialog).getByRole('menuitem', { name: /Workspace Owner/ })
      expect(ownerItem).toHaveAttribute('aria-disabled', 'true')
      expect(ownerItem).toHaveTextContent('common.operation.added')
      expect(ownerItem).not.toHaveTextContent('+ common.operation.add')
      expect(onAddAccessSubject).not.toHaveBeenCalled()
    },
  )

  it.each([
    ['app', 'admin', 'Workspace Admin'],
    ['dataset', '', 'admin'],
  ] as const)(
    'should identify and protect a workspace admin in %s access',
    async (resourceType, roleTag, roleName) => {
      const user = userEvent.setup()
      const adminSetting: ResourceUserAccessSetting = {
        ...createUserAccessSetting(resourceType),
        account: {
          account_id: 'workspace-admin',
          account_name: 'Workspace Admin',
          email: '[email protected]',
        },
        roles: [
          {
            id: 'admin-role',
            type: 'workspace',
            category: 'global_system_default',
            name: roleName,
            is_builtin: true,
            permission_keys: [],
            role_tag: roleTag,
          },
        ],
      }
      const memberSetting: ResourceUserAccessSetting = {
        ...createDefaultUserAccessSetting(),
        account: {
          account_id: 'account-2',
          account_name: 'Mia',
          email: '[email protected]',
        },
      }
      const onUserAccessPoliciesChange = vi.fn()
      const onRemoveAccessPolicyMemberBinding = vi.fn()
      const onBatchRemoveAccessPolicyMemberBindings = vi.fn().mockResolvedValue(undefined)

      render(
        <AccessRulesEditor
          rules={[createRule(resourceType)]}
          userAccessSettings={[adminSetting, memberSetting]}
          isLoadingRules={false}
          isLoadingUserAccessSettings={false}
          automaticIncludeWorkspaceMembers={false}
          isUpdatingAutomaticIncludeWorkspaceMembers={false}
          updatingAccountId={null}
          onUserAccessPoliciesChange={onUserAccessPoliciesChange}
          onRemoveAccessPolicyMemberBinding={onRemoveAccessPolicyMemberBinding}
          onBatchRemoveAccessPolicyMemberBindings={onBatchRemoveAccessPolicyMemberBindings}
        />,
      )

      const adminRow = screen.getByRole('row', { name: /Workspace Admin/ })
      expect(within(adminRow).getByText('permission.accessRule.workspaceAdmin')).toBeInTheDocument()
      expect(within(adminRow).getByRole('checkbox', { name: 'Workspace Admin' })).toHaveAttribute(
        'aria-disabled',
        'true',
      )
      expect(
        within(adminRow).getByLabelText(/permission\.accessRule\.exceptionPermissionFor/),
      ).toBeDisabled()
      expect(
        within(adminRow).getByRole('button', { name: 'common.operation.remove' }),
      ).toBeDisabled()

      await user.click(screen.getByRole('checkbox', { name: 'common.operation.selectAll' }))
      expect(screen.getByRole('checkbox', { name: 'Workspace Admin' })).not.toBeChecked()
      expect(screen.getByRole('checkbox', { name: 'Mia' })).toBeChecked()

      await user.click(screen.getByRole('button', { name: 'common.operation.delete' }))
      const dialog = screen.getByRole('alertdialog', {
        name: 'permission.accessRule.batchRemoveTitle',
      })
      await user.click(within(dialog).getByRole('button', { name: 'common.operation.sure' }))

      expect(onBatchRemoveAccessPolicyMemberBindings).toHaveBeenCalledWith([
        { accessPolicyId: 'default', accountIds: ['account-2'] },
      ])
      expect(onUserAccessPoliciesChange).not.toHaveBeenCalled()
      expect(onRemoveAccessPolicyMemberBinding).not.toHaveBeenCalled()
    },
  )

  it('should navigate through member pages and change the page size', async () => {
    const user = userEvent.setup()
    const onPageChange = vi.fn()
    const onPageSizeChange = vi.fn()

    render(
      <AccessRulesEditor
        rules={[]}
        userAccessSettings={[]}
        isLoadingRules={false}
        isLoadingUserAccessSettings={false}
        automaticIncludeWorkspaceMembers={false}
        isUpdatingAutomaticIncludeWorkspaceMembers={false}
        currentPage={1}
        pageSize={10}
        totalPages={3}
        updatingAccountId={null}
        onPageChange={onPageChange}
        onPageSizeChange={onPageSizeChange}
      />,
    )

    await user.click(screen.getByRole('button', { name: 'common.pagination.next' }))
    expect(onPageChange).toHaveBeenCalledWith(2)

    expect(screen.getByRole('radio', { name: '10' })).toBeChecked()
    await user.click(screen.getByRole('radio', { name: '25' }))
    expect(onPageSizeChange).toHaveBeenCalledWith(25)
  })

  it('should load members only after opening the add-member popover', async () => {
    const user = userEvent.setup()
    const onAddAccessSubject = vi.fn()
    mockMembers.accounts = [
      createMember({
        id: 'account-1',
        name: 'Evan',
        email: '[email protected]',
      }),
      createMember(),
    ]

    render(
      <AccessRulesEditor
        rules={[]}
        userAccessSettings={[createUserAccessSetting()]}
        isLoadingRules={false}
        isLoadingUserAccessSettings={false}
        automaticIncludeWorkspaceMembers={false}
        isUpdatingAutomaticIncludeWorkspaceMembers={false}
        existingAccountIds={['account-1']}
        updatingAccountId={null}
        onAddAccessSubject={onAddAccessSubject}
      />,
    )

    expect(mockMembersQuery).not.toHaveBeenCalled()
    await user.click(screen.getByRole('button', { name: 'common.operation.add' }))
    expect(mockMembersQuery).toHaveBeenCalledTimes(1)

    const dialog = await screen.findByRole('dialog', {
      name: 'permission.accessRule.addMembersTitle',
    })
    expect(await within(dialog).findByText('Evan')).toBeInTheDocument()
    const evanItem = within(dialog).getByRole('menuitem', { name: /Evan/ })
    expect(evanItem).toHaveAttribute('aria-disabled', 'true')
    expect(evanItem).toHaveTextContent('common.operation.added')
    expect(evanItem).not.toHaveTextContent('+ common.operation.add')
    expect(within(dialog).getByText('Mia')).toBeInTheDocument()
    expect(within(dialog).queryByRole('tablist')).not.toBeInTheDocument()

    const miaItem = within(dialog).getByRole('menuitem', { name: /Mia/ })
    expect(miaItem).toHaveTextContent('+ common.operation.add')
    await user.click(miaItem)

    expect(onAddAccessSubject).toHaveBeenCalledWith('account-2', ['default'])
  })

  it('should filter add-member candidates by name and email, not by the row action text', async () => {
    const user = userEvent.setup()
    mockMembers.accounts = [
      createMember({ id: 'account-3', name: 'Evan', email: '[email protected]' }),
      createMember(),
    ]

    render(
      <AccessRulesEditor
        rules={[]}
        userAccessSettings={[]}
        isLoadingRules={false}
        isLoadingUserAccessSettings={false}
        automaticIncludeWorkspaceMembers={false}
        isUpdatingAutomaticIncludeWorkspaceMembers={false}
        existingAccountIds={[]}
        updatingAccountId={null}
        onAddAccessSubject={vi.fn()}
      />,
    )

    await user.click(screen.getByRole('button', { name: 'common.operation.add' }))
    const dialog = await screen.findByRole('dialog', {
      name: 'permission.accessRule.addMembersTitle',
    })
    const search = within(dialog).getByRole('searchbox', { name: 'common.operation.search' })

    await user.type(search, 'mia@')
    expect(within(dialog).getByRole('menuitem', { name: /Mia/ })).toBeInTheDocument()
    expect(within(dialog).queryByRole('menuitem', { name: /Evan/ })).not.toBeInTheDocument()

    // Every row shows the add action, so a query for its text must not match them all.
    await user.clear(search)
    await user.type(search, 'operation.add')
    expect(within(dialog).queryByRole('menuitem')).not.toBeInTheDocument()
    expect(
      await within(dialog).findByText('permission.accessRule.noAvailableMembers'),
    ).toHaveAttribute('role', 'status')
  })

  it('should announce a failed member load without offering candidates', async () => {
    const user = userEvent.setup()
    mockMembersQuery.mockRejectedValue(new Error('Request failed'))

    render(
      <AccessRulesEditor
        rules={[]}
        userAccessSettings={[]}
        isLoadingRules={false}
        isLoadingUserAccessSettings={false}
        automaticIncludeWorkspaceMembers={false}
        isUpdatingAutomaticIncludeWorkspaceMembers={false}
        existingAccountIds={[]}
        updatingAccountId={null}
        onAddAccessSubject={vi.fn()}
      />,
    )

    await user.click(screen.getByRole('button', { name: 'common.operation.add' }))
    const dialog = await screen.findByRole('dialog', {
      name: 'permission.accessRule.addMembersTitle',
    })

    expect(await within(dialog).findByRole('alert')).toHaveTextContent('common.api.actionFailed')
    expect(within(dialog).queryByRole('menuitem')).not.toBeInTheDocument()
    expect(
      within(dialog).queryByText('permission.accessRule.noAvailableMembers'),
    ).not.toBeInTheDocument()
  })

  it('should select individual rows and all selectable rows on the current page', async () => {
    const user = userEvent.setup()
    const firstSetting = createUserAccessSetting()
    const secondSetting = {
      ...createUserAccessSetting(),
      account: {
        account_id: 'account-2',
        account_name: 'Mia',
        email: '[email protected]',
      },
    }

    render(
      <AccessRulesEditor
        rules={[createRule('app')]}
        userAccessSettings={[firstSetting, secondSetting]}
        isLoadingRules={false}
        isLoadingUserAccessSettings={false}
        automaticIncludeWorkspaceMembers={false}
        isUpdatingAutomaticIncludeWorkspaceMembers={false}
        updatingAccountId={null}
        onBatchRemoveAccessPolicyMemberBindings={vi.fn().mockResolvedValue(undefined)}
      />,
    )

    const selectAll = screen.getByRole('checkbox', { name: 'common.operation.selectAll' })
    const evan = screen.getByRole('checkbox', { name: 'Evan' })
    const mia = screen.getByRole('checkbox', { name: 'Mia' })

    await user.click(evan)
    expect(evan).toBeChecked()
    expect(selectAll).toHaveAttribute('data-indeterminate')

    await user.click(selectAll)
    expect(evan).toBeChecked()
    expect(mia).toBeChecked()
    expect(selectAll).toBeChecked()
  })

  it('should batch-remove selected members grouped by access policy', async () => {
    const user = userEvent.setup()
    let rejectRemoval!: (error: Error) => void
    const onBatchRemoveAccessPolicyMemberBindings = vi
      .fn()
      .mockImplementationOnce(
        () =>
          new Promise<void>((_resolve, reject) => {
            rejectRemoval = reject
          }),
      )
      .mockResolvedValue(undefined)
    const firstSetting = createUserAccessSetting()
    const secondSetting = {
      ...createUserAccessSetting(),
      account: {
        account_id: 'account-2',
        account_name: 'Mia',
        email: '[email protected]',
      },
    }
    const thirdSetting = {
      ...createDefaultUserAccessSetting(),
      account: {
        account_id: 'account-3',
        account_name: 'Zoe',
        email: '[email protected]',
      },
    }

    render(
      <AccessRulesEditor
        rules={[createRule('app')]}
        userAccessSettings={[firstSetting, secondSetting, thirdSetting]}
        isLoadingRules={false}
        isLoadingUserAccessSettings={false}
        automaticIncludeWorkspaceMembers={false}
        isUpdatingAutomaticIncludeWorkspaceMembers={false}
        updatingAccountId={null}
        onBatchRemoveAccessPolicyMemberBindings={onBatchRemoveAccessPolicyMemberBindings}
      />,
    )

    await user.click(screen.getByRole('checkbox', { name: 'common.operation.selectAll' }))
    expect(screen.getByText('permission.accessRule.selected')).toBeInTheDocument()

    await user.click(screen.getByRole('button', { name: 'common.operation.delete' }))
    const dialog = screen.getByRole('alertdialog', {
      name: 'permission.accessRule.batchRemoveTitle',
    })
    await user.click(within(dialog).getByRole('button', { name: 'common.operation.sure' }))

    expect(onBatchRemoveAccessPolicyMemberBindings).toHaveBeenCalledOnce()
    await user.click(within(dialog).getByRole('button', { name: 'common.operation.sure' }))
    expect(onBatchRemoveAccessPolicyMemberBindings).toHaveBeenCalledOnce()
    await act(async () => {
      rejectRemoval(new Error('Removal failed'))
    })
    expect(await within(dialog).findByRole('alert')).toHaveTextContent('common.api.actionFailed')
    expect(screen.getByRole('checkbox', { name: 'Evan', hidden: true })).toBeChecked()
    await user.click(within(dialog).getByRole('button', { name: 'common.operation.sure' }))
    await waitFor(() => expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument())
    expect(onBatchRemoveAccessPolicyMemberBindings).toHaveBeenCalledTimes(2)
    expect(onBatchRemoveAccessPolicyMemberBindings).toHaveBeenCalledWith([
      { accessPolicyId: 'app-policy-id', accountIds: ['account-1', 'account-2'] },
      { accessPolicyId: 'default', accountIds: ['account-3'] },
    ])
    expect(screen.queryByText('permission.accessRule.selected')).not.toBeInTheDocument()
  })
})

vi.mock('@/service/console', async (importOriginal) => {
  const actual = await importOriginal<typeof import('@/service/console')>()
  const { withConsoleResponses } = await import('@/test/console/service-responses')
  return {
    ...actual,
    consoleQuery: withConsoleResponses(actual.consoleQuery, {
      'workspaces.current.members.get': (input) => mockMembersQuery(input),
    }),
  }
})