2023-04-07 10:56:46 +09:00
|
|
|
import Resolver from "../../src/remote/activitypub/resolver.js";
|
|
|
|
import { IObject } from "../../src/remote/activitypub/type.js";
|
2021-07-10 23:14:57 +09:00
|
|
|
|
|
|
|
type MockResponse = {
|
|
|
|
type: string;
|
|
|
|
content: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export class MockResolver extends Resolver {
|
|
|
|
private _rs = new Map<string, MockResponse>();
|
2023-04-07 10:56:46 +09:00
|
|
|
public async _register(
|
|
|
|
uri: string,
|
|
|
|
content: string | Record<string, any>,
|
|
|
|
type = "application/activity+json",
|
|
|
|
) {
|
2021-07-10 23:14:57 +09:00
|
|
|
this._rs.set(uri, {
|
|
|
|
type,
|
2023-04-07 10:56:46 +09:00
|
|
|
content: typeof content === "string" ? content : JSON.stringify(content),
|
2021-07-10 23:14:57 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async resolve(value: string | IObject): Promise<IObject> {
|
2023-04-07 10:56:46 +09:00
|
|
|
if (typeof value !== "string") return value;
|
2021-07-10 23:14:57 +09:00
|
|
|
|
|
|
|
const r = this._rs.get(value);
|
|
|
|
|
|
|
|
if (!r) {
|
|
|
|
throw {
|
2023-04-07 10:56:46 +09:00
|
|
|
name: "StatusError",
|
2021-07-10 23:14:57 +09:00
|
|
|
statusCode: 404,
|
2023-04-07 10:56:46 +09:00
|
|
|
message: "Not registed for mock",
|
2021-07-10 23:14:57 +09:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const object = JSON.parse(r.content);
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
}
|