summaryrefslogtreecommitdiff
path: root/packages/backend/test/misc/FakeInternalEventService.ts
blob: d18a080eafe0cc5c2697bb793d782bc9fe3851bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import type { Listener, ListenerProps } from '@/core/InternalEventService.js';
import type Redis from 'ioredis';
import type { GlobalEventService, InternalEventTypes } from '@/core/GlobalEventService.js';
import { InternalEventService } from '@/core/InternalEventService.js';
import { bindThis } from '@/decorators.js';

type FakeCall<K extends keyof InternalEventService> = [K, Parameters<InternalEventService[K]>];
type FakeListener<K extends keyof InternalEventTypes> = [K, Listener<K>, ListenerProps];

/**
 * Minimal implementation of InternalEventService meant for use in unit tests.
 * There is no redis connection, and metadata is tracked in the public _calls and _listeners arrays.
 * The on/off/emit methods are fully functional and can be called in tests to invoke any registered listeners.
 */
export class FakeInternalEventService extends InternalEventService {
	/**
	 * List of calls to public methods, in chronological order.
	 */
	public _calls: FakeCall<keyof InternalEventService>[] = [];

	/**
	 * List of currently registered listeners.
	 */
	public _listeners: FakeListener<keyof InternalEventTypes>[] = [];

	/**
	 * Resets the mock.
	 * Clears all listeners and tracked calls.
	 */
	public _reset() {
		this._calls = [];
		this._listeners = [];
	}

	/**
	 * Simulates a remote event sent from another process in the cluster via redis.
	 */
	@bindThis
	public async _emitRedis<K extends keyof InternalEventTypes>(type: K, value: InternalEventTypes[K]): Promise<void> {
		await this.emit(type, value, false);
	}

	constructor() {
		super(
			{ on: () => {} } as unknown as Redis.Redis,
			{} as unknown as GlobalEventService,
		);
	}

	@bindThis
	public on<K extends keyof InternalEventTypes>(type: K, listener: Listener<K>, props?: ListenerProps): void {
		if (!this._listeners.some(l => l[0] === type && l[1] === listener)) {
			this._listeners.push([type, listener as Listener<keyof InternalEventTypes>, props ?? {}]);
		}
		this._calls.push(['on', [type, listener as Listener<keyof InternalEventTypes>, props]]);
	}

	@bindThis
	public off<K extends keyof InternalEventTypes>(type: K, listener: Listener<K>): void {
		this._listeners = this._listeners.filter(l => l[0] !== type || l[1] !== listener);
		this._calls.push(['off', [type, listener as Listener<keyof InternalEventTypes>]]);
	}

	@bindThis
	public async emit<K extends keyof InternalEventTypes>(type: K, value: InternalEventTypes[K], isLocal = true): Promise<void> {
		for (const listener of this._listeners) {
			if (listener[0] === type) {
				if ((isLocal && !listener[2].ignoreLocal) || (!isLocal && !listener[2].ignoreRemote)) {
					await listener[1](value, type, isLocal);
				}
			}
		}
		this._calls.push(['emit', [type, value]]);
	}

	@bindThis
	public dispose(): void {
		this._listeners = [];
		this._calls.push(['dispose', []]);
	}

	@bindThis
	public onApplicationShutdown(): void {
		this._calls.push(['onApplicationShutdown', []]);
	}
}