// jest setup: RTL jest-dom + mock DB reset per test.
import '@testing-library/jest-dom';
import { resetMockDb } from './__mocks__/firebase/_mock-db';

// RTL v14: tell React this is an act environment. Without it every
// async update warns "current testing environment is not configured to
// support act(...)". RTL reads getGlobalThis(), so set on globalThis.
// Must be set before any component renders.
globalThis.IS_REACT_ACT_ENVIRONMENT = true;

// Test timeout guard. CRA blocks `testTimeout` in package.json jest config
// (not in allowlist). Set via jest global here instead. Fails fast on hang.
jest.setTimeout(10000);

// WARNING = FAILURE. Fail test on any console.error/warn (React act warnings,
// deprecations, prop errors). App code's own error logs allowed only inside
// try/catch failure paths — those tests should assert the error was handled,
// not silently log it. Override the spies to throw.
const originalError = console.error;
const originalWarn = console.warn;
console.error = (...args) => {
  originalError(...args);
  throw new Error(`console.error in test: ${args.map(String).join(' ').slice(0, 500)}`);
};
console.warn = (...args) => {
  originalWarn(...args);
  throw new Error(`console.warn in test: ${args.map(String).join(' ').slice(0, 500)}`);
};

// polyfill crypto.randomUUID for jsdom (used by generateId in App.js).
if (!global.crypto) global.crypto = {};
if (!global.crypto.randomUUID) {
  global.crypto.randomUUID = () => 'test-uuid-' + Math.random().toString(36).slice(2, 10);
}

// Stub Firebase env vars so initializeFirebase() succeeds under test.
// Real SDK calls are mocked via __mocks__/firebase/*.
process.env.REACT_APP_FIREBASE_API_KEY = 'test-api-key';
process.env.REACT_APP_FIREBASE_AUTH_DOMAIN = 'test.firebaseapp.com';
process.env.REACT_APP_FIREBASE_PROJECT_ID = 'test-project';
process.env.REACT_APP_FIREBASE_STORAGE_BUCKET = 'test.appspot.com';
process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID = '1234567890';
process.env.REACT_APP_FIREBASE_APP_ID = '1:1234567890:web:abcdef';

beforeEach(() => {
  resetMockDb();
});
