<commit_before>import { PublicationService } from "services/client/PublicationService";
import { TestBase } from "sdl-models";
import { FakeXMLHttpRequest } from "test/mocks/XmlHttpRequest";

class PublicationServiceTests extends TestBase {

    public runTests(): void {
        const publicationService = new PublicationService();
        const publicationId = "1656863";

        describe(`Publication service tests.`, (): void => {

            it("returns a proper error when publications cannot be retrieved", (done: () => void): void => {
                // Put this test first, otherwise the publication would be already in the cache and the spy would not work
                const failMessage = "failure-retrieving-publications";
                spyOn(window, "XMLHttpRequest").and.callFake(() => new FakeXMLHttpRequest(failMessage));
                publicationService.getPublications().then(() => {
                    fail("An error was expected.");
                    done();
                }).catch(error => {
                    expect(error).toContain(failMessage);
                    done();
                });
            });

            it("returns a proper error when publication title cannot be retrieved", (done: () => void): void => {
                // Put this test first, otherwise the publication would be already in the cache and the spy would not work
                const failMessage = "failure-retrieving-publication-title";
                spyOn(window, "XMLHttpRequest").and.callFake(() => new FakeXMLHttpRequest(failMessage));
                publicationService.getPublicationTitle(failMessage).then(() => {
                    fail("An error was expected.");
                    done();
                }).catch(error => {
                    expect(error).toContain(failMessage);
                    done();
                });
            });

            it("can get the publications", (done: () => void): void => {
                publicationService.getPublications().then(publications => {
                    expect(publications).toBeDefined();
                    if (publications) {
                        expect(publications.length).toBe(8);
                        expect(publications[1].title).toBe("Publication MP330");
                    }
                    done();
                }).catch(error => {
                    fail(`Unexpected error: ${error}`);
                    done();
                });
            });

            it("can get the publications from memory", (done: () => void): void => {
                const spy = spyOn(window, "XMLHttpRequest").and.callThrough();
                publicationService.getPublications().then(publications => {
                    expect(publications).toBeDefined();
                    if (publications) {
                        expect(publications.length).toBe(7);
                        expect(publications[1].title).toBe("Publication MP330");
                        expect(spy).not.toHaveBeenCalled();
                    }
                    done();
                }).catch(error => {
                    fail(`Unexpected error: ${error}`);
                    done();
                });
            });

            it("can get a publication title", (done: () => void): void => {
                publicationService.getPublicationTitle(publicationId).then(title => {
                    expect(title).toBe("Publication MP330");
                    done();
                }).catch(error => {
                    fail(`Unexpected error: ${error}`);
                    done();
                });
            });

            it("can get a publication title from memory", (done: () => void): void => {
                const spy = spyOn(window, "XMLHttpRequest").and.callThrough();
                publicationService.getPublicationTitle(publicationId).then(title => {
                    expect(title).toBe("Publication MP330");
                    expect(spy).not.toHaveBeenCalled();
                    done();
                }).catch(error => {
                    fail(`Unexpected error: ${error}`);
                    done();
                });
            });

            it("returns a proper error when a publication title cannot be resolved", (done: () => void): void => {
                publicationService.getPublicationTitle("does-not-exist").then(() => {
                    fail("An error was expected.");
                    done();
                }).catch(error => {
                    expect(error).toContain("does-not-exist");
                    done();
                });
            });

        });

    }
}

new PublicationServiceTests().runTests();
<commit_msg>KCWA-471 One more update to the test that I missed last time<commit_after>import { PublicationService } from "services/client/PublicationService";
import { TestBase } from "sdl-models";
import { FakeXMLHttpRequest } from "test/mocks/XmlHttpRequest";

class PublicationServiceTests extends TestBase {

    public runTests(): void {
        const publicationService = new PublicationService();
        const publicationId = "1656863";

        describe(`Publication service tests.`, (): void => {

            it("returns a proper error when publications cannot be retrieved", (done: () => void): void => {
                // Put this test first, otherwise the publication would be already in the cache and the spy would not work
                const failMessage = "failure-retrieving-publications";
                spyOn(window, "XMLHttpRequest").and.callFake(() => new FakeXMLHttpRequest(failMessage));
                publicationService.getPublications().then(() => {
                    fail("An error was expected.");
                    done();
                }).catch(error => {
                    expect(error).toContain(failMessage);
                    done();
                });
            });

            it("returns a proper error when publication title cannot be retrieved", (done: () => void): void => {
                // Put this test first, otherwise the publication would be already in the cache and the spy would not work
                const failMessage = "failure-retrieving-publication-title";
                spyOn(window, "XMLHttpRequest").and.callFake(() => new FakeXMLHttpRequest(failMessage));
                publicationService.getPublicationTitle(failMessage).then(() => {
                    fail("An error was expected.");
                    done();
                }).catch(error => {
                    expect(error).toContain(failMessage);
                    done();
                });
            });

            it("can get the publications", (done: () => void): void => {
                publicationService.getPublications().then(publications => {
                    expect(publications).toBeDefined();
                    if (publications) {
                        expect(publications.length).toBe(8);
                        expect(publications[1].title).toBe("Publication MP330");
                    }
                    done();
                }).catch(error => {
                    fail(`Unexpected error: ${error}`);
                    done();
                });
            });

            it("can get the publications from memory", (done: () => void): void => {
                const spy = spyOn(window, "XMLHttpRequest").and.callThrough();
                publicationService.getPublications().then(publications => {
                    expect(publications).toBeDefined();
                    if (publications) {
                        expect(publications.length).toBe(8);
                        expect(publications[1].title).toBe("Publication MP330");
                        expect(spy).not.toHaveBeenCalled();
                    }
                    done();
                }).catch(error => {
                    fail(`Unexpected error: ${error}`);
                    done();
                });
            });

            it("can get a publication title", (done: () => void): void => {
                publicationService.getPublicationTitle(publicationId).then(title => {
                    expect(title).toBe("Publication MP330");
                    done();
                }).catch(error => {
                    fail(`Unexpected error: ${error}`);
                    done();
                });
            });

            it("can get a publication title from memory", (done: () => void): void => {
                const spy = spyOn(window, "XMLHttpRequest").and.callThrough();
                publicationService.getPublicationTitle(publicationId).then(title => {
                    expect(title).toBe("Publication MP330");
                    expect(spy).not.toHaveBeenCalled();
                    done();
                }).catch(error => {
                    fail(`Unexpected error: ${error}`);
                    done();
                });
            });

            it("returns a proper error when a publication title cannot be resolved", (done: () => void): void => {
                publicationService.getPublicationTitle("does-not-exist").then(() => {
                    fail("An error was expected.");
                    done();
                }).catch(error => {
                    expect(error).toContain("does-not-exist");
                    done();
                });
            });

        });

    }
}

new PublicationServiceTests().runTests();

{'fraction_non_alphanumeric': 0.12343766139861584, 'fraction_numerical': 0.005061460592913955, 'mean_word_length': 1.439405391786344, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 65, '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': '130924', 'n_tokens_mistral': 2264, 'n_tokens_neox': 2063, 'n_words': 493}