-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnnouncementService.ts
210 lines (182 loc) · 6.58 KB
/
AnnouncementService.ts
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
process.env.NODE_ENV = 'test';
import { jest } from '@jest/globals';
import { ModuleMocker } from 'jest-mock';
import { Test } from '@nestjs/testing';
import { GlobalModule } from '@/GlobalModule.js';
import { AnnouncementService } from '@/core/AnnouncementService.js';
import { AnnouncementEntityService } from '@/core/entities/AnnouncementEntityService.js';
import type {
AnnouncementReadsRepository,
AnnouncementsRepository,
MiAnnouncement,
MiUser,
UsersRepository,
} from '@/models/_.js';
import { DI } from '@/di-symbols.js';
import { genAidx } from '@/misc/id/aidx.js';
import { CacheService } from '@/core/CacheService.js';
import { IdService } from '@/core/IdService.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { ModerationLogService } from '@/core/ModerationLogService.js';
import { secureRndstr } from '@/misc/secure-rndstr.js';
import type { TestingModule } from '@nestjs/testing';
import type { MockFunctionMetadata } from 'jest-mock';
const moduleMocker = new ModuleMocker(global);
describe('AnnouncementService', () => {
let app: TestingModule;
let announcementService: AnnouncementService;
let usersRepository: UsersRepository;
let announcementsRepository: AnnouncementsRepository;
let announcementReadsRepository: AnnouncementReadsRepository;
let globalEventService: jest.Mocked<GlobalEventService>;
let moderationLogService: jest.Mocked<ModerationLogService>;
function createUser(data: Partial<MiUser> = {}) {
const un = secureRndstr(16);
return usersRepository.insert({
id: genAidx(Date.now()),
username: un,
usernameLower: un,
...data,
})
.then(x => usersRepository.findOneByOrFail(x.identifiers[0]));
}
function createAnnouncement(data: Partial<MiAnnouncement & { createdAt: Date }> = {}) {
return announcementsRepository.insert({
id: genAidx(data.createdAt?.getTime() ?? Date.now()),
updatedAt: null,
title: 'Title',
text: 'Text',
...data,
})
.then(x => announcementsRepository.findOneByOrFail(x.identifiers[0]));
}
beforeEach(async () => {
app = await Test.createTestingModule({
imports: [
GlobalModule,
],
providers: [
AnnouncementService,
AnnouncementEntityService,
CacheService,
IdService,
],
})
.useMocker((token) => {
if (token === GlobalEventService) {
return {
publishMainStream: jest.fn(),
publishBroadcastStream: jest.fn(),
};
} else if (token === ModerationLogService) {
return {
log: jest.fn(),
};
} else if (typeof token === 'function') {
const mockMetadata = moduleMocker.getMetadata(token) as MockFunctionMetadata<any, any>;
const Mock = moduleMocker.generateFromMetadata(mockMetadata);
return new Mock();
}
})
.compile();
app.enableShutdownHooks();
announcementService = app.get<AnnouncementService>(AnnouncementService);
usersRepository = app.get<UsersRepository>(DI.usersRepository);
announcementsRepository = app.get<AnnouncementsRepository>(DI.announcementsRepository);
announcementReadsRepository = app.get<AnnouncementReadsRepository>(DI.announcementReadsRepository);
globalEventService = app.get<GlobalEventService>(GlobalEventService) as jest.Mocked<GlobalEventService>;
moderationLogService = app.get<ModerationLogService>(ModerationLogService) as jest.Mocked<ModerationLogService>;
});
afterEach(async () => {
await Promise.all([
app.get(DI.metasRepository).delete({}),
usersRepository.delete({}),
announcementsRepository.delete({}),
announcementReadsRepository.delete({}),
]);
await app.close();
});
describe('getUnreadAnnouncements', () => {
test('通常', async () => {
const user = await createUser();
const announcement = await createAnnouncement({
title: '1',
});
const result = await announcementService.getUnreadAnnouncements(user);
expect(result.length).toBe(1);
expect(result[0].title).toBe(announcement.title);
});
test('isActiveがfalseは除外', async () => {
const user = await createUser();
await createAnnouncement({
isActive: false,
});
const result = await announcementService.getUnreadAnnouncements(user);
expect(result.length).toBe(0);
});
test('forExistingUsers', async () => {
const user = await createUser();
const [announcementAfter, announcementBefore, announcementBefore2] = await Promise.all([
createAnnouncement({
title: 'after',
createdAt: new Date(),
forExistingUsers: true,
}),
createAnnouncement({
title: 'before',
createdAt: new Date(Date.now() - 1000),
forExistingUsers: true,
}),
createAnnouncement({
title: 'before2',
createdAt: new Date(Date.now() - 1000),
forExistingUsers: false,
}),
]);
const result = await announcementService.getUnreadAnnouncements(user);
expect(result.length).toBe(2);
expect(result.some(a => a.title === announcementAfter.title)).toBe(true);
expect(result.some(a => a.title === announcementBefore.title)).toBe(false);
expect(result.some(a => a.title === announcementBefore2.title)).toBe(true);
});
});
describe('create', () => {
test('通常', async () => {
const me = await createUser();
const result = await announcementService.create({
title: 'Title',
text: 'Text',
}, me);
expect(result.raw.title).toBe('Title');
expect(result.packed.title).toBe('Title');
expect(globalEventService.publishBroadcastStream).toHaveBeenCalled();
expect(globalEventService.publishBroadcastStream.mock.lastCall![0]).toBe('announcementCreated');
expect((globalEventService.publishBroadcastStream.mock.lastCall![1] as any).announcement).toBe(result.packed);
expect(moderationLogService.log).toHaveBeenCalled();
});
test('ユーザー指定', async () => {
const me = await createUser();
const user = await createUser();
const result = await announcementService.create({
title: 'Title',
text: 'Text',
userId: user.id,
}, me);
expect(result.raw.title).toBe('Title');
expect(result.packed.title).toBe('Title');
expect(globalEventService.publishBroadcastStream).not.toHaveBeenCalled();
expect(globalEventService.publishMainStream).toHaveBeenCalled();
expect(globalEventService.publishMainStream.mock.lastCall![0]).toBe(user.id);
expect(globalEventService.publishMainStream.mock.lastCall![1]).toBe('announcementCreated');
expect((globalEventService.publishMainStream.mock.lastCall![2] as any).announcement).toBe(result.packed);
expect(moderationLogService.log).toHaveBeenCalled();
});
});
describe('read', () => {
// TODO
});
});