const { expect } = require('chai')
const { appendIndex, stripTags, extractMentions } = require('../src/utils')

describe('utils', () => {
    describe('appendIndex', () => {
        it('should append index.html to file', () => {
            const a = { getAttribute: () => '/foo/' }

            const base = '/'
            const expected = '/foo/index.html'

            expect(appendIndex(a, base)).to.eq(expected)
        })

        it('should split by hash', () => {
            const a = { getAttribute: () => '/foo/#bar' }

            const base = '/base'
            const expected = '/base/foo/index.html#bar'

            expect(appendIndex(a, base)).to.eq(expected)
        })

        it('should handle a relative url', () => {
            const a = { getAttribute: () => '../foo/' }

            const base = '/base/'
            const expected = '/foo/index.html'

            expect(appendIndex(a, base)).to.eq(expected)
        })

        it('should handle a relative url with multiple directories', () => {
            const a = { getAttribute: () => '../../foo/#bar' }

            const base = '/many/multiple/folders'
            const expected = '/many/foo/index.html#bar'

            expect(appendIndex(a, base)).to.eq(expected)
        })

        it('should handle a base url with index.html in it', () => {
            const a = { getAttribute: () => '../../foo/#bar' }

            const base = '/many/multiple/folders/index.html'
            const expected = '/many/foo/index.html#bar'

            expect(appendIndex(a, base)).to.eq(expected)
        })

        it('should handle a base url with index.html in it', () => {
            const a = { getAttribute: () => '/foo/#bar' }

            const base = '/many/multiple/folders/index.html'
            const expected = '/many/multiple/folders/foo/index.html#bar'

            expect(appendIndex(a, base)).to.eq(expected)
        })
    })

    describe('stripTags', () => {
        it('should strip tags', () => {
            const input = '<b>hello</b> world'
            const expected = 'hello world'

            expect(stripTags(input)).to.eq(expected)
        })
    })

    describe('extractMentions', () => {
        it('should provide an array of mentions', () => {
            const body = `6.2. Export to JSON This section describes procedures that can be used to export data in JSON format. The export JSON procedures export data into a format that’s supported by JavaScript based visualisation tools. We may also want to export data into JSON format for importing into other tools or for general sharing of query results. The procedures described in this section support exporting to a file or as a stream. This section includes: Available Procedures Exporting to a file Exporting a stream Examples Export whole database Export specified nodes and relationships Export virtual graph Export results of Cypher query 6.2.1. Available Procedures The table below describes the available procedures: type qualified name signature description procedure apoc.export.json.all apoc.export.json.all(file :: STRING?, config = {} :: MAP?) :: (file :: STRING?, source :: STRING?, format :: STRING?, nodes :: INTEGER?, relationships :: INTEGER?, properties :: INTEGER?, time :: INTEGER?, rows :: INTEGER?, batchSize :: INTEGER?, batches :: INTEGER?, done :: BOOLEAN?, data :: STRING?) apoc.export.json.all(file,config) - exports whole database as json to the provided file procedure apoc.export.json.data apoc.export.json.data(nodes :: LIST? OF NODE?, rels :: LIST? OF RELATIONSHIP?, file :: STRING?, config = {} :: MAP?) :: (file :: STRING?, source :: STRING?, format :: STRING?, nodes :: INTEGER?, relationships :: INTEGER?, properties :: INTEGER?, time :: INTEGER?, rows :: INTEGER?, batchSize :: INTEGER?, batches :: INTEGER?, done :: BOOLEAN?, data :: STRING?) apoc.export.json.data(nodes,rels,file,config) - exports given nodes and relationships as json to the provided file procedure apoc.export.json.graph apoc.export.json.graph(graph :: MAP?, file :: STRING?, config = {} :: MAP?) :: (file :: STRING?, source :: STRING?, format :: STRING?, nodes :: INTEGER?, relationships :: INTEGER?, properties :: INTEGER?, time :: INTEGER?, rows :: INTEGER?, batchSize :: INTEGER?, batches :: INTEGER?, done :: BOOLEAN?, data :: STRING?) apoc.export.json.graph(graph,file,config) - exports given graph object as json to the provided file procedure apoc.export.json.query apoc.export.json.query(query :: STRING?, file :: STRING?, config = {} :: MAP?) :: (file :: STRING?, source :: STRING?, format :: STRING?, nodes :: INTEGER?, relationships :: INTEGER?, properties :: INTEGER?, time :: INTEGER?, rows :: INTEGER?, batchSize :: INTEGER?, batches :: INTEGER?, done :: BOOLEAN?, data :: STRING?) apoc.export.json.query(query,file,{config,…​,params:{params}}) - exports results from the cypher statement as json to the provided file Table 6.11. Config name type default description writeNodeProperties boolean false if true export properties too. stream boolean false stream the json directly to the client into the data field The labels exported are ordered alphabetically. The output of labels() function is not sorted, use it in combination with apoc.coll.sort(). `
            const expected = [`&hellip; procedures that can be used to export data in JS <strong>JSON</strong> format. The export JSON procedures export data into a format &hellip;`]
            const result = {
                "ref": "./apoc/4.0/export/json/index.html",
                "score": 5.502,
                "matchData": {
                    "metadata": {
                        "json": {
                            "title": {
                                "position": [[20, 4]]
                            },
                            "description": {
                                "position": [[69, 4]]
                            },
                            "body": {
                                "position": [
                                    [16, 4],
                                    [90, 4],
                                    [114, 4],
                                    [251, 4], [1145, 4],
                                ]
                            }
                        }
                    }
                }
            }

            const actual = extractMentions(body, result)

            expect(actual).to.deep.equal(expected)
        })
    })


})