var assert = require("assert");
var path = require("path");
var fs = require("fs");

var promises = require("../lib/promises");

var mammoth = require("../")
var htmlPaths = require("../lib/html-paths");
var documentMatchers = require("../lib/document-matchers");

var testing = require("./testing");
var test = testing.test;
var testData = testing.testData;
var createFakeDocxFile = testing.createFakeDocxFile;


describe('mammoth', function() {
    test('should convert docx containing one paragraph to single p element', function() {
        var docxPath = path.join(__dirname, "test-data/single-paragraph.docx");
        return mammoth.convertToHtml({path: docxPath}).then(function(result) {
            assert.equal(result.value, "<p>Walking on imported air</p>");
            assert.deepEqual(result.messages, []);
        });
    });
    
    test('style map can be expressed as string', function() {
        var docxFile = createFakeDocxFile({
            "word/document.xml": testData("simple/word/document.xml")
        });
        var options = {
            styleMap: "p => h1"
        };
        return mammoth.convertToHtml({file: docxFile}, options).then(function(result) {
            assert.equal("<h1>Hello.</h1>", result.value);
        });
    });
    
    test('style map can be expressed as array of style mappings', function() {
        var docxFile = createFakeDocxFile({
            "word/document.xml": testData("simple/word/document.xml")
        });
        var options = {
            styleMap: ["p => h1"]
        };
        return mammoth.convertToHtml({file: docxFile}, options).then(function(result) {
            assert.equal("<h1>Hello.</h1>", result.value);
        });
    });
    
    test('options are passed to document converter when calling mammoth.convertToHtml', function() {
        var docxFile = createFakeDocxFile({
            "word/document.xml": testData("simple/word/document.xml")
        });
        var options = {
            styleMap: "p => h1"
        };
        return mammoth.convertToHtml({file: docxFile}, options).then(function(result) {
            assert.equal("<h1>Hello.</h1>", result.value);
        });
    });
    
    test('options.transformDocument is used to transform document if set', function() {
        var docxFile = createFakeDocxFile({
            "word/document.xml": testData("simple/word/document.xml")
        });
        var options = {
            transformDocument: function(document) {
                document.children[0].styleId = "Heading1";
                return document;
            }
        };
        return mammoth.convertToHtml({file: docxFile}, options).then(function(result) {
            assert.equal("<h1>Hello.</h1>", result.value);
        });
    });
    
    test('inline images are included in output', function() {
        var docxPath = path.join(__dirname, "test-data/tiny-picture.docx");
        return mammoth.convertToHtml({path: docxPath}).then(function(result) {
            assert.equal(result.value, '<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>');
        });
    });
    
    test('simple list is converted to list elements', function() {
        var docxPath = path.join(__dirname, "test-data/simple-list.docx");
        return mammoth.convertToHtml({path: docxPath}).then(function(result) {
            assert.equal(result.value, '<ul><li>Apple</li><li>Banana</li></ul>');
        });
    });
    
    test('indentation is used if prettyPrint is true', function() {
        var docxPath = path.join(__dirname, "test-data/single-paragraph.docx");
        return mammoth.convertToHtml({path: docxPath}, {prettyPrint: true}).then(function(result) {
            assert.equal(result.value, "<p>\n  Walking on imported air\n</p>");
            assert.deepEqual(result.messages, []);
        });
    });
    
    test('using styleMapping throws error', function() {
        try {
            mammoth.styleMapping()
        } catch (error) {
            assert.equal(
                error.message,
                'Use a raw string instead of mammoth.styleMapping e.g. "p[style-naming=\'Title\'] => h1" instead of mammoth.styleMapping("p[style-naming=\'Title\'] => h1")'
            );
        }
    });
})

@@ -3554,32 +3554,747 @@
 );%0A    %7D);%0A    %0A
+    test('footnotes are appended to text', function() %7B%0A        // TODO: generate unique IDs for footnotes%0A        // TODO: generate links back to main body%0A        // TODO: not yet implemented%0A        return;%0A        var docxPath = path.join(__dirname, %22test-data/footnotes.docx%22);%0A        return mammoth.convertToHtml(%7Bpath: docxPath%7D).then(function(result) %7B%0A            var expectedOutput = '%3Cp%3EOuch%3Csup%3E%3Ca href=%22#footnote-1%22%3E1%3C/a%3E%3C/sup%3E.%3Csup%3E%3Ca href=%22#footnote-2%22%3E2%3C/a%3E%3C/sup%3E%3C/p%3E' +%0A                '%3Col%3E%3Cli id=%22footnote-1%22%3E%3Cp%3E A tachyon walks into a bar.%3C/p%3E%3C/li%3E' +%0A                '%3Cli id=%22footnote-2%22%3E%3Cp%3E Fin.%3C/p%3E%3C/li%3E%3C/ol%3E'%0A            assert.equal(result.value, expectedOutput);%0A        %7D);%0A    %7D);%0A    %0A
     test('indent

Add failing test for footnotes