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

const conversationWorkspaceSource = readFileSync(new URL("../src/features/chat/ConversationWorkspaceView.tsx", import.meta.url), "utf8")
const expertsViewSource = readFileSync(new URL("../src/features/experts/ExpertsView.tsx", import.meta.url), "utf8")
const chatStylesSource = readFileSync(new URL("../src/styles/chat.css", import.meta.url), "utf8")

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

test("branded expert workspace heading renders as icon followed by plain text", () => {
  assert.match(conversationWorkspaceSource, /expertWorkspaceLogo \? \(\s*<div className="expert-hero-heading">[\s\S]*?\{expertWorkspaceLogo\}[\s\S]*?<strong className="expert-hero-title">\{activeExpertName\}<\/strong>[\s\S]*?<\/div>\s*\) : \(/)
  assert.match(expertsViewSource, /expertWorkspaceLogo \? \(\s*<div className="expert-hero-heading">[\s\S]*?\{expertWorkspaceLogo\}[\s\S]*?<strong className="expert-hero-title">\{activeExpertName\}<\/strong>[\s\S]*?<\/div>\s*\) : \(/)
  assert.doesNotMatch(conversationWorkspaceSource.match(/expertWorkspaceLogo \? \([\s\S]*?\) : \(/)?.[0] ?? "", /conversation-panel-kicker/)
  assert.doesNotMatch(expertsViewSource.match(/expertWorkspaceLogo \? \([\s\S]*?\) : \(/)?.[0] ?? "", /conversation-panel-kicker/)
  assert.match(cssBlock(chatStylesSource, ".conversation-shell .expert-hero-heading"), /display:\s*inline-flex;[\s\S]*?gap:\s*1em;/)
})
