refactor(server): mail service (#10934)

This commit is contained in:
forehalo
2025-03-19 17:00:19 +00:00
parent b3a245f47a
commit 21c4a29f55
47 changed files with 2076 additions and 2131 deletions
@@ -10,8 +10,10 @@ import supertest from 'supertest';
import { AFFiNELogger, ApplyType, GlobalExceptionFilter } from '../../base';
import { AuthService } from '../../core/auth';
import { Mailer } from '../../core/mail';
import { UserModel } from '../../models';
import { createFactory, MockedUser, MockUser, MockUserInput } from '../mocks';
import { MockMailer } from '../mocks/mailer.mock';
import { createTestingModule } from './testing-module';
import { initTestingDB, TEST_LOG_LEVEL } from './utils';
@@ -82,6 +84,7 @@ export class TestingApp extends ApplyType<INestApplication>() {
private readonly userCookies: Set<string> = new Set();
readonly create!: ReturnType<typeof createFactory>;
readonly mails!: MockMailer;
[Symbol.asyncDispose](): Promise<void> {
return this.close();
@@ -280,6 +283,8 @@ function makeTestingApp(app: INestApplication): TestingApp {
// @ts-expect-error allow
testingApp.create = createFactory(app.get(PrismaClient, { strict: false }));
// @ts-expect-error allow
testingApp.mails = app.get(Mailer, { strict: false }) as MockMailer;
return new Proxy(testingApp, {
get(target, prop) {
@@ -12,11 +12,13 @@ import { AppModule, FunctionalityModules } from '../../app.module';
import { AFFiNELogger, Runtime } from '../../base';
import { GqlModule } from '../../base/graphql';
import { AuthGuard, AuthModule } from '../../core/auth';
import { Mailer, MailModule } from '../../core/mail';
import { ModelsModule } from '../../models';
// for jsdoc inference
// oxlint-disable-next-line no-unused-vars
import type { createModule } from '../create-module';
import { createFactory } from '../mocks';
import { MockMailer } from '../mocks/mailer.mock';
import { initTestingDB, TEST_LOG_LEVEL } from './utils';
interface TestingModuleMeatdata extends ModuleMetadata {
@@ -26,6 +28,7 @@ interface TestingModuleMeatdata extends ModuleMetadata {
export interface TestingModule extends BaseTestingModule {
initTestingDB(): Promise<void>;
create: ReturnType<typeof createFactory>;
mails: MockMailer;
[Symbol.asyncDispose](): Promise<void>;
}
@@ -68,6 +71,7 @@ export async function createTestingModule(
ModelsModule,
AuthModule,
GqlModule,
MailModule,
...imports,
]);
@@ -87,6 +91,7 @@ export async function createTestingModule(
if (moduleDef.tapModule) {
moduleDef.tapModule(builder);
}
builder.overrideProvider(Mailer).useClass(MockMailer);
const module = await builder.compile();
@@ -108,6 +113,8 @@ export async function createTestingModule(
await module.close();
};
testingModule.mails = module.get(Mailer, { strict: false }) as MockMailer;
const logger = new AFFiNELogger();
// we got a lot smoking tests try to break nestjs
// can't tolerate the noisy logs