'use strict';

const { mkdirs, readFile, rmdir, unlink, writeFile } = require('hexo-fs');
const { join } = require('path');
const { load } = require('js-yaml');
const { stub } = require('sinon');

describe('config', () => {
  const Hexo = require('../../../lib/hexo');
  const hexo = new Hexo(join(__dirname, 'config_test'), {silent: true});
  const config = require('../../../lib/plugins/console/config').bind(hexo);

  before(async () => {
    await mkdirs(hexo.base_dir);
    hexo.init();
  });

  beforeEach(() => writeFile(hexo.config_path, ''));

  after(() => rmdir(hexo.base_dir));

  it('read all config', async () => {
    const _stub = stub(console, 'log');

    try {
      await config({_: []});
    } finally {
      _stub.restore();
    }

    _stub.calledWith(hexo.config).should.be.true;
  });

  it('read config', async () => {
    const _stub = stub(console, 'log');

    try {
      await config({_: ['title']});
    } finally {
      _stub.restore();
    }

    _stub.calledWith(hexo.config.title).should.be.true;
  });

  it('read nested config', async () => {
    const _stub = stub(console, 'log');

    try {
      hexo.config.server = {
        port: 12345
      };

      await config({_: ['server.port']});
      _stub.calledWith(hexo.config.server.port).should.be.true;
    } finally {
      delete hexo.config.server;
      _stub.restore();
    }
  });

  async function writeConfig(...args) {
    await config({_: args});
    const content = await readFile(hexo.config_path);
    return load(content);
  }

  it('write config', async () => {
    const config = await writeConfig('title', 'My Blog');
    config.title.should.eql('My Blog');
  });

  it('write config: number', async () => {
    const config = await writeConfig('server.port', '5000');
    config.server.port.should.eql(5000);
  });

  it('write config: false', async () => {
    const config = await writeConfig('post_asset_folder', 'false');
    config.post_asset_folder.should.be.false;
  });

  it('write config: true', async () => {
    const config = await writeConfig('post_asset_folder', 'true');
    config.post_asset_folder.should.be.true;
  });

  it('write config: null', async () => {
    const config = await writeConfig('language', 'null');
    should.not.exist(config.language);
  });

  it('write config: undefined', async () => {
    const config = await writeConfig('meta_generator', 'undefined');
    should.not.exist(config.meta_generator);
  });

  it('write config: regex', async () => {
    const config = await writeConfig('include', /^pattern$/gim);
    config.include.should.eql(/^pattern$/gim);
  });

  it('write config: json', async () => {
    const configPath = join(hexo.base_dir, '_config.json');
    hexo.config_path = join(hexo.base_dir, '_config.json');

    await writeFile(configPath, '{}');
    await config({_: ['title', 'My Blog']});

    const content = await readFile(configPath);

    const json = JSON.parse(content);

    json.title.should.eql('My Blog');

    hexo.config_path = join(hexo.base_dir, '_config.yml');
    return unlink(configPath);
  });

  it('create config if not exist', async () => {
    await unlink(hexo.config_path);
    const config = await writeConfig('subtitle', 'Hello world');
    config.subtitle.should.eql('Hello world');
  });
});

@@ -2874,53 +2874,56 @@
     
-const content = await readFile(configPath);%0A%0A
+return readFile(configPath).then(content =%3E %7B%0A  
     
@@ -2961,16 +2961,18 @@
 );%0A%0A    
+  
 json.tit
@@ -2994,24 +2994,26 @@
 My Blog');%0A%0A
+  
     hexo.con
@@ -3059,24 +3059,26 @@
 .yml');%0A    
+  
 return unlin
@@ -3092,16 +3092,24 @@
 gPath);%0A
+    %7D);%0A
   %7D);%0A%0A 

fix lint error "require-atomic-updates"