const path = require('path');
const fs = require('fs-extra');
const prettier = require('prettier');
const prettierConfig = { semi: false, parser: 'css' };

const buildDir = require('./.boltrc.multi-lang.js').buildDir;

describe('Tests to confirm that the latest upcoming release of the @bolt/build-tools is compiling as expected', () => {
  test('Check that Bolt Build Tools is generating JSON data from Sass files', async () => {
    const dataFileExists = async function() {
      try {
        await fs.promises.access(
          path.resolve(
            process.cwd(),
            `${buildDir}/data/colors/brand.bolt.json`,
          ),
        );
        return true;
      } catch (error) {
        return false;
      }
    };

    expect(await dataFileExists()).toBe(true);
  });

  test('Check JSON data for colors was generated and outputted by the @bolt/build-tools', async () => {
    const getColorData = async function() {
      try {
        await fs.promises.access(
          path.resolve(
            process.cwd(),
            `${buildDir}/data/colors/brand.bolt.json`,
          ),
        );
        const colorData = JSON.parse(
          fs.readFileSync(
            path.resolve(
              process.cwd(),
              `${buildDir}/data/colors/brand.bolt.json`,
            ),
          ),
        );
        return colorData;
      } catch (error) {
        return false;
      }
    };

    const colorData = await getColorData();

    expect(colorData).toMatchSnapshot();

    expect(colorData.indigo.xdark).toBe('rgb(6, 9, 35)');
    expect(colorData.yellow.base).toBe('rgb(255, 204, 77)');
  });

  test('Check that the Japanese-specific CSS bundle was generated and contains the correct font', async () => {
    const getCSSData = async function() {
      try {
        const cssData = fs
          .readFileSync(
            path.resolve(process.cwd(), `${buildDir}/bolt-global-ja.css`),
          )
          .toString();
        return cssData;
      } catch (error) {
        return false;
      }
    };

    const cssFile = await getCSSData();
    const prettyCss = await prettier.format(cssFile, prettierConfig);

    expect(prettyCss).toMatchSnapshot();

    const japaneseFontRule =
      'font-family:-apple-system,BlinkMacSystemFont,ヒラギノ角ゴ ProN,Hiragino Kaku Gothic ProN,游ゴシック,游ゴシック体,YuGothic,Yu Gothic,メイリオ,Meiryo,ＭＳ ゴシック,MS Gothic,HiraKakuProN-W3,TakaoExゴシック,TakaoExGothic,MotoyaLCedar,Droid Sans Japanese,sans-serif;';

    const englishFontFamilyRule =
      'font-family:"Open Sans","Helvetica Neue",sans-serif;font-family:var(--bolt-font-family-heading)';

    expect(cssFile).toEqual(expect.stringContaining(japaneseFontRule));
    expect(cssFile).toEqual(expect.not.stringContaining(englishFontFamilyRule));
  });

  test('Check that the Japanese-specific bolt-global-ja.css file built contains the correct utility class styles for the older u-bolt-margin-bottom syntax.', async () => {
    const getCSSData = async function() {
      try {
        const cssData = fs
          .readFileSync(
            path.resolve(process.cwd(), `${buildDir}/bolt-global-ja.css`),
          )
          .toString();
        return cssData;
      } catch (error) {
        return false;
      }
    };

    const cssFile = await getCSSData();
    expect(cssFile).toEqual(
      expect.stringContaining(
        '.u-bolt-margin-bottom{margin-bottom:1.155rem!important}',
      ),
    );
  });

  test('Check that the default English-specific bolt-global.css file built contains the correct utility class styles for the older u-bolt-margin-bottom syntax.', async () => {
    const getCSSData = async function() {
      try {
        const cssData = fs
          .readFileSync(
            path.resolve(process.cwd(), `${buildDir}/bolt-global-en.css`),
          )
          .toString();
        return cssData;
      } catch (error) {
        return false;
      }
    };

    const cssFile = await getCSSData();
    expect(cssFile).toEqual(
      expect.stringContaining(
        '.u-bolt-margin-bottom{margin-bottom:1.65rem!important}',
      ),
    );

    const prettyCss = await prettier.format(cssFile, prettierConfig);
    expect(prettyCss).toMatchSnapshot();
  });
});

@@ -2081,120 +2081,8 @@
 a();
-%0A    const prettyCss = await prettier.format(cssFile, prettierConfig);%0A%0A    expect(prettyCss).toMatchSnapshot();
 %0A%0A  
@@ -2642,16 +2642,128 @@
 yRule));
+%0A%0A    const prettyCss = await prettier.format(cssFile, prettierConfig);%0A    expect(prettyCss).toMatchSnapshot();
 %0A  %7D);%0A%0A
@@ -3276,38 +3276,111 @@
 Data();%0A    
-expect(cssFile
+const prettyCss = await prettier.format(cssFile, prettierConfig);%0A%0A    expect(prettyCss
 ).toEqual(%0A 
@@ -3409,32 +3409,54 @@
 aining(%0A        
+await prettier.format(
 '.u-bolt-margin-
@@ -3489,32 +3489,49 @@
 rem!important%7D',
+ prettierConfig),
 %0A      ),%0A    );
@@ -4056,38 +4056,111 @@
 Data();%0A    
-expect(cssFile
+const prettyCss = await prettier.format(cssFile, prettierConfig);%0A%0A    expect(prettyCss
 ).toEqual(%0A 
@@ -4192,18 +4192,31 @@
 ing(
-%0A        '
+await prettier.format(%60
 .u-b
@@ -4224,32 +4224,33 @@
 lt-margin-bottom
+ 
 %7Bmargin-bottom:1
@@ -4252,15 +4252,17 @@
 tom:
+ 
 1.65rem
+ 
 !imp
@@ -4271,80 +4271,11 @@
 tant
-%7D',%0A      ),%0A    );%0A%0A    const prettyCss = await prettier.format(cssFile
+;%7D%60
 , pr
@@ -4279,33 +4279,42 @@
  prettierConfig)
-;
+),%0A    );%0A
 %0A    expect(pret

update build tool tests to format CSS before comparing results