<commit_before>const Writers = require('@cardstack/server/writers');
const Writer = require('@cardstack/git/writer');
const { inRepo } = require('./support');
const {
  createDefaultEnvironment,
  destroyDefaultEnvironment
} = require('@cardstack/server/node-tests/support');
const JSONAPIFactory = require('@cardstack/test-support/jsonapi-factory');
const { grantAllPermissions } = require('@cardstack/test-support/permissions');

describe('git/writer', function() {

  let env, writers, user;

  beforeEach(async function() {
    let factory = new JSONAPIFactory();

    grantAllPermissions(factory);

    factory.addResource('content-types', 'articles')
      .withRelated('fields', [
        factory.addResource('fields', 'title').withAttributes({ fieldType: '@cardstack/core-types::string' }),
        factory.addResource('fields', 'primary-image').withAttributes({ fieldType: '@cardstack/core-types::belongs-to' })
      ]);

    factory.addResource('content-types', 'people')
      .withRelated('fields', [
        factory.addResource('fields', 'first-name').withAttributes({ fieldType: '@cardstack/core-types::string' }),
        factory.addResource('fields', 'last-name').withAttributes({ fieldType: '@cardstack/core-types::string' }),
        factory.addResource('fields', 'age').withAttributes({ fieldType: '@cardstack/core-types::integer' })
      ]);

    factory.addResource('articles', 1)
      .withAttributes({
          title: 'First Article'
      });

    factory.addResource('people', 1)
      .withAttributes({
        firstName: 'Quint',
        lastName: 'Faulkner',
        age: 6
      });

    factory.addResource('people', 2)
      .withAttributes({
        firstName: 'Arthur',
        lastName: 'Faulkner',
        age: 1
      });

    factory.addResource('content-types', 'things-with-defaults')
      .withRelated('fields', [
        factory.addResource('fields', 'coolness')
          .withAttributes({
            fieldType: '@cardstack/core-types::integer'
          }).withRelated(
            'defaultAtCreate',
            factory.addResource('default-values').withAttributes({ value: 42 })
          ),
        factory.addResource('fields', 'karma')
          .withAttributes({
            fieldType: '@cardstack/core-types::integer'
          }).withRelated(
            'defaultAtUpdate',
            factory.addResource('default-values').withAttributes({ value: 0 })
          )
      ]);

    factory.addResource('things-with-defaults', 4)
      .withAttributes({
        coolness: 100,
        karma: 10
      });

    env = await createDefaultEnvironment(factory.getModels());

    user = {
      fullName: 'Sample User',
      email: '<EMAIL>'
    };

    writers = new Writers(env.schemaCache);

  });

  afterEach(async function() {
    await destroyDefaultEnvironment(env);
  });

  describe('create', function() {
    it('saves attributes', async function () {
      let record = await writers.create('master', user, 'articles', {
        type: 'articles',<commit_msg>simplify environment in git/writers test<commit_after>const Writer = require('@cardstack/git/writer');
const { inRepo } = require('./support');
const {
  createDefaultEnvironment,
  destroyDefaultEnvironment
} = require('@cardstack/server/node-tests/support');
const JSONAPIFactory = require('@cardstack/test-support/jsonapi-factory');

describe('git/writer', function() {

  let env, writers, user;

  beforeEach(async function() {
    let factory = new JSONAPIFactory();

    factory.addResource('content-types', 'articles')
      .withRelated('fields', [
        factory.addResource('fields', 'title').withAttributes({ fieldType: '@cardstack/core-types::string' }),
        factory.addResource('fields', 'primary-image').withAttributes({ fieldType: '@cardstack/core-types::belongs-to' })
      ]);

    factory.addResource('content-types', 'people')
      .withRelated('fields', [
        factory.addResource('fields', 'first-name').withAttributes({ fieldType: '@cardstack/core-types::string' }),
        factory.addResource('fields', 'last-name').withAttributes({ fieldType: '@cardstack/core-types::string' }),
        factory.addResource('fields', 'age').withAttributes({ fieldType: '@cardstack/core-types::integer' })
      ]);

    factory.addResource('articles', 1)
      .withAttributes({
          title: 'First Article'
      });

    factory.addResource('people', 1)
      .withAttributes({
        firstName: 'Quint',
        lastName: 'Faulkner',
        age: 6
      });

    factory.addResource('people', 2)
      .withAttributes({
        firstName: 'Arthur',
        lastName: 'Faulkner',
        age: 1
      });

    factory.addResource('content-types', 'things-with-defaults')
      .withRelated('fields', [
        factory.addResource('fields', 'coolness')
          .withAttributes({
            fieldType: '@cardstack/core-types::integer'
          }).withRelated(
            'defaultAtCreate',
            factory.addResource('default-values').withAttributes({ value: 42 })
          ),
        factory.addResource('fields', 'karma')
          .withAttributes({
            fieldType: '@cardstack/core-types::integer'
          }).withRelated(
            'defaultAtUpdate',
            factory.addResource('default-values').withAttributes({ value: 0 })
          )
      ]);

    factory.addResource('things-with-defaults', 4)
      .withAttributes({
        coolness: 100,
        karma: 10
      });

    env = await createDefaultEnvironment(factory.getModels());

    writers = env.writers;
    user = env.user;

  });

  afterEach(async function() {
    await destroyDefaultEnvironment(env);
  });

  describe('create', function() {
    it('saves attributes', async function () {
      let record = await writers.create('master', user, 'articles', {
        type: 'articles',
{'fraction_non_alphanumeric': 0.15811158798283262, 'fraction_numerical': 0.0048068669527896995, 'mean_word_length': 3.0684357541899443, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '4137492', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1585, 'n_words': 275}