import test from 'node:test'
import assert from 'node:assert/strict'
import { existsSync, readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, resolve } from 'node:path'

const currentDir = dirname(fileURLToPath(import.meta.url))
const srcDir = resolve(currentDir, '..')

function readSource(relativePath) {
  return readFileSync(resolve(srcDir, relativePath), 'utf8')
}

test('standalone module diagram editor page exists and embeds the shared component', () => {
  assert.equal(existsSync(resolve(srcDir, 'views/ModuleDiagramEditorView.vue')), true)
  const source = readSource('views/ModuleDiagramEditorView.vue')

  assert.equal(source.includes("import AppModuleDiagramView from '@/components/AppModuleDiagramView.vue'"), true)
  assert.equal(source.includes('module-editor-layout'), true)
  assert.equal(source.includes('module-editor-side'), true)
  assert.equal(source.includes('module-editor-canvas'), true)
  assert.equal(source.includes('v-model:source-mode="sourceMode"'), true)
  assert.equal(source.includes('v-model:outline-text="outlineText"'), true)
  assert.equal(source.includes('v-model:layout-options="layoutOptions"'), true)
  assert.equal(source.includes(':editable="false"'), true)
})

test('standalone module diagram editor supports optional project loading and saving', () => {
  const source = readSource('views/ModuleDiagramEditorView.vue')

  assert.equal(source.includes('const projectId = computed'), true)
  assert.equal(source.includes('getProject(projectId.value)'), true)
  assert.equal(source.includes('listProjectDiagrams(projectId.value)'), true)
  assert.equal(source.includes('saveProjectDiagram(projectId.value'), true)
  assert.equal(source.includes('function editorDiagramPayload'), true)
  assert.equal(source.includes('sourceMode: sourceMode.value'), true)
  assert.equal(source.includes('outlineText: outlineText.value'), true)
  assert.equal(source.includes('layoutOptions: layoutOptions.value'), true)
})

test('router exposes project and local module diagram editor routes', () => {
  const source = readSource('router/index.js')

  assert.equal(source.includes("import ModuleDiagramEditorView from '@/views/ModuleDiagramEditorView.vue'"), true)
  assert.equal(source.includes("path: '/module-diagram'"), true)
  assert.equal(source.includes("path: '/project/:projectId/module-diagram'"), true)
  assert.equal(source.includes("name: 'module-diagram-editor'"), true)
})
