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

const appSource = readFileSync(new URL("../src/App.tsx", import.meta.url), "utf8")
const sidebarSource = readFileSync(new URL("../src/features/shell/AppSidebar.tsx", import.meta.url), "utf8")
const mockApiSource = readFileSync(new URL("../src/lib/mock-desktop-api.ts", import.meta.url), "utf8")
const desktopApiSource = readFileSync(new URL("../src/lib/desktop-api.ts", import.meta.url), "utf8")
const automationStyles = readFileSync(new URL("../src/styles/automation.css", import.meta.url), "utf8")

test("automation tasks view is available from the sidebar above settings", () => {
  assert.match(appSource, /AutomationTasksView/)
  assert.match(appSource, /viewMode === "automation"/)
  assert.match(sidebarSource, /id: "automation" as const, label: "自动化任务"/)
  assert.match(sidebarSource, /id: "settings" as const/)
  assert.ok(sidebarSource.indexOf('id: "automation"') < sidebarSource.indexOf('id: "settings"'))
})

test("mock desktop API exposes automation task methods for UI development", () => {
  assert.match(desktopApiSource, /mockDesktopApi/)
  assert.match(mockApiSource, /automationTasks:\s*\{/)
  assert.match(mockApiSource, /listRuns/)
  assert.match(mockApiSource, /runNow/)
})

test("automation task destructive and utility actions are explicit", () => {
  const viewSource = readFileSync(new URL("../src/features/automation/AutomationTasksView.tsx", import.meta.url), "utf8")

  assert.match(viewSource, /确认删除自动化任务/)
  assert.match(viewSource, />刷新</)
  assert.match(viewSource, />新建任务</)
  assert.match(viewSource, />编辑</)
  assert.match(viewSource, />立即执行</)
  assert.match(viewSource, />删除</)
  assert.doesNotMatch(viewSource, /automation-button-toggle/)
  assert.doesNotMatch(viewSource, /aria-label="删除自动化任务" onClick=\{\(\) => void deleteTask/)
})

test("automation task view exposes lifecycle text without quick enable and disable controls", () => {
  const viewSource = readFileSync(new URL("../src/features/automation/AutomationTasksView.tsx", import.meta.url), "utf8")

  assert.doesNotMatch(viewSource, /toggleTaskEnabled/)
  assert.doesNotMatch(viewSource, /automationTasks\.update\(task\.id,\s*\{\s*enabled: !task\.enabled\s*\}\)/s)
  assert.doesNotMatch(viewSource, /automation-task-row-actions/)
  assert.doesNotMatch(viewSource, /automation-row-toggle-button/)
  assert.match(viewSource, /已完成，已停用/)
  assert.match(viewSource, /已错过，已停用/)
  assert.match(viewSource, /已停用/)
})

test("automation task list shows only task title and prompt", () => {
  const viewSource = readFileSync(new URL("../src/features/automation/AutomationTasksView.tsx", import.meta.url), "utf8")

  assert.match(viewSource, /<strong>\{task\.title\}<\/strong>/)
  assert.match(viewSource, /<small>\{task\.prompt\}<\/small>/)
  assert.doesNotMatch(viewSource, /下次执行：\{getTaskNextRunLabel\(task\)\}/)
  assert.doesNotMatch(viewSource, /<StatusChip tone=\{getTaskLifecycleTone\(task\)\}>\{getTaskLifecycleLabel\(task\)\}<\/StatusChip>/)
})

test("automation task edit can clear the selected expert", () => {
  const viewSource = readFileSync(new URL("../src/features/automation/AutomationTasksView.tsx", import.meta.url), "utf8")

  assert.match(viewSource, /const expertId = form\.expertId \|\| \(form\.id \? null : undefined\)/)
  assert.match(viewSource, /const expertName = form\.expertId \? selectedExpert\?\.label : \(form\.id \? null : undefined\)/)
})

test("automation run replies use shared markdown rendering", () => {
  const viewSource = readFileSync(new URL("../src/features/automation/AutomationTasksView.tsx", import.meta.url), "utf8")

  assert.match(viewSource, /renderChatMessageContent/)
  assert.match(viewSource, /markdown-body automation-run-markdown/)
  assert.match(viewSource, /className="automation-run-meta"/)
  assert.match(viewSource, /run\.replyText/)
  assert.doesNotMatch(viewSource, /\{run\.replyText\}<\/p>/)
  assert.match(automationStyles, /\.automation-run-list\s*\{[^}]*overflow:\s*auto/is)
  assert.doesNotMatch(automationStyles, /\.automation-run-item\s*>\s*div\s*\{/)
  assert.match(automationStyles, /\.automation-detail-pane\s*\{[^}]*grid-template-rows:\s*auto auto minmax\(0,\s*1fr\)/is)
  assert.match(automationStyles, /\.automation-runs\s*\{[^}]*grid-template-rows:\s*auto minmax\(0,\s*1fr\)/is)
  assert.match(automationStyles, /\.automation-page\s*\{[^}]*display:\s*grid[^}]*grid-template-rows:\s*auto minmax\(0,\s*1fr\)/is)
  assert.match(automationStyles, /\.automation-run-meta\s*\{[^}]*display:\s*flex/is)
})

test("run-now action uses WeChat green accent", () => {
  assert.match(automationStyles, /\.automation-button-accent\s*\{[^}]*#07C160/is)
})
