import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";

const viewPath = path.resolve("src/views/PurchaseOrderView.vue");
const source = fs.readFileSync(viewPath, "utf8");

assert.match(
  source,
  /function\s+scheduleSalesOrderPurchaseAutoFill\s*\(/,
  "purchase order view should schedule automatic sales-order purchase detail generation"
);

assert.match(
  source,
  /watch\(\s*\(\)\s*=>\s*normalizedSalesOrderIds\(form\.sales_order_ids\)\.join\(","\)[\s\S]*scheduleSalesOrderPurchaseAutoFill\(\)/,
  "sales order selection changes should trigger automatic purchase detail generation"
);

assert.match(
  source,
  /function\s+applySalesOrderPurchase\s*\(\s*\{\s*automatic\s*=\s*false\s*}\s*=\s*\{\}\s*\)/,
  "sales order purchase generation should support automatic and manual refresh modes"
);

assert.match(
  source,
  /重新带出采购明细/,
  "manual button should be kept as a refresh action instead of the required first step"
);

assert.match(
  source,
  /ref="salesOrderPickerRef"\s+class="order-tag-picker"\s+@pointerdown\.stop\s+@mousedown\.stop\s+@click\.stop/,
  "sales order picker should keep internal clicks from bubbling into unrelated close handlers"
);

assert.match(
  source,
  /@click\.stop="toggleSalesOrder\(order\.order_id\)"/,
  "sales order option clicks should be handled by the picker itself"
);

assert.match(
  source,
  /function\s+toggleSalesOrder\s*\(orderId\)[\s\S]*salesOrderPickerOpen\.value\s*=\s*false/,
  "selecting or unselecting one sales order should close the picker; selecting another requires focusing the search box again"
);

assert.doesNotMatch(
  source,
  /function\s+toggleSalesOrder\s*\(orderId\)[\s\S]*salesOrderPickerOpen\.value\s*=\s*true/,
  "sales order selection should not force the picker to stay open after choosing one row"
);

assert.doesNotMatch(
  source,
  /请重新按销售订单带出采购建议/,
  "selection changes should no longer ask the user to manually bring out purchase suggestions"
);

console.log("purchase order sales auto-fill checks passed");
