diff --git a/tests/department-single-transparent.spec.ts b/tests/department-single-transparent.spec.ts new file mode 100644 index 0000000..4971b12 --- /dev/null +++ b/tests/department-single-transparent.spec.ts @@ -0,0 +1,98 @@ +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 }); + + // 身份选择 + if ( + await page + .getByText('请选择您的登录身份') + .isVisible() + .catch(() => false) + ) { + await page.locator('.van-grid-item').first().click(); + } + + // 等待路由稳定、机构上下文写入 + 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)); + expect(payload.current_department_id).toBeTruthy(); + expect(typeof payload.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(); + } + + await page.waitForTimeout(3_000); + + // 导航到排课页,页面应正常渲染 + await page.goto('/clazz', { + waitUntil: 'domcontentloaded', + timeout: 60_000, + }); + await expect(page.locator('#app')).toBeVisible({ timeout: 60_000 }); + }); +});