M5_smoke.ps1 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. <#
  2. .SYNOPSIS
  3. 健康服务人员助手 · M5 上线试点 · 端到端冒烟脚本(backend-engineer 产出)
  4. .DESCRIPTION
  5. 配套 docs/M5_seed.sql 使用:先在试点库执行种子,再启动后端(STORAGE_TYPE=local),最后跑本脚本。
  6. 覆盖 17 步全链路:管理端登录 → 机构核验 → 身份自举+加成员 → 建批次/发布 → 报名 → 发码签到
  7. → 学时补跑 → 学时台账 → 派单建单(建单留痕+金额恒等)→ 指派(资质闸口放行/拒绝)→ 四层评价 → 晋升 + 证据包
  8. → 履约链(到岗/完成/客户确认)→ 客户评价 → 六契约端点。
  9. .NOTES
  10. 1) 毕业口径红线 1:学时一律「分钟」整数(120 而非 2.0),本脚本在 S6/S8/S17 强校验。
  11. 1b) 毕业口径红线 2:金额恒等 amount = platform_fee + staff_income(整数分),S9 反例强校验。
  12. 2) 登录自举:wechat test-mode(application.yml wechat.test-mode=true)下 openid = 'test_' + code,
  13. 命中 seed 预置的 test_m5t1 / test_m5none / test_m5stu 三个用户,无需人工取 userId。
  14. 3) 组织派单只走 assign,不调 /api/order/accept —— claimOrder 的 CAS 是
  15. `status='pending' AND worker_id=#{userId}`,语义为「自接单」(建单时 worker_id 已=self);
  16. 组织建单 worker_id=NULL,assignOrder 指派即置 accepted,不存在 accept 窗口(Q2 裁决:不改)。
  17. 4) STORAGE_TYPE=local:默认 storage.type=jdcloud 会写生产 OSS bucket「aimate」;
  18. 冒烟/测试启动必须 STORAGE_TYPE=local 让证据包落 ./uploads,严禁污染生产 OSS。
  19. 5) 兼容 PowerShell 5.1,仅依赖 Invoke-RestMethod(不依赖 curl.exe / PS7)。
  20. 6) S13-S17 履约链非幂等(状态机单向):S9 每轮新单驱动,反例单另建一次性 accepted 单;
  21. S9 金额恒等反例会再建一单(死于 MoneyCheck.check,不落库)。
  22. .EXAMPLE
  23. powershell -ExecutionPolicy Bypass -File docs/M5_smoke.ps1 -BaseUrl http://127.0.0.1:9083
  24. #>
  25. [CmdletBinding()]
  26. param(
  27. [string]$BaseUrl = 'http://127.0.0.1:9083',
  28. # 管理端账号(DatabaseInitializer 启动自举 admin/admin123)
  29. [string]$AdminUser = 'admin',
  30. [string]$AdminPass = 'admin123',
  31. # 服务人员/学员 userId(可选)。留空 = 由 wechat-login 自举自动发现(推荐,配合 seed)。
  32. # 如需显式注入(部署时替换 token 占位,勿留假 id):
  33. # -T1WorkerUserId :T1_WORKER_USER_ID: -NoneWorkerUserId :NONE_WORKER_USER_ID: -StudentUserId :STUDENT_USER_ID:
  34. [string]$T1WorkerUserId = '',
  35. [string]$NoneWorkerUserId = '',
  36. [string]$StudentUserId = '',
  37. # 证据包轮询
  38. [int]$PollTimes = 10,
  39. [int]$PollIntervalSec = 2
  40. )
  41. $ErrorActionPreference = 'Stop'
  42. $script:BaseUrl = $BaseUrl
  43. $script:Step = 0
  44. $script:FailCount = 0
  45. # ============================================================
  46. # helpers
  47. # ============================================================
  48. function Invoke-Api {
  49. <# 统一 API 调用。后端契约:HTTP 200 + body{code,message,data},业务失败 code!=200。
  50. 鉴权:Authorization 头直接放 token(admin token 已带 'admin:' 前缀,user token 为裸 JWT)。#>
  51. param(
  52. [Parameter(Mandatory=$true)][string]$Method,
  53. [Parameter(Mandatory=$true)][string]$Path,
  54. [string]$Token = '',
  55. [object]$Body = $null
  56. )
  57. $url = $script:BaseUrl.TrimEnd('/') + $Path
  58. $headers = @{ 'Accept' = 'application/json' }
  59. if ($Token -ne '') { $headers['Authorization'] = $Token }
  60. $invoke = @{ Uri = $url; Method = $Method; Headers = $headers; UseBasicParsing = $true; TimeoutSec = 30 }
  61. if ($null -ne $Body) {
  62. $invoke['Body'] = $Body | ConvertTo-Json -Depth 20 -Compress
  63. $invoke['ContentType'] = 'application/json; charset=utf-8'
  64. }
  65. try {
  66. # PATCH(UTF-8): 后端 Content-Type=application/json 无 charset,PS5.1 IRM 按 Latin-1 解码致中文乱码,
  67. # 令 S10 资质不符 / S4 批次号已存在 的中文 -match 失配。仅对 message 做 Latin-1→UTF-8 roundtrip 修复,
  68. # 其余结构保持 IRM 原生解析(避免 Invoke-WebRequest 对部分响应流读取为空)。
  69. $r = Invoke-RestMethod @invoke
  70. if ($r -and $r.message) {
  71. $m = [string]$r.message
  72. try {
  73. $fixed = [Text.Encoding]::UTF8.GetString([Text.Encoding]::GetEncoding(28591).GetBytes($m))
  74. if ($fixed -match '[\u4e00-\u9fff]') { $r.message = $fixed }
  75. } catch {}
  76. }
  77. return $r
  78. }
  79. catch {
  80. # HTTP 非 2xx:尝试读响应体(后端可能直接返回 Result JSON)
  81. if ($_.Exception.Response) {
  82. try {
  83. $sr = New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream())
  84. $errBody = $sr.ReadToEnd()
  85. if ($errBody) {
  86. $parsed = $errBody | ConvertFrom-Json
  87. if ($null -ne $parsed -and $null -ne $parsed.code) { return $parsed }
  88. }
  89. } catch {}
  90. }
  91. return [pscustomobject]@{ code = -1; message = ('TRANSPORT_ERROR: ' + $_.Exception.Message); data = $null }
  92. }
  93. }
  94. function Get-Arr($v) {
  95. if ($null -eq $v) { return @() }
  96. return @($v)
  97. }
  98. function Assert-Fail {
  99. <# 断言失败:标红并立即退出(冒烟门禁语义)#>
  100. param([string]$StepName, $Resp, [string]$Detail)
  101. $httpCode = -1
  102. if ($null -ne $Resp -and $null -ne $Resp.code) { $httpCode = [int]$Resp.code }
  103. $msg = '(no message)'
  104. if ($null -ne $Resp -and $null -ne $Resp.message) { $msg = [string]$Resp.message }
  105. Write-Host ("S{0} [FAIL] {1} HTTP={2} {3} | msg={4}" -f $script:Step, $StepName, $httpCode, $Detail, $msg) -ForegroundColor Red
  106. $script:FailCount = $script:FailCount + 1
  107. exit 1
  108. }
  109. function Assert-Pass {
  110. param([string]$StepName, $Resp, [string]$Detail)
  111. $httpCode = -1
  112. if ($null -ne $Resp -and $null -ne $Resp.code) { $httpCode = [int]$Resp.code }
  113. Write-Host ("S{0} [PASS] {1} HTTP={2} {3}" -f $script:Step, $StepName, $httpCode, $Detail) -ForegroundColor Green
  114. }
  115. function Assert-True {
  116. param([bool]$Cond, [string]$StepName, $Resp, [string]$Detail)
  117. if ($Cond) { Assert-Pass -StepName $StepName -Resp $Resp -Detail $Detail }
  118. else { Assert-Fail -StepName $StepName -Resp $Resp -Detail $Detail }
  119. }
  120. function Begin-Step([string]$Name) {
  121. $script:Step = $script:Step + 1
  122. Write-Host ("`n===== S{0} {1} =====" -f $script:Step, $Name) -ForegroundColor Cyan
  123. }
  124. function Fix-MojibakeText([string]$s) {
  125. <# IRM 对 application/json 无 charset 的中文响应按 Latin-1 解码致乱码(S10 同源缺陷)。
  126. 仅对已知含中文的出参字段做 Latin-1→UTF-8 roundtrip;调用方仅用修复值做展示/打印,
  127. 不做 -match 断言(编码不可控时宁可只断言字段存在性)。#>
  128. if ([string]::IsNullOrEmpty($s)) { return '' }
  129. try {
  130. $fixed = [Text.Encoding]::UTF8.GetString([Text.Encoding]::GetEncoding(28591).GetBytes($s))
  131. if ($fixed -match '[\u4e00-\u9fff]') { return $fixed }
  132. } catch {}
  133. return $s
  134. }
  135. function Test-MinuteInteger($v) {
  136. <# 分钟整数红线:值必须为非负整数(拒绝小时/浮点/负数)#>
  137. if ($null -eq $v) { return $false }
  138. $n = 0
  139. if (-not [long]::TryParse([string]$v, [ref]$n)) { return $false }
  140. if ($n -lt 0) { return $false }
  141. $d = [double]$v
  142. return ($d -eq [math]::Floor($d))
  143. }
  144. # ============================================================
  145. # S1 管理端登录
  146. # ============================================================
  147. Begin-Step '管理端登录 /api/admin/auth/login'
  148. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/auth/login' -Body @{ username = $AdminUser; password = $AdminPass }
  149. $adminToken = ''
  150. if ($resp.code -eq 200 -and $resp.data -and $resp.data.token) { $adminToken = [string]$resp.data.token }
  151. Assert-True ([bool]($resp.code -eq 200 -and $adminToken -ne '')) 'admin login 取 token' $resp ("tokenLen=" + $adminToken.Length)
  152. # ============================================================
  153. # S2 机构核验 + 三身份登录自举
  154. # ============================================================
  155. Begin-Step '机构核验 /api/admin/org/list + 身份自举 /api/auth/wechat-login'
  156. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/org/list' -Token $adminToken -Body @{}
  157. Assert-True ($resp.code -eq 200) 'org/list 可用' $resp ''
  158. $orgId = $null
  159. foreach ($row in (Get-Arr $resp.data.list)) {
  160. if ($row.orgCode -eq 'PILOT-HS-001') { $orgId = [long]$row.id; break }
  161. }
  162. if ($null -eq $orgId) {
  163. Assert-Fail '捕获种子机构 PILOT-HS-001' $resp '未找到 org_code=PILOT-HS-001,请先执行 docs/M5_seed.sql'
  164. }
  165. Assert-Pass '捕获种子机构 PILOT-HS-001' $resp ("orgId=" + $orgId)
  166. # 身份自举:test-mode openid='test_'+code 命中 seed 预置用户
  167. $loginUser = {
  168. param([string]$code)
  169. $r = Invoke-Api -Method 'POST' -Path '/api/auth/wechat-login' -Body @{ code = $code }
  170. if ($r.code -ne 200 -or -not $r.data -or -not $r.data.token -or -not $r.data.userId) {
  171. Assert-Fail ("wechat-login 自举 code=" + $code) $r 'token/userId 缺失'
  172. }
  173. return [pscustomobject]@{ token = [string]$r.data.token; userId = [long]$r.data.userId }
  174. }
  175. $uT1 = & $loginUser 'm5t1' # T1 已认证服务人员(seed: org_admin)
  176. $uNone = & $loginUser 'm5none' # 未认证服务人员(seed: dispatcher)
  177. $uStu = & $loginUser 'm5stu' # 培训学员
  178. # 显式参数覆盖(部署期注入时以参数为准)
  179. if ($T1WorkerUserId -ne '') { $uT1.userId = [long]$T1WorkerUserId }
  180. if ($NoneWorkerUserId -ne '') { $uNone.userId = [long]$NoneWorkerUserId }
  181. if ($StudentUserId -ne '') { $uStu.userId = [long]$StudentUserId }
  182. $t1Token = $uT1.token; $t1UserId = $uT1.userId
  183. $noneToken = $uNone.token; $noneUserId = $uNone.userId
  184. $stuToken = $uStu.token; $stuUserId = $uStu.userId
  185. Assert-Pass '三身份自举 m5t1/m5none/m5stu' $resp ("T1=$t1UserId none=$noneUserId stu=$stuUserId")
  186. # ============================================================
  187. # S3 组织加成员(m5t1 以 org_admin 身份加成员;已是成员则跳过,保持 org_admin 身份)
  188. # ============================================================
  189. Begin-Step '组织加成员 /api/admin/org/member/add(org_admin 身份,非 admin token)'
  190. # 幂等守卫(重跑不挂):train_org_member 单角色(uk org_id+user_id),把 m5t1 自加为
  191. # lecturer 会消耗掉 org_admin 身份——而守卫 requireOrgAdmin 恰依赖 org_admin,重跑必失败。
  192. # 已是机构成员即证明加成员链路可用,跳过自加以保持 org_admin(后续 S6 发码走机构管理者权限)。
  193. $clist = Invoke-Api -Method 'POST' -Path '/api/admin/org/member/list' -Token $adminToken -Body @{ orgId = $orgId }
  194. $t1AlreadyMember = $false
  195. foreach ($row in (Get-Arr $clist.data)) {
  196. if ($row.userId -ne $null -and [long]$row.userId -eq $t1UserId) { $t1AlreadyMember = $true; break }
  197. }
  198. if (-not $t1AlreadyMember) {
  199. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/org/member/add' -Token $t1Token -Body @{
  200. orgId = $orgId; userId = $t1UserId; role = 'lecturer'
  201. }
  202. Assert-True ($resp.code -eq 200) 'm5t1 以 org_admin 自加为 lecturer' $resp ''
  203. } else {
  204. Assert-True $true 'm5t1 已是机构成员(跳过自加,保持 org_admin 身份)' $clist ("memberId=" + $t1UserId)
  205. }
  206. # member/list 复核
  207. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/org/member/list' -Token $adminToken -Body @{ orgId = $orgId }
  208. $found = $false
  209. foreach ($row in (Get-Arr $resp.data)) {
  210. if ([long]$row.userId -eq $t1UserId) { $found = $true; break }
  211. }
  212. Assert-True ($resp.code -eq 200 -and $found) 'member/list 含 m5t1' $resp ("memberId=" + $t1UserId)
  213. # ============================================================
  214. # S4 课程自举 + 建批次 + 发布
  215. # ============================================================
  216. Begin-Step '课程自举 /api/admin/course/list → 建批次 /api/admin/cohort/create → publish'
  217. $courseId = $null
  218. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/course/list' -Token $adminToken -Body @{}
  219. foreach ($c in (Get-Arr $resp.data)) {
  220. if ($c.level -eq 'L9') { $courseId = [long]$c.id; break }
  221. }
  222. if ($null -eq $courseId) {
  223. # seed 已建 L9;此处兜底(与 seed 的 uk_level 幂等配合:仅查不到才建)
  224. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/course/create' -Token $adminToken -Body @{
  225. level = 'L9'; name = 'M5试点·健康服务人员认证实训'; status = 'active'
  226. price = 0; memberPrice = 0
  227. }
  228. if ($resp.code -ne 200 -or -not $resp.data -or -not $resp.data.id) {
  229. Assert-Fail 'L9 课程缺失且创建失败' $resp '请确认 docs/M5_seed.sql 已执行'
  230. }
  231. $courseId = [long]$resp.data.id
  232. }
  233. Assert-Pass 'L9 课程就绪' $resp ("courseId=" + $courseId)
  234. $cohortName = 'M5试点-健康服务人员实训批次01'
  235. $cohortCode = 'M5-COHORT-001'
  236. $cohortId = $null
  237. # 先按 courseId 查已有批次(幂等:复用,避免 uk_code 冲突)
  238. $resp = Invoke-Api -Method 'POST' -Path '/api/cohort/list' -Token $adminToken -Body @{ courseId = $courseId; page = 1; size = 20 }
  239. foreach ($c in (Get-Arr $resp.data)) {
  240. if ($c.code -eq $cohortCode) { $cohortId = [long]$c.cohortId; break }
  241. }
  242. if ($null -eq $cohortId) {
  243. # 学时一律分钟整数:理论 2400(=40h)、实践 1200(=20h)
  244. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/cohort/create' -Token $adminToken -Body @{
  245. courseId = $courseId
  246. name = $cohortName
  247. code = $cohortCode
  248. capacity = 50
  249. place = '浠艾福试点实训基地'
  250. lecturerId = $t1UserId # 讲师=m5t1,供 S6 讲师侧发码走讲师兜底
  251. creditHours = 3600 # 3600 分钟 = 60h
  252. creditTheoryHours = 2400 # 2400 分钟(红线:分钟整数)
  253. creditPracticeHours = 1200 # 1200 分钟(红线:分钟整数)
  254. startAt = '2026-12-01 09:00:00'
  255. endAt = '2026-12-31 18:00:00'
  256. signupDeadline = '2026-11-30 23:59:59'
  257. }
  258. if ($resp.code -ne 200 -or -not $resp.data -or -not $resp.data.cohortId) {
  259. # 批次号唯一冲突:可能存在上一轮遗留的 draft 批次(/api/cohort/list 只列 published,查不到)
  260. $msg = if ($resp.message) { [string]$resp.message } else { '' }
  261. if ($msg -match '批次号已存在') {
  262. Assert-Fail '存在未发布的 draft 批次 M5-COHORT-001' $resp '请手动 publish 该批次,或换批次号后重跑(避免 uk_code 冲突)'
  263. }
  264. Assert-Fail '建批次失败' $resp ''
  265. }
  266. $cohortId = [long]$resp.data.cohortId
  267. }
  268. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/cohort/detail' -Token $adminToken -Body @{ cohortId = $cohortId }
  269. Assert-True ($resp.code -eq 200 -and $resp.data) '批次详情可查' $resp ("cohortId=" + $cohortId)
  270. $theory = [long]$resp.data.creditTheoryHours
  271. $practice = [long]$resp.data.creditPracticeHours
  272. Assert-True (($theory -eq 2400) -and ($practice -eq 1200)) '批次学时=理论2400/实践1200(分钟)' $resp ("theory=$theory practice=$practice")
  273. if ($resp.data.status -eq 'draft') {
  274. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/cohort/publish' -Token $adminToken -Body @{ cohortId = $cohortId }
  275. Assert-True ($resp.code -eq 200 -and $resp.data.status -eq 'published') '发布批次 draft→published' $resp ''
  276. } else {
  277. Assert-True ($resp.data.status -eq 'published') '批次已处于 published(幂等复用)' $resp ("status=" + $resp.data.status)
  278. }
  279. # ============================================================
  280. # S5 学员报名 + 幂等复跑
  281. # ============================================================
  282. Begin-Step '学员报名 /api/cohort/signup(幂等)'
  283. $resp = Invoke-Api -Method 'POST' -Path '/api/cohort/signup' -Token $stuToken -Body @{ cohortId = $cohortId }
  284. Assert-True ($resp.code -eq 200 -and $resp.data) '学员首次报名' $resp ("signupId=" + $resp.data.signupId + " status=" + $resp.data.status)
  285. $signupId = [long]$resp.data.signupId
  286. # 幂等复跑:二次报名应返回「已报名」的成功体,而非业务报错
  287. $resp2 = Invoke-Api -Method 'POST' -Path '/api/cohort/signup' -Token $stuToken -Body @{ cohortId = $cohortId }
  288. Assert-True ($resp2.code -eq 200 -and $resp2.data.signupId -eq $signupId) '报名幂等(二次调用返回同 signupId)' $resp2 ("signupId=" + $resp2.data.signupId)
  289. # 管理端审核通过 pending→approved(签到前置);已 approved 则跳过(重跑幂等)
  290. if ([string]$resp.data.status -eq 'pending') {
  291. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/cohort/auditSignup' -Token $adminToken -Body @{ signupId = $signupId; pass = $true }
  292. Assert-True ($resp.code -eq 200 -and $resp.data.status -eq 'approved') '报名审核 approved' $resp ("status=" + $resp.data.status)
  293. } else {
  294. Assert-True ([string]$resp.data.status -eq 'approved') '报名已 approved(重跑幂等,跳过审核)' $resp ("status=" + $resp.data.status)
  295. }
  296. # 报名列表复核
  297. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/cohort/signupList' -Token $adminToken -Body @{ cohortId = $cohortId; page = 1; size = 10 }
  298. $approvedCnt = 0
  299. foreach ($row in (Get-Arr $resp.data.list)) {
  300. if ($row.status -eq 'approved') { $approvedCnt = $approvedCnt + 1 }
  301. }
  302. Assert-True ($resp.code -eq 200 -and $approvedCnt -ge 1) 'signupList 含 approved' $resp ("approved=" + $approvedCnt)
  303. # ============================================================
  304. # S6 发码(讲师侧)+ 学员签到
  305. # ============================================================
  306. Begin-Step '发码 /api/attendance/session/code → 学员签到 /api/attendance/scan'
  307. $sessionId = "${cohortId}:1"
  308. $resp = Invoke-Api -Method 'POST' -Path '/api/attendance/session/code' -Token $t1Token -Body @{
  309. sessionId = $sessionId; sessionHours = 120; category = 'theory'
  310. }
  311. if ($resp.code -ne 200) {
  312. # 兜底:复用历史批次可能未设 lecturerId=m5t1(讲师兜底失效)→ 用 admin 放行
  313. Write-Host ("S{0} [WARN] 讲师发码失败({1}),回退 admin 发码" -f $script:Step, $resp.message) -ForegroundColor Yellow
  314. $resp = Invoke-Api -Method 'POST' -Path '/api/attendance/session/code' -Token $adminToken -Body @{
  315. sessionId = $sessionId; sessionHours = 120; category = 'theory'
  316. }
  317. }
  318. Assert-True ($resp.code -eq 200 -and $resp.data -and $resp.data.code) '讲师/组织者发码' $resp ("sessionId=" + $sessionId)
  319. $attendCode = [string]$resp.data.code
  320. $codeHours = [long]$resp.data.hours
  321. Assert-True ($codeHours -eq 120) '签到码 sessionHours=120(分钟整数红线)' $resp ("hours=$codeHours")
  322. # 学员扫码签到(幂等):断言 hours=120 且入 pending 台账
  323. $resp = Invoke-Api -Method 'POST' -Path '/api/attendance/scan' -Token $stuToken -Body @{ code = $attendCode }
  324. $attHours = if ($resp.data -and $null -ne $resp.data.hours) { [long]$resp.data.hours } else { -1 }
  325. $attStatus = if ($resp.data -and $resp.data.status) { [string]$resp.data.status } else { '' }
  326. Assert-True ($resp.code -eq 200 -and $attHours -eq 120) '学员签到 hours=120' $resp ("attendanceId=" + $resp.data.attendanceId + " hours=$attHours status=$attStatus")
  327. Assert-True ($attStatus -eq 'pending') '签到台账 status=pending(T+3 生效)' $resp ("status=$attStatus")
  328. # 幂等复扫:不重复入账
  329. $resp2 = Invoke-Api -Method 'POST' -Path '/api/attendance/scan' -Token $stuToken -Body @{ code = $attendCode }
  330. $dupHours = if ($resp2.data -and $null -ne $resp2.data.hours) { [long]$resp2.data.hours } else { -1 }
  331. $dupFlag = if ($resp2.data -and $null -ne $resp2.data.alreadyExists) { [bool]$resp2.data.alreadyExists } else { $false }
  332. Assert-True ($resp2.code -eq 200 -and $dupHours -eq 120 -and $dupFlag) '签到幂等(alreadyExists=true)' $resp2 ("alreadyExists=$dupFlag")
  333. # ============================================================
  334. # S7 admin 补跑学时(T+3 到期生效 + 异议回滚 + cfc 重推)
  335. # ============================================================
  336. Begin-Step 'admin 补跑 /api/admin/credit/scan'
  337. # 注意:签到当刻 effective_at = now + 3 天,补跑不会把新台账置 effective(符合 T+3 设计)。
  338. # 本步断言的是「补跑端点可执行、返回计数」,而非 effectiveCount 必须≥1。
  339. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/credit/scan' -Token $adminToken -Body @{}
  340. $effCnt = -1
  341. if ($resp.code -eq 200 -and $resp.data -and $null -ne $resp.data.effectiveCount) { $effCnt = [long]$resp.data.effectiveCount }
  342. Assert-True ($resp.code -eq 200) 'credit/scan 可执行(T+3 未到期容差:effectiveCount 可为 0)' $resp ("effectiveCount=$effCnt")
  343. # 待生效台账复核(admin 视角)
  344. # 注意:Invoke-RestMethod 对单元素 JSON 数组会解包成单个对象,$pendRows.Count 取空 →
  345. # 必须 @() 强制数组化再取 Count(PS 5.1 解析坑,非产品缺陷)。
  346. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/credit/ledgerPending' -Token $adminToken -Body @{ page = 1; size = 10 }
  347. $pendRows = @($resp.data.list)
  348. Assert-True ($resp.code -eq 200 -and $pendRows.Count -ge 1) 'admin 待生效台账非空' $resp ("rows=" + $pendRows.Count)
  349. # ============================================================
  350. # S8 学员查学时(分钟整数红线)
  351. # ============================================================
  352. Begin-Step '学员学时 /api/credit/mine(分钟整数红线)'
  353. $resp = Invoke-Api -Method 'POST' -Path '/api/credit/mine' -Token $stuToken -Body @{}
  354. if ($resp.code -ne 200 -or -not $resp.data) { Assert-Fail 'credit/mine 可用' $resp '' }
  355. $total = [long]$resp.data.totalHours
  356. $theoryH = [long]$resp.data.theoryHours
  357. $practice = [long]$resp.data.practiceHours
  358. $effH = [long]$resp.data.effectiveHours
  359. $pendH = [long]$resp.data.pendingHours
  360. $allInt = (Test-MinuteInteger $total) -and (Test-MinuteInteger $theoryH) -and (Test-MinuteInteger $practice) -and (Test-MinuteInteger $effH) -and (Test-MinuteInteger $pendH)
  361. Assert-True $allInt '学时五字段均为分钟整数(非小时/浮点)' $resp ("total=$total theory=$theoryH practice=$practice effective=$effH pending=$pendH")
  362. Assert-True ($pendH -ge 120) '待生效学时 ≥ 120 分钟(签到入账)' $resp ("pending=$pendH")
  363. # 待生效明细
  364. $resp = Invoke-Api -Method 'POST' -Path '/api/credit/pending' -Token $stuToken -Body @{}
  365. $ok = $resp.code -eq 200
  366. $matched = $false
  367. foreach ($row in (Get-Arr $resp.data)) {
  368. if ([long]$row.hours -eq 120 -and $row.status -eq 'pending') { $matched = $true; break }
  369. }
  370. Assert-True ($ok -and $matched) 'credit/pending 含 120 分钟 pending 明细' $resp ''
  371. # ============================================================
  372. # S9 派单建单(建单留痕:dispatchId 必非空)
  373. # ============================================================
  374. Begin-Step '派单建单 /api/admin/org/dispatch/create(断言 dispatchId 非空)'
  375. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/org/dispatch/create' -Token $adminToken -Body @{
  376. orgId = $orgId
  377. familyName = '王阿姨家'
  378. contactPhone = '13900000009'
  379. address = '湖北省黄冈市浠水县试点路 1 号'
  380. serviceType = 'cleaning'
  381. orderDate = '2026-12-05'
  382. amount = 18000 # 整数分
  383. platformFee = 1800 # 整数分
  384. remark = 'M5 冒烟建单'
  385. }
  386. if ($resp.code -ne 200 -or -not $resp.data) { Assert-Fail '组织建单失败' $resp '' }
  387. $orderId = [long]$resp.data.orderId
  388. $orderNo = [string]$resp.data.orderNo
  389. $dispatchId = $resp.data.dispatchId
  390. Assert-True ($null -ne $dispatchId -and [long]$dispatchId -gt 0) '建单留痕 dispatchId 非空(created 行同事务落库)' $resp ("orderId=$orderId orderNo=$orderNo dispatchId=$dispatchId")
  391. Assert-True ([string]$resp.data.status -eq 'pending') '建单只置 pending(不置 accepted)' $resp ("status=" + $resp.data.status)
  392. # 金额恒等红线(HEAD 5aad440 已落地 MoneyCheck.check):amount<platformFee → 硬拒绝「金额口径不正确」
  393. $negMoney = Invoke-Api -Method 'POST' -Path '/api/admin/org/dispatch/create' -Token $adminToken -Body @{
  394. orgId = $orgId; familyName = '金额反例户'; contactPhone = '13900000111'
  395. address = '湖北省黄冈市浠水县试点路 3 号'; serviceType = 'cleaning'
  396. orderDate = '2026-12-05'; amount = 1000; platformFee = 1800; remark = 'M5 金额恒等反例'
  397. }
  398. $negMoneyMsg = if ($negMoney.message) { [string]$negMoney.message } else { '' }
  399. Assert-True (($negMoney.code -ne 200) -and ($negMoneyMsg -match '金额口径不正确')) '金额恒等反例 amount(1000)<platformFee(1800) 被拒' $negMoney ("msg=$negMoneyMsg")
  400. # 派单列表可见 + 金额恒等正例(staff_income = amount - platform_fee = 16200)
  401. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/org/dispatch/list' -Token $adminToken -Body @{ orgId = $orgId; page = 1; size = 10 }
  402. $seen = $false
  403. $identityOk = $false
  404. foreach ($row in (Get-Arr $resp.data.list)) {
  405. if ([long]$row.id -eq $orderId) {
  406. $seen = $true
  407. $identityOk = ([long]$row.amount -eq 18000 -and [long]$row.platformFee -eq 1800 -and [long]$row.staffIncome -eq 16200)
  408. break
  409. }
  410. }
  411. Assert-True ($resp.code -eq 200 -and $seen) 'dispatch/list 可见新单' $resp ''
  412. Assert-True $identityOk '金额恒等正例 staff_income(16200) = amount(18000) - platform_fee(1800)' $resp ''
  413. # ============================================================
  414. # S10 指派(资质闸口:m5none 拒绝 / m5t1 放行);只走 assign 不走 accept
  415. # ============================================================
  416. Begin-Step '指派 /api/admin/org/dispatch/assign(资质闸口放行/拒绝)'
  417. # 反例:未认证 m5none → 资质不符
  418. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/org/dispatch/assign' -Token $adminToken -Body @{
  419. orderId = $orderId; workerId = $noneUserId; serviceType = 'cleaning'
  420. }
  421. $rejectMsg = if ($resp.message) { [string]$resp.message } else { '' }
  422. Assert-True (($resp.code -ne 200) -and ($rejectMsg -match '资质不符')) '未认证 m5none 被资质闸口拒绝' $resp ("msg=$rejectMsg")
  423. # 正例:T1 m5t1 → 放行($T1WorkerUserId 空则已由自举补齐,必非空)
  424. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/org/dispatch/assign' -Token $adminToken -Body @{
  425. orderId = $orderId; workerId = $t1UserId; serviceType = 'cleaning'; remark = '指派 T1 服务人员'
  426. }
  427. Assert-True ($resp.code -eq 200 -and [string]$resp.data.status -eq 'accepted' -and [long]$resp.data.workerId -eq $t1UserId) 'T1 m5t1 指派放行 → accepted' $resp ("workerId=" + $resp.data.workerId + " status=" + $resp.data.status)
  428. # 可选服务人员候选(含 m5t1,含 rating=450)
  429. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/org/dispatch/workers' -Token $adminToken -Body @{ orgId = $orgId }
  430. $hasT1 = $false
  431. foreach ($w in (Get-Arr $resp.data.list)) {
  432. if ([long]$w.userId -eq $t1UserId -and $w.authLevel -eq 'T1') { $hasT1 = $true; break }
  433. }
  434. Assert-True ($resp.code -eq 200 -and $hasT1) 'workers 候选含 T1 m5t1' $resp ''
  435. # 组织派单只走 assign 不调 accept(Q2 裁决:claimOrder 仅自接单场景)
  436. # worker 侧可见该单(accepted 分组)
  437. $resp = Invoke-Api -Method 'POST' -Path '/api/order/mine' -Token $t1Token -Body @{}
  438. $mineOk = $resp.code -eq 200
  439. Assert-True $mineOk 'worker /api/order/mine 可查' $resp ''
  440. # ============================================================
  441. # S11 四层评价(机构绩效)
  442. # ============================================================
  443. Begin-Step '机构绩效评价 /api/eval/org → 汇总 /api/eval/mine'
  444. $resp = Invoke-Api -Method 'POST' -Path '/api/eval/org' -Token $t1Token -Body @{
  445. userId = $t1UserId; score = 90; dimension = 'punctuality'; comment = 'M5 冒烟绩效评价'
  446. }
  447. Assert-True ($resp.code -eq 200 -and $resp.data) 'eval/org 提交' $resp ("evalId=" + $resp.data.evalId)
  448. $resp = Invoke-Api -Method 'POST' -Path '/api/eval/mine' -Token $t1Token -Body @{}
  449. $hasComposite = $false
  450. $hasOrg = $false
  451. if ($resp.code -eq 200 -and $resp.data) {
  452. if ($null -ne $resp.data.PSObject.Properties['composite']) { $hasComposite = $true }
  453. if ($resp.data.org -and [long]$resp.data.org.count -ge 1) { $hasOrg = $true }
  454. }
  455. $compVal = 0
  456. if ($hasComposite) { $compVal = [double]$resp.data.composite }
  457. Assert-True ($hasComposite -and $hasOrg -and $compVal -gt 0) 'eval/mine composite>0 且 org 评价计数≥1' $resp ("composite=$compVal")
  458. # ============================================================
  459. # S12 晋升 + 证据包
  460. # ============================================================
  461. Begin-Step '晋升规则/记录 /api/admin/promotion + 闸口 /api/promotion/unlock + 证据包 /api/evidence'
  462. # T2 规则(seed 预置:serve_count>=10 且 rating>=450)
  463. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/promotion/rule/list' -Token $adminToken -Body @{ page = 1; size = 20 }
  464. $hasT2Rule = $false
  465. foreach ($r in (Get-Arr $resp.data.list)) {
  466. if ($r.authLevel -eq 'T2' -and [int]$r.enabled -eq 1) { $hasT2Rule = $true; break }
  467. }
  468. Assert-True ($resp.code -eq 200 -and $hasT2Rule) 'T2 晋升规则存在且启用(seed 预置)' $resp ''
  469. # T1 晋升断言:rating=450 达标、serve_count 不足 → unmet 应只含 serve_count
  470. $resp = Invoke-Api -Method 'POST' -Path '/api/promotion/apply' -Token $t1Token -Body @{ authLevel = 'T2' }
  471. $unmetMetrics = @()
  472. if ($resp.code -eq 200 -and $resp.data -and $resp.data.unmet) { $unmetMetrics = Get-Arr $resp.data.unmet | ForEach-Object { [string]$_.metric } }
  473. $expectUnmet = ($resp.code -eq 200) -and ($resp.data.eligible -eq $false) -and ($unmetMetrics -contains 'serve_count') -and (-not ($unmetMetrics -contains 'rating'))
  474. Assert-True $expectUnmet 'apply T2 未达标:unmet 仅 serve_count(rating 450 已达标)' $resp ("eligible=" + $resp.data.eligible + " unmet=[" + ($unmetMetrics -join ',') + "]")
  475. # 晋升履历
  476. $resp = Invoke-Api -Method 'POST' -Path '/api/promotion/records' -Token $t1Token -Body @{ page = 1; size = 10 }
  477. Assert-True ($resp.code -eq 200 -and $null -ne $resp.data) 'promotion/records 可查' $resp ''
  478. # 闸口:T1 放行 / 未认证拒绝
  479. $resp = Invoke-Api -Method 'POST' -Path '/api/promotion/unlock' -Token $t1Token -Body @{ serviceType = 'cleaning' }
  480. Assert-True ($resp.code -eq 200 -and $resp.data.allowed -eq $true) 'unlock T1 allowed=true(闸口双值容错放行)' $resp ("authLevel=" + $resp.data.authLevel)
  481. $resp = Invoke-Api -Method 'POST' -Path '/api/promotion/unlock' -Token $noneToken -Body @{ serviceType = 'cleaning' }
  482. Assert-True ($resp.code -eq 200 -and $resp.data.allowed -eq $false) 'unlock m5none allowed=false' $resp ("reason=" + $resp.data.reason)
  483. # 证据包:发起 + 轮询
  484. $resp = Invoke-Api -Method 'POST' -Path '/api/evidence/export' -Token $t1Token -Body @{ type = 'training' }
  485. if ($resp.code -ne 200 -or -not $resp.data -or -not $resp.data.taskNo) { Assert-Fail '发起证据包导出失败' $resp '' }
  486. $taskNo = [string]$resp.data.taskNo
  487. Assert-Pass '发起培训证据包导出' $resp ("taskNo=$taskNo status=" + $resp.data.status)
  488. $taskStatus = ''
  489. $taskProgress = -1
  490. $fileUrl = ''
  491. $seenStates = @()
  492. for ($i = 0; $i -lt [Math]::Max($PollTimes, 1); $i++) {
  493. Start-Sleep -Seconds $PollIntervalSec
  494. $sr2 = Invoke-Api -Method 'POST' -Path '/api/evidence/status' -Token $t1Token -Body @{ taskNo = $taskNo }
  495. if ($sr2.code -eq 200 -and $sr2.data) {
  496. $taskStatus = [string]$sr2.data.status
  497. $taskProgress = [long]$sr2.data.progress
  498. $fileUrl = if ($sr2.data.fileUrl) { [string]$sr2.data.fileUrl } else { '' }
  499. if (-not ($seenStates -contains $taskStatus)) { $seenStates = $seenStates + $taskStatus }
  500. if ($taskStatus -in @('done', 'failed')) { break }
  501. }
  502. }
  503. $stateOk = ($seenStates.Count -gt 0) -and (($seenStates | Where-Object { $_ -in @('pending','processing','done') }).Count -gt 0)
  504. Assert-True $stateOk '证据包状态流转 ∈ {pending,processing,done}' $resp ("seen=[" + ($seenStates -join ',') + "] progress=$taskProgress")
  505. if ($taskStatus -eq 'done') {
  506. Assert-True (($taskProgress -eq 100) -and ($fileUrl -ne '')) '证据包 done:progress=100 且 fileUrl 非空' $resp ("fileUrl=$fileUrl")
  507. } else {
  508. Write-Host ("S{0} [INFO] 轮询结束时 status={1}(异步导出未完成,不阻断;可手动复跑观察 done)" -f $script:Step, $taskStatus) -ForegroundColor Yellow
  509. }
  510. # ============================================================
  511. # S13-S17 履约链 + 评价 + 六契约端点(M5 试点上线冒烟扩写)
  512. # 前提:S9 建单 + S10 指派成功($orderId 已由 worker=本人置 accepted)。
  513. # 契约来源(2026-11-26 实读源码,非文档假设):
  514. # /api/order/arrive body{lat,lng,photo} accepted→arrived;文案「请先接单再打卡」
  515. # /api/order/finish body{items,photos} arrived/doing→done,返回 6 位 clientConfirmCode
  516. # /api/order/confirm body{code} done→confirmed;错码「确认码不正确」;重复「当前工单无需确认」
  517. # /api/eval/submit body{orderId,score(1-5),comment} ★only-after-confirmed,uk(order_id) 幂等,必须本人
  518. # 六契约端点实为管理端前缀:/api/admin/org/member/disable、/api/admin/cohort/list、
  519. # /api/admin/attendance/list、/api/admin/credit/account/list、
  520. # /api/admin/credit/ledger/detail、/api/admin/credit/ledger/objected
  521. # 注意:本步非幂等(状态机单向),重跑同一库须重新 dispatch/create 一单。
  522. # ============================================================
  523. $skipFulfillment = ($null -eq $orderId -or [long]$orderId -le 0)
  524. if ($skipFulfillment) {
  525. Write-Host "`n[WARN] S9/S10 未产出可用工单(orderId 为空),跳过 S13-S17 履约链与契约端点。" -ForegroundColor Yellow
  526. }
  527. # ============================================================
  528. # S13 履约① 到岗打卡 /api/order/arrive
  529. # ============================================================
  530. if (-not $skipFulfillment) {
  531. Begin-Step '履约① 到岗打卡 /api/order/arrive(accepted→arrived)'
  532. # 反例:未到岗直接 finish → 状态门禁拒绝(门禁先于业务写入,本调用不改库)
  533. $resp = Invoke-Api -Method 'POST' -Path '/api/order/finish' -Token $t1Token -Body @{
  534. orderId = $orderId; items = @(); photos = ''
  535. }
  536. $badFinish = if ($resp.message) { [string]$resp.message } else { '' }
  537. Assert-True (($resp.code -ne 200) -and ($badFinish -match '请先完成到岗打卡')) '反例:未到岗即 finish 被门禁拒绝' $resp ("msg=$badFinish")
  538. # 正例:定位 + 门牌照
  539. $resp = Invoke-Api -Method 'POST' -Path '/api/order/arrive' -Token $t1Token -Body @{
  540. orderId = $orderId; lat = 30.521; lng = 114.393; photo = 'm5/smoke-arrive.jpg'
  541. }
  542. Assert-True ($resp.code -eq 200 -and $resp.data -eq $true) 'worker 本人打卡' $resp ''
  543. $resp = Invoke-Api -Method 'POST' -Path '/api/order/detail' -Token $t1Token -Body @{ orderId = $orderId }
  544. Assert-True ($resp.code -eq 200 -and [string]$resp.data.status -eq 'arrived') '工单状态 → arrived' $resp ("status=" + $resp.data.status)
  545. Assert-True ($resp.data.arriveAt) 'arriveAt 服务端时间戳已落库' $resp ("arriveAt=" + $resp.data.arriveAt)
  546. Assert-True ($resp.data.arrivePhoto) 'arrivePhoto 门牌照已落库' $resp ("arrivePhoto=" + $resp.data.arrivePhoto)
  547. $resp = Invoke-Api -Method 'POST' -Path '/api/order/mine' -Token $t1Token -Body @{}
  548. # 契约实测:mine 分组键为 today/pending/doing/done,accepted/arrived/doing 统一归入 'doing'(进行中),
  549. # 无独立 'arrived' 分组;断言按 doing 分组复核。
  550. $seenMine = $false
  551. foreach ($g in (Get-Arr $resp.data)) {
  552. if ($g.key -eq 'doing') {
  553. foreach ($it in (Get-Arr $g.list)) { if ([long]$it.id -eq $orderId) { $seenMine = $true; break } }
  554. }
  555. }
  556. Assert-True ($resp.code -eq 200 -and $seenMine) 'worker /api/order/mine 该单出现在 doing(进行中)分组' $resp ''
  557. # 归属越权反例:m5none 非该单 worker(arrive 与 detail 均按 worker_id 本人校验)
  558. $resp = Invoke-Api -Method 'POST' -Path '/api/order/arrive' -Token $noneToken -Body @{
  559. orderId = $orderId; lat = 30.5; lng = 114.4
  560. }
  561. Assert-True ($resp.code -ne 200) '反例:非本人(m5none)打卡被拒' $resp ("msg=" + $resp.message)
  562. }
  563. # ============================================================
  564. # S14 履约② 完成服务 /api/order/finish(生成 6 位客户确认码)
  565. # ============================================================
  566. if (-not $skipFulfillment) {
  567. Begin-Step '履约② 完成服务 /api/order/finish(arrived→done + 6 位确认码)'
  568. $items = @(@{ name = '深度保洁'; done = 1 }, @{ name = '消毒'; done = 1 })
  569. $resp = Invoke-Api -Method 'POST' -Path '/api/order/finish' -Token $t1Token -Body @{
  570. orderId = $orderId
  571. items = $items
  572. photos = 'm5/smoke-fin1.jpg,m5/smoke-fin2.jpg'
  573. }
  574. $confirmCode = ''
  575. if ($resp.data -and $resp.data.clientConfirmCode) { $confirmCode = [string]$resp.data.clientConfirmCode }
  576. # 批③ 5e097cf 修复点验证:finish 响应体须回显**更新后**的 status/finishAt
  577. # (此前 finish 用 toVO(updateById 前实体) 造 VO,响应体 status 仍为 arrived、finishAt 为空;
  578. # 已改为 selectById 重取实体构造 VO)。此处直接断言响应体,不再只靠 detail 复核。
  579. Assert-True ($resp.code -eq 200) 'worker 完成服务受理' $resp ("clientConfirmCode=$confirmCode")
  580. Assert-True ([string]$resp.data.status -eq 'done') 'finish 响应体 status == done(5e097cf 修复点验证)' $resp ("status=" + $resp.data.status)
  581. Assert-True ($resp.data.finishAt) 'finish 响应体 finishAt 非空(5e097cf 修复点验证)' $resp ("finishAt=" + $resp.data.finishAt)
  582. Assert-True ($confirmCode.Length -eq 6 -and $confirmCode -match '^[0-9]{6}$') '生成 6 位数字客户确认码' $resp ("clientConfirmCode=$confirmCode")
  583. $finPhotos = @($resp.data.finishPhotos)
  584. Assert-True ($finPhotos.Count -eq 2) 'finishPhotos 回显 2 张' $resp ("count=" + $finPhotos.Count)
  585. $resp = Invoke-Api -Method 'POST' -Path '/api/order/detail' -Token $t1Token -Body @{ orderId = $orderId }
  586. $detailCode = if ($resp.data.clientConfirmCode) { [string]$resp.data.clientConfirmCode } else { '' }
  587. Assert-True ($resp.code -eq 200 -and $resp.data.status -eq 'done' -and $detailCode -eq $confirmCode) '工单详情复核:done + 同码' $resp ("code=$detailCode")
  588. Assert-True ($resp.data.finishAt) '详情 finishAt 服务端时间戳已落库' $resp ("finishAt=" + $resp.data.finishAt)
  589. }
  590. # ============================================================
  591. # S15 履约③ 客户确认 /api/order/confirm(正反例)
  592. # ============================================================
  593. if (-not $skipFulfillment) {
  594. Begin-Step '履约③ 客户确认 /api/order/confirm(done→confirmed 正反例)'
  595. # 反例:错码 → 拒绝(不落状态)
  596. $wrongCode = if ($confirmCode -match '^1') { '200000' } else { '100000' }
  597. $resp = Invoke-Api -Method 'POST' -Path '/api/order/confirm' -Token $t1Token -Body @{
  598. orderId = $orderId; code = $wrongCode
  599. }
  600. $badCodeMsg = if ($resp.message) { [string]$resp.message } else { '' }
  601. Assert-True (($resp.code -ne 200) -and ($badCodeMsg -match '确认码不正确')) '反例:错误确认码被拒' $resp ("msg=$badCodeMsg")
  602. $chk = Invoke-Api -Method 'POST' -Path '/api/order/detail' -Token $t1Token -Body @{ orderId = $orderId }
  603. Assert-True ([string]$chk.data.status -eq 'done') '错码后状态未漂移(仍 done)' $chk ("status=" + $chk.data.status)
  604. # 正例:正确码 → confirmed
  605. $resp = Invoke-Api -Method 'POST' -Path '/api/order/confirm' -Token $t1Token -Body @{
  606. orderId = $orderId; code = $confirmCode
  607. }
  608. Assert-True ($resp.code -eq 200 -and $resp.data -eq $true) '正确确认码核销 → confirmed' $resp ''
  609. $resp = Invoke-Api -Method 'POST' -Path '/api/order/detail' -Token $t1Token -Body @{ orderId = $orderId }
  610. Assert-True ([string]$resp.data.status -eq 'confirmed') '工单详情 → confirmed(终态)' $resp ("status=" + $resp.data.status)
  611. # 反例:重复确认 → 当前工单无需确认
  612. $resp = Invoke-Api -Method 'POST' -Path '/api/order/confirm' -Token $t1Token -Body @{
  613. orderId = $orderId; code = $confirmCode
  614. }
  615. $dupMsg = if ($resp.message) { [string]$resp.message } else { '' }
  616. Assert-True (($resp.code -ne 200) -and ($dupMsg -match '当前工单无需确认')) '反例:重复确认被拒(终态不可重入)' $resp ("msg=$dupMsg")
  617. # 终态入账:已确认单计入累计所得(金额为整数分,staff_income=16200 分)
  618. $resp = Invoke-Api -Method 'POST' -Path '/api/order/earnings' -Token $t1Token -Body @{}
  619. Assert-True ($resp.code -eq 200 -and $resp.data.totalIncome -ge 16200) 'earnings.totalIncome 计入已确认单(16200 分)' $resp ("totalIncome=" + $resp.data.totalIncome)
  620. }
  621. # ============================================================
  622. # S16 评价 /api/eval/submit(only-after-confirmed,正反例)
  623. # ============================================================
  624. if (-not $skipFulfillment) {
  625. Begin-Step '评价 /api/eval/submit(EVA-TC-001 正例 / 002 反例)'
  626. # 正例:confirmed 后可评
  627. $resp = Invoke-Api -Method 'POST' -Path '/api/eval/submit' -Token $t1Token -Body @{
  628. orderId = $orderId; score = 5; comment = 'M5 冒烟履约评价'
  629. }
  630. $evalId = if ($resp.data -and $resp.data.evalId) { [long]$resp.data.evalId } else { -1 }
  631. Assert-True ($resp.code -eq 200 -and $resp.data -and $resp.data.score -eq 5 -and $resp.data.orderId -eq $orderId) 'EVA-TC-001:confirmed 后提交成功' $resp ("evalId=" + $resp.data.evalId + " evalType=" + $resp.data.evalType)
  632. # 幂等:二次提交返回同一条(uk(order_id) once-per-order)
  633. $resp2 = Invoke-Api -Method 'POST' -Path '/api/eval/submit' -Token $t1Token -Body @{
  634. orderId = $orderId; score = 4; comment = '二次提交'
  635. }
  636. $evalId2 = if ($resp2.data -and $resp2.data.evalId) { [long]$resp2.data.evalId } else { -2 }
  637. Assert-True ($resp2.code -eq 200 -and $evalId2 -eq $evalId) '评价幂等(同 orderId 返回同 evalId,不覆盖首评)' $resp2 ("evalId=$evalId/$evalId2")
  638. # 汇总可见(含本人履约评价计数)
  639. $resp = Invoke-Api -Method 'POST' -Path '/api/eval/mine' -Token $t1Token -Body @{}
  640. Assert-True ($resp.code -eq 200 -and $resp.data) 'eval/mine 可查(履约评价并入汇总)' $resp ''
  641. # --- 一次性反例工单:accepted(未 confirmed)→ 评价必须拒绝 ---
  642. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/org/dispatch/create' -Token $adminToken -Body @{
  643. orgId = $orgId; familyName = 'M5冒烟评价反例户'; contactPhone = '13900000099'
  644. address = '湖北省黄冈市浠水县试点路 2 号'; serviceType = 'cleaning'
  645. orderDate = '2026-12-05'; amount = 1000; platformFee = 100; remark = 'M5 冒烟一次性反例单'
  646. }
  647. if ($resp.code -ne 200 -or -not $resp.data) { Assert-Fail '反例单建单失败' $resp '' }
  648. $negOrderId = [long]$resp.data.orderId
  649. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/org/dispatch/assign' -Token $adminToken -Body @{
  650. orderId = $negOrderId; workerId = $t1UserId; serviceType = 'cleaning'; remark = '指派反例单'
  651. }
  652. Assert-True ($resp.code -eq 200 -and [string]$resp.data.status -eq 'accepted') '反例单指派 → accepted(未 confirmed)' $resp ("negOrderId=$negOrderId")
  653. # 履约门禁反例:accepted 未打卡即 finish → 拒绝(同时证明本单停在 accepted)
  654. $resp = Invoke-Api -Method 'POST' -Path '/api/order/finish' -Token $t1Token -Body @{
  655. orderId = $negOrderId; items = @(); photos = ''
  656. }
  657. Assert-True ($resp.code -ne 200) '反例单:未打卡即 finish 被拒(停在 accepted)' $resp ("msg=" + $resp.message)
  658. # EVA-TC-002:未 confirmed 工单不可评价
  659. $resp = Invoke-Api -Method 'POST' -Path '/api/eval/submit' -Token $t1Token -Body @{
  660. orderId = $negOrderId; score = 5; comment = '反例:未确认即评'
  661. }
  662. $negMsg = if ($resp.message) { [string]$resp.message } else { '' }
  663. Assert-True (($resp.code -ne 200) -and ($negMsg -match '工单未确认')) 'EVA-TC-002:未 confirmed 工单评价被拒' $resp ("msg=$negMsg")
  664. # 评分越界反例
  665. $resp = Invoke-Api -Method 'POST' -Path '/api/eval/submit' -Token $t1Token -Body @{
  666. orderId = $orderId; score = 6
  667. }
  668. $scoreMsg = if ($resp.message) { [string]$resp.message } else { '' }
  669. Assert-True (($resp.code -ne 200) -and ($scoreMsg -match '评分须为1-5星')) '反例:score=6 越界被拒' $resp ("msg=$scoreMsg")
  670. }
  671. # ============================================================
  672. # S17 六契约端点(管理端前缀实测):各 200 + 关键字段
  673. # ============================================================
  674. Begin-Step '六契约端点(member/disable、cohort/list、attendance/list、credit 三端点)'
  675. # ① /api/admin/org/member/disable:重复禁用 → 幂等成功(不动 S3 依赖的 m5t1 org_admin 身份)
  676. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/org/member/disable' -Token $adminToken -Body @{
  677. orgId = $orgId; userId = $noneUserId
  678. }
  679. Assert-True ($resp.code -eq 200 -and $resp.data -eq $true) 'member/disable 重复调用幂等成功' $resp ("memberUserId=$noneUserId")
  680. # ② /api/admin/cohort/list:每行含 enrolledCount(契约别名,与 signedCount 同源)
  681. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/cohort/list' -Token $adminToken -Body @{
  682. courseId = $courseId; page = 1; size = 10
  683. }
  684. $hasEnrolled = $false
  685. foreach ($row in (Get-Arr $resp.data.list)) {
  686. if ($null -ne $row.PSObject.Properties['enrolledCount']) { $hasEnrolled = $true; break }
  687. }
  688. Assert-True ($resp.code -eq 200 -and $hasEnrolled) 'cohort/list 行含 enrolledCount' $resp ("total=" + $resp.data.total)
  689. # ③ /api/admin/attendance/list:行含 userName(按签到 cohort 过滤,避免命中历史测试批次)
  690. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/attendance/list' -Token $adminToken -Body @{
  691. cohortId = $cohortId; page = 1; size = 10
  692. }
  693. $hasName = $false
  694. $showName = ''
  695. foreach ($row in (Get-Arr $resp.data.list)) {
  696. if ($null -ne $row.PSObject.Properties['userName']) { $hasName = $true; $showName = [string]$row.userName; break }
  697. }
  698. Assert-True ($resp.code -eq 200 -and $hasName) 'attendance/list 行含 userName' $resp ("userName=" + (Fix-MojibakeText $showName))
  699. # ④ /api/admin/credit/account/list:行含 userId/year/totalHours(学时分钟整数)
  700. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/credit/account/list' -Token $adminToken -Body @{
  701. year = 2026; page = 1; size = 10
  702. }
  703. $accOk = $false
  704. if ($resp.code -eq 200 -and $resp.data) {
  705. $accOk = ($null -ne $resp.data.PSObject.Properties['list'])
  706. foreach ($row in (Get-Arr $resp.data.list)) {
  707. if ($row.userId -and $row.year -and $null -ne $row.totalHours) { $accOk = $true; break }
  708. }
  709. }
  710. Assert-True $accOk 'credit/account/list 行含 userId/year/totalHours' $resp ("total=" + $resp.data.total)
  711. # ⑤ /api/admin/credit/ledger/detail:单学员单年度明细(hours 分钟整数红线)
  712. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/credit/ledger/detail' -Token $adminToken -Body @{
  713. userId = $stuUserId; year = 2026
  714. }
  715. $rows = Get-Arr $resp.data
  716. $detailOk = ($rows.Count -ge 1)
  717. foreach ($row in $rows) {
  718. if (-not (Test-MinuteInteger $row.hours)) { $detailOk = $false }
  719. }
  720. Assert-True ($resp.code -eq 200 -and $detailOk) 'credit/ledger/detail 返回明细且 hours 为分钟整数' $resp ("rows=" + $rows.Count)
  721. # ⑥ /api/admin/credit/ledger/objected:列表模式 + 处置模式(双模,同一路径)
  722. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/credit/ledger/objected' -Token $adminToken -Body @{ page = 1; size = 10 }
  723. $hasList = ($resp.code -eq 200 -and $resp.data -and ($null -ne $resp.data.PSObject.Properties['list']))
  724. Assert-True $hasList 'ledger/objected 列表模式返回 {total,page,size,list}' $resp ("total=" + $resp.data.total)
  725. # 处置分支:故意传不存在的台账 id → 业务拒绝且不改库(避免处置真实异议台账)
  726. $resp = Invoke-Api -Method 'POST' -Path '/api/admin/credit/ledger/objected' -Token $adminToken -Body @{
  727. id = 999999999; approved = $false; reason = 'M5 冒烟双模探针'
  728. }
  729. $dispMsg = if ($resp.message) { [string]$resp.message } else { '' }
  730. Assert-True (($resp.code -ne 200) -and ($dispMsg -match '学时记录不存在')) 'ledger/objected 处置分支:不存在台账被拒(双模路由命中)' $resp ("msg=$dispMsg")
  731. Write-Host ("S{0} [PASS] 六契约端点各 200 + 关键字段齐全" -f $script:Step) -ForegroundColor Green
  732. # ============================================================
  733. # 总结
  734. # ============================================================
  735. Write-Host "`n========================================" -ForegroundColor Cyan
  736. if ($skipFulfillment) {
  737. Write-Host ("冒烟完成:S1-S12 通过,S13-S17 跳过(无可用工单)。orgId=$orgId cohortId=$cohortId") -ForegroundColor Yellow
  738. } else {
  739. Write-Host ("冒烟完成:17 步全部通过(orgId=$orgId cohortId=$cohortId orderId=$orderId dispatchId=$dispatchId code=$confirmCode negOrderId=$negOrderId)") -ForegroundColor Green
  740. Write-Host "关键断言:学时分钟整数 ✓ 建单留痕 dispatchId ✓ 资质闸口放行/拒绝 ✓" -ForegroundColor Green
  741. Write-Host "履约链:arrived → done(6 位确认码) → confirmed → 可评价;错码/重复/未确认/越权均拒绝 ✓" -ForegroundColor Green
  742. }
  743. Write-Host "========================================" -ForegroundColor Cyan
  744. exit 0