import { readdirSync, readFileSync, statSync } from 'node:fs';
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';

function sourceFiles(dir: string): string[] {
  return readdirSync(dir).flatMap((entry) => {
    const path = join(dir, entry);
    return statSync(path).isDirectory() ? sourceFiles(path) : path.endsWith('.ts') ? [path] : [];
  });
}

describe('orders persistence boundary', () => {
  it('migration contains no external table references', () => {
    const migration = readFileSync(
      new URL('../../../../migrations/016_orders.js', import.meta.url),
      'utf8',
    );
    expect(migration).not.toMatch(
      /catalog_|inventory_|pricing_|cart_|shipping_|promotions_|users_/,
    );
  });

  it('orders module does not import catalog/inventory/pricing/cart/shipping/promotions internals', () => {
    const dir = new URL('..', import.meta.url);
    for (const file of sourceFiles(dir.pathname)) {
      const source = readFileSync(file, 'utf8');
      expect(source).not.toMatch(
        /modules\/(catalog|inventory|pricing|cart|shipping|promotions)\/(api|application|domain|infrastructure|tests)/,
      );
    }
  });
});
