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('payments domain boundary', () => {
  it('domain code does not import Stripe SDK', () => {
    const root = new URL('..', import.meta.url);
    const domainDir = new URL('../domain', import.meta.url);
    const applicationDir = new URL('../application', import.meta.url);
    for (const file of [
      ...sourceFiles(domainDir.pathname),
      ...sourceFiles(applicationDir.pathname),
    ]) {
      const source = readFileSync(file, 'utf8');
      expect(source).not.toMatch(/stripe/i);
    }
    // Also verify the API does not import Stripe SDK directly.
    const apiDir = new URL('../api', import.meta.url);
    for (const file of sourceFiles(apiDir.pathname)) {
      const source = readFileSync(file, 'utf8');
      expect(source).not.toMatch(/from\s+['"]stripe/);
    }
    void root;
  });
});
