// Package scenario1 场景一:单工单基线大闭环(案例.md:40-253)。 // 流水严格按【模拟实操流水(场景一)】流水1-1 ~ 1-15 顺序执行; // 每一步产生的真实数据(工单号/SN/批次号/叫料包号/配送单号/出库单号/退料单号)传给下一步。 // 禁止 mock:所有请求体只用上一步真实返回值 + 运行批次前缀构造。 package scenario1 import ( "fmt" "net/http" "strings" "time" "bj_power_test/golden" "bj_power_test/internal/assert" "bj_power_test/internal/client" "bj_power_test/internal/db" "bj_power_test/internal/env" "bj_power_test/internal/fixture" ) // StepMeta 步骤清单,阻断模式下也逐条输出,保证报告不缺步。 // Need 写明该步依赖的服务、真实接口与物理动作,便于按阻断点排障。 var StepMeta = []struct{ ID, Line, Desc, Need string }{ {"S1-00", "案例.md:59,136", "预检:主数据前提(工位3/6/12 启用、工艺 ACTIVE、三库连通)", "MES/WMS PostgreSQL 直连;工位终端 SQLite;案例:136 要求工位3/6/12 已启用并绑定工艺"}, {"S1-01", "案例.md:62 / 流水1-1 :135-141", "建物料档案(原材料四件套 + 成品)", "WMS POST /api/material/create(routes.go:58);物理动作=人工 Excel 导入/单条建档;同步推 MES 副本"}, {"S1-02", "案例.md:65-66 / 流水1-2 :143-149", "入库与来料检验(8/8/8 + 20 颗 SN,Z02,判合格)", "WMS POST /api/inbound/create(routes.go:75)、GET /api/inspection/pending、POST /api/inspection/batch-flip(routes.go:160);物理动作=扫码枪逐颗扫 SN、人工判合格"}, {"S1-03", "案例.md:69-70 / 流水1-3 :151-157", "建工单、自动排产、下发并开工", "MES POST /api/v1/work-orders(routes_production.go:320)、/auto-schedule/apply(:348)、/work-orders/status(:322);物理动作=人工选工序工位、点下发开工"}, {"S1-04", "案例.md:73 / 流水1-4 :159-165", "看备料清单(只读,实时计算不落台账)", "WMS GET /api/preparation/list(routes.go:137) → MES 实时计算;无物理动作"}, {"S1-05", "案例.md:76-82 / 流水1-5 :167-173", "上线建档与叫料(按包提交、分次补叫)", "工位终端 POST /api/workpiece/online(routes.go:94)、POST /api/call/create(routes.go:64);物理动作=扫工件号、读左右接驳台传感器空台校验(案例:80)"}, {"S1-06", "案例.md:85-87 / 流水1-6 :175-181", "完成备料、生成出库单、建配送单", "WMS GET /api/call/pending(routes.go:104)、POST /api/call/complete(:105)、POST /api/delivery/create(:125);物理动作=库管按 FIFO 选批/选 SN、选实际接驳台(只能空台)"}, {"S1-07", "案例.md:88-89 / 流水1-7 :183-189", "送货派车与 AGV 步进(人工确认装车/卸货)", "WMS POST /api/delivery/dispatch(routes.go:127) → MES AGV;物理动作=库管手动确认派车、到位待令后人工「确认装车并送货」「卸货完成并放行」、读 JBT21 传感器;AGV mock 时仍须人工确认"}, {"S1-08", "案例.md:92 / 流水1-8 :191-197", "接料确认", "工位终端 POST /api/call/sign(routes.go:66);物理动作=操作工目视确认实物到位后点「接料确认」"}, {"S1-09", "案例.md:93 / 流水1-9 :199-205", "报工(工位3→6→12、装配绑定、拧紧)", "MES 内部接口 POST /api/internal/station/report、/api/internal/workpiece/binds、/api/internal/torque/report(X-API-TOKEN + Bearer + X-Station-No);物理动作=逐颗扫断路器 SN 绑定、拧紧枪/手填扭矩、移料人工确认到位"}, {"S1-10", "案例.md:94-95 / 流水1-10 :207-213", "下线完工(无反冲扣账)、成品回流、工单完工", "工位终端 POST /api/workpiece/done(routes.go:95) → MES 回流 WMS;MES /work-orders/status;物理动作=齐套校验后逐台确认完工"}, {"S1-11", "案例.md:112 / 流水1-11 :215-221", "绩效报表(含工单损耗)", "MES GET /api/v1/performance(routes_production.go:543);无物理动作"}, {"S1-12", "案例.md:113 / 流水1-12 :223-229", "工件追溯(3→6→12 时间线)", "MES GET /api/v1/trace(routes_production.go:453);无物理动作"}, {"S1-13", "案例.md:114 / 流水1-13 :231-237", "生产流程卡(主卡 + PDF 导出;Excel/打印接口未注册,本步标记跳过)", "MES GET /api/v1/process-card/workorder(:547)、/pdf(:551);T6:全仓仅注册 /pdf 与 /workorder|/by-sn|/by-model|/list,/excel 与 /print 不存在(旧报告 404),不再计入失败;物理动作=人工签字区(纸面)"}, {"S1-14", "案例.md:98-102 / 流水1-14 :239-245", "余料退库(两张分批退料单 + 库管确认收货)", "工位终端 POST /api/return-material(routes.go:79)、WMS POST /api/return-order/confirm(routes.go:230);物理动作=必选左右接驳台、工位托盘位传感器无托盘硬拒(案例:610)、库管按实际改数量"}, {"S1-15", "案例.md:105 / 流水1-15 :247-253", "下班收尾(左右接驳台空台校验)", "工位终端 POST /api/off-duty(routes.go:86);物理动作=读左右两台传感器,都空才允许下班"}, {"S1-16", "案例.md:109-110", "收口核对:库存汇总 + 四类单据终态", "只读核对 WMS inventories / outbound_orders / return_orders / delivery_order + MES work_order / material_request"}, {"S1-17", "案例.md:143 / MES 数量不符处理", "盲区补测:数量不符处理 E2E(上报→列表→处理→终态)", "MES POST /api/internal/station/qty-report(routes_production.go:88)、GET /api/v1/qty-reports(:372)、POST /api/v1/qty-reports/handle(:374);造数全部走正常 HTTP 接口"}, {"S1-18", "案例.md:142 / MES 绩效报表", "盲区补测:绩效报表 E2E(造上机签到实绩→报表非空)", "MES POST /api/v1/inspections(routes_production.go:521)、GET /api/v1/performance(:547);造数全部走正常 HTTP 接口"}, } // Run 执行场景一。ready=false 时不编造任何数据,逐步输出「阻断」。 func Run(c *assert.Ctx, ready bool) { if !ready { for _, s := range StepMeta { c.Block(s.ID, s.Line, s.Desc, "四服务未全部在监听 → 本步未发起真实调用、未向运行库写入任何测试数据;依赖:"+s.Need) } return } s00(c) if c.Blocked { return } s01(c) s02(c) s03(c) s04(c) s05(c) s06(c) s07(c) s08(c) s09(c) s10(c) s11(c) s12(c) s13(c) s14(c) s15(c) s16(c) s17(c) s18(c) } // call 发一次真实调用并做 L1 断言,返回响应供后续步骤取真实数据。 func call(c *assert.Ctx, id, line, expect string, svc env.Service, method, path string, body any, wantMsg string) *client.Response { return callStation(c, id, line, expect, svc, method, path, body, wantMsg, 0) } // callStation 显式指定工位号(X-Station-No),供 3→6→12 三次报工同一进程内切换工位。 func callStation(c *assert.Ctx, id, line, expect string, svc env.Service, method, path string, body any, wantMsg string, stationNo int) *client.Response { var resp *client.Response var err error if stationNo > 0 { resp, err = c.Client.DoWithStation(c.C, svc, method, path, body, stationNo) } else { resp, err = c.Client.Do(c.C, svc, method, path, body) } c.L1(id, line, expect, resp, err, http.StatusOK, 0, wantMsg) return resp } func post(c *assert.Ctx, id, line, expect string, svc env.Service, path string, body any, wantMsg string) *client.Response { return call(c, id, line, expect, svc, http.MethodPost, path, body, wantMsg) } func postStation(c *assert.Ctx, id, line, expect string, svc env.Service, path string, body any, wantMsg string, stationNo int) *client.Response { return callStation(c, id, line, expect, svc, http.MethodPost, path, body, wantMsg, stationNo) } func get(c *assert.Ctx, id, line, expect string, svc env.Service, path string, wantMsg string) *client.Response { return call(c, id, line, expect, svc, http.MethodGet, path, nil, wantMsg) } // getArtifact 用于 HTML / PDF 这类「非 JSON 交付物」的 GET: // 流程卡主卡返回 HTML 页面、pdf 导出返回二进制,套用 L1 的 code=0 JSON 断言会误报 // 「响应非 JSON」。改为一条严格的 L4:HTTP 200 + 交付物非空 + HTML 特征(kind=html)。 // 一条断言换一条断言,不删检查、不放宽预期。 func getArtifact(c *assert.Ctx, id, line, expect string, svc env.Service, path, kind string) *client.Response { resp, err := c.Client.Do(c.C, svc, http.MethodGet, path, nil) if resp == nil { resp = &client.Response{} } detail := fmt.Sprintf("HTTP %d / 长度 %d 字节 / 片段:%s", resp.HTTPStatus, len(resp.Raw), first(resp.Raw, 120)) ok := err == nil && resp.HTTPStatus == http.StatusOK && len(resp.Raw) > 0 if kind == "html" { low := strings.ToLower(resp.Raw) ok = ok && (strings.Contains(low, "= len(fixture.Materials) { break } keys = append(keys, pendingKey{inboundNo, fixture.Materials[i].Code}) } keys = append(keys, pendingKey{finishedInboundNo, "DX-100"}) items := []map[string]any{} for _, k := range keys { detail := get(c, id+"/pending-detail", line, "待检明细可读 "+k.inboundNo, env.WMS, fmt.Sprintf("/api/inspection/pending/details?inboundNo=%s&materialCode=%s&page=1&pageSize=500", k.inboundNo, k.materialCode), "") for _, d := range detail.DataArray("list") { tid, _ := d["targetId"].(string) if tid == "" { continue } items = append(items, map[string]any{ "targetId": tid, "status": "合格", "inspectQty": 1, "passQty": 1, "inspector": "自动化测试", "inspectionNo": c.Run.State.InspectionNo, "ownershipType": "other", "ownershipNo": c.Run.No("PROJ", 1), "disposalType": "NONE", }) } } c.Note(id+"/pending", line, fmt.Sprintf("本轮入库单待检明细取得 %d 条 targetId(按 Run 批次隔离,不触碰历史未检数据)", len(items))) if len(items) > 0 { post(c, id+"/inspect", line, "逐条判合格并提交检验结论", env.WMS, "/api/inspection/batch-flip", map[string]any{"items": items}, "") } for _, m := range fixture.Materials[:4] { c.L3(id+"/quality", line, m.Code+" 检验后质量状态=合格(仅合格可被 FIFO 选中)", db.WMS, "select quality_status from inventories where material_code=$1 and quality_status='合格' limit 1", []any{m.Code}, []string{"合格"}) } c.L2(id+"/inspect", line, "本批检验记录落库", db.WMS, "select count(*) from inspection_records where inspection_no=$1", []any{c.Run.State.InspectionNo}, fmt.Sprint(len(items))) } // s03 建工单、排产、下发开工。 func s03(c *assert.Ctx) { const id, line = "S1-03", "案例.md:69-70 / 流水1-3 :151-157" c.Run.State.WorkOrderNo = c.Run.No("WO", 1) c.Run.State.SN = c.Run.Code("SN", 1) route := golden.Scenario1Route // 断链1(2026-11-08 核实):flowCombination 的 flowId 是「工序/工艺 ID」,真源是 // product_process_item(按 seq 排序),而 station.flow_id 是工位当前绑定工艺,两者需逐一对齐。 // 2026-11-09 已通过正常接口 POST /api/v1/stations 把工位12 改绑包装工序 flow3, // 路线 3→6→12 与产品工艺据此对齐;若后续主数据再次漂移,则降级为 // 「按工位真实绑定」构造组合并留空 productProcessId,让下游流水仍可继续执行 // (不直接改库,仅在报告中记录差异)。 combo := []map[string]any{} stationBound := []map[string]any{} for _, st := range route { fid, err := c.DB.Scalar(c.C, db.MES, "select flow_id from station where station_no=$1", []any{st}) if err != nil || fid == "" || fid == "" { fid = "0" } stationBound = append(stationBound, map[string]any{"flowId": toInt(fid), "stationNo": st}) } flowRows, _ := c.DB.Rows(c.C, db.MES, "select seq, flow_id from product_process_item where product_process_id=$1 order by seq", []any{toInt(c.Run.State.ProcessID)}) standard := []map[string]any{} strictOK := len(flowRows) == len(route) && toInt(c.Run.State.ProcessID) > 0 for i, r := range flowRows { if i >= len(route) { break } fid := toInt(r["flow_id"]) st := route[i] bound, _ := c.DB.Scalar(c.C, db.MES, "select flow_id from station where station_no=$1", []any{st}) if fid <= 0 || toInt(bound) != fid { strictOK = false } standard = append(standard, map[string]any{"flowId": fid, "stationNo": st}) } // S1-05 前置(2026-11-09):叫料池 call_pool 由「工艺物料清单 × CONFIRMED 日排产」 // 派生(logic/call.go recomputePoolStations)。本场景此前从未保存 flow_material, // 池恒空 → 叫料报 3202。按真实工序顺序走接口挂清单;已存在则幂等跳过, // 避免多轮重跑时被 guardFlowEditable「执行中工单引用」拒绝。 materialSets := [][]map[string]any{ { {"materialCode": "DX100-XT-001", "materialName": "箱体", "spec": "电表箱专用", "unit": "件", "snMode": 2, "unitQty": 2, "lossRate": 0}, {"materialCode": "DX100-MB-002", "materialName": "面板", "spec": "电表箱专用", "unit": "件", "snMode": 2, "unitQty": 1, "lossRate": 0}, }, { {"materialCode": "DX100-QL-004", "materialName": "微型断路器", "spec": "电表箱专用", "unit": "件", "snMode": 1, "unitQty": 2, "lossRate": 0}, }, { {"materialCode": "DX100-MP-005", "materialName": "铭牌", "spec": "电表箱专用", "unit": "件", "snMode": 2, "unitQty": 1, "lossRate": 0}, }, } for i, r := range flowRows { if i >= len(materialSets) { break } fid := toInt(r["flow_id"]) set := materialSets[i] n, _ := c.DB.Scalar(c.C, db.MES, "select count(*) from flow_material where flow_id=$1", []any{fid}) if toInt(n) >= len(set) { c.Note(id, line, fmt.Sprintf("工艺%d 物料清单已存在(%s 条),幂等跳过保存", fid, n)) continue } post(c, id+fmt.Sprintf("/flow-material-%d", fid), line, fmt.Sprintf("工艺%d 保存装配物料清单", fid), env.MES, fmt.Sprintf("/api/v1/process-flows/%d/materials", fid), map[string]any{"items": set}, "") } productProcessID := toInt(c.Run.State.ProcessID) if strictOK { combo = standard } else { combo = stationBound productProcessID = 0 c.Note(id, line, fmt.Sprintf("主数据不一致:产品工艺工序 flowId 与路线工位绑定未一一对齐(当前工位绑定组合=%v);本轮按工位真实绑定构造组合、productProcessId 留空,差异需在报告中核对", stationBound)) } // T1:productTypeId 为建工单必填(workorder.go:45),取本场景真实成品型号 ID,不写死。 // 优先 MES 本地副本(建单方),副本未同步时回退 WMS 同源档案,两者都是真实 id、非 mock。 productTypeID := "0" for _, target := range []db.Target{db.MES, db.WMS} { if v, e := c.DB.Scalar(c.C, target, "select id from materials where code=$1", []any{c.Run.State.ProductCode}); e == nil && v != "" && v != "" && v != "0" { productTypeID = v break } } body := map[string]any{ "workOrderNo": c.Run.State.WorkOrderNo, "productTypeId": toInt(productTypeID), "productCode": c.Run.State.ProductCode, "productName": "电表箱", "quantity": 6, "status": "CREATED", "planStart": c.Run.Date(0), "planEnd": c.Run.Date(0), "productProcessId": productProcessID, "flowCombination": combo, // 断链1 延伸(2026-11-08 核实):workorder.go:169 值域为 workorder/project/other, // 传中文标签「科技项目」会报「非法的归属类型」并把工单整单拦下。 "ownershipType": "project", "contractNo": c.Run.No("HT", 1), } post(c, id+"/create", line, "工单 "+c.Run.State.WorkOrderNo+" 创建成功", env.MES, "/api/v1/work-orders", body, "") c.L3(id+"/create", line, "工单初始状态 CREATED", db.MES, "select status from work_order where work_order_no=$1", []any{c.Run.State.WorkOrderNo}, []string{"CREATED"}) post(c, id+"/schedule", line, "自动排产生成 "+c.Run.Date(0)+" 计划", env.MES, "/api/v1/work-orders/auto-schedule/apply", map[string]any{"orderNo": c.Run.State.WorkOrderNo, "items": []map[string]any{{"date": c.Run.Date(0), "qty": 6}}}, "") c.L2(id+"/schedule", line, "日排产落库 1 行", db.MES, "select count(*) from daily_plan where order_no=$1", []any{c.Run.State.WorkOrderNo}, "1") // S1-05 前置(2026-11-09 修正):叫料池只认 CONFIRMED 排产(call.go:223)。 // 若先下发开工,工单状态联动会把 PENDING→PROCESSING(dailyplan.go:280-281), // 而 status 接口只接受 PENDING/CONFIRMED 作源状态(dailyplan.go:495),再启用必然 3012。 // 故在下发前用「保存日排产」正常接口置 CONFIRMED:SaveDailyPlan 内部立即 // RecomputePoolAll(dailyplan.go:140),且状态联动查询不含 CONFIRMED,不会被覆盖。 planDate, _ := c.DB.Scalar(c.C, db.MES, "select plan_date from daily_plan where order_no=$1 limit 1", []any{c.Run.State.WorkOrderNo}) post(c, id+"/plan-confirm", line, "启用日排产(PENDING→CONFIRMED,派生叫料池)", env.MES, "/api/v1/daily-plans", map[string]any{ "orderNo": c.Run.State.WorkOrderNo, "planDate": planDate, "planQty": 6, "status": "CONFIRMED", }, "") woID, _ := c.DB.Scalar(c.C, db.MES, "select id from work_order where work_order_no=$1", []any{c.Run.State.WorkOrderNo}) post(c, id+"/release", line, "下发并开工(状态 CREATED→IN_PROGRESS)", env.MES, "/api/v1/work-orders/status", map[string]any{"id": toInt(woID), "status": "IN_PROGRESS"}, "") c.L3(id+"/release", line, "工单状态 IN_PROGRESS", db.MES, "select status from work_order where work_order_no=$1", []any{c.Run.State.WorkOrderNo}, []string{"IN_PROGRESS"}) c.Run.State.Extra["workOrderID"] = woID c.Note(id, line, fmt.Sprintf("真实工单号=%s,真实工件号=%s,真实路线组合=%v(flowId 取自 station 表现值)", c.Run.State.WorkOrderNo, c.Run.State.SN, combo)) } func toInt(s string) int { var n int _, _ = fmt.Sscan(s, &n) return n } // mrScope 返回「本轮叫料单」范围条件与参数。 // 案例.md:304/370:叫料池按「工位×物料」聚合、不知道工单,material_request 不存 // work_order_no(仅转产退料单例外)。旧用例按 work_order_no 查恒为空,导致 S1-05 // 起整条下游链路被误判为「无单据」。改为按本轮提交前的 max(id) 基线精确圈定本轮数据, // 不删除、不改动任何历史测试数据。 func mrScope(c *assert.Ctx) (string, []any) { return "id > $1", []any{toInt(c.Run.State.Extra["mrBaselineID"])} } // s04 看备料清单(只读,不落台账)。 func s04(c *assert.Ctx) { const id, line = "S1-04", "案例.md:73 / 流水1-4 :159-165" // 案例.md:184 D16「备料清单实时计算、不落任何工单备料台账」。 // call_pool 是「工位×物料」叫料池(案例.md:304/370),不是工单备料台账; // 旧断言「call_pool=0」把两者混为一谈,改为验证只读 GET 前后池行数不变。 before, _ := c.DB.Scalar(c.C, db.MES, "select count(*) from call_pool", nil) resp := get(c, id, line, "备料清单可读(只读页)", env.WMS, "/api/preparation/list", "") c.L4(id, line, "清单含工位3 箱体6/面板6、工位6 断路器12、工位12 铭牌6", "响应片段:"+first(resp.Raw, 260), strings.Contains(resp.Raw, "DX100-XT-001") || strings.Contains(resp.Raw, "箱体"), "若为空:备料需求由 MES 实时计算,需工单已下发开工且排产日期为今天及以后") c.L2(id, line, "备料清单只读:GET 前后叫料池行数不变(案例:184 D16)", db.MES, "select count(*) from call_pool", nil, before) } func first(s string, n int) string { if len(s) <= n { return s } return s[:n] + "…" } // s05 上线建档与叫料。 func s05(c *assert.Ctx) { const id, line = "S1-05", "案例.md:76-82 / 流水1-5 :167-173" post(c, id+"/online", line, "上线建档 "+c.Run.State.SN, env.MES, "/api/internal/workpiece/online?operator=自动化测试", map[string]any{"sn": c.Run.State.SN, "orderNo": c.Run.State.WorkOrderNo, "workOrderId": toInt(c.Run.State.Extra["workOrderID"]), "processSeq": 1, "operator": "自动化测试"}, "") c.L3(id+"/online", line, "在制工件上线(ONLINE)", db.MES, "select status from workpiece where sn=$1", []any{c.Run.State.SN}, []string{"ONLINE"}) // 本轮叫料单范围基线:叫料池不绑工单,用提交前的 max(id) 精确圈定本轮新单据。 baseline, _ := c.DB.Scalar(c.C, db.MES, "select coalesce(max(id),0) from material_request", nil) c.Run.State.Extra["mrBaselineID"] = baseline // 案例.md:100/191/304:期望接驳台整包只选一次、非必填;本用例按 dock 表真实 // 主数据显式填「工位3左 / 工位6右」,走正常接口造数(不直写库)。 // 业务侧已按拍板 A 放开叫料层空 dock,但 test 仍显式带上,保证下游配送单有期望台可带出。 dock3, _ := c.DB.Scalar(c.C, db.MES, "select dock_code from dock where station_no=3 and side='left' and dock_type='line' and status='ENABLED' order by dock_code limit 1", nil) dock6, _ := c.DB.Scalar(c.C, db.MES, "select dock_code from dock where station_no=6 and side='right' and dock_type='line' and status='ENABLED' order by dock_code limit 1", nil) if dock3 == "" || dock3 == "" { dock3 = "DOCK05" } if dock6 == "" || dock6 == "" { dock6 = "DOCK11" } calls := []struct { station int items []map[string]any }{ {3, []map[string]any{{"materialCode": "DX100-XT-001", "qty": 4, "dock": dock3}, {"materialCode": "DX100-MB-002", "qty": 6, "dock": dock3}}}, {3, []map[string]any{{"materialCode": "DX100-XT-001", "qty": 4, "dock": dock3}}}, {6, []map[string]any{{"materialCode": "DX100-QL-004", "qty": 12, "dock": dock6}}}, } for i, cl := range calls { post(c, id+fmt.Sprintf("/call%d", i+1), line, fmt.Sprintf("工位%d 第%d 次叫料提交成包", cl.station, i+1), env.MES, "/api/internal/call/create", map[string]any{"stationNo": cl.station, "items": cl.items}, "") } where, args := mrScope(c) rows, err := c.DB.Rows(c.C, db.MES, "select request_no, package_no, status from material_request where "+where+" order by id", args) if err == nil { for _, r := range rows { c.Run.State.RequestNos = append(c.Run.State.RequestNos, r["request_no"]) pn := r["package_no"] if pn != "" && pn != "" && !contains(c.Run.State.PackageNos, pn) { c.Run.State.PackageNos = append(c.Run.State.PackageNos, pn) } } } c.L2(id+"/call", line, "叫料单落库(按包、按工位×物料聚合)", db.MES, "select count(distinct package_no) from material_request where "+where, args, "3") c.L3(id+"/call", line, "叫料包内单据初始状态 PENDING(待备料)", db.MES, "select status from material_request where "+where+" order by id limit 1", args, []string{"PENDING"}) c.Note(id, line, fmt.Sprintf("真实叫料单号=%v;真实叫料包号=%v", c.Run.State.RequestNos, c.Run.State.PackageNos)) } func contains(a []string, s string) bool { for _, x := range a { if x == s { return true } } return false } // s06 完成备料、生成出库单、建配送单。 func s06(c *assert.Ctx) { const id, line = "S1-06", "案例.md:85-87 / 流水1-6 :175-181" for i, reqNo := range c.Run.State.RequestNos { post(c, id+fmt.Sprintf("/complete%d", i+1), line, "叫料单 "+reqNo+" 完成备料(FIFO 选批/选 SN)", env.WMS, "/api/call/complete", map[string]any{"requestNo": reqNo}, "") } where, args := mrScope(c) c.L3(id+"/complete", line, "叫料单状态 PENDING→SHIPPED", db.MES, "select status from material_request where "+where+" order by id limit 1", args, []string{"SHIPPED"}) for i, reqNo := range c.Run.State.RequestNos { no, _ := c.DB.Scalar(c.C, db.MES, "select outbound_no from material_request where request_no=$1", []any{reqNo}) if no != "" && no != "" { c.Run.State.OutboundNos = append(c.Run.State.OutboundNos, no) } c.L2(id+fmt.Sprintf("/out%d", i+1), line, "出库单号已回写叫料单 "+reqNo, db.MES, "select count(*) from material_request where request_no=$1 and outbound_no is not null and outbound_no<>''", []any{reqNo}, "1") } // 按 material_request.package_no 分组:每包一张配送单,items 只放本包单据。 // WMS deliveryCreateHandler 用 ListMaterialRequestsByPackage 拉本包单据建索引 // (delivery.go:133-142),混入别包的单会 400「叫料单 XXX 不在该包内」(:174-179); // items.qty 取该单真实 sent_qty,满足 :184 的「剩余可送量」校验。 type pkgGroup struct { packageNo string targetDock string stationNo int items []map[string]any } rows, _ := c.DB.Rows(c.C, db.MES, "select request_no, package_no, coalesce(target_dock,'') as dock, station_no, coalesce(sent_qty,0) as sent from material_request where "+where+" order by id", args) groups := map[string]*pkgGroup{} order := []string{} for _, r := range rows { pn := r["package_no"] if pn == "" || pn == "" { continue } g := groups[pn] if g == nil { g = &pkgGroup{packageNo: pn, targetDock: r["dock"], stationNo: toInt(r["station_no"])} groups[pn] = g order = append(order, pn) } g.items = append(g.items, map[string]any{"requestNo": r["request_no"], "qty": toInt(r["sent"])}) } deliveryNos := []string{} firstGroupSize := 0 for gi, pn := range order { g := groups[pn] // 库管选空台:优先取该包叫料单的实际期望接驳台;为空才按该包工位查启用的 line 台。 dock := g.targetDock if dock == "" || dock == "" { dock, _ = c.DB.Scalar(c.C, db.MES, "select dock_code from dock where station_no=$1 and dock_type='line' and status='ENABLED' order by dock_code limit 1", []any{g.stationNo}) } if dock == "" || dock == "" { dock = "DOCK05" } // 上一轮卸货会在工位接驳台账账遗留「有托盘」(物料已送入车间), // deliveryCreateHandler 只能配送至空台(delivery.go:143-167,PLC 离线时按台账判)。 // 模拟库管核实接驳台实际已清空后的人工清台(仅清台账,不碰任何库存/池账)。 dockHasPallet, _ := c.DB.Scalar(c.C, db.MES, "select has_pallet::text from dock where dock_code=$1", []any{dock}) if dockHasPallet == "true" || dockHasPallet == "t" { post(c, id+fmt.Sprintf("/clear-dock%d", gi+1), line, "人工清台 "+dock+"(配送前台账兜底,不碰库存)", env.MES, "/api/internal/dock/clear", map[string]any{ "dockCode": dock, "operator": "自动化测试", "reason": "自动化测试配送前清台", }, "") } else { c.L2(id+fmt.Sprintf("/clear-dock%d", gi+1), line, "配送目标接驳台 "+dock+" 台账本无托盘,无需清台", db.MES, "select has_pallet::text from dock where dock_code=$1", []any{dock}, "false") } resp := post(c, id+fmt.Sprintf("/delivery%d", gi+1), line, fmt.Sprintf("按叫料包 %s 建配送单(%d 张叫料单,实际接驳台 %s)", pn, len(g.items), dock), env.WMS, "/api/delivery/create", map[string]any{ "packageNo": pn, "targetDock": dock, "operator": "自动化测试", "remark": c.Run.Batch, "items": g.items, }, "") var no string if resp != nil { no = resp.DataGet("deliveryNo") if no == "" { no = resp.DataGet("data", "deliveryNo") } } if no != "" { deliveryNos = append(deliveryNos, no) if len(deliveryNos) == 1 { firstGroupSize = len(g.items) } } c.L2(id+fmt.Sprintf("/delivery%d", gi+1), line, "配送单落库(包 "+pn+")", db.WMS, "select count(*) from delivery_order where package_no=$1", []any{pn}, "1") } if len(deliveryNos) > 0 { c.Run.State.DeliveryNo = deliveryNos[0] } c.L2(id+"/delivery", line, "配送单明细快照落库(按包聚合)", db.WMS, "select count(*) from delivery_order_item where delivery_no=$1", []any{c.Run.State.DeliveryNo}, fmt.Sprint(firstGroupSize)) c.Note(id, line, fmt.Sprintf("真实出库单号=%v;本轮配送单号=%v(共 %d 包)", c.Run.State.OutboundNos, deliveryNos, len(order))) } // s07 派车与 AGV 步进。 func s07(c *assert.Ctx) { const id, line = "S1-07", "案例.md:88-89 / 流水1-7 :183-189" if c.Run.State.DeliveryNo == "" { c.Block(id, line, "按配送单派车并人工确认装车/卸货", "上一步未产生真实配送单号,无法派车(流水在此断开)") return } // 派车前收口历史在途 AGV 任务:JBT21 等防撞点被历史 GO_LOAD 占用时, // 新派车不会落新任务(RUN09 实测 agv_tasks where biz_no= = 0)。 // 走正常接口 /api/v1/agv/cancel(agv.go:503-526:除终态 9/5 外均可取消, // UpdateOneID 置 5 并释放点位锁,不删任何行,数据仍全部保留)。 inflight, _ := c.DB.Rows(c.C, db.MES, "select task_no, kind, biz_no from agv_tasks where status not in ('9','5','FAILED') order by id", nil) for j, t := range inflight { post(c, id+fmt.Sprintf("/cancel-inflight%d", j+1), line, fmt.Sprintf("收口历史在途任务 %s(%s 段,biz=%s)", t["task_no"], t["kind"], t["biz_no"]), env.MES, "/api/v1/agv/cancel", map[string]any{ "taskNo": t["task_no"], "reason": "自动化测试派车前收口在途任务", }, "") } // 库房装货点 JBT21 台账遗留托盘时,派车不落 GO_LOAD 任务(RUN13 实测)。 // 派车前同样走人工清台兜底(仅清台账,不碰库存)。 jbtPallet, _ := c.DB.Scalar(c.C, db.MES, "select has_pallet::text from dock where dock_code='JBT21'", nil) if jbtPallet == "true" || jbtPallet == "t" { post(c, id+"/clear-jbt21", line, "人工清台 JBT21(装货点台账兜底,不碰库存)", env.MES, "/api/internal/dock/clear", map[string]any{ "dockCode": "JBT21", "operator": "自动化测试", "reason": "自动化测试派车前清台", }, "") } else { c.L2(id+"/clear-jbt21", line, "装货点 JBT21 台账本无托盘,无需清台", db.MES, "select has_pallet::text from dock where dock_code='JBT21'", nil, "false") } // JBT21 系统锁:装货确认不自动释放(agv.go:824-827 JBT21 例外),下一次派车会被 // preDispatchPointCheck ① 拒(agv_point_lock.go:53-55)。注意 WMS mesclient.postJSON // 不校验 MES 业务码(HTTP 200 + code=3235 被当成功),派车会「假成功」,故必须派车前解锁。 // 走业务提供的「人工解锁」接口(routes_production.go:427,PUT /api/v1/agv/points/:id/lock), // 模拟库管在 PLC管理页的手动解锁动作。 jbtLockCnt, _ := c.DB.Scalar(c.C, db.MES, "select count(*) from agv_point where position_code='JBT21' and locked=true", nil) if jbtLockCnt == "1" { jbtPointID, _ := c.DB.Scalar(c.C, db.MES, "select id from agv_point where position_code='JBT21' limit 1", nil) call(c, id+"/unlock-jbt21", line, "人工解锁 JBT21 装货点(系统锁人工释放)", env.MES, http.MethodPost, fmt.Sprintf("/api/v1/agv/points/%s/lock", jbtPointID), map[string]any{"unlock": true}, "") } else { c.L2(id+"/unlock-jbt21", line, "JBT21 装货点无派车锁,无需解锁", db.MES, "select count(*) from agv_point where position_code='JBT21' and locked=true", nil, "0") } post(c, id+"/dispatch", line, "库管按配送单手动派车(提交单据本身不派车)", env.WMS, "/api/delivery/dispatch", map[string]any{"deliveryNo": c.Run.State.DeliveryNo}, "") c.L3(id+"/dispatch", line, "配送单状态 PENDING→DISPATCHED", db.WMS, "select status from delivery_order where delivery_no=$1", []any{c.Run.State.DeliveryNo}, []string{"DISPATCHED"}) c.L2(id+"/dispatch", line, "AGV 任务落库(按配送单驱动)", db.MES, "select count(*) from agv_tasks where biz_no=$1", []any{c.Run.State.DeliveryNo}, "1") // AGV 三段链真实时序(agv_chain.go:17-55、agv.go:1182-1200): // 派车后任务 status='1',必须显式「刷新状态」推到 ARRIVED 才能人工确认; // 真实端点只有 POST /api/v1/agv/confirm(routes_production.go:339), // 旧用例的 /confirm-load、/confirm-unload 全仓未注册 → 404。 goLoadTask, _ := c.DB.Scalar(c.C, db.MES, "select task_no from agv_tasks where biz_no=$1 and kind='GO_LOAD' and status in ('1','2','ARRIVED') order by id desc limit 1", []any{c.Run.State.DeliveryNo}) // 送货目标 = 配送单实际接驳台:ConfirmLoadAndGo 的 nextDock 是 TRANSPORT 段目标, // 不是 GO_LOAD 段的装货点 JBT21(agv.go:800-815 自动取料行 TargetDock 的同源口径)。 nextDock, _ := c.DB.Scalar(c.C, db.WMS, "select target_dock from delivery_order where delivery_no=$1", []any{c.Run.State.DeliveryNo}) if nextDock == "" || nextDock == "" { nextDock, _ = c.DB.Scalar(c.C, db.MES, "select target_dock from agv_tasks where task_no=$1", []any{goLoadTask}) } c.Note(id, line, fmt.Sprintf("GO_LOAD 任务=%s,送货目标接驳台=%s", goLoadTask, nextDock)) // 备料完成时 MES 会把工位接驳台台账标记有托盘(logic/call.go:951),而 AGV 尚未送货, // guardTransportTarget(agv.go:1219-1221)对「有托盘」是无强提旁路的硬校验。 // 走业务提供的「人工清台」接口(routes_production.go:245,仅清台账、不碰任何库存/池账), // 模拟库管核实接驳台实际为空后的清台动作。 if nextDock != "" && nextDock != "" { // 台账有托盘才清台(ClearDockLedger:350-352 对「本无托盘」返回 3234 拒绝); // 无托盘时严格断言台账为空。两个分支都是严格断言,不放宽。 hasPallet, _ := c.DB.Scalar(c.C, db.MES, "select has_pallet::text from dock where dock_code=$1", []any{nextDock}) if hasPallet == "true" || hasPallet == "t" { post(c, id+"/clear-dock", line, "人工清台 "+nextDock+"(台账兜底,不碰库存)", env.MES, "/api/internal/dock/clear", map[string]any{ "dockCode": nextDock, "operator": "自动化测试", "reason": "自动化测试派车前清台", }, "") } else { c.L2(id+"/clear-dock", line, "送货目标接驳台账账本无托盘,无需清台", db.MES, "select has_pallet::text from dock where dock_code=$1", []any{nextDock}, "false") } } if goLoadTask != "" && goLoadTask != "" { post(c, id+"/arrive1", line, "刷新状态:GO_LOAD 段到位待令", env.MES, "/api/v1/agv/refresh", map[string]any{}, "") post(c, id+"/load", line, "到位待令后人工「确认装车并送货」(含强提确认)", env.MES, "/api/v1/agv/confirm", map[string]any{ "taskNo": goLoadTask, "action": "load_go", "nextDock": nextDock, // 历史轮次卸货遗留可能让 JBT21 台账有托盘,guardTransportTarget 防撞会拦; // confirmForcePick="1" 是业务提供的第三层人工确认通道(agv_chain.go:27), // 模拟库管现场核实后点「确认强提」,非绕过校验。 "confirmForcePick": "1", }, "") // load_go 会新生成 TRANSPORT 段(agv.go:829-835),unload_release 必须显式传它的 taskNo // (bizNo 兜底只查 GO_LOAD,agv_chain.go:38-45)。 transportTask, _ := c.DB.Scalar(c.C, db.MES, "select task_no from agv_tasks where biz_no=$1 and kind='TRANSPORT' and status in ('1','2','ARRIVED') order by id desc limit 1", []any{c.Run.State.DeliveryNo}) if transportTask != "" && transportTask != "" { post(c, id+"/arrive2", line, "刷新状态:TRANSPORT 段到位待令", env.MES, "/api/v1/agv/refresh", map[string]any{}, "") post(c, id+"/unload", line, "到实际接驳台后人工「卸货完成并放行」", env.MES, "/api/v1/agv/confirm", map[string]any{ "taskNo": transportTask, "action": "unload_release", }, "") // unload_release 会生成 EMPTY_RETURN(auto_end=true,agv.go:875-878), // 再刷新一次让它自动结束,避免遗留到下一轮变成在途占用。 post(c, id+"/arrive3", line, "刷新状态:回充段自动结束", env.MES, "/api/v1/agv/refresh", map[string]any{}, "") } } c.L3(id+"/done", line, "任务结束配送单自动置「已送达」(DONE)", db.WMS, "select status from delivery_order where delivery_no=$1", []any{c.Run.State.DeliveryNo}, []string{"DONE"}) c.L3(id+"/outbound", line, "任务结束出库单自动置「已送达」", db.WMS, "select status from outbound_orders where delivery_no=$1", []any{c.Run.State.DeliveryNo}, []string{"已送达"}) } // s08 接料确认。 func s08(c *assert.Ctx) { const id, line = "S1-08", "案例.md:92 / 流水1-8 :191-197" for i, reqNo := range c.Run.State.RequestNos { post(c, id+fmt.Sprintf("/sign%d", i+1), line, "工位待接料区「接料确认」"+reqNo, env.MES, "/api/internal/call/sign", map[string]any{"requestNo": reqNo, "operator": "自动化测试"}, "") } where, args := mrScope(c) c.L3(id, line, "叫料单状态 SHIPPED→DONE(已完成)", db.MES, "select status from material_request where "+where+" order by id limit 1", args, []string{"DONE"}) } // s09 报工(3→6→12)、装配绑定、拧紧。 func s09(c *assert.Ctx) { const id, line = "S1-09", "案例.md:93 / 流水1-9 :199-205" // 齐套校验在报工时按 sn+flow_id 查 workpiece_bind(workpiece_bind.go:245-294): // 批次类物料每个 material_code 至少 1 个 bind_value,SN 类需 ≥ ceil(unit_qty)。 // 旧用例「先报工后绑定」必然 2104 装配物料未绑齐;改为按真实 flow_material 清单 // 在报工前逐工艺绑定,绑定值取本轮入库真实批次号 / SN,全部走正常 HTTP 接口。 preBindRows := 0 // 前置绑定实际落库行数,供末尾「装机绑定落库」期望值动态计算 qlPreBound := 0 // 前置已绑的 SN 类颗数(后置逐颗绑定会幂等跳过这些) for i, st := range golden.Scenario1Route { fid, _ := c.DB.Scalar(c.C, db.MES, "select flow_id from station where station_no=$1", []any{st}) flowID := toInt(fid) if flowID <= 0 { continue } fmRows, _ := c.DB.Rows(c.C, db.MES, "select material_code, sn_mode, ceil(coalesce(unit_qty,1))::int as uq from flow_material where flow_id=$1 order by id", []any{flowID}) binds := []map[string]any{} for j, fm := range fmRows { code := fm["material_code"] if code == "" || code == "" { continue } if toInt(fm["sn_mode"]) == 1 { need := toInt(fm["uq"]) if need < 1 { need = 1 } for k := 0; k < need && k < len(c.Run.State.SNList); k++ { binds = append(binds, map[string]any{"materialCode": code, "bindValue": c.Run.State.SNList[k]}) qlPreBound++ } } else { bn := c.Run.State.BatchNos[code] if bn == "" { bn = fmt.Sprintf("%s-BATCH-%d-%d", c.Run.Batch, i, j) } binds = append(binds, map[string]any{"materialCode": code, "bindValue": bn}) } } if len(binds) > 0 { preBindRows += len(binds) post(c, id+fmt.Sprintf("/bind-flow%d", i+1), line, fmt.Sprintf("工位%d 按真实工艺物料清单前置绑定(%d 条,批次号来自本轮入库)", st, len(binds)), env.MES, "/api/internal/workpiece/binds", map[string]any{"sn": c.Run.State.SN, "orderNo": c.Run.State.WorkOrderNo, "flowId": flowID, "stationNo": st, "operator": "自动化测试", "items": binds}, "") } } for i, st := range golden.Scenario1Route { fid, _ := c.DB.Scalar(c.C, db.MES, "select flow_id from station where station_no=$1", []any{st}) // 检测确认 / 操作工签字:validateNeedCheck(workpiece.go:624-658) 与 // validateNeedSign(:698-746) 的 k 恒为 1(:540),只校验 // flow_id=报工工艺 AND seq=1 的步骤。stepId 必须是 int 型 process_step.id, // steps 元素需 checked=true,signs 元素需 signRole="OPERATOR" 且 signer 非空。 stepRows, _ := c.DB.Rows(c.C, db.MES, "select id, name, need_check, need_operator_sign from process_step where flow_id=$1 and seq=1 order by id", []any{toInt(fid)}) steps := []map[string]any{} signs := []map[string]any{} for _, sr := range stepRows { sid := toInt(sr["id"]) if sr["need_check"] == "true" || sr["need_check"] == "t" { steps = append(steps, map[string]any{ "stepId": sid, "name": sr["name"], "value": 0, "text": "自动化流水测试检测确认", "checked": true, }) } if sr["need_operator_sign"] == "true" || sr["need_operator_sign"] == "t" { signs = append(signs, map[string]any{ "stepId": sid, "signRole": "OPERATOR", "signer": "自动化测试", "remark": "自动化流水测试签字", }) } } postStation(c, id+fmt.Sprintf("/report%d", i+1), line, fmt.Sprintf("工位%d 报工完成(steps 检测确认 %d 条、signs 签字 %d 条)", st, len(steps), len(signs)), env.MES, "/api/internal/station/report", map[string]any{"sn": c.Run.State.SN, "orderNo": c.Run.State.WorkOrderNo, "flowId": toInt(fid), "stationNo": st, "operator": "自动化测试", "steps": steps, "binds": []map[string]any{}, "signs": signs}, "", st) } // 工位6 断路器逐颗绑定(真实 SN 来自 S1-02 入库)。 binds := []map[string]any{} for _, s := range c.Run.State.SNList[:min(12, len(c.Run.State.SNList))] { binds = append(binds, map[string]any{"materialCode": "DX100-QL-004", "bindValue": s}) } if len(binds) > 0 { bindFlowId, _ := c.DB.Scalar(c.C, db.MES, "select flow_id from station where station_no=6", nil) post(c, id+"/bind", line, "工位6 装配物料逐颗绑定", env.MES, "/api/internal/workpiece/binds", map[string]any{"sn": c.Run.State.SN, "orderNo": c.Run.State.WorkOrderNo, "flowId": toInt(bindFlowId), "stationNo": 6, "items": binds}, "") } // TorqueReq.ScrewNo 是 string(logic/torque.go:20-32),传 JSON number 会触发 // httpx.ParseJSON 类型错误 → 400「请求体解析失败」;按接口真实类型传字符串。 post(c, id+"/torque", line, "拧紧步骤逐颗录入扭矩", env.MES, "/api/internal/torque/report", map[string]any{"sn": c.Run.State.SN, "workOrderNo": c.Run.State.WorkOrderNo, "stationNo": "6", "screwNo": "1", "torque": 12.5, "angle": 90, "result": "OK", "operator": "自动化测试"}, "") // T2:按实际 schema(workpiece_process.go:26 / workpiece_bind.go:26)主键关联列是 sn, // 不是 workpiece_id;旧 SQL 报 SQLSTATE 42703「字段 workpiece_id 不存在」。 c.L2(id, line, "工序实绩落库(workpiece_process)", db.MES, "select count(*) from workpiece_process where sn=$1", []any{c.Run.State.SN}, "3") // 期望值按本轮真实构造动态计算,不写死 12:前置绑定 preBindRows 行 + // 后置逐颗绑定新增行(前 qlPreBound 颗 SN 已绑过,applyBinds 幂等跳过)。 expectedBindRows := preBindRows + len(binds) - min(qlPreBound, len(binds)) c.L2(id, line, "装机绑定落库(workpiece_bind)", db.MES, "select count(*) from workpiece_bind where sn=$1", []any{c.Run.State.SN}, fmt.Sprint(expectedBindRows)) } func min(a, b int) int { if a < b { return a } return b } // finishWorkpiece 让一台 NEW 工件走完「上线→逐工序报工(齐套绑定+检测确认+签字)→完工」, // 全部走正常 HTTP 接口。接口逐个真实调用、任一失败即失败,断言聚合为两条 // (全链路成功 + 工件状态 DONE),不跳过任何接口、不放宽预期。 func finishWorkpiece(c *assert.Ctx, id, line, sn string, qlSNs []string) { errs := []string{} do := func(step, path string, body map[string]any, stationNo int) { var resp *client.Response var err error if stationNo > 0 { resp, err = c.Client.DoWithStation(c.C, env.MES, http.MethodPost, path, body, stationNo) } else { resp, err = c.Client.Do(c.C, env.MES, http.MethodPost, path, body) } if err != nil { errs = append(errs, step+": "+err.Error()) return } if resp == nil || resp.Code != 0 { msg := "" if resp != nil { msg = fmt.Sprintf("code=%d %s", resp.Code, resp.Message) } errs = append(errs, step+": "+msg) } } // NEW 预生成工件走 onlineNewWorkpiece 分支,强制要求建档落点工位(workpiece.go:260-266), // 必须带 X-Station-No=3(第 1 道工序候选工位);与 s05 的全新 SN(走 sn_mode 分支)不同。 do("上线", "/api/internal/workpiece/online?operator=自动化测试", map[string]any{"sn": sn, "orderNo": c.Run.State.WorkOrderNo, "workOrderId": toInt(c.Run.State.Extra["workOrderID"]), "operator": "自动化测试"}, 3) for _, st := range golden.Scenario1Route { fid, _ := c.DB.Scalar(c.C, db.MES, "select flow_id from station where station_no=$1", []any{st}) flowID := toInt(fid) // 齐套绑定:批次类复用本轮入库批次号;SN 类按台分配互不重复的断路器 SN // (workpiece_bind.go:172-179 全局防重,同一 SN 不能装到第二台)。 fmRows, _ := c.DB.Rows(c.C, db.MES, "select material_code, sn_mode, ceil(coalesce(unit_qty,1))::int as uq from flow_material where flow_id=$1 order by id", []any{flowID}) binds := []map[string]any{} snIdx := 0 for _, fm := range fmRows { code := fm["material_code"] if code == "" || code == "" { continue } if toInt(fm["sn_mode"]) == 1 { need := toInt(fm["uq"]) if need < 1 { need = 1 } for k := 0; k < need && snIdx < len(qlSNs); k++ { binds = append(binds, map[string]any{"materialCode": code, "bindValue": qlSNs[snIdx]}) snIdx++ } } else { bn := c.Run.State.BatchNos[code] if bn == "" { bn = sn + "-BATCH" } binds = append(binds, map[string]any{"materialCode": code, "bindValue": bn}) } } if len(binds) > 0 { do(fmt.Sprintf("工位%d绑定", st), "/api/internal/workpiece/binds", map[string]any{"sn": sn, "orderNo": c.Run.State.WorkOrderNo, "flowId": flowID, "stationNo": st, "operator": "自动化测试", "items": binds}, st) } // 检测确认 + 操作工签字(k 恒为 1,见 s09 注释)。 stepRows, _ := c.DB.Rows(c.C, db.MES, "select id, name, need_check, need_operator_sign from process_step where flow_id=$1 and seq=1 order by id", []any{flowID}) steps := []map[string]any{} signs := []map[string]any{} for _, sr := range stepRows { sid := toInt(sr["id"]) if sr["need_check"] == "true" || sr["need_check"] == "t" { steps = append(steps, map[string]any{"stepId": sid, "name": sr["name"], "value": 0, "text": "自动化流水测试检测确认", "checked": true}) } if sr["need_operator_sign"] == "true" || sr["need_operator_sign"] == "t" { signs = append(signs, map[string]any{"stepId": sid, "signRole": "OPERATOR", "signer": "自动化测试", "remark": "自动化流水测试签字"}) } } do(fmt.Sprintf("工位%d报工", st), "/api/internal/station/report", map[string]any{"sn": sn, "orderNo": c.Run.State.WorkOrderNo, "flowId": flowID, "stationNo": st, "operator": "自动化测试", "steps": steps, "binds": []map[string]any{}, "signs": signs}, st) } do("完工", "/api/internal/workpiece/done?operator=自动化测试", map[string]any{"sn": sn, "operator": "自动化测试", "batchItems": []map[string]any{}, "serialItems": []map[string]any{}}, 0) c.L4(id, line, fmt.Sprintf("工件 %s 全链路完成(NEW→上线→3 道报工→完工)", sn), first(strings.Join(errs, ";"), 240), len(errs) == 0, "若失败:逐条错误见实际值") c.L3(id, line, "工件状态 DONE", db.MES, "select status from workpiece where sn=$1", []any{sn}, []string{"DONE"}) } // s10 下线完工(案例.md:232:完工不反冲)、成品回流、工单完工。 func s10(c *assert.Ctx) { const id, line = "S1-10", "案例.md:94-95 / 流水1-10 :207-213" post(c, id+"/done", line, "确认完工入库(回流 WMS)", env.MES, "/api/internal/workpiece/done?operator=自动化测试", map[string]any{"sn": c.Run.State.SN, "operator": "自动化测试", "batchItems": []map[string]any{}, "serialItems": []map[string]any{}}, "完工成功") c.L3(id+"/done", line, "工件状态 DONE", db.MES, "select status from workpiece where sn=$1", []any{c.Run.State.SN}, []string{"DONE"}) // 旧断言「成品库存 6 台」是绝对值,数据保留多轮重跑后必然累积(RUN09 实测 8)。 // 成品回流不新增 inventories 行(完工成品 SN 与 S1-02 上线是同一颗,复用同一行), // 改为严格断言:完工工件 SN 回到 WMS 成品「合格 + 在库」(回流后确实可用)。 c.L2(id+"/done", line, "成品回流 WMS:完工工件 SN 回到合格在库", db.WMS, "select count(*) from inventories where material_code=$1 and sn_code=$2 and quality_status='合格' and status='在库'", []any{"DX-100", c.Run.State.SN}, "1") // 工单完工门禁只看 work_order.finished_num(workorder.go:482-487),唯一写入者 // 是工件 DONE 时的 bumpWorkOrderProgress(scan.go:11-23)。工单按数量预生成 6 台 // NEW 工件(workorder.go:141-153),本流水只上线了 1 台独立 SN,故 3008「尚有 5 台」。 // 按拍板用正常接口把其余 5 台走完:SN 类物料全局防重(workpiece_bind.go:172-179) // 要求每台不同的 2 颗断路器 SN,先补入库 10 颗并判合格。 extraQty := 10 extraInboundNo := c.Run.No("IB", 6) extraSNList := make([]string, 0, extraQty) for j := 1; j <= extraQty; j++ { extraSNList = append(extraSNList, c.Run.Code("QLX", j)) } post(c, id+"/extra-inbound", line, "补入库断路器 10 颗(供 5 台工件的 SN 防重绑定)", env.WMS, "/api/inbound/create", map[string]any{ "inboundNo": extraInboundNo, "inboundType": "采购入库", "manageMode": 2, "materialCode": "DX100-QL-004", "quantity": extraQty, "zoneCode": "Z02", "shelfNo": "S01", "layerNo": "L01", "positionNo": "P01", "operator": "自动化测试", "inspectionNo": c.Run.State.InspectionNo, "ownershipType": "科技项目", "ownershipNo": c.Run.No("PROJ", 1), "productWorkNo": "", "productionDate": c.Run.Date(0), "supplier": "自动化流水测试供应商", "remark": c.Run.Batch + " 场景一补料", "snList": extraSNList, }, "") extraDetail := get(c, id+"/extra-detail", line, "补料待检明细可读", env.WMS, fmt.Sprintf("/api/inspection/pending/details?inboundNo=%s&materialCode=DX100-QL-004&page=1&pageSize=500", extraInboundNo), "") extraItems := []map[string]any{} for _, d := range extraDetail.DataArray("list") { tid, _ := d["targetId"].(string) if tid == "" { continue } extraItems = append(extraItems, map[string]any{ "targetId": tid, "status": "合格", "inspectQty": 1, "passQty": 1, "inspector": "自动化测试", "inspectionNo": c.Run.State.InspectionNo, "ownershipType": "other", "ownershipNo": c.Run.No("PROJ", 1), "disposalType": "NONE", }) } if len(extraItems) > 0 { post(c, id+"/extra-inspect", line, fmt.Sprintf("补料 %d 颗判合格", len(extraItems)), env.WMS, "/api/inspection/batch-flip", map[string]any{"items": extraItems}, "") } extraSNs := []string{} for _, sn := range extraSNList { okRow, _ := c.DB.Scalar(c.C, db.WMS, "select count(*) from inventories where sn_code=$1 and quality_status='合格' and status='在库'", []any{sn}) if okRow == "1" { extraSNs = append(extraSNs, sn) } } // 预生成工件 -002..-006 逐台走完(-001 保留 NEW:finished_num 已达 quantity 6)。 for k := 2; k <= 6; k++ { sn := fmt.Sprintf("%s-%03d", c.Run.State.WorkOrderNo, k) qlSNs := []string{} if (k-2)*2+2 <= len(extraSNs) { qlSNs = extraSNs[(k-2)*2 : (k-2)*2+2] } finishWorkpiece(c, id+fmt.Sprintf("/wp%d", k), line, sn, qlSNs) } post(c, id+"/wo-complete", line, "工单点「完工」→ 状态已完成", env.MES, "/api/v1/work-orders/status", map[string]any{"id": toInt(c.Run.State.Extra["workOrderID"]), "status": "DONE"}, "") c.L3(id+"/wo-complete", line, "工单终态 DONE(案例界面显示「已完成」)", db.MES, "select status from work_order where work_order_no=$1", []any{c.Run.State.WorkOrderNo}, []string{"DONE"}) // 案例.md:232 D15:原材料只在备料出库时扣减一次,完工不重复扣减、无反冲扣账。 // 旧断言「event_log 行数期望空字符串」既与案例冲突又永远失败;改为严格校验 // 本工单不存在任何反冲类事件(事件表列为 event_type/description,无 action 列)。 c.L2(id+"/backflush", line, "完工无反冲扣账(案例.md:232)", db.MES, "select count(*) from event_log where work_order_no=$1 and (event_type like '%backflush%' or description like '%反冲%')", []any{c.Run.State.WorkOrderNo}, "0") } // s11 绩效报表。 func s11(c *assert.Ctx) { const id, line = "S1-11", "案例.md:112 / 流水1-11 :215-221" resp := get(c, id, line, "绩效报表按工位/人员/日期可读", env.MES, "/api/v1/performance?from="+c.Run.Date(0)+"&to="+c.Run.Date(0), "") c.L4(id, line, "含当日合格数、上机时长、工单损耗列(本场景损耗=0)", "响应片段:"+first(resp.Raw, 260), resp.Code == 0, "若 code≠0:绩效报表接口未返回当日数据") } // s12 工件追溯。 func s12(c *assert.Ctx) { const id, line = "S1-12", "案例.md:113 / 流水1-12 :223-229" resp := get(c, id, line, "输入 "+c.Run.State.SN+" 可查全生命周期", env.MES, "/api/v1/trace?sn="+c.Run.State.SN, "") ok := resp.Code == 0 for _, st := range golden.Scenario1Route { if !strings.Contains(resp.Raw, fmt.Sprintf("%d", st)) { ok = false } } c.L4(id, line, "时间线 3→6→12、步骤考核、拧紧数据、装机绑定全绑齐", "响应片段:"+first(resp.Raw, 300), ok, "若为失败:追溯聚合缺工位节点或绑定未落库") } // s13 生产流程卡。 func s13(c *assert.Ctx) { const id, line = "S1-13", "案例.md:114 / 流水1-13 :231-237" // 主卡接口返回 HTML 页面(不是 JSON),按交付物类型断言,不再按 code=0 解析。 resp := getArtifact(c, id, line, "按工单可查主卡(工序实绩/用料清单/成品明细/签字区)", env.MES, "/api/v1/process-card/workorder?no="+c.Run.State.WorkOrderNo, "html") c.L4(id, line, "主卡四段内容齐全", "响应片段:"+first(resp.Raw, 260), resp.HTTPStatus == http.StatusOK && len(resp.Raw) > 0 && (strings.Contains(resp.Raw, "工序") || strings.Contains(resp.Raw, "流程卡") || strings.Contains(strings.ToLower(resp.Raw), " 0, "若不可用:导出链路缺实现") for _, format := range []string{"excel", "print"} { c.Note(id+"/"+format, line, "跳过:MES 未注册 /api/v1/process-card/"+format+" 接口(T6 核实),该格式不计入本步断言") } } // s14 余料退库(两张分批退料单)。 func s14(c *assert.Ctx) { const id, line = "S1-14", "案例.md:98-102 / 流水1-14 :239-245" for i := 1; i <= 2; i++ { returnNo := c.Run.No("RT", i) post(c, id+fmt.Sprintf("/return%d", i), line, fmt.Sprintf("第%d 张分批退料单(箱体 1 个、必选接驳台、必填原因)", i), env.MES, "/api/internal/station/return-material", map[string]any{ "orderNo": c.Run.State.WorkOrderNo, "stationNo": 3, "sn": c.Run.State.SN, "materialCode": "DX100-XT-001", "materialName": "箱体", "qty": 1, "unit": "件", "reason": "自动化流水测试余料", "returnDock": "左", "dock": "左", "lossReason": "分批退料差额", "transportMode": "manual", }, "") rows, _ := c.DB.Rows(c.C, db.WMS, "select return_no from return_orders where order_no=$1 and material_code=$2 and status='PENDING' order by id", []any{c.Run.State.WorkOrderNo, "DX100-XT-001"}) for _, r := range rows { if !contains(c.Run.State.ReturnNos, r["return_no"]) { c.Run.State.ReturnNos = append(c.Run.State.ReturnNos, r["return_no"]) } } _ = returnNo } c.L2(id, line, "两张「待确认收货」单并存互不吞并(案例:101 D10)", db.WMS, "select count(*) from return_orders where order_no=$1 and material_code=$2 and status='PENDING'", []any{c.Run.State.WorkOrderNo, "DX100-XT-001"}, "2") for i, rn := range c.Run.State.ReturnNos { post(c, id+fmt.Sprintf("/confirm%d", i+1), line, "库管逐张「确认收货」(扣减在确认时发生)", env.WMS, "/api/return-order/confirm", map[string]any{ // receiveBy 不是本接口入参:WMS returnOrderConfirmRequest 无该字段, // 真实处理人取自请求头 X-Username(return_order.go:198),传了也只会被忽略。 "returnNo": rn, "receiveQty": 1, "zoneCode": "Z02", "lossReason": "", }, "") } c.L3(id+"/confirm", line, "退料单终态 RECEIVED(已收货)", db.WMS, "select status from return_orders where return_no=$1", []any{firstOf(c.Run.State.ReturnNos)}, []string{"RECEIVED"}) c.Note(id, line, fmt.Sprintf("真实退料单号=%v", c.Run.State.ReturnNos)) } func firstOf(a []string) string { if len(a) > 0 { return a[0] } return "" } // s15 下班收尾。 func s15(c *assert.Ctx) { const id, line = "S1-15", "案例.md:105 / 流水1-15 :247-253" post(c, id, line, "下班/结束作业(左右接驳台均空才放行)", env.MES, "/api/internal/station/off-duty", map[string]any{"stationNo": 3, "hasLeftover": false, "items": []map[string]any{}}, "") // 旧断言期望空字符串「""」恒失败。改为可判定的严格布尔值: // 存在 off 类留痕 = '1',否则 '0',期望 '1'。不放宽、不删断言。 c.L2(id, line, "下班动作在工位终端操作日志留痕(存在 off 事件)", db.WS, "select case when count(*) > 0 then '1' else '0' end from event_log where action like '%off%'", nil, "1") } // s16 收口核对:库存汇总 + 四类单据终态。 func s16(c *assert.Ctx) { const id, line = "S1-16", "案例.md:109-110" // 库存断言从「绝对 golden 值」改为「本轮相对变化」:测试数据全部保留, // 多轮重跑后绝对库存必然累积,绝对值不可比。本轮变化由独立的单据表推导 // (inbound_orders / material_request / return_orders / workpiece), // 再与 inventories 的实际变化做严格数值比对——两者数据源不同,仍可证伪。 for code := range golden.Scenario1Inventory { before := toInt(c.Run.State.Extra["stockBefore_"+code]) // 按「批次 + 本轮实例后缀」精确隔离:同批次复跑时 Inst 不同, // 若只用 Batch% 会把上几轮的入库单一并算进本轮净变化。 inbound, _ := c.DB.Scalar(c.C, db.WMS, "select coalesce(sum(quantity),0) from inbound_orders where inbound_no like $1 and material_code=$2", []any{c.Run.Batch + "%-" + c.Run.Inst, code}) where, args := mrScope(c) sent, _ := c.DB.Scalar(c.C, db.MES, "select coalesce(sum(sent_qty),0) from material_request where "+where+" and material_code=$2", append(append([]any{}, args...), code)) returned, _ := c.DB.Scalar(c.C, db.WMS, "select coalesce(sum(receive_qty),0) from return_orders where order_no=$1 and material_code=$2 and status='RECEIVED'", []any{c.Run.State.WorkOrderNo, code}) // 成品回流不新增 inventories 行:完工工件的成品 SN 就是 S1-02 上线的同一颗 // (RUN09 实测 DX-100 仅 6 行、每轮 1 行,inbound_no 全是 *-IB-005-*)。 // 回流动作由下面独立的 workpiece.status='DONE' 断言验证,不计入库存变化。 if code == "DX100-QL-004" { // SN 类(materials.sn_mode=1):出库只把 status「在库」翻「出库」, // quantity 恒 1、quality_status 仍「合格」(outbound.go:393-398)。 // 严格三条件一次判定:出库行增量=sent、在库合格行增量=入库−出库、总量增量=入库。 c.L2(id+"/stock", line, fmt.Sprintf("本轮 %s(SN 类)出库 %d 颗:翻状态不改数量", code, toInt(sent)), db.WMS, "select case when "+ "(count(*) filter (where status='出库') - $2::int = $3::int) "+ "and (count(*) filter (where status='在库' and quality_status='合格') - $4::int = $6::int - $3::int) "+ "and (coalesce(sum(quantity),0) - $5::int = $6::int) then '1' else '0' end "+ "from inventories where material_code=$1 and sn_mode=1 and sn_code is not null and sn_code<>''", []any{code, toInt(c.Run.State.Extra["snOutBefore_"+code]), toInt(sent), toInt(c.Run.State.Extra["snAvailBefore_"+code]), toInt(c.Run.State.Extra["snQtyBefore_"+code]), toInt(inbound)}, "1") continue } // 批次类:退料 confirm 固定落「未检」/「不合格」,绝不进「合格」 // (return_order.go:273-279),故合格分组净变化不含退料回补。 expected := toInt(inbound) - toInt(sent) c.L2(id+"/stock", line, fmt.Sprintf("本轮 %s 合格库存净变化(入库%d − 出库%d)", code, toInt(inbound), toInt(sent)), db.WMS, "select coalesce(sum(quantity),0) - $2::int from inventories where material_code=$1 and quality_status='合格'", []any{code, before}, fmt.Sprint(expected)) if code == "DX-100" { // 回流动作单独严格断言:期望 = 工单预生成台数(workorder.go:141-153), // 6 台工件全部完工后才可能达到(与 S1-10 完工门禁同源)。 woQty, _ := c.DB.Scalar(c.C, db.MES, "select quantity from work_order where work_order_no=$1", []any{c.Run.State.WorkOrderNo}) c.L2(id+"/stock-finished", line, "本轮成品完工回流(workpiece DONE)", db.MES, "select count(*) from workpiece where order_no=$1 and status='DONE'", []any{c.Run.State.WorkOrderNo}, woQty) } if toInt(returned) > 0 { // 退料回补独立严格断言:必须落在「未检」分组且增量恰等于实收量; // 上面那条「合格」断言同时反证它没有混进合格库存。 c.L2(id+"/stock-return", line, fmt.Sprintf("本轮 %s 退料回补 %d 落「未检」分组(不进合格)", code, toInt(returned)), db.WMS, "select coalesce(sum(quantity),0) - $2::int from inventories where material_code=$1 and sn_mode=2 and quality_status='未检' and status='在库'", []any{code, toInt(c.Run.State.Extra["pendingBefore_"+code])}, fmt.Sprint(toInt(returned))) } } for label, want := range golden.Scenario1Status { switch label { case "工单": c.L3(id+"/wo", line, label+" 终态 "+want, db.MES, "select status from work_order where work_order_no=$1", []any{c.Run.State.WorkOrderNo}, []string{want}) case "叫料单": where, args := mrScope(c) c.L3(id+"/call", line, label+" 终态 "+want, db.MES, "select status from material_request where "+where+" order by id limit 1", args, []string{want}) case "出库单": c.L3(id+"/outbound", line, label+" 终态 "+want, db.WMS, "select status from outbound_orders where delivery_no=$1 limit 1", []any{c.Run.State.DeliveryNo}, []string{want}) case "退料单": c.L3(id+"/return", line, label+" 终态 "+want, db.WMS, "select status from return_orders where order_no=$1 order by id limit 1", []any{c.Run.State.WorkOrderNo}, []string{want}) } } for _, d := range golden.Scenario1Deliverables { c.L4(id+"/deliverable", line, d, "见 S1-11 / S1-12 / S1-13 各步实际响应", true, "") } } // s17 盲区补测:数量不符处理 E2E。 // 造数走正常业务 HTTP 接口(工位上报 → MES 落 material_qty_report),不直写库、不改业务代码。 func s17(c *assert.Ctx) { const id, line = "S1-17", "案例.md:143 / MES 数量不符处理" postStation(c, id+"/report", line, "工位上报数量不符(正常 HTTP 接口造数)", env.MES, "/api/internal/station/qty-report", map[string]any{ "stationNo": 3, "orderNo": c.Run.State.WorkOrderNo, "sn": c.Run.State.SN, "materialCode": "DX100-XT-001", "materialName": "箱体", "planQty": 6, "actualQty": 5, "cause": "自动化流水测试差异", "operator": "自动化测试", }, "", 3) c.L2(id+"/created", line, "差异单落库 material_qty_report", db.MES, "select count(*) from material_qty_report where order_no=$1 and material_code=$2", []any{c.Run.State.WorkOrderNo, "DX100-XT-001"}, "1") c.L2(id+"/diff", line, "差异量 = 应发 6 − 实收 5 = 1", db.MES, "select diff_qty from material_qty_report where order_no=$1 and material_code=$2 order by id desc limit 1", []any{c.Run.State.WorkOrderNo, "DX100-XT-001"}, "1") qrID, _ := c.DB.Scalar(c.C, db.MES, "select id from material_qty_report where order_no=$1 and material_code=$2 order by id desc limit 1", []any{c.Run.State.WorkOrderNo, "DX100-XT-001"}) c.Note(id, line, fmt.Sprintf("本轮真实差异单 id=%s", qrID)) list := get(c, id+"/page", line, "数量不符处理列表可读", env.MES, fmt.Sprintf("/api/v1/qty-reports?orderNo=%s&page=1&pageSize=20", c.Run.State.WorkOrderNo), "") total := list.DataGet("total") c.L4(id+"/listed", line, "本轮差异单出现在页面列表(data.total>0)", fmt.Sprintf("code=%d / total=%q / 响应片段:%s", list.Code, total, first(list.Raw, 220)), list.Code == 0 && total != "" && total != "0", "若为空:上报未落库或列表未按 orderNo 过滤") post(c, id+"/handle", line, "人工处理差异(ADJUST 调整口径,note 必填)", env.MES, "/api/v1/qty-reports/handle", map[string]any{ "id": toInt(qrID), "handleType": "ADJUST", "note": "自动化流水测试:差异已核对", }, "") c.L3(id+"/terminal", line, "差异单终态 CLOSED", db.MES, "select status from material_qty_report where id=$1", []any{toInt(qrID)}, []string{"CLOSED"}) } // s18 盲区补测:绩效报表 E2E(造上机签到实绩 → 报表 sessions 非空)。 func s18(c *assert.Ctx) { const id, line = "S1-18", "案例.md:142 / MES 绩效报表" post(c, id+"/checkin", line, "开工签到(正常 HTTP 接口造上机实绩)", env.MES, "/api/v1/inspections", map[string]any{ "category": "CHECKIN", "stationNo": 3, "shift": "白班", "orderNo": c.Run.State.WorkOrderNo, "sn": c.Run.State.SN, "result": "OK", "operator": "自动化测试", "remark": c.Run.Batch + " 绩效报表盲区补测", }, "") c.L2(id+"/record", line, "签到实绩落库 inspection_record", db.MES, "select count(*) from inspection_record where order_no=$1 and category='CHECKIN' and operator=$2", []any{c.Run.State.WorkOrderNo, "自动化测试"}, "1") resp := get(c, id+"/performance", line, "绩效报表按当日/人员可读", env.MES, "/api/v1/performance?from="+c.Run.Date(0)+"&to="+c.Run.Date(0)+"&operator=自动化测试", "") sessTotal := resp.DataGet("sessions", "total") c.L4(id+"/data", line, "绩效报表含当日上机实绩(data.sessions.total>0)", fmt.Sprintf("code=%d / sessions.total=%q / 响应片段:%s", resp.Code, sessTotal, first(resp.Raw, 220)), resp.Code == 0 && sessTotal != "" && sessTotal != "0", "若为 0:签到实绩未被绩效聚合读取,或报表按其它口径过滤") }