<commit_before>import {Document} from 'idai-components-2/core';
import {ProjectConfiguration} from 'idai-components-2/configuration'
import {IdaiFieldDocument} from 'idai-components-2/idai-field-model';
import {Static} from '../../helper/static';
import {CachedDatastore} from '../../../../app/core/datastore/core/cached-datastore';
import {ViewFacade} from '../../../../app/components/resources/view/view-facade';
import {ResourcesState} from '../../../../app/components/resources/view/resources-state';
import {IdaiFieldDocumentDatastore} from '../../../../app/core/datastore/idai-field-document-datastore';
import {IdaiFieldTypeConverter} from '../../../../app/core/datastore/idai-field-type-converter';
import {ImageTypeUtility} from '../../../../app/common/image-type-utility';

/**
 * This is a subsystem test.
 * The use of mocks is intentionally reduced.
 * The subsystem gets assembled in the ViewFacade's constructor.
 *
 * @author <NAME>
 */
export function main() {

    describe('ViewFacade/Subsystem', () => {

        const viewsList = [
            {
                'mainTypeLabel': 'Schnitt',
                'label': 'Ausgrabung',
                'operationSubtype': 'Trench',
                'name': 'excavation'
            }
        ];

        const pc = {
            types: [
                { 'type': 'Trench', 'fields': [] },
                { 'type': 'Image', 'fields': [] },
                { 'type': 'Find', 'fields': [] },
                { 'type': 'Project', 'fields': [] }
            ]
        };

        let viewFacade: ViewFacade;
        let operationTypeDocument1: Document;
        let operationTypeDocument2: Document;
        let document1: Document;
        let document2: Document;
        let document3: Document;
        let idaiFieldDocumentDatastore: CachedDatastore<IdaiFieldDocument>;


        beforeEach(async done => {

            spyOn(console, 'debug'); // suppress console.debug

            const {datastore, documentCache} = Static.createPouchdbDatastore('testdb');
            idaiFieldDocumentDatastore = new IdaiFieldDocumentDatastore(
                datastore, documentCache,
                new IdaiFieldTypeConverter(new ImageTypeUtility(new ProjectConfiguration(pc))));

            const projectDocument = Static.doc('testdb','testdb','Project','testdb');
            operationTypeDocument1 = Static.doc('trench1','trench1','Trench','t1');
            operationTypeDocument2 = Static.doc('trench2','trench2','Trench','t2');
            operationTypeDocument1.resource.relations['isRecordedIn'] = ['testdb'];
            operationTypeDocument2.resource.relations['isRecordedIn'] = ['testdb'];

            document1 = Static.doc('find1','find1','Find');
            document1.resource.relations['isRecordedIn'] = [operationTypeDocument1.resource.id];
            document2 = Static.doc('find2','find2','Find');
            document2.resource.relations['isRecordedIn'] = [operationTypeDocument1.resource.id];
            document3 = Static.doc('find3','find3','Find');
            document3.resource.relations['isRecordedIn'] = [operationTypeDocument2.resource.id];

            await idaiFieldDocumentDatastore.create(projectDocument);
            await idaiFieldDocumentDatastore.create(operationTypeDocument1);
            await idaiFieldDocumentDatastore.create(operationTypeDocument2);
            await idaiFieldDocumentDatastore.create(document1);
            await idaiFieldDocumentDatastore.create(document2);
            await idaiFieldDocumentDatastore.create(document3);
            done();
        });


        beforeEach(() => {

            const settingsService =
                jasmine.createSpyObj('settingsService', ['getUsername', 'getSelectedProject']);
            settingsService.getUsername.and.returnValue('user');
            settingsService.getSelectedProject.and.returnValue('testdb');

            const stateSerializer = jasmine.createSpyObj('stateSerializer', ['load', 'store']);
            stateSerializer.load.and.returnValue(Promise.resolve({}));
            stateSerializer.store.and.returnValue(Promise.resolve());

            const changesStream = jasmine.createSpyObj('changesStream', ['remoteChangesNotifications']);
            changesStream.remoteChangesNotifications.and.returnValue({
                subscribe: () => {}
            });

            viewFacade = new ViewFacade(
                idaiFieldDocumentDatastore,
                changesStream,
                settingsService,
                new ResourcesState(stateSerializer),
                viewsList
            );
        });


        afterEach((done) => new PouchDB('testdb').destroy().then(() => {done()}), 5000);



        it('populate document list in operations view', async done => {

            await viewFacade.setupView('excavation', undefined);
            expect(viewFacade.getDocuments().length).toBe(2);
            const identifiers = viewFacade.getDocuments().map(document => document.resource.identifier);
            expect(identifiers).toContain('find1');
            expect(identifiers).toContain('find2');
            done();
        });


        it('operations overview: populate document list', async done => {

            await viewFacade.setupView('project', undefined);
            expect(viewFacade.getDocuments().length).toBe(2);
            const identifiers = viewFacade.getDocuments().map(document => document.resource.identifier);
            expect(identifiers).toContain('trench1');
            expect(identifiers).toContain('trench2');
            done();
        });


        it('operations view: select operations type document', async done => {

            await viewFacade.setupView('excavation', undefined);
            await viewFacade.selectMainTypeDocument(operationTypeDocument2);
            expect(viewFacade.getDocuments().length).toBe(1);
            expect(viewFacade.getDocuments()[0].resource.identifier).toEqual('find3');
            done();
        });


        it('operations view: search', async done => {

            await viewFacade.setupView('excavation', undefined);
            await viewFacade.setQueryString('find2');
            expect(viewFacade.getDocuments().length).toBe(1);
            expect(viewFacade.getDocuments()[0].resource.identifier).toEqual('find2');
            done();
        });


        it('operations overview: search', async done => {

            await viewFacade.setupView('project', undefined);
            await viewFacade.setQueryString('trench2');
            expect(viewFacade.getDocuments().length).toBe(1);
            expect(viewFacade.getDocuments()[0].resource.identifier).toEqual('trench2');
            done();
        });


        it('operations view: set selected, query invalidated', async done => {

            await viewFacade.setupView('excavation', undefined);
            await viewFacade.setQueryString('find1');
            await viewFacade.setSelectedDocument(document2);
            expect(viewFacade.getQueryString()).toEqual('');
            expect(viewFacade.getDocuments().length).toBe(2);
            done();<commit_msg>change method names & order in view facade subsystem unit tests<commit_after>import {Document} from 'idai-components-2/core';
import {ProjectConfiguration} from 'idai-components-2/configuration'
import {IdaiFieldDocument} from 'idai-components-2/idai-field-model';
import {Static} from '../../helper/static';
import {CachedDatastore} from '../../../../app/core/datastore/core/cached-datastore';
import {ViewFacade} from '../../../../app/components/resources/view/view-facade';
import {ResourcesState} from '../../../../app/components/resources/view/resources-state';
import {IdaiFieldDocumentDatastore} from '../../../../app/core/datastore/idai-field-document-datastore';
import {IdaiFieldTypeConverter} from '../../../../app/core/datastore/idai-field-type-converter';
import {ImageTypeUtility} from '../../../../app/common/image-type-utility';

/**
 * This is a subsystem test.
 * The use of mocks is intentionally reduced.
 * The subsystem gets assembled in the ViewFacade's constructor.
 *
 * @author <NAME>
 */
export function main() {

    describe('ViewFacade/Subsystem', () => {

        const viewsList = [
            {
                'mainTypeLabel': 'Schnitt',
                'label': 'Ausgrabung',
                'operationSubtype': 'Trench',
                'name': 'excavation'
            }
        ];

        const pc = {
            types: [
                { 'type': 'Trench', 'fields': [] },
                { 'type': 'Image', 'fields': [] },
                { 'type': 'Find', 'fields': [] },
                { 'type': 'Project', 'fields': [] }
            ]
        };

        let viewFacade: ViewFacade;
        let operationTypeDocument1: Document;
        let operationTypeDocument2: Document;
        let document1: Document;
        let document2: Document;
        let document3: Document;
        let idaiFieldDocumentDatastore: CachedDatastore<IdaiFieldDocument>;


        beforeEach(async done => {

            spyOn(console, 'debug'); // suppress console.debug

            const {datastore, documentCache} = Static.createPouchdbDatastore('testdb');
            idaiFieldDocumentDatastore = new IdaiFieldDocumentDatastore(
                datastore, documentCache,
                new IdaiFieldTypeConverter(new ImageTypeUtility(new ProjectConfiguration(pc))));

            const projectDocument = Static.doc('testdb','testdb','Project','testdb');
            operationTypeDocument1 = Static.doc('trench1','trench1','Trench','t1');
            operationTypeDocument2 = Static.doc('trench2','trench2','Trench','t2');
            operationTypeDocument1.resource.relations['isRecordedIn'] = ['testdb'];
            operationTypeDocument2.resource.relations['isRecordedIn'] = ['testdb'];

            document1 = Static.doc('find1','find1','Find');
            document1.resource.relations['isRecordedIn'] = [operationTypeDocument1.resource.id];
            document2 = Static.doc('find2','find2','Find');
            document2.resource.relations['isRecordedIn'] = [operationTypeDocument1.resource.id];
            document3 = Static.doc('find3','find3','Find');
            document3.resource.relations['isRecordedIn'] = [operationTypeDocument2.resource.id];

            await idaiFieldDocumentDatastore.create(projectDocument);
            await idaiFieldDocumentDatastore.create(operationTypeDocument1);
            await idaiFieldDocumentDatastore.create(operationTypeDocument2);
            await idaiFieldDocumentDatastore.create(document1);
            await idaiFieldDocumentDatastore.create(document2);
            await idaiFieldDocumentDatastore.create(document3);
            done();
        });


        beforeEach(() => {

            const settingsService =
                jasmine.createSpyObj('settingsService', ['getUsername', 'getSelectedProject']);
            settingsService.getUsername.and.returnValue('user');
            settingsService.getSelectedProject.and.returnValue('testdb');

            const stateSerializer = jasmine.createSpyObj('stateSerializer', ['load', 'store']);
            stateSerializer.load.and.returnValue(Promise.resolve({}));
            stateSerializer.store.and.returnValue(Promise.resolve());

            const changesStream = jasmine.createSpyObj('changesStream', ['remoteChangesNotifications']);
            changesStream.remoteChangesNotifications.and.returnValue({
                subscribe: () => {}
            });

            viewFacade = new ViewFacade(
                idaiFieldDocumentDatastore,
                changesStream,
                settingsService,
                new ResourcesState(stateSerializer),
                viewsList
            );
        });


        afterEach((done) => new PouchDB('testdb').destroy().then(() => {done()}), 5000);


        it('operations overview: populate document list', async done => {

            await viewFacade.setupView('project', undefined);
            expect(viewFacade.getDocuments().length).toBe(2);
            const identifiers = viewFacade.getDocuments().map(document => document.resource.identifier);
            expect(identifiers).toContain('trench1');
            expect(identifiers).toContain('trench2');
            done();
        });


        it('operations overview: search', async done => {

            await viewFacade.setupView('project', undefined);
            await viewFacade.setQueryString('trench2');
            expect(viewFacade.getDocuments().length).toBe(1);
            expect(viewFacade.getDocuments()[0].resource.identifier).toEqual('trench2');
            done();
        });


        it('operations view: populate document list', async done => {

            await viewFacade.setupView('excavation', undefined);
            expect(viewFacade.getDocuments().length).toBe(2);
            const identifiers = viewFacade.getDocuments().map(document => document.resource.identifier);
            expect(identifiers).toContain('find1');
            expect(identifiers).toContain('find2');
            done();
        });


        it('operations view: select operations type document', async done => {

            await viewFacade.setupView('excavation', undefined);
            await viewFacade.selectMainTypeDocument(operationTypeDocument2);
            expect(viewFacade.getDocuments().length).toBe(1);
            expect(viewFacade.getDocuments()[0].resource.identifier).toEqual('find3');
            done();
        });


        it('operations view: search', async done => {

            await viewFacade.setupView('excavation', undefined);
            await viewFacade.setQueryString('find2');
            expect(viewFacade.getDocuments().length).toBe(1);
            expect(viewFacade.getDocuments()[0].resource.identifier).toEqual('find2');
            done();
        });


        it('operations view: set selected, query invalidated', async done => {

            await viewFacade.setupView('excavation', undefined);
            await viewFacade.setQueryString('find1');
            await viewFacade.setSelectedDocument(document2);
            expect(viewFacade.getQueryString()).toEqual('');
            expect(viewFacade.getDocuments().length).toBe(2);
            done();
{'fraction_non_alphanumeric': 0.13735226239597176, 'fraction_numerical': 0.008811805021330162, 'mean_word_length': 2.595675132009052, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '137662', 'n_tokens_mistral': 3766, 'n_tokens_neox': 3654, 'n_words': 605}