order-complete.spec.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. import { TIMEOUTS } from '../../utils/timeouts';
  2. import { test, expect } from '../../utils/test-setup';
  3. import { readFileSync } from 'fs';
  4. import { join, dirname } from 'path';
  5. import { fileURLToPath } from 'url';
  6. const __filename = fileURLToPath(import.meta.url);
  7. const __dirname = dirname(__filename);
  8. const testUsers = JSON.parse(readFileSync(join(__dirname, '../../fixtures/test-users.json'), 'utf-8'));
  9. // 获取认证 token
  10. async function getAuthToken(request: Parameters<typeof test>[0]['request']): Promise<string | null> {
  11. const loginResponse = await request.post('http://localhost:8080/api/v1/auth/login', {
  12. data: {
  13. username: testUsers.admin.username,
  14. password: testUsers.admin.password
  15. }
  16. });
  17. if (!loginResponse.ok()) {
  18. console.debug('API 登录失败:', await loginResponse.text());
  19. return null;
  20. }
  21. const loginData = await loginResponse.json();
  22. return loginData.data?.token || loginData.token || null;
  23. }
  24. // API 调用辅助函数 - 使用 API 直接创建残疾人数据
  25. async function createDisabledPersonViaAPI(
  26. request: Parameters<typeof test>[0]['request'],
  27. personData: {
  28. name: string;
  29. gender: string;
  30. idCard: string;
  31. disabilityId: string;
  32. disabilityType: string;
  33. disabilityLevel: string;
  34. idAddress: string;
  35. phone: string;
  36. province: string;
  37. city: string;
  38. }
  39. ): Promise<{ id: number; name: string } | null> {
  40. try {
  41. const token = await getAuthToken(request);
  42. if (!token) return null;
  43. const createResponse = await request.post('http://localhost:8080/api/v1/disability/createDisabledPerson', {
  44. headers: {
  45. 'Authorization': `Bearer ${token}`,
  46. 'Content-Type': 'application/json'
  47. },
  48. data: personData
  49. });
  50. if (!createResponse.ok()) {
  51. const errorText = await createResponse.text();
  52. console.debug('API 创建残疾人失败:', createResponse.status(), errorText);
  53. return null;
  54. }
  55. const result = await createResponse.json();
  56. console.debug('API 创建残疾人成功:', result.name);
  57. return { id: result.id, name: result.name };
  58. } catch (error) {
  59. console.debug('API 调用出错:', error);
  60. return null;
  61. }
  62. }
  63. // 创建测试平台
  64. async function createPlatformViaAPI(
  65. request: Parameters<typeof test>[0]['request']
  66. ): Promise<{ id: number; name: string } | null> {
  67. try {
  68. const token = await getAuthToken(request);
  69. if (!token) return null;
  70. const timestamp = Date.now();
  71. const platformData = {
  72. platformName: `测试平台_${timestamp}`,
  73. contactPerson: '测试联系人',
  74. contactPhone: '13800138000',
  75. contactEmail: 'test@example.com'
  76. };
  77. const createResponse = await request.post('http://localhost:8080/api/v1/platform/createPlatform', {
  78. headers: {
  79. 'Authorization': `Bearer ${token}`,
  80. 'Content-Type': 'application/json'
  81. },
  82. data: platformData
  83. });
  84. if (!createResponse.ok()) {
  85. const errorText = await createResponse.text();
  86. console.debug('API 创建平台失败:', createResponse.status(), errorText);
  87. return null;
  88. }
  89. const result = await createResponse.json();
  90. console.debug('API 创建平台成功:', result.id, result.platformName);
  91. return { id: result.id, name: result.platformName };
  92. } catch (error) {
  93. console.debug('创建平台 API 调用出错:', error);
  94. return null;
  95. }
  96. }
  97. // 创建测试公司
  98. async function createCompanyViaAPI(
  99. request: Parameters<typeof test>[0]['request'],
  100. platformId: number
  101. ): Promise<{ id: number; name: string } | null> {
  102. try {
  103. const token = await getAuthToken(request);
  104. if (!token) return null;
  105. const timestamp = Date.now();
  106. const companyName = `测试公司_${timestamp}`;
  107. const companyData = {
  108. companyName: companyName,
  109. platformId: platformId,
  110. contactPerson: '测试联系人',
  111. contactPhone: '13900139000',
  112. contactEmail: 'company@example.com'
  113. };
  114. const createResponse = await request.post('http://localhost:8080/api/v1/company/createCompany', {
  115. headers: {
  116. 'Authorization': `Bearer ${token}`,
  117. 'Content-Type': 'application/json'
  118. },
  119. data: companyData
  120. });
  121. if (!createResponse.ok()) {
  122. const errorText = await createResponse.text();
  123. console.debug('API 创建公司失败:', createResponse.status(), errorText);
  124. return null;
  125. }
  126. const createResult = await createResponse.json();
  127. if (!createResult.success) {
  128. console.debug('API 创建公司返回 success=false');
  129. return null;
  130. }
  131. // 创建成功后,通过平台ID查询公司列表来获取公司ID
  132. const listResponse = await request.get(`http://localhost:8080/api/v1/company/getCompaniesByPlatform/${platformId}`, {
  133. headers: {
  134. 'Authorization': `Bearer ${token}`
  135. }
  136. });
  137. if (!listResponse.ok()) {
  138. console.debug('API 获取公司列表失败');
  139. return null;
  140. }
  141. const companies = await listResponse.json();
  142. const createdCompany = companies.find((c: { companyName: string }) => c.companyName === companyName);
  143. if (createdCompany) {
  144. console.debug('API 创建公司成功:', createdCompany.id, createdCompany.companyName);
  145. return { id: createdCompany.id, name: createdCompany.companyName };
  146. }
  147. console.debug('未找到创建的公司');
  148. return null;
  149. } catch (error) {
  150. console.debug('创建公司 API 调用出错:', error);
  151. return null;
  152. }
  153. }
  154. async function selectDisabledPersonInAddDialog(
  155. page: Parameters<typeof test>[0]['page'],
  156. _personName?: string
  157. ): Promise<boolean> {
  158. const selectPersonButton = page.getByRole('button', { name: '选择残疾人' });
  159. await selectPersonButton.click();
  160. // 使用唯一的 test ID 精确定位残疾人选择对话框
  161. const dialog = page.getByTestId('disabled-person-selector-dialog');
  162. // 测试环境:组件会自动选中第一个残疾人并确认,只需等待对话框关闭
  163. console.debug('等待残疾人选择器对话框自动关闭...');
  164. // 等待对话框消失(自动选择后会关闭)
  165. await dialog.waitFor({ state: 'hidden', timeout: TIMEOUTS.TABLE_LOAD });
  166. console.debug('残疾人选择器对话框已关闭');
  167. // 等待一下让状态同步
  168. await page.waitForTimeout(TIMEOUTS.MEDIUM);
  169. return true;
  170. }
  171. // 全局计数器,确保每个测试生成唯一的数据
  172. let testDataCounter = 0;
  173. // 生成随机身份证号,确保唯一性
  174. function generateUniqueIdCard(): string {
  175. // 地区码(6位)
  176. const areaCode = '110101';
  177. // 出生日期(8位)- 使用 1980-01-01 到 2005-12-31 之间的随机日期
  178. const startYear = 1980;
  179. const endYear = 2005;
  180. const year = startYear + Math.floor(Math.random() * (endYear - startYear + 1));
  181. const month = String(Math.floor(Math.random() * 12) + 1).padStart(2, '0');
  182. const day = String(Math.floor(Math.random() * 28) + 1).padStart(2, '0');
  183. const birthDate = `${year}${month}${day}`;
  184. // 顺序码(3位)- 使用时间戳后3位
  185. const sequence = String(Date.now()).slice(-3).padStart(3, '0');
  186. // 校验码(1位)- 随机数字
  187. const checkCode = String(Math.floor(Math.random() * 10));
  188. return areaCode + birthDate + sequence + checkCode;
  189. }
  190. function generateUniqueTestData() {
  191. const timestamp = Date.now();
  192. const counter = ++testDataCounter;
  193. const random = Math.floor(Math.random() * 10000);
  194. const idCard = generateUniqueIdCard();
  195. // 生成18位身份证号
  196. return {
  197. orderName: '测试订单_' + timestamp + '_' + counter + '_' + random,
  198. personName: '测试残疾人_' + timestamp + '_' + counter + '_' + random,
  199. personName2: '测试残疾人2_' + timestamp + '_' + counter + '_' + random,
  200. idCard,
  201. idCard2: generateUniqueIdCard(),
  202. phone: '138' + String(counter).padStart(4, '0') + String(random).padStart(4, '0'),
  203. phone2: '139' + String(counter).padStart(4, '0') + String(random).padStart(4, '0'),
  204. gender: '男',
  205. disabilityType: '视力残疾',
  206. disabilityLevel: '一级',
  207. disabilityId: '残疾证' + String(timestamp).slice(-6) + String(random).padStart(4, '0'),
  208. idAddress: '北京市东城区测试地址' + timestamp + '_' + counter,
  209. province: '北京市',
  210. city: '北京市',
  211. hireDate: '2025-01-15',
  212. salary: 5000,
  213. platformName: `测试平台_${timestamp}_${counter}_${random}`,
  214. companyName: `测试公司_${timestamp}_${counter}_${random}`,
  215. channelName: `测试渠道_${timestamp}_${counter}_${random}`,
  216. };
  217. }
  218. // 等待订单行出现在表格中
  219. async function waitForOrderRow(page: Parameters<typeof test>[0]['page'], orderName: string, timeout = 15000) {
  220. const startTime = Date.now();
  221. while (Date.now() - startTime < timeout) {
  222. const table = page.locator('table');
  223. const orderRow = table.locator('tbody tr').filter({ hasText: orderName });
  224. const count = await orderRow.count();
  225. if (count > 0) {
  226. console.debug('找到订单行:', orderName);
  227. return true;
  228. }
  229. await page.waitForTimeout(TIMEOUTS.MEDIUM);
  230. }
  231. console.debug('等待订单行超时:', orderName);
  232. return false;
  233. }
  234. test.describe('订单完整流程测试', () => {
  235. test.describe('新增订单完整流程', () => {
  236. test('应该能完成新增订单的完整流程:创建订单 → 添加人员 → 添加附件 → 激活订单', async ({
  237. adminLoginPage,
  238. orderManagementPage,
  239. request,
  240. page
  241. }) => {
  242. // 登录
  243. await adminLoginPage.goto();
  244. await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
  245. await adminLoginPage.expectLoginSuccess();
  246. await orderManagementPage.goto();
  247. // 使用 API 创建平台和公司测试数据
  248. const createdPlatform = await createPlatformViaAPI(request);
  249. if (!createdPlatform) {
  250. test.skip(true, '无法创建平台数据');
  251. return;
  252. }
  253. const createdCompany = await createCompanyViaAPI(request, createdPlatform.id);
  254. if (!createdCompany) {
  255. test.skip(true, '无法创建公司数据');
  256. return;
  257. }
  258. // 生成唯一测试数据
  259. const testData = generateUniqueTestData();
  260. // 使用 API 创建残疾人测试数据
  261. const personData = {
  262. name: testData.personName,
  263. gender: testData.gender,
  264. idCard: testData.idCard,
  265. disabilityId: testData.disabilityId,
  266. disabilityType: testData.disabilityType,
  267. disabilityLevel: testData.disabilityLevel,
  268. idAddress: testData.idAddress,
  269. phone: testData.phone,
  270. province: testData.province,
  271. city: testData.city,
  272. };
  273. const createdPerson = await createDisabledPersonViaAPI(request, personData);
  274. if (!createdPerson) {
  275. test.skip(true, '无法创建残疾人数据');
  276. return;
  277. }
  278. console.debug('已创建残疾人:', createdPerson.name, 'ID:', createdPerson.id);
  279. // 步骤 1: 创建订单(填写所有字段)
  280. await orderManagementPage.openCreateDialog();
  281. await page.getByLabel(/订单名称|名称/).fill(testData.orderName);
  282. // 选择平台
  283. const platformTrigger = page.locator('[data-testid="platform-selector-create"]');
  284. if (await platformTrigger.count() > 0) {
  285. await platformTrigger.click();
  286. await page.waitForTimeout(TIMEOUTS.MEDIUM_LONG);
  287. const allOptions = page.getByRole('option');
  288. const count = await allOptions.count();
  289. if (count > 0) {
  290. await allOptions.first().click();
  291. }
  292. await page.waitForTimeout(TIMEOUTS.VERY_SHORT);
  293. }
  294. // 选择公司
  295. const companyTrigger = page.locator('[data-testid="company-selector-create"]');
  296. if (await companyTrigger.count() > 0) {
  297. await companyTrigger.click();
  298. await page.waitForTimeout(TIMEOUTS.MEDIUM_LONG);
  299. const allCompanyOptions = page.getByRole('option');
  300. const companyCount = await allCompanyOptions.count();
  301. if (companyCount > 0) {
  302. await allCompanyOptions.first().click();
  303. }
  304. await page.waitForTimeout(TIMEOUTS.VERY_SHORT);
  305. }
  306. // 填写预计开始日期
  307. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  308. // 选择残疾人
  309. const hasPerson = await selectDisabledPersonInAddDialog(page, createdPerson.name);
  310. if (!hasPerson) {
  311. await orderManagementPage.cancelDialog();
  312. test.skip(true, '没有可用的残疾人数据');
  313. return;
  314. }
  315. // 提交订单
  316. await page.waitForTimeout(TIMEOUTS.VERY_LONG);
  317. await orderManagementPage.submitForm();
  318. await orderManagementPage.waitForDialogClosed();
  319. // 验证订单创建成功
  320. await page.waitForTimeout(TIMEOUTS.LONG);
  321. const successToast = page.locator('[data-sonner-toast][data-type="success"]');
  322. const hasSuccess = await successToast.count() > 0;
  323. expect(hasSuccess).toBe(true);
  324. // 等待订单行出现在表格中
  325. const orderFound = await waitForOrderRow(page, testData.orderName);
  326. expect(orderFound).toBe(true);
  327. // 步骤 2: 打开订单详情并验证人员已添加
  328. await orderManagementPage.openDetailDialog(testData.orderName);
  329. // 获取人员列表
  330. const personList = await orderManagementPage.getPersonListFromDetail();
  331. console.debug('订单人员列表:', personList);
  332. expect(personList.length).toBeGreaterThan(0);
  333. // 步骤 3: 为人员添加附件(暂时跳过,因为 Story 10.10 还在 review 中)
  334. // TODO: 启用附件上传测试,等待 Story 10.10 完成
  335. // await orderManagementPage.openAddAttachmentDialog();
  336. // const fileName = 'images/photo.jpg';
  337. // await orderManagementPage.uploadAttachment(personList[0].name || createdPerson.name, fileName, 'image/jpeg', '其他');
  338. // await page.waitForTimeout(TIMEOUTS.LONG);
  339. // await orderManagementPage.closeUploadDialog();
  340. // 关闭订单详情对话框
  341. await orderManagementPage.closeDetailDialog();
  342. // 步骤 4: 激活订单(草稿 → 进行中)
  343. const activated = await orderManagementPage.activateOrder(testData.orderName);
  344. expect(activated).toBe(true);
  345. // 步骤 5: 验证订单状态(激活后变为已确认)
  346. await orderManagementPage.expectOrderStatus(testData.orderName, 'confirmed');
  347. console.debug('新增订单完整流程测试通过');
  348. });
  349. });
  350. test.describe('编辑订单完整流程', () => {
  351. test('应该能完成编辑订单的完整流程:打开订单 → 修改信息 → 添加人员 → 关闭订单', async ({
  352. adminLoginPage,
  353. orderManagementPage,
  354. request,
  355. page
  356. }) => {
  357. // 登录
  358. await adminLoginPage.goto();
  359. await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
  360. await adminLoginPage.expectLoginSuccess();
  361. await orderManagementPage.goto();
  362. // 使用 API 创建平台和公司测试数据
  363. const createdPlatform = await createPlatformViaAPI(request);
  364. if (!createdPlatform) {
  365. test.skip(true, '无法创建平台数据');
  366. return;
  367. }
  368. const createdCompany = await createCompanyViaAPI(request, createdPlatform.id);
  369. if (!createdCompany) {
  370. test.skip(true, '无法创建公司数据');
  371. return;
  372. }
  373. // 生成唯一测试数据
  374. const testData = generateUniqueTestData();
  375. // 使用 API 创建两个残疾人测试数据
  376. const personData = {
  377. name: testData.personName,
  378. gender: testData.gender,
  379. idCard: testData.idCard,
  380. disabilityId: testData.disabilityId,
  381. disabilityType: testData.disabilityType,
  382. disabilityLevel: testData.disabilityLevel,
  383. idAddress: testData.idAddress,
  384. phone: testData.phone,
  385. province: testData.province,
  386. city: testData.city,
  387. };
  388. const personData2 = {
  389. name: testData.personName2,
  390. gender: testData.gender,
  391. idCard: testData.idCard2,
  392. disabilityId: '残疾证' + String(Date.now()).slice(-6) + String(Math.floor(Math.random() * 10000)).padStart(4, '0'),
  393. disabilityType: testData.disabilityType,
  394. disabilityLevel: testData.disabilityLevel,
  395. idAddress: testData.idAddress,
  396. phone: testData.phone2,
  397. province: testData.province,
  398. city: testData.city,
  399. };
  400. const createdPerson = await createDisabledPersonViaAPI(request, personData);
  401. if (!createdPerson) {
  402. test.skip(true, '无法创建残疾人数据');
  403. return;
  404. }
  405. console.debug('已创建残疾人1:', createdPerson.name, 'ID:', createdPerson.id);
  406. const createdPerson2 = await createDisabledPersonViaAPI(request, personData2);
  407. if (!createdPerson2) {
  408. test.skip(true, '无法创建残疾人数据2');
  409. return;
  410. }
  411. console.debug('已创建残疾人2:', createdPerson2.name, 'ID:', createdPerson2.id);
  412. // 步骤 1: 创建初始订单
  413. await orderManagementPage.openCreateDialog();
  414. await page.getByLabel(/订单名称|名称/).fill(testData.orderName);
  415. // 选择平台
  416. const platformTrigger = page.locator('[data-testid="platform-selector-create"]');
  417. if (await platformTrigger.count() > 0) {
  418. await platformTrigger.click();
  419. await page.waitForTimeout(TIMEOUTS.MEDIUM_LONG);
  420. const allOptions = page.getByRole('option');
  421. const count = await allOptions.count();
  422. if (count > 0) {
  423. await allOptions.first().click();
  424. }
  425. await page.waitForTimeout(TIMEOUTS.VERY_SHORT);
  426. }
  427. // 选择公司
  428. const companyTrigger = page.locator('[data-testid="company-selector-create"]');
  429. if (await companyTrigger.count() > 0) {
  430. await companyTrigger.click();
  431. await page.waitForTimeout(TIMEOUTS.MEDIUM_LONG);
  432. const allCompanyOptions = page.getByRole('option');
  433. const companyCount = await allCompanyOptions.count();
  434. if (companyCount > 0) {
  435. await allCompanyOptions.first().click();
  436. }
  437. await page.waitForTimeout(TIMEOUTS.VERY_SHORT);
  438. }
  439. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  440. const hasPerson = await selectDisabledPersonInAddDialog(page, createdPerson.name);
  441. if (!hasPerson) {
  442. await orderManagementPage.cancelDialog();
  443. test.skip(true, '没有可用的残疾人数据');
  444. return;
  445. }
  446. await page.waitForTimeout(TIMEOUTS.VERY_LONG);
  447. await orderManagementPage.submitForm();
  448. await orderManagementPage.waitForDialogClosed();
  449. // 验证订单创建成功
  450. await page.waitForTimeout(TIMEOUTS.LONG);
  451. const successToast = page.locator('[data-sonner-toast][data-type="success"]');
  452. const hasSuccess = await successToast.count() > 0;
  453. expect(hasSuccess).toBe(true);
  454. const orderFound = await waitForOrderRow(page, testData.orderName);
  455. expect(orderFound).toBe(true);
  456. // 步骤 2: 激活订单(使其可以进行状态流转)
  457. const activated = await orderManagementPage.activateOrder(testData.orderName);
  458. expect(activated).toBe(true);
  459. // 步骤 3: 打开订单详情对话框
  460. await orderManagementPage.openDetailDialog(testData.orderName);
  461. // 步骤 3.5: 确认待添加的人员(创建订单时选择的人员在待添加列表中)
  462. // 检查是否有待添加人员
  463. const confirmAddButton = page.getByTestId('confirm-add-persons-button');
  464. const confirmButtonCount = await confirmAddButton.count();
  465. if (confirmButtonCount > 0) {
  466. console.debug('发现待添加人员,点击确认添加按钮');
  467. await confirmAddButton.click();
  468. // 等待成功 toast
  469. await page.waitForTimeout(TIMEOUTS.LONG);
  470. const addSuccessToast = page.locator('[data-sonner-toast][data-type="success"]');
  471. const hasAddSuccess = await addSuccessToast.count() > 0;
  472. if (hasAddSuccess) {
  473. console.debug('待添加人员已确认添加');
  474. }
  475. }
  476. // 验证人员列表包含已确认的人员
  477. const personListAfter = await orderManagementPage.getPersonListFromDetail();
  478. console.debug('人员列表:', personListAfter);
  479. expect(personListAfter.length).toBeGreaterThan(0); // 至少有1个人
  480. // 步骤 4: 关闭订单(已确认 → 已完成)
  481. await orderManagementPage.closeDetailDialog();
  482. const closed = await orderManagementPage.closeOrder(testData.orderName);
  483. expect(closed).toBe(true);
  484. // 步骤 6: 验证订单状态为已完成
  485. await orderManagementPage.expectOrderStatus(testData.orderName, 'completed');
  486. console.debug('编辑订单完整流程测试通过');
  487. });
  488. });
  489. });