<commit_before>import * as path from 'path';
import * as resources from '../resources';
import * as util from '@ionic/cli-utils';

import { ImageResource, SourceImage } from '../../definitions';

const resourcesJson = require('./fixtures/resources.json');

describe('resources', () => {

  it('flattenResourceJsonStructure should do take the resources.json structure and flatten it', () => {
    const result = resources.flattenResourceJsonStructure(resourcesJson);

    expect(result).toEqual(jasmine.any(Array));
    expect(result.length).toEqual(51);
    expect(result.find(img => img.name === 'Default-568h@2x~iphone.png')).toEqual({
      platform: 'ios',
      resType: 'splash',
      name: 'Default-568h@2x~iphone.png',
      width: 640,
      height: 1136,
      density: undefined,
      nodeName: 'splash',
      nodeAttributes: ['src', 'width', 'height']
    });
  });

  describe('createImgDestinationDirectories', () => {
    const imgResources: ImageResource[] = [{
      platform: 'ios',
      resType: 'splash',
      name: 'Default-568h@2x~iphone.png',
      width: 640,
      height: 1136,
      density: undefined,
      nodeName: 'splash',
      nodeAttributes: ['src', 'width', 'height'],
      imageId: null
    }, {
      platform: 'ios',
      resType: 'splash',
      name: 'Default-Landscape-736h.png',
      width: 2208,
      height: 1242,
      density: undefined,
      nodeName: 'splash',
      nodeAttributes: ['src', 'width', 'height'],
      imageId: null
    }, {
      platform: 'android',
      resType: 'splash',
      name: 'drawable-land-ldpi-screen.png',
      width: 320,
      height: 240,
      density: 'land-ldpi',
      nodeName: 'splash' ,
      nodeAttributes: ['src', 'density'],
      imageId: null
    }]
      .map((img) => ({
        ...img,
        dest: path.join('/resourcesDir', img.platform, img.resType, img.name)
      }));

    it('should create directories that required for all of the images', async function() {
      spyOn(util, 'fsMkdirp').and.callFake((dir) => Promise.resolve(dir));
      await resources.createImgDestinationDirectories(imgResources);

      expect((<jasmine.Spy>util.fsMkdirp).calls.count()).toEqual(2);
      expect((<jasmine.Spy>util.fsMkdirp).calls.argsFor(0)).toEqual(['/resourcesDir/ios/splash']);
      expect((<jasmine.Spy>util.fsMkdirp).calls.argsFor(1)).toEqual(['/resourcesDir/android/splash']);
    });
  });

  describe('getSourceImages', () => {
    it('should look in resources directory and platform directories to find images', async function() {
      spyOn(util, 'fsReadDir').and.returnValue(Promise.resolve([]));

      await resources.getSourceImages(['ios', 'android'], ['splash', 'icon'], '/resourceDir');

      expect((<jasmine.Spy>util.fsReadDir).calls.count()).toEqual(3);
      expect((<jasmine.Spy>util.fsReadDir).calls.argsFor(0)).toEqual(['/resourceDir/ios']);
      expect((<jasmine.Spy>util.fsReadDir).calls.argsFor(1)).toEqual(['/resourceDir/android']);
      expect((<jasmine.Spy>util.fsReadDir).calls.argsFor(2)).toEqual(['/resourceDir']);
    });

    it('should find all sourceImages available and prioritize based on specificity', async function() {
      spyOn(util, 'getFileChecksum').and.returnValue(Promise.resolve('FJDKLFJDKL'));
      spyOn(util, 'fsReadDir').and.callFake((dir) => {
        switch (dir) {
        case '/resourceDir/ios':
          return Promise.resolve([
            'icon.png',
            'splash.jpg',
            'things.ai'
          ]);
        case '/resourceDir/android':
          return Promise.resolve([
            'icon.ai',
            'splash.png'
          ]);
        case '/resourceDir':
          return Promise.resolve([
            'icon.png',
            'splash.psd'
          ]);<commit_msg>chore(): fix mocks<commit_after>import * as path from 'path';
import * as resources from '../resources';
import * as util from '@ionic/cli-utils';

import { ImageResource, SourceImage } from '../../definitions';

const resourcesJson = require('./fixtures/resources.json');

describe('resources', () => {

  it('flattenResourceJsonStructure should do take the resources.json structure and flatten it', () => {
    const result = resources.flattenResourceJsonStructure(resourcesJson);

    expect(result).toEqual(jasmine.any(Array));
    expect(result.length).toEqual(51);
    expect(result.find(img => img.name === 'Default-568h@2x~iphone.png')).toEqual({
      platform: 'ios',
      resType: 'splash',
      name: 'Default-568h@2x~iphone.png',
      width: 640,
      height: 1136,
      density: undefined,
      nodeName: 'splash',
      nodeAttributes: ['src', 'width', 'height']
    });
  });

  describe('createImgDestinationDirectories', () => {
    const imgResources: ImageResource[] = [{
      platform: 'ios',
      resType: 'splash',
      name: 'Default-568h@2x~iphone.png',
      width: 640,
      height: 1136,
      density: undefined,
      nodeName: 'splash',
      nodeAttributes: ['src', 'width', 'height'],
      imageId: null
    }, {
      platform: 'ios',
      resType: 'splash',
      name: 'Default-Landscape-736h.png',
      width: 2208,
      height: 1242,
      density: undefined,
      nodeName: 'splash',
      nodeAttributes: ['src', 'width', 'height'],
      imageId: null
    }, {
      platform: 'android',
      resType: 'splash',
      name: 'drawable-land-ldpi-screen.png',
      width: 320,
      height: 240,
      density: 'land-ldpi',
      nodeName: 'splash' ,
      nodeAttributes: ['src', 'density'],
      imageId: null
    }]
      .map((img) => ({
        ...img,
        dest: path.join('/resourcesDir', img.platform, img.resType, img.name)
      }));

    it('should create directories that required for all of the images', async function() {
      spyOn(util, 'fsMkdirp').and.callFake((dir) => Promise.resolve(dir));
      await resources.createImgDestinationDirectories(imgResources);

      expect((<jasmine.Spy>util.fsMkdirp).calls.count()).toEqual(2);
      expect((<jasmine.Spy>util.fsMkdirp).calls.argsFor(0)).toEqual(['/resourcesDir/ios/splash']);
      expect((<jasmine.Spy>util.fsMkdirp).calls.argsFor(1)).toEqual(['/resourcesDir/android/splash']);
    });
  });

  describe('getSourceImages', () => {
    it('should look in resources directory and platform directories to find images', async function() {
      spyOn(util, 'readDir').and.returnValue(Promise.resolve([]));

      await resources.getSourceImages(['ios', 'android'], ['splash', 'icon'], '/resourceDir');

      expect((<jasmine.Spy>util.readDir).calls.count()).toEqual(3);
      expect((<jasmine.Spy>util.readDir).calls.argsFor(0)).toEqual(['/resourceDir/ios']);
      expect((<jasmine.Spy>util.readDir).calls.argsFor(1)).toEqual(['/resourceDir/android']);
      expect((<jasmine.Spy>util.readDir).calls.argsFor(2)).toEqual(['/resourceDir']);
    });

    it('should find all sourceImages available and prioritize based on specificity', async function() {
      spyOn(util, 'getFileChecksum').and.returnValue(Promise.resolve('FJDKLFJDKL'));
      spyOn(util, 'readDir').and.callFake((dir) => {
        switch (dir) {
        case '/resourceDir/ios':
          return Promise.resolve([
            'icon.png',
            'splash.jpg',
            'things.ai'
          ]);
        case '/resourceDir/android':
          return Promise.resolve([
            'icon.ai',
            'splash.png'
          ]);
        case '/resourceDir':
          return Promise.resolve([
            'icon.png',
            'splash.psd'
          ]);
{'fraction_non_alphanumeric': 0.17125016606881893, 'fraction_numerical': 0.013816925734024179, 'mean_word_length': 2.951706036745407, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 33, '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': '6741130', 'n_tokens_mistral': 2384, 'n_tokens_neox': 2321, 'n_words': 428}