/*global define*/
/*global describe, it, expect*/
/*global jasmine*/
/*global beforeEach, afterEach, beforeAll*/
/*jslint white: true*/

define ([
    'jquery',
    'narrativeConfig',
    'kbaseNarrative',
    'base/js/namespace',
    'narrativeLogin'
], (
    $,
    Config,
    Narrative,
    Jupyter,
    NarrativeLogin
) => {
    'use strict';

    const DEFAULT_FULLY_LOADED = false,
        DEFAULT_WRITABLE = false,
        DEFAULT_NOTEBOOK_NAME = 'some notebook',
        FAKE_TOKEN = 'foo';

    describe('Test the kbaseNarrative module', () => {
        let loginDiv = $('<div>');

        beforeEach(async () => {
            // we need to be "logged in" for various tests to work, especially initing the Narrative object.
            // this means mocking up some auth responses, and the NarrativeLogin object.
            document.cookie='kbase_session=' + FAKE_TOKEN;
            jasmine.Ajax.install();
            jasmine.Ajax.stubRequest(/\/token$/).andReturn({
                status: 200,
                statusText: 'HTTP/1.1 200 OK',
                contentType: 'application/json',
                responseText: JSON.stringify({
                    type: 'Login',
                    id: 'some-token-id',
                    expires: 16027145808440,
                    created: 1601504980844,
                    name: null,
                    user: 'some_user',
                    custom: {},
                    cachefor: 300000
                })
            });
            jasmine.Ajax.stubRequest(/\/me$/).andReturn({
                status: 200,
                statusText: 'HTTP/1.1 200 OK',
                contentType: 'application/json',
                responseText: JSON.stringify({
                    created: 1490219221579,
                    lastlogin: 1601504980844,
                    display: 'Some User',
                    roles:[
                        {id: 'DevToken', desc: 'Create developer tokens'},
                        {id: 'ServToken', desc: 'Create server tokens'}
                    ],
                    customroles:['role1', 'role2'],
                    policyids:[
                        {id: 'data-policy.1', agreedon: 1497637084128},
                        {id: 'data-policy.3', agreedon: 1490285343224},
                        {id: 'kbase-user.1', agreedon: 1497637084130},
                        {id: 'kbase-user.2', agreedon: 1490285343222}
                    ],
                    user: 'some_user',
                    local: false,
                    email: 'some_user@somewhere.com',
                    idents:[
                        {provusername: 'some_user@somewhere.org', provider:'Globus', id: 'some-globus-ident-id'},
                        {provusername: '1234-5678-9012-3456', provider:'OrcID', id: 'some-orcid-ident-id'}
                    ]
                })
            });

            // mock a jupyter notebook.
            // namespace should already be loaded
            Jupyter.notebook = {
                _fully_loaded: DEFAULT_FULLY_LOADED,
                writable: DEFAULT_WRITABLE,
                keyboard_manager: {
                    edit_shortcuts: {
                        remove_shortcut: () => {}
                    },
                    command_shortcuts: {
                        remove_shortcut: () => {}
                    }
                },
                kernel: {
                    is_connected: () => false,
                    comm_info: (comm_name, callback) => {
                        callback({content: {
                            comms: {
                                'some_comm_id': {
                                    target_name: 'KBaseJobs'
                                }
                            }
                        }});
                    },
                    comm_manager: {
                        register_comm: () => {}
                    },
                    execute: (code, callbacks) => {
                        callbacks.shell.reply({
                            content: {}
                        });
                    }
                },
                notebook_name: DEFAULT_NOTEBOOK_NAME
            };
            Jupyter.keyboard_manager = Jupyter.notebook.keyboard_manager;

            // The NarrativeLogin.init call invokes both of the above token/user profile calls.
            // It's called before the creation of the Narrative object. So that needs to happen here.
            await NarrativeLogin.init(loginDiv);
        });

        afterEach(() => {
            jasmine.Ajax.uninstall();
            Jupyter.notebook = null;
        });

        it('Should instantiate', () => {
            const narr = new Narrative();
            expect(narr.maxNarrativeSize).toBe('10 MB');
        });

        it('Should have an init function that responds when the kernel is connected', () => {
            return new Promise((resolve, reject) => {
                const narr = new Narrative();
                const jobsReadyCallback = (err) => {
                    if (err) {
                        reject('This should not have failed', err);
                    }
                    else {
                        resolve();
                    }
                };
                narr.init(jobsReadyCallback);
                $([Jupyter.events]).trigger('kernel_connected.Kernel');
            });
        });

        it('init should fail as expected when the job connection fails', () => {
            return new Promise((resolve, reject) => {
                Jupyter.notebook.kernel.comm_info = () => {
                    throw new Error('an error happened');
                };
                const narr = new Narrative();
                const jobsReadyCallback = (err) => {
                    if (err) {
                        resolve();
                    }
                    else {
                        reject('expected an error');
                    }
                };
                narr.init(jobsReadyCallback);
                $([Jupyter.events]).trigger('kernel_connected.Kernel');
            });
        });

        it('Should return a boolean for is_loaded', () => {
            const narr = new Narrative();
            expect(narr.isLoaded()).toEqual(DEFAULT_FULLY_LOADED);  // default as set in
        });

        it('Should test ui mode', () => {
            const narr = new Narrative();
            expect(narr.uiModeIs('edit')).toBe(DEFAULT_WRITABLE);
            expect(narr.uiModeIs('view')).toBe(!DEFAULT_WRITABLE);
            Jupyter.notebook.writable = !Jupyter.notebook.writable;
            expect(narr.uiModeIs('edit')).toBe(!DEFAULT_WRITABLE);
            expect(narr.uiModeIs('view')).toBe(DEFAULT_WRITABLE);
        });

        it('Should provide an auth token when requested', () => {
            const narr = new Narrative();
            expect(narr.getAuthToken()).toBe(FAKE_TOKEN);
        });

    });
});

@@ -1244,216 +1244,58 @@
     
-expires: 16027145808440,%0A                    created: 1601504980844,%0A                    name: null,%0A                    user: 'some_user',%0A                    custom: %7B%7D,%0A                    cachefor: 300000
+name: null,%0A                    user: 'some_user',
 %0A   
@@ -1580,1108 +1580,68 @@
     
-created: 1490219221579,%0A                    lastlogin: 1601504980844,%0A                    display: 'Some User',%0A                    roles:%5B%0A                        %7Bid: 'DevToken', desc: 'Create developer tokens'%7D,%0A                        %7Bid: 'ServToken', desc: 'Create server tokens'%7D%0A                    %5D,%0A                    customroles:%5B'role1', 'role2'%5D,%0A                    policyids:%5B%0A                        %7Bid: 'data-policy.1', agreedon: 1497637084128%7D,%0A                        %7Bid: 'data-policy.3', agreedon: 1490285343224%7D,%0A                        %7Bid: 'kbase-user.1', agreedon: 1497637084130%7D,%0A                        %7Bid: 'kbase-user.2', agreedon: 1490285343222%7D%0A                    %5D,%0A                    user: 'some_user',%0A                    local: false,%0A                    email: 'some_user@somewhere.com',%0A                    idents:%5B%0A                        %7Bprovusername: 'some_user@somewhere.org', provider:'Globus', id: 'some-globus-ident-id'%7D,%0A                        %7Bprovusername: '1234-5678-9012-3456', provider:'OrcID', id: 'some-orcid-ident-id'%7D%0A                    %5D
+display: 'Some User',%0A                    user: 'some_user',
 %0A   

fix tests to make codacy happy