import test from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, resolve } from 'node:path'

const currentDir = dirname(fileURLToPath(import.meta.url))

function readView(name) {
  return readFileSync(resolve(currentDir, name), 'utf8')
}

test('project list row actions use compact icon buttons like profile project actions', () => {
  const source = readView('ProjectListView.vue')

  assert.equal(source.includes('project-icon-button'), true)
  assert.equal(source.includes('el-tooltip content="专家编辑"'), true)
  assert.equal(source.includes('aria-label="专家编辑"'), true)
  assert.equal(source.includes('aria-label="预览"'), true)
  assert.equal(source.includes('aria-label="下载"'), true)
  assert.equal(source.includes('aria-label="删除"'), true)
  assert.match(source, /<el-table-column label="操作" width="328" fixed="right">/)
  assert.doesNotMatch(source, /<el-button[^>]*>\s*继续生成\s*<\/el-button>/)
  assert.doesNotMatch(source, /<el-button[^>]*>\s*预览\s*<\/el-button>/)
  assert.doesNotMatch(source, /<el-button[^>]*>\s*下载\s*<\/el-button>/)
  assert.doesNotMatch(source, /<el-button[^>]*>\s*删除\s*<\/el-button>/)
})

test('empty project state does not include a duplicate create project button', () => {
  const source = readView('ProjectListView.vue')

  assert.equal(source.includes("router.push({ path: '/generate', query: { mode: 'one-click' } })"), true)
  assert.doesNotMatch(source, /<el-empty[\s\S]*?<el-button[\s\S]*?<\/el-empty>/)
})

test('project list truncates long project descriptions after two lines', () => {
  const source = readView('ProjectListView.vue')

  const styleMatch = source.match(/\.project-desc\s*\{([\s\S]*?)\n\}/)
  assert.notEqual(styleMatch, null)
  assert.match(styleMatch[1], /display:\s*-webkit-box;/)
  assert.match(styleMatch[1], /-webkit-box-orient:\s*vertical;/)
  assert.match(styleMatch[1], /-webkit-line-clamp:\s*2;/)
  assert.match(styleMatch[1], /overflow:\s*hidden;/)
})

test('project list exposes compact module diagram action', () => {
  const source = readView('ProjectListView.vue')

  assert.equal(source.includes('el-tooltip content="模块图"'), true)
  assert.equal(source.includes('aria-label="模块图"'), true)
  assert.equal(source.includes('openModuleDiagramEditor'), true)
  assert.equal(source.includes('`/project/${projectId}/module-diagram`'), true)
})
