import assert from 'node:assert/strict';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';
import { canPreviewAsset, renderAssetPreviewHtml, renderImageAssetViewerHtml, wantsInlineImageViewer } from './mindspace-asset-preview.mjs';

test('canPreviewAsset covers common workspace file types', () => {
  assert.equal(canPreviewAsset('text/html'), true);
  assert.equal(canPreviewAsset('application/pdf'), true);
  assert.equal(
    canPreviewAsset('application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
    true,
  );
  assert.equal(canPreviewAsset('application/vnd.ms-excel'), false);
});

test('renderAssetPreviewHtml renders csv as table', () => {
  const html = renderAssetPreviewHtml({
    asset: {
      displayName: '导出',
      filename: 'export.csv',
      mimeType: 'text/csv',
    },
    buffer: Buffer.from('name,value\nfoo,1\nbar,2\n'),
    downloadUrl: '/api/mindspace/v1/assets/a/download?inline=1',
  });
  assert.match(html, /<table>/);
  assert.match(html, /foo/);
  assert.match(html, /下载原文件/);
});

test('renderAssetPreviewHtml renders image lightbox controls', () => {
  const html = renderAssetPreviewHtml({
    asset: {
      displayName: '封面',
      filename: 'cover.jpg',
      mimeType: 'image/jpeg',
    },
    buffer: Buffer.from('fake'),
    downloadUrl: '/api/mindspace/v1/assets/a/download?inline=1',
  });
  assert.match(html, /data-image-lightbox-trigger/);
  assert.match(html, /image-lightbox-close/);
  assert.match(html, /event\.key === 'Escape'/);
  assert.match(html, /script-src 'unsafe-inline'/);
});

test('renderImageAssetViewerHtml uses minimal image viewer shell', () => {
  const html = renderImageAssetViewerHtml({
    asset: {
      displayName: '封面',
      filename: 'cover.jpg',
      mimeType: 'image/jpeg',
    },
    downloadUrl: '/api/mindspace/v1/assets/a/download?inline=1',
  });
  assert.match(html, /class="image-viewer"/);
  assert.match(html, /aria-label="关闭"/);
});

test('wantsInlineImageViewer distinguishes embed vs page navigation', () => {
  assert.equal(
    wantsInlineImageViewer({ get: (name) => (name === 'sec-fetch-dest' ? 'image' : ''), query: {} }),
    false,
  );
  assert.equal(
    wantsInlineImageViewer({ get: (name) => (name === 'sec-fetch-dest' ? 'document' : ''), query: {} }),
    true,
  );
  assert.equal(
    wantsInlineImageViewer({ get: () => '', query: { viewer: '1' } }),
    true,
  );
});

test('renderAssetPreviewHtml extracts docx text', async () => {
  const docPath = path.join(
    process.cwd(),
    'MindSpace/john/oa/端午感怀.docx',
  );
  let buffer;
  try {
    buffer = await fs.readFile(docPath);
  } catch {
    return;
  }
  const html = renderAssetPreviewHtml({
    asset: {
      displayName: '端午感怀',
      filename: '端午感怀.docx',
      mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    },
    buffer,
    downloadUrl: '/api/mindspace/v1/assets/a/download?inline=1',
  });
  assert.match(html, /端午/);
  assert.match(html, /docx-preview/);
});
