selftest-lead.mjs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import assert from "node:assert/strict"
  2. const SRC = "../../../../../src/modules/cockpit/"
  3. const { fetchEventAnalysis, fetchHandlingEfficiency, fetchEventHistory } = await import(SRC + "services/warningResponseApi.js")
  4. const { fetchCurrentEvents } = await import(SRC + "services/eventApi.js")
  5. const { panelView } = await import(SRC + "domain/panelViews.js")
  6. const { SECTION_STATE, section } = await import(SRC + "domain/sectionState.js")
  7. const config = { dataSource: "real", api: { baseUrl: "http://test.invalid" } }
  8. const urls = []
  9. const respond = { ok: true, status: 200, json: async () => ({ code: 200, msg: "ok", data: {} }) }
  10. const fetchImpl = async (url) => { urls.push(String(url)); return respond }
  11. async function collect(fn, options) {
  12. urls.length = 0
  13. await fn({ ...options, config, fetchImpl })
  14. return [...urls]
  15. }
  16. // A. 预警分析:真收片区
  17. for (const [scope, expect] of [["south","district=0"],["north","district=1"],["taichang","district=2"],["unassigned","district=UNASSIGNED"]]) {
  18. const list = await collect(fetchEventAnalysis, { mode: "WARNING", rangeType: "LAST_7_DAYS", scope })
  19. const hit = list.filter((u) => u.includes(expect))
  20. console.log("analysis", scope, "requests:", list.length, "with", expect + ":", hit.length)
  21. assert.ok(hit.length >= 1, "analysis 缺 " + expect)
  22. }
  23. const allScope = await collect(fetchEventAnalysis, { mode: "WARNING", rangeType: "LAST_7_DAYS", scope: "all" })
  24. console.log("analysis all -> district present:", allScope.some((u) => u.includes("district=")))
  25. assert.equal(allScope.some((u) => u.includes("district=")), false, "all 不该带 district")
  26. // 多窗(90 天)每窗都要带
  27. const multi = await collect(fetchEventAnalysis, { mode: "WARNING", rangeType: "LAST_90_DAYS", scope: "south" })
  28. console.log("analysis 90d windows:", multi.length, "all district=0:", multi.every((u) => u.includes("district=0")))
  29. assert.ok(multi.length > 1, "90d 应是多窗")
  30. assert.ok(multi.every((u) => u.includes("district=0")), "多窗每窗都要带 district")
  31. // B. 不认片区的三个端点一个都不许带
  32. for (const [name, fn, opt] of [
  33. ["history", fetchEventHistory, { mode: "WARNING", rangeType: "LAST_7_DAYS", scope: "south" }],
  34. ["efficiency", fetchHandlingEfficiency, { mode: "WARNING", rangeType: "LAST_7_DAYS", scope: "south" }],
  35. ["current", fetchCurrentEvents, { mode: "WARNING", scope: "south" }],
  36. ]) {
  37. const list = await collect(fn, opt)
  38. const leak = list.filter((u) => u.includes("district="))
  39. console.log(name, "requests:", list.length, "leaking district:", leak.length)
  40. assert.equal(leak.length, 0, name + " 不该带 district: " + leak.join(","))
  41. }
  42. // D. 面板口径说明
  43. const empty = section(SECTION_STATE.EMPTY, { data: null })
  44. const { currentEventsView } = await import(SRC + "domain/eventList.js")
  45. const curView = currentEventsView(null, empty)
  46. console.log("currentEvents gap:", String(curView.gap).slice(0, 50) + "…")
  47. assert.ok(curView.gap && curView.gap.includes("当前范围"), "currentEvents 缺片区口径说明")
  48. const mustHave = ["historyAnalysis","responseEfficiency","monitoringOverview","operationOverview","operation","emergency","perceptionDevices","pipeNetwork","warningStudy","alarmDisposal"]
  49. const mustNot = ["archive","management","monitoringDistribution","eventAnalysis"]
  50. for (const id of mustHave) {
  51. const view = panelView(id, null, empty)
  52. assert.ok(view && view.gap && view.gap.includes("当前范围"), id + " 缺片区口径说明: " + (view && view.gap))
  53. console.log("gap ok:", id, "->", String(view.gap).slice(0, 40) + "…")
  54. }
  55. for (const id of mustNot) {
  56. const view = panelView(id, null, empty)
  57. assert.ok(!view || !String(view.gap || "").includes("不作用于本面板"), id + " 不该有片区缺口: " + (view && view.gap))
  58. console.log("no district gap ok:", id)
  59. }
  60. console.log("SELFTEST PASS")