summaryrefslogtreecommitdiff
path: root/packages/backend/test
diff options
context:
space:
mode:
authorおさむのひと <46447427+samunohito@users.noreply.github.com>2025-12-06 18:25:20 +0900
committerGitHub <noreply@github.com>2025-12-06 18:25:20 +0900
commitdc77d59f8712d3fe0b73cd4af2035133839cd57b (patch)
treed539d214fda2bcc85e40ac89782fbde0432c4d89 /packages/backend/test
parentBump version to 2025.12.0-beta.0 (diff)
downloadmisskey-dc77d59f8712d3fe0b73cd4af2035133839cd57b.tar.gz
misskey-dc77d59f8712d3fe0b73cd4af2035133839cd57b.tar.bz2
misskey-dc77d59f8712d3fe0b73cd4af2035133839cd57b.zip
Merge commit from fork
Diffstat (limited to 'packages/backend/test')
-rw-r--r--packages/backend/test/unit/misc/should-hide-note-by-time.ts131
1 files changed, 131 insertions, 0 deletions
diff --git a/packages/backend/test/unit/misc/should-hide-note-by-time.ts b/packages/backend/test/unit/misc/should-hide-note-by-time.ts
new file mode 100644
index 0000000000..29cbd751a3
--- /dev/null
+++ b/packages/backend/test/unit/misc/should-hide-note-by-time.ts
@@ -0,0 +1,131 @@
+/*
+ * SPDX-FileCopyrightText: syuilo and misskey-project
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+import { describe, expect, test, beforeEach, afterEach, jest } from '@jest/globals';
+import { shouldHideNoteByTime } from '@/misc/should-hide-note-by-time.js';
+
+describe('misc:should-hide-note-by-time', () => {
+ let now: number;
+
+ beforeEach(() => {
+ now = Date.now();
+ jest.useFakeTimers();
+ jest.setSystemTime(now);
+ });
+
+ afterEach(() => {
+ jest.useRealTimers();
+ });
+
+ describe('hiddenBefore が null または undefined の場合', () => {
+ test('hiddenBefore が null のときは false を返す(非表示機能が有効でない)', () => {
+ const createdAt = new Date(now - 86400000); // 1 day ago
+ expect(shouldHideNoteByTime(null, createdAt)).toBe(false);
+ });
+
+ test('hiddenBefore が undefined のときは false を返す(非表示機能が有効でない)', () => {
+ const createdAt = new Date(now - 86400000); // 1 day ago
+ expect(shouldHideNoteByTime(undefined, createdAt)).toBe(false);
+ });
+ });
+
+ describe('相対時間モード (hiddenBefore <= 0)', () => {
+ test('閾値内に作成されたノートは false を返す(作成からの経過時間がまだ短い→表示)', () => {
+ const hiddenBefore = -86400; // 1 day in seconds
+ const createdAt = new Date(now - 3600000); // 1 hour ago
+ expect(shouldHideNoteByTime(hiddenBefore, createdAt)).toBe(false);
+ });
+
+ test('閾値を超えて作成されたノートは true を返す(指定期間以上経過している→非表示)', () => {
+ const hiddenBefore = -86400; // 1 day in seconds
+ const createdAt = new Date(now - 172800000); // 2 days ago
+ expect(shouldHideNoteByTime(hiddenBefore, createdAt)).toBe(true);
+ });
+
+ test('ちょうど閾値で作成されたノートは true を返す(閾値に達したら非表示)', () => {
+ const hiddenBefore = -86400; // 1 day in seconds
+ const createdAt = new Date(now - 86400000); // exactly 1 day ago
+ expect(shouldHideNoteByTime(hiddenBefore, createdAt)).toBe(true);
+ });
+
+ test('異なる相対時間値で判定できる(1時間設定と3時間設定の異なる結果)', () => {
+ const createdAt = new Date(now - 7200000); // 2 hours ago
+ expect(shouldHideNoteByTime(-3600, createdAt)).toBe(true); // 1時間経過→非表示
+ expect(shouldHideNoteByTime(-10800, createdAt)).toBe(false); // 3時間未経過→表示
+ });
+
+ test('ISO 8601 形式の文字列の createdAt に対応できる(文字列でも正しく判定)', () => {
+ const createdAtString = new Date(now - 86400000).toISOString();
+ const hiddenBefore = -86400; // 1 day in seconds
+ expect(shouldHideNoteByTime(hiddenBefore, createdAtString)).toBe(true);
+ });
+
+ test('hiddenBefore が 0 の場合に対応できる(0秒以上経過で非表示→ほぼ全て非表示)', () => {
+ const hiddenBefore = 0;
+ const createdAt = new Date(now - 1); // 1ms ago
+ expect(shouldHideNoteByTime(hiddenBefore, createdAt)).toBe(true);
+ });
+ });
+
+ describe('絶対時間モード (hiddenBefore > 0)', () => {
+ test('閾値タイムスタンプより後に作成されたノートは false を返す(指定日時より後→表示)', () => {
+ const thresholdSeconds = Math.floor(now / 1000);
+ const createdAt = new Date(now + 3600000); // 1 hour from now
+ expect(shouldHideNoteByTime(thresholdSeconds, createdAt)).toBe(false);
+ });
+
+ test('閾値タイムスタンプより前に作成されたノートは true を返す(指定日時より前→非表示)', () => {
+ const thresholdSeconds = Math.floor(now / 1000);
+ const createdAt = new Date(now - 3600000); // 1 hour ago
+ expect(shouldHideNoteByTime(thresholdSeconds, createdAt)).toBe(true);
+ });
+
+ test('ちょうど閾値タイムスタンプで作成されたノートは true を返す(指定日時に達したら非表示)', () => {
+ const thresholdSeconds = Math.floor(now / 1000);
+ const createdAt = new Date(now); // exactly now
+ expect(shouldHideNoteByTime(thresholdSeconds, createdAt)).toBe(true);
+ });
+
+ test('ISO 8601 形式の文字列の createdAt に対応できる(文字列でも正しく判定)', () => {
+ const thresholdSeconds = Math.floor(now / 1000);
+ const createdAtString = new Date(now - 3600000).toISOString();
+ expect(shouldHideNoteByTime(thresholdSeconds, createdAtString)).toBe(true);
+ });
+
+ test('異なる閾値タイムスタンプで判定できる(2021年設定と現在より1時間前設定の異なる結果)', () => {
+ const thresholdSeconds = Math.floor((now - 86400000) / 1000); // 1 day ago
+ const createdAtBefore = new Date(now - 172800000); // 2 days ago
+ const createdAtAfter = new Date(now - 3600000); // 1 hour ago
+ expect(shouldHideNoteByTime(thresholdSeconds, createdAtBefore)).toBe(true); // 閾値より前→非表示
+ expect(shouldHideNoteByTime(thresholdSeconds, createdAtAfter)).toBe(false); // 閾値より後→表示
+ });
+ });
+
+ describe('エッジケース', () => {
+ test('相対時間モードで非常に古いノートに対応できる(非常に古い→閾値超→非表示)', () => {
+ const hiddenBefore = -1; // hide notes older than 1 second
+ const createdAt = new Date(now - 1000000); // very old
+ expect(shouldHideNoteByTime(hiddenBefore, createdAt)).toBe(true);
+ });
+
+ test('相対時間モードで非常に新しいノートに対応できる(非常に新しい→閾値未満→表示)', () => {
+ const hiddenBefore = -86400; // 1 day
+ const createdAt = new Date(now - 1); // 1ms ago
+ expect(shouldHideNoteByTime(hiddenBefore, createdAt)).toBe(false);
+ });
+
+ test('大きなタイムスタンプ値に対応できる(未来の日時を指定→現在のノートは全て非表示)', () => {
+ const thresholdSeconds = Math.floor(now / 1000) + 86400; // 1 day from now
+ const createdAt = new Date(now); // created now
+ expect(shouldHideNoteByTime(thresholdSeconds, createdAt)).toBe(true);
+ });
+
+ test('小さな相対時間値に対応できる(1秒設定で2秒前→非表示)', () => {
+ const hiddenBefore = -1; // 1 second
+ const createdAt = new Date(now - 2000); // 2 seconds ago
+ expect(shouldHideNoteByTime(hiddenBefore, createdAt)).toBe(true);
+ });
+ });
+});