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

import { selectCloudtentaclesSourceForFulfillment } from './account-selector.js'

const contexts = {
  'account-a': {
    baseUrl: 'https://cloud.example.com',
    token: 'token-a',
    deviceId: '-',
    deviceType: 0,
    resolvedSourceKey: 'account-a',
  },
  'account-b': {
    baseUrl: 'https://cloud.example.com',
    token: 'token-b',
    deviceId: '-',
    deviceType: 0,
    resolvedSourceKey: 'account-b',
  },
}

test('selectCloudtentaclesSourceForFulfillment 已固定账号时不按压力切换', async () => {
  const selection = await selectCloudtentaclesSourceForFulfillment(
    {
      binding: {
        resolvedSourceKey: 'account-a',
        cloudSourceKeys: ['account-a', 'account-b'],
      },
    },
    {
      resolveContextBySourceKeys: (sourceKeys) => {
        const key = String(sourceKeys[0] || '').trim()
        return contexts[key as keyof typeof contexts]
      },
      listSourceLoadStats: async () => [
        { sourceKey: 'account-a', activeCount: 99, redeemingCount: 99 },
        { sourceKey: 'account-b', activeCount: 0, redeemingCount: 0 },
      ],
    },
  )

  assert.equal(selection.sourceKey, 'account-a')
  assert.equal(selection.fixed, true)
})

test('selectCloudtentaclesSourceForFulfillment 多候选时选择压力最低账号', async () => {
  const selection = await selectCloudtentaclesSourceForFulfillment(
    {
      binding: {
        cloudSourceKeys: ['account-a', 'account-b'],
      },
    },
    {
      resolveContextBySourceKeys: (sourceKeys) => {
        const key = String(sourceKeys[0] || '').trim()
        return contexts[key as keyof typeof contexts]
      },
      listSourceLoadStats: async () => [
        { sourceKey: 'account-a', activeCount: 3, redeemingCount: 1 },
        { sourceKey: 'account-b', activeCount: 0, redeemingCount: 0 },
      ],
    },
  )

  try {
    assert.equal(selection.sourceKey, 'account-b')
    assert.equal(selection.fixed, false)
  } finally {
    selection.release()
  }
})

test('selectCloudtentaclesSourceForFulfillment 会跳过不可用账号', async () => {
  const selection = await selectCloudtentaclesSourceForFulfillment(
    {
      binding: {
        cloudSourceKeys: ['account-a', 'account-b'],
      },
    },
    {
      resolveContextBySourceKeys: (sourceKeys) => {
        const key = String(sourceKeys[0] || '').trim()
        if (key === 'account-a') {
          throw new Error('账号已停用')
        }
        return contexts[key as keyof typeof contexts]
      },
      listSourceLoadStats: async () => [],
    },
  )

  try {
    assert.equal(selection.sourceKey, 'account-b')
    assert.equal(selection.candidateCount, 1)
  } finally {
    selection.release()
  }
})

test('selectCloudtentaclesSourceForFulfillment 用进程内占位分散并发准备', async () => {
  const deps = {
    resolveContextBySourceKeys: (sourceKeys: unknown[] = []) => {
      const key = String(sourceKeys[0] || '').trim()
      return contexts[key as keyof typeof contexts]
    },
    listSourceLoadStats: async () => [],
  }
  const flow = {
    binding: {
      cloudSourceKeys: ['account-a', 'account-b'],
    },
  }
  const first = await selectCloudtentaclesSourceForFulfillment(flow, deps)
  const second = await selectCloudtentaclesSourceForFulfillment(flow, deps)

  try {
    assert.equal(first.sourceKey, 'account-a')
    assert.equal(second.sourceKey, 'account-b')
  } finally {
    first.release()
    second.release()
  }
})
