import { describe, it } from 'node:test';
import { strict as assert } from 'node:assert';
import { readFileSync, existsSync } from 'node:fs';
import { resolve } from 'node:path';

const postsDir = resolve('src/content/posts');

const post1Path = resolve(postsDir, 'localize-react-without-bloating-the-bundle.md');
const post2Path = resolve(postsDir, 'speeding-up-webpack-typescript-incremental-builds-by-7x.md');

const originalPost1Path = resolve('content/posts/localize-react-without-bloating-the-bundle.md');
const originalPost2Path = resolve('content/posts/speeding-up-webpack-typescript-incremental-builds-by-7x.mdx');

function extractFrontmatter(content) {
  const match = content.match(/^---\n([\s\S]*?)\n---/);
  return match ? match[1] : null;
}

function extractBody(content) {
  const match = content.match(/^---\n[\s\S]*?\n---\n([\s\S]*)$/);
  return match ? match[1] : null;
}

describe('Migrated post: localize-react-without-bloating-the-bundle', () => {
  it('should exist at src/content/posts/localize-react-without-bloating-the-bundle.md', () => {
    assert.ok(existsSync(post1Path), 'Post file should exist');
  });

  describe('Frontmatter', () => {
    it('should have title field', () => {
      const content = readFileSync(post1Path, 'utf-8');
      const fm = extractFrontmatter(content);
      assert.match(fm, /title:\s*['"]?Localize React without Bloating the Bundle['"]?/, 'should have correct title');
    });

    it('should have date as 2019-08-16 (YYYY-MM-DD)', () => {
      const content = readFileSync(post1Path, 'utf-8');
      const fm = extractFrontmatter(content);
      assert.match(fm, /date:\s*2019-08-16\s*$/m, 'should have simplified date');
    });

    it('should have type: post', () => {
      const content = readFileSync(post1Path, 'utf-8');
      const fm = extractFrontmatter(content);
      assert.match(fm, /type:\s*post/, 'should have type: post');
    });

    it('should have correct tags', () => {
      const content = readFileSync(post1Path, 'utf-8');
      const fm = extractFrontmatter(content);
      assert.match(fm, /tags:/, 'should have tags field');
      assert.match(fm, /react/, 'should include react tag');
      assert.match(fm, /localization/, 'should include localization tag');
      assert.match(fm, /webpack/, 'should include webpack tag');
      assert.match(fm, /performance/, 'should include performance tag');
    });

    it('should have summary field', () => {
      const content = readFileSync(post1Path, 'utf-8');
      const fm = extractFrontmatter(content);
      assert.match(fm, /summary:/, 'should have summary field');
    });

    it('should NOT have path field', () => {
      const content = readFileSync(post1Path, 'utf-8');
      const fm = extractFrontmatter(content);
      assert.doesNotMatch(fm, /^path:/m, 'should not have path field');
    });

    it('should NOT have heroImage field', () => {
      const content = readFileSync(post1Path, 'utf-8');
      const fm = extractFrontmatter(content);
      assert.doesNotMatch(fm, /heroImage:/, 'should not have heroImage field');
    });
  });

  describe('Body content', () => {
    it('should have body matching original post verbatim', () => {
      const newContent = readFileSync(post1Path, 'utf-8');
      const originalContent = readFileSync(originalPost1Path, 'utf-8');
      const newBody = extractBody(newContent);
      const originalBody = extractBody(originalContent);
      assert.equal(newBody, originalBody, 'Body content should match original exactly');
    });
  });
});

describe('Migrated post: speeding-up-webpack-typescript-incremental-builds-by-7x', () => {
  it('should exist at src/content/posts/speeding-up-webpack-typescript-incremental-builds-by-7x.md', () => {
    assert.ok(existsSync(post2Path), 'Post file should exist');
  });

  it('should be a .md file (not .mdx)', () => {
    assert.ok(post2Path.endsWith('.md'), 'should be .md extension');
    assert.ok(!post2Path.endsWith('.mdx'), 'should not be .mdx extension');
  });

  describe('Frontmatter', () => {
    it('should have title field', () => {
      const content = readFileSync(post2Path, 'utf-8');
      const fm = extractFrontmatter(content);
      assert.match(fm, /title:\s*['"]?Speeding Up Webpack Typescript Incremental Builds by 7x['"]?/, 'should have correct title');
    });

    it('should have date as 2019-08-16 (YYYY-MM-DD)', () => {
      const content = readFileSync(post2Path, 'utf-8');
      const fm = extractFrontmatter(content);
      assert.match(fm, /date:\s*2019-08-16\s*$/m, 'should have simplified date');
    });

    it('should have type: post', () => {
      const content = readFileSync(post2Path, 'utf-8');
      const fm = extractFrontmatter(content);
      assert.match(fm, /type:\s*post/, 'should have type: post');
    });

    it('should have correct tags', () => {
      const content = readFileSync(post2Path, 'utf-8');
      const fm = extractFrontmatter(content);
      assert.match(fm, /tags:/, 'should have tags field');
      assert.match(fm, /webpack/, 'should include webpack tag');
      assert.match(fm, /typescript/, 'should include typescript tag');
      assert.match(fm, /performance/, 'should include performance tag');
      assert.match(fm, /microsoft/, 'should include microsoft tag');
    });

    it('should have summary field', () => {
      const content = readFileSync(post2Path, 'utf-8');
      const fm = extractFrontmatter(content);
      assert.match(fm, /summary:/, 'should have summary field');
    });

    it('should NOT have path field', () => {
      const content = readFileSync(post2Path, 'utf-8');
      const fm = extractFrontmatter(content);
      assert.doesNotMatch(fm, /^path:/m, 'should not have path field');
    });

    it('should NOT have heroImage field', () => {
      const content = readFileSync(post2Path, 'utf-8');
      const fm = extractFrontmatter(content);
      assert.doesNotMatch(fm, /heroImage:/, 'should not have heroImage field');
    });
  });

  describe('Body content', () => {
    it('should have body matching original post verbatim', () => {
      const newContent = readFileSync(post2Path, 'utf-8');
      const originalContent = readFileSync(originalPost2Path, 'utf-8');
      const newBody = extractBody(newContent);
      const originalBody = extractBody(originalContent);
      assert.equal(newBody, originalBody, 'Body content should match original exactly');
    });
  });
});