import test from 'node:test'
import assert from 'node:assert/strict'
import {
  missingAgentRunConfig,
  rowToAgentRunProvider,
  sanitizeAgentRunPatch,
  secretPreview,
} from './agentrun-config.js'

test('rowToAgentRunProvider hides all AgentRun secrets from API responses', () => {
  const provider = rowToAgentRunProvider({
    code: 'agentrun',
    name: '阿里云 AgentRun',
    protocol: 'agentrun',
    enabled: 1,
    is_active: 1,
    access_key_id: 'LTAI5tExampleKeyId',
    access_key_secret: 'very-secret-value',
    api_key: 'ak-secret-runtime-key',
    security_token: 'sts-token-value',
    account_id: '1234567890',
    region_id: 'cn-hangzhou',
    runtime_name: 'aipros-runtime',
    endpoint_name: 'default',
    endpoint_public_url: 'https://example.aliyun.com',
  })

  assert.equal(provider.hasAccessKeyId, true)
  assert.equal(provider.hasAccessKeySecret, true)
  assert.equal(provider.hasInvocationKey, true)
  assert.equal(provider.hasSecurityToken, true)
  assert.equal(provider.keyPreview, '...-key')
  assert.equal(provider.accessKeyIdPreview, '...eyId')
  assert.equal(JSON.stringify(provider).includes('very-secret-value'), false)
  assert.equal(JSON.stringify(provider).includes('ak-secret-runtime-key'), false)
  assert.equal(JSON.stringify(provider).includes('sts-token-value'), false)
})

test('missingAgentRunConfig reports only the fields required for invoking a runtime', () => {
  assert.deepEqual(missingAgentRunConfig({
    enabled: 1,
    access_key_id: '',
    access_key_secret: '',
    account_id: '',
    region_id: 'cn-hangzhou',
    runtime_name: '',
    endpoint_name: '',
  }), ['access_key_id', 'access_key_secret', 'account_id', 'runtime_name'])

  assert.deepEqual(missingAgentRunConfig({
    enabled: 1,
    access_key_id: 'id',
    access_key_secret: 'secret',
    account_id: '123',
    region_id: 'cn-hangzhou',
    runtime_name: 'runtime',
    endpoint_name: '',
  }), [])
})

test('missingAgentRunConfig accepts a direct AgentRun integration URL with invocation key', () => {
  assert.deepEqual(missingAgentRunConfig({
    enabled: 1,
    endpoint_public_url: 'https://1242739972938523.agentrun-data.cn-hangzhou.aliyuncs.com/agent-runtimes/AIPRO-SP/endpoints/Default/invocations/openai/v1/chat/completions',
    api_key: 'ak-secret',
    access_key_id: '',
    access_key_secret: '',
    account_id: '',
    runtime_name: '',
  }), [])
})

test('missingAgentRunConfig requires invocation key for direct AgentRun integration URL', () => {
  assert.deepEqual(missingAgentRunConfig({
    enabled: 1,
    endpoint_public_url: 'https://1242739972938523.agentrun-data.cn-hangzhou.aliyuncs.com/agent-runtimes/AIPRO-SP/endpoints/Default/invocations/openai/v1/chat/completions',
    api_key: '',
  }), ['api_key'])
})

test('secretPreview exposes only the last four characters', () => {
  assert.equal(secretPreview('abcdefg'), '...defg')
  assert.equal(secretPreview('abc'), '已保存')
  assert.equal(secretPreview(''), null)
})

test('sanitizeAgentRunPatch keeps stored secrets when secret inputs are blank', () => {
  const { patch, merged } = sanitizeAgentRunPatch({
    accessKeyId: '',
    accessKeySecret: '',
    securityToken: '',
    accountId: '123',
  }, {
    access_key_id: 'stored-id',
    access_key_secret: 'stored-secret',
    security_token: 'stored-token',
    region_id: 'cn-hangzhou',
    endpoint_name: 'default',
  })

  assert.equal('access_key_id' in patch, false)
  assert.equal('access_key_secret' in patch, false)
  assert.equal('security_token' in patch, false)
  assert.equal(merged.access_key_id, 'stored-id')
  assert.equal(merged.access_key_secret, 'stored-secret')
  assert.equal(merged.security_token, 'stored-token')
})

test('sanitizeAgentRunPatch derives AgentRun fields from integration URL', () => {
  const { patch, merged } = sanitizeAgentRunPatch({
    endpointPublicUrl: 'https://1242739972938523.agentrun-data.cn-hangzhou.aliyuncs.com/agent-runtimes/AIPRO-SP/endpoints/Default/invocations/openai/v1/chat/completions',
    invocationKey: 'ak-secret',
  }, {})

  assert.equal(patch.api_key, 'ak-secret')
  assert.equal(patch.account_id, '1242739972938523')
  assert.equal(patch.region_id, 'cn-hangzhou')
  assert.equal(patch.runtime_name, 'AIPRO-SP')
  assert.equal(patch.endpoint_name, 'Default')
  assert.equal(merged.account_id, '1242739972938523')
})
