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

import {
  canViewerConfirmAssistedRole,
  canViewerCloseTask,
  createAdminViewerContext,
  resolveAdminTaskScreenshotUrl,
} from './admin-read-shared-helpers.js'

test('resolveAdminTaskScreenshotUrl lets support view final redeemed screenshot', async () => {
  const task = {
    id: 12,
    screenshot_path: '/tmp/redeemed-12.png',
    browser_session_id: '',
  }

  const screenshotUrl = await resolveAdminTaskScreenshotUrl(task, createAdminViewerContext({ role: 'support' }))

  assert.equal(screenshotUrl, '/api/v1/admin/tasks/12/screenshot')
})

test('resolveAdminTaskScreenshotUrl falls back to review screenshot when only browser session exists', async () => {
  const task = {
    id: 13,
    screenshot_path: '',
    browser_session_id: 'browser-session-13',
  }

  const screenshotUrl = await resolveAdminTaskScreenshotUrl(task, createAdminViewerContext({ role: 'support' }))

  assert.equal(screenshotUrl, '/api/v1/admin/tasks/13/screenshot')
})

test('canViewerCloseTask allows support to close active task but not redeemed task', () => {
  const viewerContext = createAdminViewerContext({ role: 'support' })

  assert.equal(canViewerCloseTask({ task_status: 'link_generated' }, viewerContext), true)
  assert.equal(canViewerCloseTask({ task_status: 'redeemed' }, viewerContext), false)
})

test('canViewerConfirmAssistedRole blocks support when task inventory group is not bound', () => {
  const viewerContext = createAdminViewerContext({
    role: 'support',
    allowedInventoryGroups: ['A组'],
  })

  assert.equal(canViewerConfirmAssistedRole({
    task_status: 'claimed',
    executor_key: 'tencent_claim_assisted',
    context_json: {
      inventoryGroupCode: 'B组',
    },
  }, viewerContext), false)
})
