{"size": 4505, "ext": "js", "max_stars_repo_path": "test/cmsify-images.test.js", "max_stars_repo_name": "Crownpeak/DXM-SDK-Core", "max_stars_repo_stars_event_min_datetime": "2020-12-11T02:54:24.000Z", "max_stars_repo_stars_event_max_datetime": "2020-12-11T02:54:24.000Z", "max_issues_repo_path": "test/cmsify-images.test.js", "max_issues_repo_name": "Crownpeak/DXM-SDK-Core", "max_issues_repo_issues_event_min_datetime": "2020-05-28T10:28:23.000Z", "max_issues_repo_issues_event_max_datetime": "2021-10-15T14:50:16.000Z", "max_forks_repo_path": "test/cmsify-images.test.js", "max_forks_repo_name": "Crownpeak/DXM-SDK-Core", "max_forks_repo_forks_event_min_datetime": "2020-10-29T17:11:58.000Z", "max_forks_repo_forks_event_max_datetime": "2020-11-17T17:55:03.000Z"}
{"max_stars_count": 1, "max_issues_count": 110, "max_forks_count": 2, "avg_line_length": 67.2388059701, "max_line_length": 173, "alphanum_fraction": 0.6674805771}
const assert = require('assert');
const utils = require('../src/crownpeak/utils');
const fs = require('fs');
const path = require('path');

describe('Cmsify Images', () => {
    it('should find and replace an image tag on a component', () => {
        const file = path.resolve('./test/fixtures/image1.html');
        const content = fs.readFileSync(file, 'utf8');
        const result = utils.replaceAssets(file, content, null, true);
        assert.strictEqual(result.uploads.length, 1);
        assert.strictEqual(result.uploads[0].destination, 'test/fixtures/');
        assert.strictEqual(result.uploads[0].name, 'example.png');
        assert.strictEqual(result.uploads[0].source, path.resolve('./test/fixtures/example.png'));
        assert.strictEqual(result.content, "<img src=\"/cpt_internal/test/fixtures/example.png\">");
    });
    it('should find and replace an image tag on a non-component', () => {
        const file = path.resolve('./test/fixtures/image1.html');
        const content = fs.readFileSync(file, 'utf8');
        const result = utils.replaceAssets(file, content, null, false);
        assert.strictEqual(result.uploads.length, 1);
        assert.strictEqual(result.uploads[0].destination, 'test/fixtures/');
        assert.strictEqual(result.uploads[0].name, 'example.png');
        assert.strictEqual(result.uploads[0].source, path.resolve('./test/fixtures/example.png'));
        assert.strictEqual(result.content, "<img src=\"<%= Asset.Load(Asset.GetSiteRoot(asset).AssetPath + \"/test/fixtures/example.png\").GetLink(LinkType.Include) %>\">");
    });
    it('should find and replace an image tag with a relative path on a component', () => {
        const file = path.resolve('./test/fixtures/image2.html');
        const content = fs.readFileSync(file, 'utf8');
        const result = utils.replaceAssets(file, content, null, true);
        assert.strictEqual(result.uploads.length, 1);
        assert.strictEqual(result.uploads[0].destination, 'test/fixtures/');
        assert.strictEqual(result.uploads[0].name, 'example.png');
        assert.strictEqual(result.uploads[0].source, path.resolve('./test/fixtures/example.png'));
        assert.strictEqual(result.content, "<img src=\"/cpt_internal/test/fixtures/example.png\">");
    });
    it('should find and replace an image tag with a relative path on a non-component', () => {
        const file = path.resolve('./test/fixtures/image2.html');
        const content = fs.readFileSync(file, 'utf8');
        const result = utils.replaceAssets(file, content, null, false);
        assert.strictEqual(result.uploads.length, 1);
        assert.strictEqual(result.uploads[0].destination, 'test/fixtures/');
        assert.strictEqual(result.uploads[0].name, 'example.png');
        assert.strictEqual(result.uploads[0].source, path.resolve('./test/fixtures/example.png'));
        assert.strictEqual(result.content, "<img src=\"<%= Asset.Load(Asset.GetSiteRoot(asset).AssetPath + \"/test/fixtures/example.png\").GetLink(LinkType.Include) %>\">");
    });
    it('should find and replace an image tag with an absolute path on a component', () => {
        const file = path.resolve('./test/fixtures/image3.html');
        const content = fs.readFileSync(file, 'utf8');
        const result = utils.replaceAssets(file, content, null, true);
        assert.strictEqual(result.uploads.length, 1);
        assert.strictEqual(result.uploads[0].destination, 'test/fixtures/');
        assert.strictEqual(result.uploads[0].name, 'example.png');
        assert.strictEqual(result.uploads[0].source, path.resolve('./test/fixtures/example.png'));
        assert.strictEqual(result.content, "<img src=\"/cpt_internal/test/fixtures/example.png\">");
    });
    it('should find and replace an image tag with an absolute path on a non-component', () => {
        const file = path.resolve('./test/fixtures/image3.html');
        const content = fs.readFileSync(file, 'utf8');
        const result = utils.replaceAssets(file, content, null, false);
        assert.strictEqual(result.uploads.length, 1);
        assert.strictEqual(result.uploads[0].destination, 'test/fixtures/');
        assert.strictEqual(result.uploads[0].name, 'example.png');
        assert.strictEqual(result.uploads[0].source, path.resolve('./test/fixtures/example.png'));
        assert.strictEqual(result.content, "<img src=\"<%= Asset.Load(Asset.GetSiteRoot(asset).AssetPath + \"/test/fixtures/example.png\").GetLink(LinkType.Include) %>\">");
    });
});