From 9e3a4418563f308565c6bacb092dd72fc7a757a8 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 18 Aug 2023 10:04:02 +0100 Subject: [PATCH] Fix tests --- src/DateUtils.ts | 8 +- .../views/dialogs/ForwardDialog.tsx | 1 + src/languageHandler.tsx | 2 +- src/utils/exportUtils/Exporter.ts | 99 +++++++++---------- .../__snapshots__/MatrixChat-test.tsx.snap | 2 +- .../views/rooms/SearchResultTile-test.tsx | 2 +- .../settings/devices/DeviceDetails-test.tsx | 1 + test/test-utils/beacon.ts | 1 + test/test-utils/location.ts | 2 + 9 files changed, 61 insertions(+), 57 deletions(-) diff --git a/src/DateUtils.ts b/src/DateUtils.ts index 28743d2a3918..5f0b6babc82e 100644 --- a/src/DateUtils.ts +++ b/src/DateUtils.ts @@ -25,7 +25,7 @@ const MILLIS_IN_DAY = 86400000; export function getDaysArray(weekday: Intl.DateTimeFormatOptions["weekday"] = "short"): string[] { const now = new Date(); const { format } = new Intl.DateTimeFormat(getUserLanguage(), { weekday }); - return [...Array(7).keys()].map((day) => format(new Date().getTime() - (now.getDay() - day) * MILLIS_IN_DAY)); + return [...Array(7).keys()].map((day) => format(now.getTime() - (now.getDay() - day) * MILLIS_IN_DAY)); } function getMonthsArray(month: Intl.DateTimeFormatOptions["month"] = "short"): string[] { @@ -44,7 +44,7 @@ export function formatDate(date: Date, showTwelveHour = false, locale?: string): hour: "numeric", minute: "2-digit", hour12: showTwelveHour, - }).format(new Date()); + }).format(date); } else if (now.getFullYear() === date.getFullYear()) { return new Intl.DateTimeFormat(_locale, { weekday: "short", @@ -53,7 +53,7 @@ export function formatDate(date: Date, showTwelveHour = false, locale?: string): hour: "numeric", minute: "2-digit", hour12: showTwelveHour, - }).format(new Date()); + }).format(date); } return formatFullDate(date, showTwelveHour, true, _locale); } @@ -214,7 +214,7 @@ export function formatFullDateNoDayNoTime(date: Date, locale?: string): string { } export function formatRelativeTime(date: Date, showTwelveHour = false): string { - const now = new Date(Date.now()); + const now = new Date(); if (withinCurrentDay(date, now)) { return formatTime(date, showTwelveHour); } else { diff --git a/src/components/views/dialogs/ForwardDialog.tsx b/src/components/views/dialogs/ForwardDialog.tsx index 13d84b99e7e0..a9b351be309a 100644 --- a/src/components/views/dialogs/ForwardDialog.tsx +++ b/src/components/views/dialogs/ForwardDialog.tsx @@ -216,6 +216,7 @@ const ForwardDialog: React.FC = ({ matrixClient: cli, event, permalinkCr }, event_id: "$9999999999999999999999999999999999999999999", room_id: event.getRoomId(), + origin_server_ts: event.getTs(), }); mockEvent.sender = { name: profileInfo.displayname || userId, diff --git a/src/languageHandler.tsx b/src/languageHandler.tsx index c8a8c1d4b99f..3f0a6ca4cdbe 100644 --- a/src/languageHandler.tsx +++ b/src/languageHandler.tsx @@ -93,7 +93,7 @@ export class UserFriendlyError extends Error { export function getUserLanguage(): string { const language = SettingsStore.getValue("language", null, /*excludeDefault:*/ true); - if (language) { + if (typeof language === "string" && !!language) { return language; } else { return normalizeLanguageKey(getLanguageFromBrowser()); diff --git a/src/utils/exportUtils/Exporter.ts b/src/utils/exportUtils/Exporter.ts index 39ab352b94fe..5f944055683a 100644 --- a/src/utils/exportUtils/Exporter.ts +++ b/src/utils/exportUtils/Exporter.ts @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { MatrixEvent, Room, Direction } from "matrix-js-sdk/src/matrix"; +import { Direction, MatrixEvent, Room } from "matrix-js-sdk/src/matrix"; import { saveAs } from "file-saver"; import { logger } from "matrix-js-sdk/src/logger"; import sanitizeFilename from "sanitize-filename"; @@ -135,9 +135,6 @@ export default abstract class Exporter { // when export type is LastNMessages limit = this.exportOptions.numberOfMessages!; break; - case ExportType.Timeline: - limit = 40; - break; default: limit = 10 ** 8; } @@ -148,58 +145,60 @@ export default abstract class Exporter { const eventMapper = this.room.client.getEventMapper(); let prevToken: string | null = null; - let limit = this.getLimit(); - const events: MatrixEvent[] = []; - - while (limit) { - const eventsPerCrawl = Math.min(limit, 1000); - const res = await this.room.client.createMessagesRequest( - this.room.roomId, - prevToken, - eventsPerCrawl, - Direction.Backward, - ); - - if (this.cancelled) { - this.cleanUp(); - return []; - } - if (res.chunk.length === 0) break; + let events: MatrixEvent[] = []; + if (this.exportType === ExportType.Timeline) { + events = this.room.getLiveTimeline().getEvents(); + } else { + let limit = this.getLimit(); + while (limit) { + const eventsPerCrawl = Math.min(limit, 1000); + const res = await this.room.client.createMessagesRequest( + this.room.roomId, + prevToken, + eventsPerCrawl, + Direction.Backward, + ); - limit -= res.chunk.length; + if (this.cancelled) { + this.cleanUp(); + return []; + } - const matrixEvents: MatrixEvent[] = res.chunk.map(eventMapper); + if (res.chunk.length === 0) break; - for (const mxEv of matrixEvents) { - // if (this.exportOptions.startDate && mxEv.getTs() < this.exportOptions.startDate) { - // // Once the last message received is older than the start date, we break out of both the loops - // limit = 0; - // break; - // } - events.push(mxEv); - } + limit -= res.chunk.length; - if (this.exportType === ExportType.LastNMessages) { - this.updateProgress( - _t("Fetched %(count)s events out of %(total)s", { - count: events.length, - total: this.exportOptions.numberOfMessages, - }), - ); - } else { - this.updateProgress( - _t("Fetched %(count)s events so far", { - count: events.length, - }), - ); - } + const matrixEvents: MatrixEvent[] = res.chunk.map(eventMapper); - prevToken = res.end ?? null; - } - // Reverse the events so that we preserve the order - for (let i = 0; i < Math.floor(events.length / 2); i++) { - [events[i], events[events.length - i - 1]] = [events[events.length - i - 1], events[i]]; + for (const mxEv of matrixEvents) { + // if (this.exportOptions.startDate && mxEv.getTs() < this.exportOptions.startDate) { + // // Once the last message received is older than the start date, we break out of both the loops + // limit = 0; + // break; + // } + events.push(mxEv); + } + + if (this.exportType === ExportType.LastNMessages) { + this.updateProgress( + _t("Fetched %(count)s events out of %(total)s", { + count: events.length, + total: this.exportOptions.numberOfMessages, + }), + ); + } else { + this.updateProgress( + _t("Fetched %(count)s events so far", { + count: events.length, + }), + ); + } + + prevToken = res.end ?? null; + } + // Reverse the events so that we preserve the order + events.reverse(); } const decryptionPromises = events diff --git a/test/components/structures/__snapshots__/MatrixChat-test.tsx.snap b/test/components/structures/__snapshots__/MatrixChat-test.tsx.snap index 5bd7b22f4e4f..9b8a77dfe887 100644 --- a/test/components/structures/__snapshots__/MatrixChat-test.tsx.snap +++ b/test/components/structures/__snapshots__/MatrixChat-test.tsx.snap @@ -37,7 +37,7 @@ exports[` with a soft-logged-out session should show the soft-logo Matrix
{ room_id: ROOM_ID, content: { body: `Message #${i}` }, event_id: `$${i}:server`, - origin_server_ts: undefined, + origin_server_ts: i, }), ), }); diff --git a/test/components/views/settings/devices/DeviceDetails-test.tsx b/test/components/views/settings/devices/DeviceDetails-test.tsx index a3c359cdf50e..03068b0b3d1d 100644 --- a/test/components/views/settings/devices/DeviceDetails-test.tsx +++ b/test/components/views/settings/devices/DeviceDetails-test.tsx @@ -45,6 +45,7 @@ describe("", () => { beforeEach(() => { jest.setSystemTime(now); + jest.spyOn(Date, "now").mockImplementation(() => now); }); it("renders device without metadata", () => { diff --git a/test/test-utils/beacon.ts b/test/test-utils/beacon.ts index 0cea7820bc04..e519e791d401 100644 --- a/test/test-utils/beacon.ts +++ b/test/test-utils/beacon.ts @@ -103,6 +103,7 @@ export const makeBeaconEvent = ( room_id: roomId, sender, content: ContentHelpers.makeBeaconContent(geoUri, timestamp, beaconInfoId, description), + origin_server_ts: 0, }); }; diff --git a/test/test-utils/location.ts b/test/test-utils/location.ts index 548e13136040..4d22d98364c6 100644 --- a/test/test-utils/location.ts +++ b/test/test-utils/location.ts @@ -27,6 +27,7 @@ export const makeLegacyLocationEvent = (geoUri: string): MatrixEvent => { msgtype: "m.location", geo_uri: geoUri, }, + origin_server_ts: 0, }); }; @@ -41,6 +42,7 @@ export const makeLocationEvent = (geoUri: string, assetType?: LocationAssetType) "Human-readable label", assetType, ), + origin_server_ts: 0, }); };