import ElementPlus from 'element-plus' import { createPinia, setActivePinia } from 'pinia' import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils' import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { getPlatformUserOptions, listPlatformManagedUsers, type PlatformManagedUser, } from '@/modules/iam/api/platformIam' import { useAccessPreviewStore } from '@/core/access/accessPreview' import { useAuthStore } from '@/core/auth/auth' import InternalUsersView from './InternalUsersView.vue' vi.mock('@/modules/iam/api/platformIam', () => ({ listPlatformManagedUsers: vi.fn(), getPlatformUserOptions: vi.fn(), createPlatformManagedUser: vi.fn(), updatePlatformManagedUser: vi.fn(), changePlatformManagedUserStatus: vi.fn(), resetPlatformManagedUserPassword: vi.fn(), })) class ResizeObserverStub implements ResizeObserver { observe() {} unobserve() {} disconnect() {} } function createMemoryStorage(): Storage { const values = new Map() return { get length() { return values.size }, clear: () => values.clear(), getItem: (key) => values.get(key) ?? null, key: (index) => [...values.keys()][index] ?? null, removeItem: (key) => values.delete(key), setItem: (key, value) => values.set(key, value), } } const user: PlatformManagedUser = { membershipId: '30', userId: '20', authUserId: '10', username: 'zhangwei', displayName: '张伟', phone: '13800138000', email: 'zhangwei@example.com', tenantId: '10001', tenantName: '华远贸易', memberType: 'EMPLOYEE', memberNo: 'MEMBER-30', status: 'ACTIVE', roleIds: ['40'], roleNames: ['租户管理员'], lastLoginAt: '2026-06-01T10:30:00', protectedAccount: false, } async function mountView() { vi.stubGlobal('localStorage', createMemoryStorage()) vi.stubGlobal('sessionStorage', createMemoryStorage()) const pinia = createPinia() setActivePinia(pinia) const auth = useAuthStore() auth.token = 'token' auth.permissions = [ 'platform:user:list', 'platform:user:create', 'platform:user:edit', 'platform:user:status', 'platform:user:reset-password', 'platform:user:grant-role', ] useAccessPreviewStore().initializeAfterLogin() const wrapper = mount(InternalUsersView, { attachTo: document.body, global: { plugins: [pinia, ElementPlus] }, }) await flushPromises() return wrapper } enableAutoUnmount(afterEach) afterAll(() => vi.unstubAllGlobals()) describe('InternalUsersView unified platform user management', () => { beforeEach(() => { vi.stubGlobal('ResizeObserver', ResizeObserverStub) document.body.innerHTML = '' vi.mocked(listPlatformManagedUsers).mockReset().mockResolvedValue([user]) vi.mocked(getPlatformUserOptions).mockReset().mockResolvedValue({ tenants: [ { id: '0', name: '平台', status: 'ACTIVE' }, { id: '10001', name: '华远贸易', status: 'ACTIVE' }, ], roles: [{ id: '40', tenantId: '10001', name: '租户管理员', status: 'ACTIVE' }], }) }) it('loads and renders prototype fields from the unified API', async () => { const wrapper = await mountView() expect(listPlatformManagedUsers).toHaveBeenCalledOnce() expect(getPlatformUserOptions).toHaveBeenCalledOnce() expect(wrapper.text()).toContain('zhangwei') expect(wrapper.text()).toContain('张伟') expect(wrapper.text()).toContain('华远贸易') expect(wrapper.text()).toContain('租户管理员') expect(wrapper.text()).toContain('2026-06-01 10:30') expect(wrapper.text()).not.toContain('认证用户 ID') expect(wrapper.text()).not.toContain('成员编号') }) it('opens the complete prototype create form', async () => { const wrapper = await mountView() const createButton = wrapper.findAll('button').find((button) => button.text().trim() === '新增用户') await createButton?.trigger('click') await flushPromises() expect(document.querySelector('input[placeholder="登录用户名"]')).not.toBeNull() expect(document.querySelector('input[placeholder="真实姓名"]')).not.toBeNull() expect(document.querySelector('input[placeholder="手机号码"]')).not.toBeNull() expect(document.querySelector('input[placeholder="邮箱地址"]')).not.toBeNull() expect(document.body.textContent).toContain('所属租户') expect(document.body.textContent).toContain('初始密码') expect(document.body.textContent).toContain('确认添加') }) it('shows a recoverable API error without fake users', async () => { vi.mocked(listPlatformManagedUsers).mockRejectedValueOnce(new Error('Gateway unavailable')) const wrapper = await mountView() expect(wrapper.text()).toContain('Gateway unavailable') expect(wrapper.text()).toContain('重新加载') expect(wrapper.text()).not.toContain('zhangwei') }) })