Discourse: scope mirror tests as being "mode 1" (#1463)

This is to prepare for mode 2 being tested side-by-side.

The normalizeMode1Topics function enforces bumpedMs is not
updated for mode 1 tests.

Additionally describe "update semantics" is redundant,
as the mirror has no other function than update.
This commit is contained in:
Robin van Boven 2019-12-02 20:24:49 +01:00 committed by GitHub
parent 3ceb4fb7fa
commit c521acc145
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 152 additions and 143 deletions

View File

@ -169,6 +169,14 @@ class MockFetcher implements Discourse {
}
}
function normalizeMode1Topics(topics: Topic[]): Topic[] {
return topics.map((topic) => ({
...topic,
// Mode 1 didn't support bumpMs handling.
bumpedMs: topic.timestampMs,
}));
}
describe("plugins/discourse/mirror", () => {
function spyWarn(): JestMockFn<[string], void> {
return ((console.warn: any): JestMockFn<any, void>);
@ -201,6 +209,7 @@ describe("plugins/discourse/mirror", () => {
return {fetcher, mirror, reporter, url, repo};
};
describe("mirror mode 1", () => {
it("mirrors topics from the fetcher", async () => {
const {mirror, fetcher, reporter, repo} = example();
fetcher.addPost(2, null);
@ -208,7 +217,7 @@ describe("plugins/discourse/mirror", () => {
const topic2 = fetcher._topic(2);
const topic3 = fetcher._topic(3);
await mirror.update(reporter);
expect(repo.topics()).toEqual([topic2, topic3]);
expect(repo.topics()).toEqual(normalizeMode1Topics([topic2, topic3]));
});
it("mirrors posts from the fetcher", async () => {
@ -273,7 +282,6 @@ describe("plugins/discourse/mirror", () => {
expect(repo.likes()).toEqual([]);
});
describe("update semantics", () => {
it("only fetches new topics on `update`", async () => {
const {mirror, fetcher, reporter, repo} = example();
fetcher.addPost(1, null);
@ -420,7 +428,7 @@ describe("plugins/discourse/mirror", () => {
// Force the fetcher not to return topic 2
fetcher._latestTopicId--;
await mirror.update(reporter);
const topics = [fetcher._topic(1)];
const topics = normalizeMode1Topics([fetcher._topic(1)]);
expect(repo.topics()).toEqual(topics);
const posts = [fetcher._post(pid1)];
expect(repo.posts()).toEqual(posts);
@ -443,7 +451,7 @@ describe("plugins/discourse/mirror", () => {
// Force the fetcher not to return topic 2
fetcher._latestTopicId--;
await mirror.update(reporter);
const topics = [fetcher._topic(1)];
const topics = normalizeMode1Topics([fetcher._topic(1)]);
expect(repo.topics()).toEqual(topics);
const posts = [pid1, pid3].map((x) => fetcher._post(x));
expect(repo.posts()).toEqual(posts);
@ -461,7 +469,8 @@ describe("plugins/discourse/mirror", () => {
const badLike = {username: "credbot", postId: 37, timestampMs: 0};
fetcher._likes.push(badLike);
await mirror.update(reporter);
expect(repo.topics()).toEqual([fetcher._topic(1)]);
const topics = normalizeMode1Topics([fetcher._topic(1)]);
expect(repo.topics()).toEqual(topics);
expect(repo.posts()).toEqual([fetcher._post(pid)]);
expect(repo.likes()).toEqual([]);
expect(console.warn).toHaveBeenCalledWith(
@ -471,14 +480,13 @@ describe("plugins/discourse/mirror", () => {
expect(console.warn).toHaveBeenCalledTimes(1);
spyWarn().mockReset();
});
});
it("warns if a user's likes are missing", async () => {
const {mirror, fetcher, reporter, repo} = example();
const pid = fetcher.addPost(1, null, "credbot");
(fetcher: any).likesByUser = async () => null;
await mirror.update(reporter);
expect(repo.topics()).toEqual([fetcher._topic(1)]);
expect(repo.topics()).toEqual(normalizeMode1Topics([fetcher._topic(1)]));
expect(repo.posts()).toEqual([fetcher._post(pid)]);
expect(repo.likes()).toEqual([]);
});
@ -497,7 +505,7 @@ describe("plugins/discourse/mirror", () => {
return await _likesByUser(targetUsername, offset);
};
await mirror.update(reporter);
expect(repo.topics()).toEqual([fetcher._topic(1)]);
expect(repo.topics()).toEqual(normalizeMode1Topics([fetcher._topic(1)]));
expect(repo.posts()).toEqual([fetcher._post(p1), fetcher._post(p2)]);
expect(repo.likes()).toEqual([l1]);
});
@ -559,4 +567,5 @@ describe("plugins/discourse/mirror", () => {
expect(repo.findPostInTopic(1, 1)).toBe(undefined);
});
});
});
});