From e235b3b7f5a7db97fa8a71dc4a638d918af22c0e Mon Sep 17 00:00:00 2001
From: liu <2021943741@qq.com>
Date: 星期四, 17 九月 2026 13:33:18 +0800
Subject: [PATCH] feat(sql): 迁移脚本补齐审批配置地基 + 采购计划/订单变更,并加订单审批测试

---
 scripts/test-purchase-order-ui.js |  333 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 333 insertions(+), 0 deletions(-)

diff --git a/scripts/test-purchase-order-ui.js b/scripts/test-purchase-order-ui.js
new file mode 100644
index 0000000..b256e65
--- /dev/null
+++ b/scripts/test-purchase-order-ui.js
@@ -0,0 +1,333 @@
+/**
+ * 閲囪喘璁㈠崟瀹℃壒 - 娴忚鍣ㄧ鍒扮娴嬭瘯
+ *
+ * 鍓嶇疆鏁版嵁鐢� admin-api 鐩存帴鍑嗗锛堜竴娆¤窇瀹岀暀涓嬶細1 寮犳湭鎻愪氦 + 1 寮犲鎵逛腑锛夛紝
+ * 涔嬪悗鍏ㄩ儴璧扮湡瀹炴祻瑙堝櫒浜や簰楠岃瘉鎸夐挳鍙鎬с�佸脊绐楁牎楠屼笌鐘舵�佹祦杞��
+ *
+ * 鐢ㄦ硶锛歯ode scripts/test-purchase-order-ui.js
+ */
+const { chromium } = require('playwright');
+const path = require('path');
+const fs = require('fs');
+
+const API = process.env.TEST_API || 'http://127.0.0.1:48080/admin-api';
+const BASE = process.env.TEST_BASE || 'http://localhost:5666';
+const USER = process.env.TEST_USER || 'admin';
+const PASS = process.env.TEST_PASS || 'admin123';
+const BIZ_TYPE = 'erp_purchase_order_approve';
+const PRODUCT_ID = Number(process.env.PRODUCT_ID || 57);
+const PRODUCT_UNIT_ID = Number(process.env.PRODUCT_UNIT_ID || 9);
+
+const SHOT_DIR = path.join(__dirname, 'shots');
+if (!fs.existsSync(SHOT_DIR)) fs.mkdirSync(SHOT_DIR, { recursive: true });
+
+const results = [];
+function ok(name, extra = '') {
+  results.push({ pass: true, name });
+  console.log(`  鉁� ${name}${extra ? ' 鈥� ' + extra : ''}`);
+}
+function fail(name, extra = '') {
+  results.push({ pass: false, name, extra });
+  console.log(`  鉂� ${name}${extra ? ' 鈥� ' + extra : ''}`);
+}
+
+let TOKEN = '';
+async function api(method, p, body) {
+  const res = await fetch(API + p, {
+    method,
+    headers: {
+      'Content-Type': 'application/json',
+      'tenant-id': '1',
+      ...(TOKEN ? { Authorization: `Bearer ${TOKEN}` } : {}),
+    },
+    body: body ? JSON.stringify(body) : undefined,
+  });
+  try {
+    return await res.json();
+  } catch {
+    return null;
+  }
+}
+
+async function seed() {
+  console.log('\n[S] 鍑嗗娴嬭瘯鏁版嵁锛堟帴鍙o級');
+  const lg = await api('POST', '/system/auth/login', { username: USER, password: PASS });
+  if (!lg || lg.code !== 0) {
+    fail('鎺ュ彛鐧诲綍澶辫触', JSON.stringify(lg));
+    process.exit(1);
+  }
+  TOKEN = lg.data.accessToken;
+  ok('鎺ュ彛鐧诲綍鎴愬姛');
+
+  const page = await api('GET', `/system/approval-config/page?pageNo=1&pageSize=100&bizType=${BIZ_TYPE}`);
+  const cfg = (page?.data?.list || []).find((x) => x.bizType === BIZ_TYPE);
+  if (!cfg) {
+    fail('缂哄皯瀹℃壒閰嶇疆绉嶅瓙');
+    process.exit(1);
+  }
+  await api('POST', '/system/approval-config/save', {
+    id: cfg.id,
+    bizType: cfg.bizType,
+    bizName: cfg.bizName,
+    approvalEnabled: true,
+    remark: cfg.remark,
+    userIds: [1],
+  });
+  ok('瀹℃壒宸插惎鐢紝瀹℃壒浜�=admin');
+
+  // 姣忔璺戦兘鐢ㄥ敮涓�鍛藉悕鐨勪緵搴斿晢锛屼究浜庡湪鍒楄〃閲岀簿纭攣瀹氭湰娆$瀛愬崟鎹�
+  const seq = Date.now().toString().slice(-6);
+  const supplierName = `UIAUTOSUP${seq}`;
+  let supplierId = null;
+  const c = await api('POST', '/srm/supplier/create', {
+    code: `UIAUTOSUP${seq}`,
+    name: supplierName,
+    status: 0,
+  });
+  supplierId = c?.data;
+  if (!supplierId) {
+    const sup = await api('GET', '/srm/supplier/simple-list');
+    const hit = (sup?.data || []).find((x) => x.name === supplierName) || (sup?.data || [])[0];
+    supplierId = hit?.id;
+  }
+  if (!supplierId) {
+    fail('鏃犳硶鍑嗗渚涘簲鍟嗭紙娉ㄦ剰 ERP/SRM 鏁版嵁婧愪笉涓�鑷撮棶棰橈級', JSON.stringify(c));
+    process.exit(1);
+  }
+  ok('渚涘簲鍟嗗氨缁�', `supplierId=${supplierId} name=${supplierName}`);
+
+  const mk = async () => {
+    return api('POST', '/purchase/order/create', {
+      supplierId,
+      orderTime: '2026-09-16 10:00:00',
+      discountPercent: 0,
+      depositPrice: 0,
+      remark: supplierName,
+      items: [
+        {
+          productId: PRODUCT_ID,
+          productUnitId: PRODUCT_UNIT_ID,
+          productPrice: 100,
+          count: 5,
+          taxPercent: 13,
+        },
+      ],
+    });
+  };
+
+  const a = await mk();
+  const b = await mk();
+  if (a?.code !== 0 || b?.code !== 0) {
+    fail('鍒涘缓娴嬭瘯璁㈠崟澶辫触', JSON.stringify({ a, b }));
+    process.exit(1);
+  }
+  const draftId = a.data;
+  const approvingId = b.data;
+  const sub = await api('PUT', `/purchase/order/submit?id=${approvingId}`);
+  if (sub?.code !== 0) {
+    fail('棰勭疆銆屽鎵逛腑銆嶅崟鎹け璐�', JSON.stringify(sub));
+    process.exit(1);
+  }
+  ok('鏁版嵁灏辩华', `鑽夌 id=${draftId}锛屽鎵逛腑 id=${approvingId}`);
+  return { approvingId, draftId, supplierName };
+}
+
+async function shot(page, name) {
+  const p = path.join(SHOT_DIR, `${name}.png`);
+  await page.screenshot({ path: p, fullPage: true });
+  console.log(`  馃摳 ${p}`);
+}
+
+/**
+ * 鍙栨暟鎹涓庡浐瀹氬彸鍒楄锛屾寜绱㈠紩閰嶅銆�
+ * 娉ㄦ剰锛氭湰椤点�岀姸鎬併�嶅垪鏄� fixed:right锛屼笌鎿嶄綔鎸夐挳鍚屽鍥哄畾鍙冲垪瀹瑰櫒锛�
+ * 鍥犳 main 閲屼笉鍚姸鎬侊紝蹇呴』浠� fixed 鏂囨湰閲岃鐘舵�併��
+ */
+async function rowPairs(page) {
+  const dataRows = await page.locator('.vxe-table--main-wrapper .vxe-body--row').all();
+  const fixedRows = await page.locator('.vxe-table--fixed-right-wrapper .vxe-body--row').all();
+  const out = [];
+  const n = Math.min(dataRows.length, fixedRows.length);
+  for (let i = 0; i < n; i++) {
+    out.push({
+      main: (await dataRows[i].innerText()).replace(/\s+/g, ' '),
+      fixed: (await fixedRows[i].innerText()).replace(/\s+/g, ' ').trim(),
+      actionRow: fixedRows[i],
+    });
+  }
+  return out;
+}
+
+(async () => {
+  const seeded = await seed();
+
+  const browser = await chromium.launch({ headless: true, channel: 'chrome' });
+  const page = await browser.newPage({ viewport: { width: 1680, height: 950 } });
+
+  const apiErrors = [];
+  page.on('response', async (r) => {
+    if (!r.url().includes('/admin-api/')) return;
+    try {
+      const b = await r.json();
+      if (b && typeof b.code === 'number' && b.code !== 0) {
+        apiErrors.push({ url: r.url().replace(BASE, ''), code: b.code, msg: b.msg });
+      }
+    } catch {
+      /* ignore */
+    }
+  });
+
+  try {
+    // 1. 鐧诲綍
+    console.log('\n[1] 娴忚鍣ㄧ櫥褰�');
+    await page.goto(`${BASE}/auth/login`, { waitUntil: 'networkidle', timeout: 60000 });
+    await page.locator('input[placeholder="璇疯緭鍏ョ敤鎴峰悕"]').first().fill(USER);
+    await page.locator('input[type="password"]').first().fill(PASS);
+    await page.locator('button[aria-label="login"]').first().click();
+    await page.waitForURL((u) => !u.pathname.includes('/login'), { timeout: 30000 });
+    ok('鐧诲綍鎴愬姛');
+
+    // 2. 瀹℃壒閰嶇疆椤靛簲鑳界湅鍒伴噰璐鍗曞鏍�
+    console.log('\n[2] 瀹℃壒閰嶇疆椤�');
+    await page.goto(`${BASE}/system/approval-config`, { waitUntil: 'networkidle', timeout: 60000 });
+    await page.waitForTimeout(2500);
+    const cfgRow = page.locator('.vxe-body--row', { hasText: '閲囪喘璁㈠崟瀹℃牳' });
+    if ((await cfgRow.count()) > 0) {
+      const t = (await cfgRow.first().innerText()).replace(/\s+/g, ' ');
+      if (t.includes('宸插惎鐢�')) ok('閲囪喘璁㈠崟瀹℃壒鏄剧ず宸插惎鐢�', t.slice(0, 100));
+      else fail('閲囪喘璁㈠崟瀹℃壒鏈樉绀哄凡鍚敤', t.slice(0, 100));
+      if (t.includes('瓒呯骇绠$悊鍛�')) ok('瀹℃壒浜烘樉绀鸿秴绾х鐞嗗憳');
+      else fail('瀹℃壒浜烘湭鏄剧ず', t.slice(0, 100));
+    } else {
+      fail('閰嶇疆椤垫壘涓嶅埌銆岄噰璐鍗曞鏍搞�嶈');
+    }
+    await shot(page, 'po-01-config');
+
+    // 3. 閲囪喘璁㈠崟鍒楄〃鎸夐挳鍙鎬�
+    console.log('\n[3] 閲囪喘璁㈠崟鍒楄〃鎸夐挳');
+    await page.goto(`${BASE}/purchase/order`, { waitUntil: 'networkidle', timeout: 60000 });
+    await page.waitForTimeout(3000);
+    await shot(page, 'po-02-list');
+
+    const pairs = await rowPairs(page);
+    if (pairs.length === 0) {
+      fail('鍒楄〃鏃犳暟鎹');
+      throw new Error('empty grid');
+    }
+    // 鏈绉嶅瓙鍗曟嵁闈犲敮涓�渚涘簲鍟嗗悕瀹氫綅锛堝垪琛ㄤ笉灞曠ず澶囨敞鍒楋級
+    const mine = pairs.filter((p) => p.main.includes(seeded.supplierName));
+    if (mine.length < 2) {
+      fail('鏈兘鍦ㄥ垪琛ㄤ腑瀹氫綅鍒版湰娆$瀛愯鍗�', `鍛戒腑 ${mine.length} 琛� / 鍏� ${pairs.length} 琛宍);
+      pairs.forEach((p) => console.log('     ROW:', p.main.slice(0, 60), '||', p.fixed));
+    } else {
+      ok('鎸変緵搴斿晢鍚嶅畾浣嶅埌鏈绉嶅瓙鍗曟嵁', `${mine.length} 琛宍);
+    }
+    const draft = mine.find((p) => p.fixed.includes('鑽夌'));
+    const approving = mine.find((p) => p.fixed.includes('瀹℃壒涓�'));
+    if (draft) ok('鑽夌鍗曟覆鏌撲负銆岃崏绋裤��', draft.fixed);
+    else fail('鏈壘鍒拌崏绋跨姸鎬佺殑绉嶅瓙璁㈠崟', mine.map((p) => p.fixed).join(' | '));
+    if (approving) ok('瀹℃壒涓崟娓叉煋涓恒�屽鎵逛腑銆�', approving.fixed);
+    else fail('鏈壘鍒板鎵逛腑鐘舵�佺殑绉嶅瓙璁㈠崟', mine.map((p) => p.fixed).join(' | '));
+
+    if (draft) {
+      const hasSubmit = /鎻愪氦/.test(draft.fixed);
+      const hasAudit = draft.fixed.includes('瀹℃牳');
+      const hasEdit = /(淇敼|缂栬緫)/.test(draft.fixed);
+      if (hasSubmit && hasEdit && !hasAudit) ok('鑽夌琛岋細鏈夈�屾彁浜ゃ�嶃�屼慨鏀广�嶏紝鏃犲鏍告寜閽�', draft.fixed);
+      else fail('鑽夌琛屾寜閽笉绗�', draft.fixed);
+    }
+    if (approving) {
+      const hasBoth =
+        approving.fixed.includes('瀹℃牳涓嶉�氳繃') &&
+        /瀹℃牳(?!\s*涓嶉�氳繃)/.test(approving.fixed.replace('瀹℃牳涓嶉�氳繃', ''));
+      const noSubmitEdit = !/鎻愪氦/.test(approving.fixed) && !/(淇敼|缂栬緫)/.test(approving.fixed);
+      if (hasBoth && noSubmitEdit) ok('瀹℃壒涓锛氭湁銆屽鏍搞�嶃�屽鏍镐笉閫氳繃銆嶏紝鏃犳彁浜�/淇敼', approving.fixed);
+      else fail('瀹℃壒涓鎸夐挳涓嶇', approving.fixed);
+    }
+
+    // 4. 璧颁竴閬嶅鏍镐笉閫氳繃寮圭獥锛堝惈蹇呭~鏍¢獙锛�
+    if (approving) {
+      console.log('\n[4] 瀹℃牳涓嶉�氳繃寮圭獥');
+      await approving.actionRow.getByText('瀹℃牳涓嶉�氳繃', { exact: true }).first().click();
+      await page.waitForTimeout(1200);
+      const dialog = page.locator('[role="dialog"]').last();
+      if ((await dialog.count()) === 0) {
+        fail('鏈脊鍑哄鏍稿脊绐�');
+      } else {
+        const dlgText = (await dialog.innerText()).replace(/\s+/g, ' ');
+        ok('寮圭獥宸叉墦寮�', dlgText.slice(0, 120));
+        const btns = await dialog.locator('button').allInnerTexts();
+        console.log('     寮圭獥鎸夐挳:', JSON.stringify(btns.map((b) => b.trim()).filter(Boolean)));
+
+        // 绌哄師鍥犵洿鎺ョ‘璁わ紝搴旇鍓嶇蹇呭~鎷︿綇锛堝脊绐椾笉鍏筹級
+        const confirmBtn = dialog
+          .locator('button')
+          .filter({ hasText: /纭甛s*璁纭甛s*瀹殀鎻怽s*浜OK/ })
+          .last();
+        await confirmBtn.click();
+        await page.waitForTimeout(1200);
+        const stillOpen = (await page.locator('[role="dialog"]').count()) > 0;
+        if (stillOpen) ok('鏈~鍘熷洜鏃惰蹇呭~鏍¢獙鎷︿笅');
+        else fail('鏈~鍘熷洜绔熸彁浜ゆ垚鍔燂紙鍓嶇鏍¢獙缂哄け锛�');
+
+        // 濉師鍥犲悗纭
+        await dialog.locator('textarea').first().fill('UI娴嬭瘯锛氬崟浠峰亸楂橈紝閫�鍥為噸璁�');
+        const resp = page
+          .waitForResponse(
+            (r) => r.url().includes('/purchase/order/audit'),
+            { timeout: 15000 },
+          )
+          .catch(() => null);
+        await confirmBtn.click();
+        const res = await resp;
+        if (!res) fail('鏈崟鑾� audit 鍝嶅簲');
+        else {
+          const body = await res.json().catch(() => null);
+          if (body?.code === 0) ok('椹冲洖鎻愪氦鎴愬姛', JSON.stringify(body));
+          else fail('椹冲洖澶辫触', JSON.stringify(body));
+        }
+        await page.waitForTimeout(2000);
+      }
+      await shot(page, 'po-03-after-reject');
+
+      // 5. 鐘舵�佸簲鍙樹负瀹℃壒涓嶉�氳繃锛屼笖鍙噸鏂颁慨鏀�
+      await page.reload({ waitUntil: 'networkidle' });
+      await page.waitForTimeout(3000);
+      const pairs2 = await rowPairs(page);
+      const mine2 = pairs2.filter((p) => p.main.includes(seeded.supplierName));
+      const rejected = mine2.find((p) => p.fixed.includes('瀹℃壒涓嶉�氳繃'));
+      if (rejected) {
+        ok('椹冲洖鍚庢覆鏌撲负銆屽鎵逛笉閫氳繃銆�', rejected.fixed);
+        const canResubmit = /鎻愪氦/.test(rejected.fixed) && /(淇敼|缂栬緫)/.test(rejected.fixed);
+        const noAudit = !rejected.fixed.includes('瀹℃牳');
+        if (canResubmit && noAudit) ok('椹冲洖鍗曞彲銆屼慨鏀广�嶅苟鍙噸鏂般�屾彁浜ゃ�嶏紝涓斾笉鍐嶆樉绀哄鏍告寜閽�', rejected.fixed);
+        else fail('椹冲洖鍗曞叆鍙d笉绗�', rejected.fixed);
+      } else {
+        fail('鏈壘鍒板鎵逛笉閫氳繃鐨勭瀛愯鍗�', mine2.map((p) => p.fixed).join(' | '));
+      }
+      await shot(page, 'po-04-rejected');
+    }
+
+    console.log(`\n锛堢瀛愬崟鎹� id锛氳崏绋�=${seeded.draftId} 瀹℃壒涓啋椹冲洖=${seeded.approvingId}锛塦);
+  } catch (e) {
+    fail('鎵ц寮傚父', e.message);
+    await shot(page, 'po-99-crash').catch(() => {});
+  }
+
+  console.log('\n===== 鎹曡幏鍒扮殑鎺ュ彛涓氬姟閿欒 =====');
+  const relevant = apiErrors.filter(
+    (e) =>
+      !e.url.includes('/auth/login') &&
+      !e.url.includes('simple-list') &&
+      !e.url.includes('/dict') &&
+      !e.url.includes('unread-count'),
+  );
+  if (relevant.length === 0) console.log('  锛堟棤锛�');
+  relevant.forEach((e) => console.log(`  ${e.url} 鈫� code=${e.code} msg=${e.msg}`));
+
+  await browser.close();
+  const passed = results.filter((r) => r.pass).length;
+  console.log(`\n===== 姹囨�伙細${passed}/${results.length} 閫氳繃 =====`);
+  results.filter((r) => !r.pass).forEach((r) => console.log(`  澶辫触: ${r.name} ${r.extra}`));
+  process.exit(passed === results.length ? 0 : 1);
+})();

--
Gitblit v1.9.3