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

const currentDir = dirname(fileURLToPath(import.meta.url))

function readView(name) {
  return readFileSync(resolve(currentDir, name), 'utf8')
}

test('diagram center binds module diagram source and layout state', () => {
  const source = readView('DiagramCenterView.vue')

  assert.equal(source.includes('const moduleDiagramSourceMode = ref'), true)
  assert.equal(source.includes('const moduleDiagramOutlineText = ref'), true)
  assert.equal(source.includes('const moduleDiagramLayoutOptions = ref'), true)
  assert.equal(source.includes('v-model:source-mode="moduleDiagramSourceMode"'), true)
  assert.equal(source.includes('v-model:outline-text="moduleDiagramOutlineText"'), true)
  assert.equal(source.includes('v-model:layout-options="moduleDiagramLayoutOptions"'), true)
  assert.equal(source.includes(':editable="true"'), true)
})

test('diagram center saves module diagram source, outline, layout, and graph data', () => {
  const source = readView('DiagramCenterView.vue')

  assert.equal(source.includes('function moduleDiagramPayload'), true)
  assert.equal(source.includes('sourceMode: moduleDiagramSourceMode.value'), true)
  assert.equal(source.includes('outlineText,'), true)
  assert.equal(source.includes('layoutOptions: moduleDiagramLayoutOptions.value'), true)
  assert.equal(source.includes('graph: moduleDiagramGraph.value'), true)
  assert.equal(source.includes('diagramJson: JSON.stringify(moduleDiagramPayload())'), true)
})

test('diagram center restores saved module diagram payload', () => {
  const source = readView('DiagramCenterView.vue')

  assert.equal(source.includes('function applySavedModuleDiagram'), true)
  assert.equal(source.includes("savedDiagrams.value.find((item) => item.diagramType === 'module')"), true)
  assert.equal(source.includes('moduleDiagramSourceMode.value = payload.sourceMode'), true)
  assert.equal(source.includes('moduleDiagramOutlineText.value = payload.outlineText'), true)
  assert.equal(source.includes('moduleDiagramLayoutOptions.value ='), true)
})
