fix(department): handle multi-org user in single-department transparent test

CI test user has 2 orgs, so the router guard redirects to /org/select
before departments can be loaded. Added loginAndDismissSelectors helper
that clicks through org selection then role selection before navigating
to /clazz.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-02 20:09:21 +08:00
parent 5a1d903ffc
commit bfbf1e91df
+49 -57
View File
@@ -3,54 +3,65 @@ import { expect, test } from './fixtures';
/**
* 部门默认透明测试
*
* 当前测试服(moicen)两组机构(慧添翼 / 慧正书法)都只有 1 个 active 部门(默认部门),
* 测试 CI unionid 测试用户,其所有机构都只有 1 个 active 部门(默认部门),
* 因此前端不应出现部门选择入口:
* - localStorage 自动写入 CurrentDepartmentId
* - 页面路径不包含 department 参数
* - 角色选择、排课列表、课包商店等已有页面不受影响
*
* 注意:CI 用户可能有多机构,需要先选机构,再触发部门自动选中。
*/
const moicenUnionid = process.env.MOICEN_E2E_UNIONID?.trim();
/**
* 登录 → 依次处理机构选择 + 身份选择 → 到达首页
* 适用于多机构用户的场景
*/
async function loginAndDismissSelectors(page: any) {
const q = new URLSearchParams({ unionid: moicenUnionid!, status: '2' });
await page.goto(`/?${q.toString()}`, {
waitUntil: 'domcontentloaded',
timeout: 60_000,
});
await expect(page.locator('#app')).toBeVisible({ timeout: 60_000 });
// 等待 auth tokens
await expect(async () => {
const auth = await page.evaluate(() =>
window.localStorage.getItem('Authorization')
);
expect(auth).toBeTruthy();
}).toPass({ timeout: 30_000 });
// 逐层处理选择页:机构选择 → 身份选择(可能有 0~n 层)
for (let i = 0; i < 5; i++) {
const path = await page.evaluate(() => window.location.pathname);
console.log(`[DeptTest] iteration ${i}: path=${path}`);
if (path === '/org/select') {
await page.locator('.van-cell').first().click();
await page.waitForTimeout(3_000);
} else if (path === '/') {
const rs = page.getByText('请选择您的登录身份');
if (await rs.isVisible().catch(() => false)) {
await page.locator('.van-grid-item').first().click();
await page.waitForTimeout(3_000);
} else {
break;
}
} else {
break;
}
}
}
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,
});
test.setTimeout(120_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);
}
await loginAndDismissSelectors(page);
// 导航到排课页触发路由守卫中的 org 加载 + 部门自动选中
await page.goto('/clazz', {
@@ -67,6 +78,7 @@ test.describe('单部门透明', () => {
const deptId = await page.evaluate(() =>
window.localStorage.getItem('CurrentDepartmentId')
);
console.log('[DeptTest] CurrentDepartmentId:', deptId);
expect(deptId).toBeTruthy();
expect(deptId).toContain('dept_default_');
@@ -84,7 +96,6 @@ test.describe('单部门透明', () => {
expect(parts.length).toBe(3);
const payloadRaw = parts[1].replace(/-/g, '+').replace(/_/g, '/');
const payload = JSON.parse(atob(payloadRaw));
// 打印 JWT 结构用于调试
console.log('[JWT Debug] top-level keys:', Object.keys(payload));
console.log('[JWT Debug] payload.current_department_id:', payload.current_department_id);
console.log('[JWT Debug] payload.current_org_id:', payload.current_org_id);
@@ -94,7 +105,6 @@ test.describe('单部门透明', () => {
console.log('[JWT Debug] sub keys:', Object.keys(subjectPayload));
console.log('[JWT Debug] sub.current_department_id:', subjectPayload.current_department_id);
}
// 兼容 top-level 和 sub 两个位置
const deptIdInJwt = payload.current_department_id || subjectPayload.current_department_id;
expect(deptIdInJwt).toBeTruthy();
expect(typeof deptIdInJwt).toBe('string');
@@ -102,25 +112,7 @@ test.describe('单部门透明', () => {
});
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();
}
await loginAndDismissSelectors(page);
// 导航到排课页触发 org 加载 + 部门自动选中,验证页面正常渲染
await page.goto('/clazz', {