import test from "node:test"
import assert from "node:assert/strict"
import { readFileSync } from "node:fs"

const source = readFileSync(new URL("../src/features/tasks/TaskPanelView.tsx", import.meta.url), "utf8")
const taskStylesSource = readFileSync(new URL("../src/styles/tasks.css", import.meta.url), "utf8")

function cssBlock(sourceText: string, selector: string): string {
  const start = sourceText.indexOf(`${selector} {`)
  assert.notEqual(start, -1, `Missing CSS selector: ${selector}`)
  const end = sourceText.indexOf("\n}", start)
  assert.notEqual(end, -1, `Missing CSS block end: ${selector}`)
  return sourceText.slice(start, end + 2)
}

test("task panel output uses real artifact paths and produced timestamps", () => {
  assert.match(source, /artifact\.path\s*\?\?\s*artifact\.url/)
  assert.match(source, /artifact\.producedAt\s*\?\?/)
})

test("task panel output renders pathless artifacts without copy controls", () => {
  assert.match(source, /const artifactPath = artifact\.path \?\? artifact\.url/)
  assert.match(source, /<strong title=\{artifact\.name\}>\{artifact\.name\}<\/strong>/)
  assert.match(source, /<p>\{artifact\.summary \?\? task\.taskTitle\}<\/p>/)
  assert.match(source, /\{artifactPath \? \(/)
})

test("task panel output copy feedback is scoped to the clicked output row", () => {
  assert.match(source, /copiedOutputRowKey/)
  assert.match(source, /const outputRowKey = getTaskPanelOutputRowKey\(task, artifact\)/)
  assert.match(source, /copyArtifactUrl\(outputRowKey, artifactPath\)/)
  assert.match(source, /copiedOutputRowKey === outputRowKey/)
  assert.doesNotMatch(source, /copiedArtifactId === artifact\.id/)
})

test("task panel output list uses visible artifact filtering", () => {
  assert.match(source, /getVisibleTaskPanelArtifacts/)
  assert.match(source, /getVisibleTaskPanelArtifacts\(task\)\.map/)
  assert.doesNotMatch(source, /task\.artifacts\.map\(\(artifact\) => \(\{ artifact, task \}\)\)/)
})

test("task panel output summaries use a single-line visual ellipsis", () => {
  const summaryBlock = cssBlock(taskStylesSource, ".task-panel-output-main p")
  assert.match(summaryBlock, /overflow:\s*hidden;/)
  assert.match(summaryBlock, /text-overflow:\s*ellipsis;/)
  assert.match(summaryBlock, /white-space:\s*nowrap;/)
})
