/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import * as path from 'node:path';
import type {JestEnvironment} from '@jest/environment';
import {getCallsite} from '@jest/source-map';
import {
  type AssertionResult,
  type Status,
  type TestResult,
  makeCollectedTestResult,
} from '@jest/test-result';
import type {Config, Global} from '@jest/types';
import type Runtime from 'jest-runtime';
import type {SnapshotState} from 'jest-snapshot';
import {ErrorWithStack} from 'jest-util';
import installEach from './each';
import {installErrorOnPrivate} from './errorOnPrivate';
import type Spec from './jasmine/Spec';
import jasmineAsyncInstall from './jasmineAsyncInstall';
import JasmineReporter from './reporter';

export type {Jasmine} from './types';

const JASMINE = require.resolve('./jasmine/jasmineLight');

const jestEachBuildDir = path.dirname(require.resolve('jest-each'));

export type SuiteLike = {
  children: Array<SuiteLike | SpecLike>;
  description: string;
  id?: string;
  markedPending?: boolean;
};

export type SpecLike = {
  description: string;
  getFullName: () => string;
  id?: string;
  markedPending?: boolean;
  markedTodo?: boolean;
};

type CollectContext = {
  ancestors: Array<string>;
  focusedRunnableIds: Array<string>;
  parentEnabled: boolean;
  parentPending: boolean;
  testNamePatternRE: RegExp | null;
};

export const collectSpecs = (
  suite: SuiteLike,
  context: CollectContext,
): Array<AssertionResult> => {
  const results: Array<AssertionResult> = [];
  const isEnabled = (node: SuiteLike | SpecLike): boolean =>
    context.parentEnabled ||
    (node.id != null && context.focusedRunnableIds.includes(node.id));

  for (const child of suite.children) {
    if ('children' in child) {
      results.push(
        ...collectSpecs(child, {
          ...context,
          ancestors: [...context.ancestors, child.description],
          parentEnabled: isEnabled(child),
          parentPending: context.parentPending || child.markedPending === true,
        }),
      );
    } else {
      const fullName = child.getFullName();
      const deselected =
        context.testNamePatternRE !== null &&
        !context.testNamePatternRE.test(fullName);

      // Mirror `Spec.status` without executing, in the same precedence order: a
      // spec not selected by focus (`fit`/`fdescribe`) or deselected by
      // `--testNamePattern` reports as `pending` (an actual run still counts
      // these); `markedTodo` reports as `todo`; a skipped (`markedPending`) spec
      // reports as `pending`; otherwise the spec would run and is reported in
      // the passed bucket and flagged as `wouldRun`.
      let status: Status;
      let wouldRun: true | undefined;
      if (!isEnabled(child) || deselected) {
        status = 'pending';
      } else if (child.markedTodo) {
        status = 'todo';
      } else if (context.parentPending || child.markedPending) {
        status = 'pending';
      } else {
        status = 'passed';
        wouldRun = true;
      }

      results.push({
        ancestorTitles: [...context.ancestors],
        duration: null,
        failing: false,
        failureDetails: [],
        failureMessages: [],
        fullName,
        invocations: 0,
        location: null,
        numPassingAsserts: 0,
        retryReasons: [],
        startAt: null,
        status,
        title: child.description,
        wouldRun,
      });
    }
  }
  return results;
};

export const buildCollectedTestResult = ({
  config,
  focusedRunnableIds = [],
  suite,
  testNamePattern,
  testPath,
}: {
  config: Config.ProjectConfig;
  focusedRunnableIds?: Array<string>;
  suite: SuiteLike;
  testNamePattern: string | undefined;
  testPath: string;
}): TestResult => {
  const testNamePatternRE = testNamePattern
    ? new RegExp(testNamePattern, 'i')
    : null;
  const assertionResults = collectSpecs(suite, {
    ancestors: [],
    focusedRunnableIds,
    // With no focus the whole tree runs; with focus only the focused subtrees
    // do, so the root suite is enabled only when nothing is focused.
    parentEnabled: focusedRunnableIds.length === 0,
    parentPending: suite.markedPending === true,
    testNamePatternRE,
  });
  return makeCollectedTestResult(assertionResults, {
    displayName: config.displayName,
    testFilePath: testPath,
  });
};

