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('notifications swappability', () => {
  it('only infrastructure touches the email provider implementation', () => {
    const dir = new URL('..', import.meta.url);
    for (const file of sourceFiles(dir.pathname)) {
      if (!file.endsWith('.ts') || file.endsWith('boundary.test.ts')) continue;
      if (file.includes('/infrastructure/') || file.includes('/infrastructure')) continue;
      if (file.endsWith('/index.ts')) continue;
      const source = readFileSync(file, 'utf8');
      expect(source).not.toMatch(/LoggingEmailProvider|emailProvider\.send/);
    }
  });
});
