Files
huike-e2e-moicen/tests/department-single-transparent.spec.ts
T

125 lines
4.1 KiB
TypeScript
Raw Normal View History

import { expect, test } from './fixtures';
/**
* 部门默认透明测试
*
* 当前测试服(moicen)两组机构(慧添翼 / 慧正书法)都只有 1 个 active 部门(默认部门),
* 因此前端不应出现部门选择入口:
* - localStorage 自动写入 CurrentDepartmentId
* - 页面路径不包含 department 参数
* - 角色选择、排课列表、课包商店等已有页面不受影响
*/
const moicenUnionid = process.env.MOICEN_E2E_UNIONID?.trim();
test.describe('单部门透明', () => {
test.skip(!moicenUnionid, '需要 MOICEN_E2E_UNIONIDSecret 或 .env.e2e');
test('自动选中默认部门,不出现部门选择 UI', async ({ page }) => {
const queryString = new URLSearchParams({
unionid: moicenUnionid!,
status: '2',
});
await page.goto(`/?${queryString.toString()}`, {
waitUntil: 'domcontentloaded',
timeout: 60_000,
});
await expect(page.locator('#app')).toBeVisible({ timeout: 60_000 });
// 等待登录完成(Authorization + HtySudoerToken 写入 localStorage
// 避免在第一个页面的路由守卫未跑完时直接 full navigation 导致 token 丢失
await expect(async () => {
const auth = await page.evaluate(() =>
window.localStorage.getItem('Authorization')
);
expect(auth).toBeTruthy();
}).toPass({ timeout: 30_000 });
await expect(async () => {
const sudo = await page.evaluate(() =>
window.localStorage.getItem('HtySudoerToken')
);
expect(sudo).toBeTruthy();
}).toPass({ timeout: 15_000 });
// 身份选择
if (
await page
.getByText('请选择您的登录身份')
.isVisible()
.catch(() => false)
) {
await page.locator('.van-grid-item').first().click();
await page.waitForTimeout(2_000);
}
// 导航到排课页触发路由守卫中的 org 加载 + 部门自动选中
await page.goto('/clazz', {
waitUntil: 'domcontentloaded',
timeout: 60_000,
});
await expect(page.locator('#app')).toBeVisible({ timeout: 60_000 });
// 等待异步的部门加载完成
await page.waitForTimeout(3_000);
// 验证 localStorage 写入了 CurrentDepartmentId
const deptId = await page.evaluate(() =>
window.localStorage.getItem('CurrentDepartmentId')
);
expect(deptId).toBeTruthy();
expect(deptId).toContain('dept_default_');
// 验证页面路径不包含 department 相关参数
const pathname = await page.evaluate(() => window.location.pathname);
expect(pathname).not.toContain('department');
// 验证 token 中包含 department_id
const authToken = await page.evaluate(() =>
window.localStorage.getItem('Authorization')
);
expect(authToken).toBeTruthy();
if (authToken) {
const parts = authToken.split('.');
expect(parts.length).toBe(3);
const payloadRaw = parts[1].replace(/-/g, '+').replace(/_/g, '/');
const payload = JSON.parse(atob(payloadRaw));
let subjectPayload: Record<string, unknown> = {};
if (typeof payload.sub === 'string') {
subjectPayload = JSON.parse(payload.sub);
}
expect(subjectPayload.current_department_id).toBeTruthy();
expect(typeof subjectPayload.current_department_id).toBe('string');
}
});
test('已有部门上下文时不重复加载部门列表', async ({ page }) => {
const queryString = new URLSearchParams({
unionid: moicenUnionid!,
status: '2',
});
await page.goto(`/?${queryString.toString()}`, {
waitUntil: 'domcontentloaded',
timeout: 60_000,
});
await expect(page.locator('#app')).toBeVisible({ timeout: 60_000 });
if (
await page
.getByText('请选择您的登录身份')
.isVisible()
.catch(() => false)
) {
await page.locator('.van-grid-item').first().click();
}
// 导航到排课页触发 org 加载 + 部门自动选中,验证页面正常渲染
await page.goto('/clazz', {
waitUntil: 'domcontentloaded',
timeout: 60_000,
});
await expect(page.locator('#app')).toBeVisible({ timeout: 60_000 });
});
});