refactor: 迁移shared单元测试到src目录

This commit is contained in:
张翔
2026-04-05 09:11:29 +08:00
parent 993753bf4f
commit e63a23a4ad
3 changed files with 0 additions and 0 deletions
@@ -0,0 +1,69 @@
import { describe, it, expect, vi } from 'vitest';
import { PermissionHelper } from '../permission-helper';
import type { RoleDefinition } from '../../roles/base.role';
// Mock Playwright
vi.mock('@playwright/test', () => ({
expect: Object.assign(vi.fn(), {
extend: vi.fn().mockReturnValue(expect),
}),
}));
describe('PermissionHelper', () => {
it('should create PermissionHelper instance', () => {
const mockPage = {
goto: vi.fn(),
url: vi.fn().mockReturnValue('http://localhost:3000/dashboard'),
locator: vi.fn().mockReturnValue({
count: vi.fn().mockResolvedValue(0),
}),
} as any;
const helper = new PermissionHelper(mockPage);
expect(helper).toBeDefined();
});
it('should have verifyCanAccess method', () => {
const mockPage = {
goto: vi.fn(),
url: vi.fn().mockReturnValue('http://localhost:3000/dashboard'),
locator: vi.fn(),
} as any;
const helper = new PermissionHelper(mockPage);
expect(typeof helper.verifyCanAccess).toBe('function');
});
it('should have verifyCannotAccess method', () => {
const mockPage = {
goto: vi.fn(),
url: vi.fn(),
locator: vi.fn(),
} as any;
const helper = new PermissionHelper(mockPage);
expect(typeof helper.verifyCannotAccess).toBe('function');
});
it('should have verifyRolePermissions method', () => {
const mockPage = {
goto: vi.fn(),
url: vi.fn(),
locator: vi.fn(),
} as any;
const helper = new PermissionHelper(mockPage);
expect(typeof helper.verifyRolePermissions).toBe('function');
});
it('should have verifyPermissionBoundary method', () => {
const mockPage = {
goto: vi.fn(),
url: vi.fn(),
locator: vi.fn(),
} as any;
const helper = new PermissionHelper(mockPage);
expect(typeof helper.verifyPermissionBoundary).toBe('function');
});
});
@@ -0,0 +1,79 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { RoleAuthManager } from '../role-auth-manager';
// Mock fetch
global.fetch = vi.fn();
describe('RoleAuthManager', () => {
beforeEach(() => {
RoleAuthManager.clearCache();
vi.clearAllMocks();
});
it('should authenticate and cache token', async () => {
const mockToken = 'mock-jwt-token-12345';
(global.fetch as any).mockResolvedValueOnce({
ok: true,
json: async () => ({ data: { token: mockToken } })
});
const token = await RoleAuthManager.getRoleToken('admin');
expect(token).toBe(mockToken);
expect(global.fetch).toHaveBeenCalledWith(
expect.stringContaining('/api/auth/login'),
expect.objectContaining({
method: 'POST',
body: expect.stringContaining('admin')
})
);
});
it('should return cached token on second call', async () => {
const mockToken = 'cached-token';
(global.fetch as any).mockResolvedValueOnce({
ok: true,
json: async () => ({ data: { token: mockToken } })
});
const token1 = await RoleAuthManager.getRoleToken('admin');
const token2 = await RoleAuthManager.getRoleToken('admin');
expect(token1).toBe(token2);
expect(global.fetch).toHaveBeenCalledTimes(1);
});
it('should throw error for unknown role', async () => {
await expect(RoleAuthManager.getRoleToken('unknown')).rejects.toThrow("Role 'unknown' not found");
});
it('should throw error on authentication failure', async () => {
(global.fetch as any).mockResolvedValueOnce({
ok: false,
statusText: 'Unauthorized'
});
await expect(RoleAuthManager.getRoleToken('admin')).rejects.toThrow('Authentication failed');
});
it('should clear specific role token', async () => {
const mockToken = 'token-to-clear';
(global.fetch as any).mockResolvedValueOnce({
ok: true,
json: async () => ({ data: { token: mockToken } })
});
await RoleAuthManager.getRoleToken('admin');
RoleAuthManager.clearRoleToken('admin');
// 再次获取应该重新认证
(global.fetch as any).mockResolvedValueOnce({
ok: true,
json: async () => ({ data: { token: 'new-token' } })
});
const newToken = await RoleAuthManager.getRoleToken('admin');
expect(newToken).toBe('new-token');
expect(global.fetch).toHaveBeenCalledTimes(2);
});
});
@@ -0,0 +1,117 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { TestDataManager, getTestDataManager } from '../test-data-manager';
global.fetch = vi.fn();
describe('TestDataManager', () => {
let manager: TestDataManager;
beforeEach(() => {
manager = TestDataManager.getInstance();
manager.clearTracking();
vi.clearAllMocks();
});
it('should be a singleton', () => {
const instance1 = getTestDataManager();
const instance2 = getTestDataManager();
expect(instance1).toBe(instance2);
});
it('should create user and track it', async () => {
const mockUserId = 'user-123';
(global.fetch as any).mockResolvedValueOnce({
ok: true,
json: async () => ({ data: { id: mockUserId } })
});
const userData = {
username: 'testuser',
password: 'Test@123',
email: 'test@example.com',
};
const result = await manager.createUser(userData);
expect(result.id).toBe(mockUserId);
expect(result.type).toBe('user');
expect(result.data.username).toBe('testuser');
expect(manager.getCreatedData('user')).toHaveLength(1);
});
it('should create role and track it', async () => {
const mockRoleId = 'role-456';
(global.fetch as any).mockResolvedValueOnce({
ok: true,
json: async () => ({ data: { id: mockRoleId } })
});
const roleData = {
roleName: '测试角色',
roleKey: 'test_role',
};
const result = await manager.createRole(roleData);
expect(result.id).toBe(mockRoleId);
expect(result.type).toBe('role');
expect(manager.getCreatedData('role')).toHaveLength(1);
});
it('should cleanup created data', async () => {
(global.fetch as any)
.mockResolvedValueOnce({
ok: true,
json: async () => ({ data: { id: 'user-1' } })
})
.mockResolvedValueOnce({
ok: true,
json: async () => ({ data: { id: 'user-2' } })
})
.mockResolvedValueOnce({ ok: true })
.mockResolvedValueOnce({ ok: true });
await manager.createUser({ username: 'user1', password: 'Test@123', email: 'user1@test.com' });
await manager.createUser({ username: 'user2', password: 'Test@123', email: 'user2@test.com' });
expect(manager.getCreatedData('user')).toHaveLength(2);
await manager.cleanup('user');
expect(manager.getCreatedData('user')).toHaveLength(0);
expect(global.fetch).toHaveBeenCalledTimes(4); // 2 creates + 2 deletes
});
it('should cleanup all data types when no type specified', async () => {
(global.fetch as any)
.mockResolvedValueOnce({
ok: true,
json: async () => ({ data: { id: 'user-1' } })
})
.mockResolvedValueOnce({
ok: true,
json: async () => ({ data: { id: 'role-1' } })
})
.mockResolvedValueOnce({ ok: true })
.mockResolvedValueOnce({ ok: true });
await manager.createUser({ username: 'user1', password: 'Test@123', email: 'user1@test.com' });
await manager.createRole({ roleName: '角色1', roleKey: 'role1' });
await manager.cleanup();
expect(manager.getCreatedData('user')).toHaveLength(0);
expect(manager.getCreatedData('role')).toHaveLength(0);
});
it('should throw error on creation failure', async () => {
(global.fetch as any).mockResolvedValueOnce({
ok: false,
statusText: 'Bad Request'
});
await expect(
manager.createUser({ username: 'test', password: 'Test@123', email: 'test@test.com' })
).rejects.toThrow('Failed to create user');
});
});