<commit_before>import Mocha from 'mocha';
import tmp from 'tmp';
import { join } from 'path';
import FileCacheConfig from '../../services/file-cache-config';
import { contextFor, registry, setupHub } from './server';
import { CardService, INSECURE_CONTEXT } from '../../services/card-service';
import FileCache from '../../services/file-cache';
import { TEST_REALM } from '@cardstack/core/tests/helpers';
import { RealmConfig } from '@cardstack/core/src/interfaces';
import { BASE_REALM_CONFIG, DEMO_REALM_CONFIG } from '../../services/realms-config';

tmp.setGracefulCleanup();

export class TestFileCacheConfig extends FileCacheConfig {
  tmp = tmp.dirSync();

  get root() {
    return this.tmp.name;
  }

  get cacheDirectory() {
    return join(this.root, 'node_modules', this.packageName);
  }

  cleanup() {
    this.tmp.removeCallback();
  }
}

export function resolveCard(root: string, modulePath: string): string {
  return __non_webpack_require__.resolve(modulePath, { paths: [root] });
}

export function configureHubWithCompiler(mochaContext: Mocha.Suite) {
  let { realmURL, getRealmDir } = configureCompiler(mochaContext);
  let { request, getContainer } = setupHub(mochaContext);
  let { cards, resolveCard, getFileCache } = cardHelpers(mochaContext);
  return {
    realmURL,
    getRealmDir,
    request,
    getContainer,
    cards,
    resolveCard,
    getFileCache,
  };
}

/**
 *  Override default config objects on the container. Must be run before container is setup.
 */
export function configureCompiler(mochaContext: Mocha.Suite) {
  let testRealmDir: string;

  mochaContext.beforeEach(async function () {
    let reg = registry(this);

    if (process.env.COMPILER) {
      reg.register('file-cache-config', TestFileCacheConfig, { type: 'service' });

      testRealmDir = tmp.dirSync().name;
      reg.register(
        'realmsConfig',
        class TestRealmsConfig {
          realms: RealmConfig[] = [BASE_REALM_CONFIG, DEMO_REALM_CONFIG, { url: TEST_REALM, directory: testRealmDir }];
        },
        { type: 'service' }<commit_msg>Remove DEMO_REALM from tests<commit_after>import Mocha from 'mocha';
import tmp from 'tmp';
import { join } from 'path';
import FileCacheConfig from '../../services/file-cache-config';
import { contextFor, registry, setupHub } from './server';
import { CardService, INSECURE_CONTEXT } from '../../services/card-service';
import FileCache from '../../services/file-cache';
import { TEST_REALM } from '@cardstack/core/tests/helpers';
import { RealmConfig } from '@cardstack/core/src/interfaces';
import { BASE_REALM_CONFIG } from '../../services/realms-config';

tmp.setGracefulCleanup();

export class TestFileCacheConfig extends FileCacheConfig {
  tmp = tmp.dirSync();

  get root() {
    return this.tmp.name;
  }

  get cacheDirectory() {
    return join(this.root, 'node_modules', this.packageName);
  }

  cleanup() {
    this.tmp.removeCallback();
  }
}

export function resolveCard(root: string, modulePath: string): string {
  return __non_webpack_require__.resolve(modulePath, { paths: [root] });
}

export function configureHubWithCompiler(mochaContext: Mocha.Suite) {
  let { realmURL, getRealmDir } = configureCompiler(mochaContext);
  let { request, getContainer } = setupHub(mochaContext);
  let { cards, resolveCard, getFileCache } = cardHelpers(mochaContext);
  return {
    realmURL,
    getRealmDir,
    request,
    getContainer,
    cards,
    resolveCard,
    getFileCache,
  };
}

/**
 *  Override default config objects on the container. Must be run before container is setup.
 */
export function configureCompiler(mochaContext: Mocha.Suite) {
  let testRealmDir: string;

  mochaContext.beforeEach(async function () {
    let reg = registry(this);

    if (process.env.COMPILER) {
      reg.register('file-cache-config', TestFileCacheConfig, { type: 'service' });

      testRealmDir = tmp.dirSync().name;
      reg.register(
        'realmsConfig',
        class TestRealmsConfig {
          realms: RealmConfig[] = [BASE_REALM_CONFIG, { url: TEST_REALM, directory: testRealmDir }];
        },
        { type: 'service' }
{'fraction_non_alphanumeric': 0.12831105710814095, 'fraction_numerical': 0.0, 'mean_word_length': 3.888361045130641, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3092903', 'n_tokens_mistral': 1277, 'n_tokens_neox': 1260, 'n_words': 297}