1
1
mirror of https://github.com/kokonect-link/cherrypick synced 2024-12-17 16:18:27 +09:00
cherrypick/packages/backend/test/misc/mock-resolver.ts
syuilo 023fa30280
refactor/perf(backend): provide metadata statically (#14601)
* wip

* Update ReactionService.ts

* Update ApiCallService.ts

* Update timeline.ts

* Update GlobalModule.ts

* Update GlobalModule.ts

* Update NoteEntityService.ts

* wip

* wip

* wip

* Update ApPersonService.ts

* wip

* Update GlobalModule.ts

* Update mock-resolver.ts

* Update RoleService.ts

* Update activitypub.ts

* Update activitypub.ts

* Update activitypub.ts

* Update activitypub.ts

* Update activitypub.ts

* clean up

* Update utils.ts

* Update UtilityService.ts

* Revert "Update utils.ts"

This reverts commit a27d4be764b78c1b5a9eac685e261fee49331d89.

* Revert "Update UtilityService.ts"

This reverts commit e5fd9e004c482cf099252201c0c1aa888e001430.

* vuwa-

* Revert "vuwa-"

This reverts commit 0c3bd12472b4b9938cdff2d6f131e6800bc3724c.

* Update entry.ts

* Update entry.ts

* Update entry.ts

* Update entry.ts

* Update jest.setup.ts
2024-09-22 12:53:13 +09:00

87 lines
2.4 KiB
TypeScript

/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import type { Config } from '@/config.js';
import type { ApDbResolverService } from '@/core/activitypub/ApDbResolverService.js';
import type { ApRendererService } from '@/core/activitypub/ApRendererService.js';
import type { ApRequestService } from '@/core/activitypub/ApRequestService.js';
import { Resolver } from '@/core/activitypub/ApResolverService.js';
import type { IObject } from '@/core/activitypub/type.js';
import type { HttpRequestService } from '@/core/HttpRequestService.js';
import type { InstanceActorService } from '@/core/InstanceActorService.js';
import type { LoggerService } from '@/core/LoggerService.js';
import type { MetaService } from '@/core/MetaService.js';
import type { UtilityService } from '@/core/UtilityService.js';
import { bindThis } from '@/decorators.js';
import type {
FollowRequestsRepository,
MiMeta,
NoteReactionsRepository,
NotesRepository,
PollsRepository,
UsersRepository,
} from '@/models/_.js';
type MockResponse = {
type: string;
content: string;
};
export class MockResolver extends Resolver {
#responseMap = new Map<string, MockResponse>();
#remoteGetTrials: string[] = [];
constructor(loggerService: LoggerService) {
super(
{} as Config,
{} as MiMeta,
{} as UsersRepository,
{} as NotesRepository,
{} as PollsRepository,
{} as NoteReactionsRepository,
{} as FollowRequestsRepository,
{} as UtilityService,
{} as InstanceActorService,
{} as ApRequestService,
{} as HttpRequestService,
{} as ApRendererService,
{} as ApDbResolverService,
loggerService,
);
}
public register(uri: string, content: string | Record<string, any>, type = 'application/activity+json'): void {
this.#responseMap.set(uri, {
type,
content: typeof content === 'string' ? content : JSON.stringify(content),
});
}
public clear(): void {
this.#responseMap.clear();
this.#remoteGetTrials.length = 0;
}
public remoteGetTrials(): string[] {
return this.#remoteGetTrials;
}
@bindThis
public async resolve(value: string | IObject): Promise<IObject> {
if (typeof value !== 'string') return value;
this.#remoteGetTrials.push(value);
const r = this.#responseMap.get(value);
if (!r) {
throw new Error('Not registed for mock');
}
const object = JSON.parse(r.content);
return object;
}
}