export default async function jasmine2(
  globalConfig: Config.GlobalConfig,
  config: Config.ProjectConfig,
  environment: JestEnvironment,
  runtime: Runtime,
  testPath: string,
): Promise<TestResult> {
  const reporter = new JasmineReporter(globalConfig, config, testPath);
  const jasmineFactory =
    runtime.requireInternalModule<typeof import('./jasmine/jasmineLight')>(
      JASMINE,
    );
  const jasmine = jasmineFactory.create({
    process,
    testPath,
    testTimeout: globalConfig.testTimeout,
  });

  const env = jasmine.getEnv();
  const jasmineInterface = jasmineFactory._interface(jasmine, env);
  Object.assign(environment.global, jasmineInterface);
  env.addReporter(jasmineInterface.jsApiReporter);

  // TODO: Remove config option if V8 exposes some way of getting location of caller
  // in a future version
  if (config.testLocationInResults === true) {
    function wrapIt<T extends Global.ItBase>(original: T): T {
      const wrapped = (
        testName: Global.TestName,
        fn: Global.TestFn,
        timeout?: number,
      ) => {
        const sourcemaps = runtime.getSourceMaps();
        let stack = getCallsite(1, sourcemaps);
        const it = original(testName, fn, timeout);

        if (stack.getFileName()?.startsWith(jestEachBuildDir)) {
          stack = getCallsite(2, sourcemaps);
        }
        // @ts-expect-error: `it` is `void` for some reason
        it.result.__callsite = stack;

        return it;
      };
      return wrapped as any as T;
    }

    environment.global.it = wrapIt(environment.global.it);
    environment.global.xit = wrapIt(environment.global.xit);
    environment.global.fit = wrapIt(environment.global.fit);
  }

  jasmineAsyncInstall(globalConfig, environment.global);

  installEach(environment);

  const failing = () => {
    throw new ErrorWithStack(
      'Jest: `failing` tests are only supported in `jest-circus`.',
      failing,
    );
  };

  failing.each = () => {
    throw new ErrorWithStack(
      'Jest: `failing` tests are only supported in `jest-circus`.',
      failing.each,
    );
  };

  environment.global.it.failing = failing;
  environment.global.fit.failing = failing;
  environment.global.xit.failing = failing;

  environment.global.test = environment.global.it;
  environment.global.it.only = environment.global.fit;
  environment.global.it.todo = env.todo;
  environment.global.it.skip = environment.global.xit;
  environment.global.xtest = environment.global.xit;
  environment.global.describe.skip = environment.global.xdescribe;
  environment.global.describe.only = environment.global.fdescribe;

  if (config.fakeTimers.enableGlobally) {
    if (config.fakeTimers.legacyFakeTimers) {
      environment.fakeTimers!.useFakeTimers();
    } else {
      environment.fakeTimersModern!.useFakeTimers();
    }
  }

  env.beforeEach(() => {
    if (config.resetModules) {
      runtime.resetModules();
    }

    if (config.clearMocks) {
      runtime.clearAllMocks();
    }

    if (config.resetMocks) {
      runtime.resetAllMocks();

      if (
        config.fakeTimers.enableGlobally &&
        config.fakeTimers.legacyFakeTimers
      ) {
        environment.fakeTimers!.useFakeTimers();
      }
    }

    if (config.restoreMocks) {
      runtime.restoreAllMocks();
    }
  });

  env.addReporter(reporter);

  runtime
    .requireInternalModule<typeof import('./jestExpect')>(
      require.resolve('./jestExpect.js'),
    )
    .default({expand: globalConfig.expand});

  if (globalConfig.errorOnDeprecated) {
    installErrorOnPrivate(environment.global);
  } else {
    Object.defineProperty(jasmine, 'DEFAULT_TIMEOUT_INTERVAL', {
      configurable: true,
      enumerable: true,
      get() {
        return this._DEFAULT_TIMEOUT_INTERVAL;
      },
      set(value) {
        this._DEFAULT_TIMEOUT_INTERVAL = value;
      },
    });
  }

  const snapshotState: SnapshotState = await runtime
    .requireInternalModule<typeof import('./setup_jest_globals')>(
      require.resolve('./setup_jest_globals.js'),
    )
    .default({
      config,
      globalConfig,
      localRequire: runtime.requireModule.bind(runtime),
      testPath,
    });

  for (const path of config.setupFilesAfterEnv) {
    const esm = runtime.unstable_shouldLoadAsEsm(path);

    if (esm) {
      await runtime.unstable_importModule(path);
    } else {
      const setupFile = runtime.requireModule(path);
      if (typeof setupFile === 'function') {
        await setupFile();
      }
    }
  }

  if (globalConfig.testNamePattern) {
    const testNameRegex = new RegExp(globalConfig.testNamePattern, 'i');
    env.specFilter = (spec: Spec) => testNameRegex.test(spec.getFullName());
  }
  const esm = runtime.unstable_shouldLoadAsEsm(testPath);

  if (esm) {
    await runtime.unstable_importModule(testPath);
  } else {
    runtime.requireModule(testPath);
  }

  if (globalConfig.collectTests) {
    return buildCollectedTestResult({
      config,
      focusedRunnableIds: env.focusedRunnableIds(),
      suite: env.topSuite() as unknown as SuiteLike,
      testNamePattern: globalConfig.testNamePattern,
      testPath,
    });
  }

  await env.execute();

  const results = await reporter.getResults();

  return addSnapshotData(results, snapshotState);
}

const addSnapshotData = (results: TestResult, snapshotState: SnapshotState) => {
  for (const {fullName, status} of results.testResults) {
    if (status === 'pending' || status === 'failed') {
      // if test is skipped or failed, we don't want to mark
      // its snapshots as obsolete.
      snapshotState.markSnapshotsAsCheckedForTest(fullName);
    }
  }

  const uncheckedCount = snapshotState.getUncheckedCount();
  const uncheckedKeys = snapshotState.getUncheckedKeys();

  if (uncheckedCount) {
    snapshotState.removeUncheckedKeys();
  }

  const status = snapshotState.save();
  results.snapshot.fileDeleted = status.deleted;
  results.snapshot.added = snapshotState.added;
  results.snapshot.matched = snapshotState.matched;
  results.snapshot.unmatched = snapshotState.unmatched;
  results.snapshot.updated = snapshotState.updated;
  results.snapshot.unchecked = status.deleted ? 0 : uncheckedCount;
  // Copy the array to prevent memory leaks
  results.snapshot.uncheckedKeys = [...uncheckedKeys];

  return results;
};
