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('source store cards use vertical cover-first layout', () => {
  const source = readView('SourceStoreView.vue')

  assert.match(source, /\.source-card\s*\{[\s\S]*grid-template-rows:\s*auto minmax\(0,\s*1fr\);/)
  assert.doesNotMatch(source, /\.source-card\s*\{[\s\S]*grid-template-columns:\s*112px minmax\(0,\s*1fr\);/)
})

test('source store cards do not actively render project summary', () => {
  const source = readView('SourceStoreView.vue')
  const template = source.match(/<template>[\s\S]*<\/template>/)[0]
  const activeTemplate = template.replace(/<!--[\s\S]*?-->/g, '')

  assert.equal(activeTemplate.includes('project.summary'), false)
  assert.equal(source.includes('<!-- <p>{{ project.summary }}</p> -->'), true)
})

test('source store loads categories from the front source API', () => {
  const source = readView('SourceStoreView.vue')

  assert.equal(source.includes('listSourceCategories'), true)
  assert.match(source, /buildSourceCategories\(sourceProjects\.value,\s*sourceCategories\.value\)/)
})

test('source store and detail do not expose online preview actions', () => {
  const combinedSource = `${readView('SourceStoreView.vue')}\n${readView('SourceDetailView.vue')}`

  assert.equal(combinedSource.includes('在线预览'), false)
  assert.equal(combinedSource.includes('handlePreview'), false)
})

test('source detail does not show source size information', () => {
  const source = readView('SourceDetailView.vue')

  assert.equal(source.includes('源码大小'), false)
  assert.equal(source.includes('sourceSize'), false)
})

test('source detail keeps purchase as the unauthenticated primary action', () => {
  const source = readView('SourceDetailView.vue')

  assert.equal(source.includes('购买源码'), true)
  assert.equal(source.includes('handlePurchase'), true)
  assert.equal(source.includes('project.purchased ? handleDownload() : handlePurchase()'), true)
})
