import assert from 'node:assert/strict'
import test from 'node:test'

import { readSourceFile } from './helpers/sourceSurface.mjs'

const workspace = readSourceFile('components/approval/ApprovalTaskWorkspace.vue')
const documentsCenter = readSourceFile('views/DocumentsCenterView.vue')
const appShell = readSourceFile('views/AppShellRouteView.vue')

test('approval task workspace wires every server-authorized action to a real mutation', () => {
  assert.match(workspace, /approveApprovalTask/)
  assert.match(workspace, /returnApprovalTask/)
  assert.match(workspace, /escalateApprovalTask/)
  assert.match(workspace, /<ApprovalAssignmentDialog/)
  assert.match(workspace, /<ApprovalParticipantsDialog/)
  assert.match(workspace, /<ReturnReasonDialog/)
  assert.doesNotMatch(workspace, /已批量通过 23 条|mock/i)
})

test('approval task decisions keep one request id across retries and enforce task version', () => {
  assert.match(workspace, /resolveApprovalTaskRetryKey/)
  assert.match(workspace, /previousFingerprint: requestFingerprint\.value/)
  assert.match(workspace, /previousRequestId: requestId\.value/)
  assert.match(workspace, /payload\.requestId = retryKey\.requestId/)
  assert.match(workspace, /expectedTaskVersion: Number\(activeTask\.value\.version\)/)
  assert.match(workspace, /mutation\?\.replayed[\s\S]*结果已安全重放/)
  assert.match(workspace, /await queueRef\.value\?\.reload\?\.\(\)/)
})

test('documents review tab owns the task workspace and propagates updates to the app shell', () => {
  assert.match(documentsCenter, /<ApprovalTaskWorkspace[\s\S]*v-show="activeScopeTab === DOCUMENT_SCOPE_REVIEW"/)
  assert.match(documentsCenter, /@request-updated="emit\('request-updated', \$event\)"/)
  assert.match(documentsCenter, /@loaded="handleApprovalTasksLoaded"/)
  assert.doesNotMatch(documentsCenter, /fetchApprovalExpenseClaims|approvalRows/)
  assert.match(appShell, /<DocumentsCenterView[\s\S]*@request-updated="handleRequestUpdated"/)
})

test('workspace separates the unfiltered pending summary from queue filters and refreshes batches', () => {
  assert.match(workspace, /service\(\{ status: 'pending', page: 1, pageSize: 1 \}\)/)
  assert.match(workspace, /@loaded="handleQueueLoaded"/)
  assert.match(workspace, /void loadTaskSummary\(\)[\s\S]*emit\('batch-result', result\)/)
  assert.match(workspace, /emit\('request-updated', \{ claim: claims\[0\], claimId: claimIds\[0\], claimIds \}\)/)
  assert.match(workspace, /const workspaceError = computed\(\(\) => queueError\.value \|\| summaryError\.value\)/)
})
