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

import { renderRuntimeUpdate } from './apply-local-runtime-patch.mjs'

test('renderRuntimeUpdate turns setup payload into runtime_engines update fields', () => {
  const update = renderRuntimeUpdate({
    runtimeCode: 'local-qoder',
    patch: {
      name: '本地 Qoder',
      command: '/usr/local/bin/qoder',
      args: ['-'],
      healthcheckArgs: ['--version'],
      purposeScopes: ['create', 'evaluate', 'run'],
      timeoutMs: 300000,
      enabled: true,
      notes: 'Lite 学员包本地运行时',
    },
  })

  assert.equal(update.runtimeCode, 'local-qoder')
  assert.deepEqual(update.fields, {
    name: '本地 Qoder',
    local_command: '/usr/local/bin/qoder',
    local_args_json: '["-"]',
    local_healthcheck_args_json: '["--version"]',
    purpose_scopes_json: '["create","evaluate","run"]',
    timeout_ms: 300000,
    enabled: 1,
    notes: 'Lite 学员包本地运行时',
  })
})

test('renderRuntimeUpdate rejects empty custom commands before touching the database', () => {
  assert.throws(
    () => renderRuntimeUpdate({
      runtimeCode: 'local-custom',
      patch: {
        name: '自定义本地 CLI',
        command: '',
        args: ['-'],
      },
    }),
    /local runtime command is required/,
  )
})
