import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'

async function globalTeardown() {
  const tempDir = path.join(os.tmpdir(), 'nex-e2e')
  if (fs.existsSync(tempDir)) {
    await new Promise((resolve) => setTimeout(resolve, 500))
    try {
      fs.rmSync(tempDir, { recursive: true, force: true })
      console.log(`Cleaned up E2E temp dir: ${tempDir}`)
    } catch (e) {
      console.error(`Failed to clean up temp dir ${tempDir}:`, e)
    }
  }
}

export default globalTeardown
