import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import test from 'node:test'
import { fileURLToPath } from 'node:url'

const sidebar = readFileSync(
  fileURLToPath(new URL('../src/components/layout/SidebarRail.vue', import.meta.url)),
  'utf8'
)

const documentInbox = readFileSync(
  fileURLToPath(new URL('../src/composables/useDocumentCenterInbox.js', import.meta.url)),
  'utf8'
)

const documentNewState = readFileSync(
  fileURLToPath(new URL('../src/utils/documentCenterNewState.js', import.meta.url)),
  'utf8'
)

test('sidebar renders a red dot for unread document center rows', () => {
  assert.match(sidebar, /useDocumentCenterInbox/)
  assert.match(sidebar, /hasUnread: documentInboxHasUnread/)
  assert.match(sidebar, /<span v-if="item\.hasNewMessage" class="nav-unread-dot" aria-hidden="true"><\/span>/)
  assert.match(sidebar, /hasNewMessage: item\.id === 'documents' \? documentInboxHasUnread\.value : false/)
  assert.match(sidebar, /void refreshDocumentInbox\(\)/)
  assert.match(sidebar, /startDocumentInboxPolling\(\)/)
  assert.match(sidebar, /stopDocumentInboxPolling\(\)/)
  assert.match(sidebar, /\.nav-unread-dot\s*\{[\s\S]*background:\s*#ef4444;/)
  assert.match(sidebar, /\.rail-collapsed \.nav-unread-dot\s*\{[\s\S]*position:\s*absolute;/)
})

test('document inbox reuses document center viewed-key state', () => {
  assert.match(documentInbox, /DOCUMENT_VIEWED_KEYS_CHANGE_EVENT/)
  assert.match(documentInbox, /readViewedDocumentKeys/)
  assert.match(documentInbox, /countNewDocuments\(documentRows\.value, viewedDocumentKeys\.value\)/)
  assert.match(documentInbox, /fetchExpenseClaims/)
  assert.match(documentInbox, /fetchApprovalExpenseClaims/)
  assert.match(documentInbox, /fetchArchivedExpenseClaims/)
  assert.match(documentInbox, /window\.addEventListener\(DOCUMENT_VIEWED_KEYS_CHANGE_EVENT, refreshViewedDocumentKeys\)/)
  assert.match(documentNewState, /export const DOCUMENT_VIEWED_KEYS_CHANGE_EVENT/)
  assert.match(documentNewState, /window\.dispatchEvent\(new CustomEvent\(DOCUMENT_VIEWED_KEYS_CHANGE_EVENT\)\)/)
})
