2026-05-02 16:37:45 +08:00
|
|
|
|
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_UNIONID(Secret 或 .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 });
|
|
|
|
|
|
|
2026-05-02 18:30:05 +08:00
|
|
|
|
// 等待登录完成(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 });
|
|
|
|
|
|
|
2026-05-02 16:37:45 +08:00
|
|
|
|
// 身份选择
|
|
|
|
|
|
if (
|
|
|
|
|
|
await page
|
|
|
|
|
|
.getByText('请选择您的登录身份')
|
|
|
|
|
|
.isVisible()
|
|
|
|
|
|
.catch(() => false)
|
|
|
|
|
|
) {
|
|
|
|
|
|
await page.locator('.van-grid-item').first().click();
|
2026-05-02 18:30:05 +08:00
|
|
|
|
await page.waitForTimeout(2_000);
|
2026-05-02 16:37:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-02 18:30:05 +08:00
|
|
|
|
// 导航到排课页触发路由守卫中的 org 加载 + 部门自动选中
|
|
|
|
|
|
await page.goto('/clazz', {
|
|
|
|
|
|
waitUntil: 'domcontentloaded',
|
|
|
|
|
|
timeout: 60_000,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await expect(page.locator('#app')).toBeVisible({ timeout: 60_000 });
|
|
|
|
|
|
|
|
|
|
|
|
// 等待异步的部门加载完成
|
2026-05-02 16:37:45 +08:00
|
|
|
|
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));
|
2026-05-02 18:30:05 +08:00
|
|
|
|
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');
|
2026-05-02 16:37:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-02 18:30:05 +08:00
|
|
|
|
// 导航到排课页触发 org 加载 + 部门自动选中,验证页面正常渲染
|
2026-05-02 16:37:45 +08:00
|
|
|
|
await page.goto('/clazz', {
|
|
|
|
|
|
waitUntil: 'domcontentloaded',
|
|
|
|
|
|
timeout: 60_000,
|
|
|
|
|
|
});
|
|
|
|
|
|
await expect(page.locator('#app')).toBeVisible({ timeout: 60_000 });
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|