const { test } = require('node:test');
const assert = require('node:assert/strict');
const path = require('node:path');
const { pathToFileURL } = require('node:url');

async function loadWebdavController() {
  return import(pathToFileURL(path.join(__dirname, '..', 'public/js/learningHub/webdavController.js')).href);
}

test('Learning Hub WebDAV helpers preserve icons and byte labels', async () => {
  const { formatBytes, getFileIcon } = await loadWebdavController();

  assert.equal(getFileIcon('application/pdf', 'scan.bin'), 'fa-file-pdf');
  assert.equal(getFileIcon('', 'note.md'), 'fa-file-lines');
  assert.equal(getFileIcon('', 'page.html'), 'fa-file-code');
  assert.equal(getFileIcon('', 'letter.docx'), 'fa-file-word');
  assert.equal(getFileIcon('', 'archive.zip'), 'fa-file');
  assert.equal(formatBytes(512), '512 B');
  assert.equal(formatBytes(2048), '2 KB');
  assert.equal(formatBytes(1572864), '1.5 MB');
});

test('Learning Hub WebDAV list rendering escapes paths and names', async () => {
  const { renderWebdavListHtml } = await loadWebdavController();
  const html = renderWebdavListHtml({
    path: '/Learning/Sub',
    parentPath: '/Learning?<bad>',
    items: [
      { isDir: true, path: '/Learning/<script>', name: 'Peds <Cases>', contentType: '', size: 0 },
      { isDir: false, path: '/Learning/a.pdf', name: 'A&B.pdf', contentType: 'application/pdf', size: 2048 }
    ]
  });

  assert.match(html, /lh-webdav-dir/);
  assert.match(html, /data-path="\/Learning\?&lt;bad&gt;"/);
  assert.match(html, /Peds &lt;Cases&gt;/);
  assert.match(html, /A&amp;B\.pdf/);
  assert.match(html, /\(2 KB\)/);
  assert.doesNotMatch(html, /<script>/);
});

test('Learning Hub WebDAV controller tracks current and selected paths', async () => {
  const previousDocument = global.document;
  const elements = {
    'lh-ai-webdav-browser': { style: {} },
    'lh-ai-webdav-selected': { style: {} },
    'lh-ai-webdav-selected-name': { textContent: '' }
  };
  global.document = { getElementById: function(id) { return elements[id] || null; } };

  try {
    const { createWebdavController } = await loadWebdavController();
    const webdav = createWebdavController({});
    webdav.setCurrentPath('/Learning');
    webdav.selectFile('/Learning/case.pdf', 'case.pdf');

    assert.equal(webdav.getCurrentPath(), '/Learning');
    assert.equal(webdav.getSelectedPath(), '/Learning/case.pdf');
    assert.equal(webdav.getSelectedName(), 'case.pdf');
    assert.equal(elements['lh-ai-webdav-browser'].style.display, 'none');
    assert.equal(elements['lh-ai-webdav-selected'].style.display, 'flex');
    assert.equal(elements['lh-ai-webdav-selected-name'].textContent, 'case.pdf');

    webdav.deselectFile();
    assert.equal(webdav.getSelectedPath(), '');
    assert.equal(webdav.getSelectedName(), '');
    assert.equal(elements['lh-ai-webdav-selected'].style.display, 'none');
    assert.equal(elements['lh-ai-webdav-browser'].style.display, '');
  } finally {
    global.document = previousDocument;
  }
});
