2021-07-10 14:14:57 +00:00
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
|
|
|
import * as assert from 'assert';
|
2023-02-19 06:27:14 +00:00
|
|
|
import { Test } from '@nestjs/testing';
|
|
|
|
import { jest } from '@jest/globals';
|
|
|
|
|
|
|
|
import { ApNoteService } from '@/core/activitypub/models/ApNoteService.js';
|
|
|
|
import { ApPersonService } from '@/core/activitypub/models/ApPersonService.js';
|
2023-03-12 03:11:37 +00:00
|
|
|
import { ApRendererService } from '@/core/activitypub/ApRendererService.js';
|
2023-02-19 06:27:14 +00:00
|
|
|
import { GlobalModule } from '@/GlobalModule.js';
|
|
|
|
import { CoreModule } from '@/core/CoreModule.js';
|
|
|
|
import { FederatedInstanceService } from '@/core/FederatedInstanceService.js';
|
|
|
|
import { LoggerService } from '@/core/LoggerService.js';
|
2023-07-05 01:38:59 +00:00
|
|
|
import type { IActor, ICreate, IObject, IOrderedCollection, IOrderedCollectionPage, IPost } from '@/core/activitypub/type.js';
|
2023-03-12 03:11:37 +00:00
|
|
|
import { Note } from '@/models/index.js';
|
2023-06-25 02:04:33 +00:00
|
|
|
import { secureRndstr } from '@/misc/secure-rndstr.js';
|
|
|
|
import { MockResolver } from '../misc/mock-resolver.js';
|
2021-07-10 14:14:57 +00:00
|
|
|
|
2023-02-19 08:49:18 +00:00
|
|
|
const host = 'https://host1.test';
|
|
|
|
|
|
|
|
function createRandomActor(): IActor & { id: string } {
|
2023-06-25 02:04:33 +00:00
|
|
|
const preferredUsername = secureRndstr(8);
|
2023-02-19 08:49:18 +00:00
|
|
|
const actorId = `${host}/users/${preferredUsername.toLowerCase()}`;
|
|
|
|
|
|
|
|
return {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
id: actorId,
|
|
|
|
type: 'Person',
|
|
|
|
preferredUsername,
|
|
|
|
inbox: `${actorId}/inbox`,
|
|
|
|
outbox: `${actorId}/outbox`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-07-05 01:38:59 +00:00
|
|
|
function createRandomCreateActivity(actor: IActor, length: number): ICreate[] {
|
|
|
|
return new Array(length).fill(null).map((): ICreate => {
|
|
|
|
const id = secureRndstr(8);
|
|
|
|
const noteId = `${host}/notes/${id}`;
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'Create',
|
|
|
|
id: `${noteId}/activity`,
|
|
|
|
actor,
|
|
|
|
object: {
|
|
|
|
id: noteId,
|
|
|
|
type: 'Note',
|
|
|
|
attributedTo: actor.id,
|
|
|
|
content: 'test test foo',
|
|
|
|
} satisfies IPost,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function createRandomNonPagedOutbox(actor: IActor, length: number): IOrderedCollection {
|
|
|
|
const orderedItems = createRandomCreateActivity(actor, length);
|
|
|
|
|
|
|
|
return {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
type: 'OrderedCollection',
|
|
|
|
id: actor.outbox as string,
|
|
|
|
totalItems: orderedItems.length,
|
|
|
|
orderedItems,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function createRandomOutboxPage(actor: IActor, id: string, length: number): IOrderedCollectionPage {
|
|
|
|
const orderedItems = createRandomCreateActivity(actor, length);
|
|
|
|
|
|
|
|
return {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
type: 'OrderedCollectionPage',
|
|
|
|
id,
|
|
|
|
totalItems: orderedItems.length,
|
|
|
|
orderedItems,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function createRandomPagedOutbox(actor: IActor): IOrderedCollection {
|
|
|
|
return {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
type: 'OrderedCollection',
|
|
|
|
id: actor.outbox as string,
|
|
|
|
totalItems: 10,
|
|
|
|
first: `${actor.outbox}?first`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-10 14:14:57 +00:00
|
|
|
describe('ActivityPub', () => {
|
2023-02-19 06:27:14 +00:00
|
|
|
let noteService: ApNoteService;
|
|
|
|
let personService: ApPersonService;
|
2023-03-12 03:11:37 +00:00
|
|
|
let rendererService: ApRendererService;
|
2023-02-19 06:27:14 +00:00
|
|
|
let resolver: MockResolver;
|
|
|
|
|
2023-07-08 13:40:48 +00:00
|
|
|
beforeAll(async () => {
|
2023-02-19 06:27:14 +00:00
|
|
|
const app = await Test.createTestingModule({
|
|
|
|
imports: [GlobalModule, CoreModule],
|
|
|
|
}).compile();
|
|
|
|
|
|
|
|
await app.init();
|
|
|
|
app.enableShutdownHooks();
|
|
|
|
|
|
|
|
noteService = app.get<ApNoteService>(ApNoteService);
|
|
|
|
personService = app.get<ApPersonService>(ApPersonService);
|
2023-03-12 03:11:37 +00:00
|
|
|
rendererService = app.get<ApRendererService>(ApRendererService);
|
2023-02-19 06:27:14 +00:00
|
|
|
resolver = new MockResolver(await app.resolve<LoggerService>(LoggerService));
|
|
|
|
|
|
|
|
// Prevent ApPersonService from fetching instance, as it causes Jest import-after-test error
|
|
|
|
const federatedInstanceService = app.get<FederatedInstanceService>(FederatedInstanceService);
|
2023-07-05 01:38:59 +00:00
|
|
|
jest.spyOn(federatedInstanceService, 'fetch').mockImplementation(() => new Promise(() => { }));
|
2023-02-19 06:27:14 +00:00
|
|
|
});
|
|
|
|
|
2023-07-08 13:40:48 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
resolver.clear();
|
|
|
|
});
|
|
|
|
|
2021-07-10 14:14:57 +00:00
|
|
|
describe('Parse minimum object', () => {
|
2023-02-19 08:49:18 +00:00
|
|
|
const actor = createRandomActor();
|
2021-07-10 14:14:57 +00:00
|
|
|
|
|
|
|
const post = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
2023-06-25 02:04:33 +00:00
|
|
|
id: `${host}/users/${secureRndstr(8)}`,
|
2021-07-10 14:14:57 +00:00
|
|
|
type: 'Note',
|
|
|
|
attributedTo: actor.id,
|
|
|
|
to: 'https://www.w3.org/ns/activitystreams#Public',
|
|
|
|
content: 'あ',
|
|
|
|
};
|
|
|
|
|
2023-02-02 09:18:25 +00:00
|
|
|
test('Minimum Actor', async () => {
|
2021-07-10 14:14:57 +00:00
|
|
|
resolver._register(actor.id, actor);
|
|
|
|
|
2023-02-19 06:27:14 +00:00
|
|
|
const user = await personService.createPerson(actor.id, resolver);
|
2021-07-10 14:14:57 +00:00
|
|
|
|
|
|
|
assert.deepStrictEqual(user.uri, actor.id);
|
|
|
|
assert.deepStrictEqual(user.username, actor.preferredUsername);
|
|
|
|
assert.deepStrictEqual(user.inbox, actor.inbox);
|
|
|
|
});
|
|
|
|
|
2023-02-02 09:18:25 +00:00
|
|
|
test('Minimum Note', async () => {
|
2021-07-10 14:14:57 +00:00
|
|
|
resolver._register(actor.id, actor);
|
|
|
|
resolver._register(post.id, post);
|
|
|
|
|
2023-02-19 06:27:14 +00:00
|
|
|
const note = await noteService.createNote(post.id, resolver, true);
|
2021-07-10 14:14:57 +00:00
|
|
|
|
|
|
|
assert.deepStrictEqual(note?.uri, post.id);
|
2022-05-21 13:21:41 +00:00
|
|
|
assert.deepStrictEqual(note.visibility, 'public');
|
|
|
|
assert.deepStrictEqual(note.text, post.content);
|
2021-07-10 14:14:57 +00:00
|
|
|
});
|
|
|
|
});
|
2021-08-17 08:25:19 +00:00
|
|
|
|
2023-02-19 08:49:18 +00:00
|
|
|
describe('Name field', () => {
|
|
|
|
test('Truncate long name', async () => {
|
|
|
|
const actor = {
|
|
|
|
...createRandomActor(),
|
2023-06-25 02:04:33 +00:00
|
|
|
name: secureRndstr(129),
|
2023-02-19 08:49:18 +00:00
|
|
|
};
|
2021-08-17 08:25:19 +00:00
|
|
|
|
2023-02-19 08:49:18 +00:00
|
|
|
resolver._register(actor.id, actor);
|
2021-08-17 08:25:19 +00:00
|
|
|
|
2023-02-19 08:49:18 +00:00
|
|
|
const user = await personService.createPerson(actor.id, resolver);
|
|
|
|
|
|
|
|
assert.deepStrictEqual(user.name, actor.name.slice(0, 128));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Normalize empty name', async () => {
|
|
|
|
const actor = {
|
|
|
|
...createRandomActor(),
|
|
|
|
name: '',
|
|
|
|
};
|
2021-08-17 08:25:19 +00:00
|
|
|
|
|
|
|
resolver._register(actor.id, actor);
|
|
|
|
|
2023-02-19 06:27:14 +00:00
|
|
|
const user = await personService.createPerson(actor.id, resolver);
|
2021-08-17 08:25:19 +00:00
|
|
|
|
2023-02-19 08:49:18 +00:00
|
|
|
assert.strictEqual(user.name, null);
|
2021-08-17 08:25:19 +00:00
|
|
|
});
|
|
|
|
});
|
2023-03-12 03:11:37 +00:00
|
|
|
|
|
|
|
describe('Renderer', () => {
|
|
|
|
test('Render an announce with visibility: followers', () => {
|
|
|
|
rendererService.renderAnnounce(null, {
|
|
|
|
createdAt: new Date(0),
|
|
|
|
visibility: 'followers',
|
|
|
|
} as Note);
|
|
|
|
});
|
|
|
|
});
|
2023-07-05 01:38:59 +00:00
|
|
|
|
|
|
|
describe('Outbox', () => {
|
|
|
|
test('Fetch non-paged outbox from IActor', async () => {
|
|
|
|
const actor = createRandomActor();
|
|
|
|
const outbox = createRandomNonPagedOutbox(actor, 10);
|
|
|
|
|
|
|
|
resolver._register(actor.id, actor);
|
|
|
|
resolver._register(actor.outbox as string, outbox);
|
|
|
|
|
|
|
|
await personService.createPerson(actor.id, resolver);
|
|
|
|
|
|
|
|
for (const item of outbox.orderedItems as ICreate[]) {
|
|
|
|
const note = await noteService.fetchNote(item.object);
|
|
|
|
assert.ok(note);
|
|
|
|
assert.strictEqual(note.text, 'test test foo');
|
|
|
|
assert.strictEqual(note.uri, (item.object as IObject).id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Fetch paged outbox from IActor', async () => {
|
|
|
|
const actor = createRandomActor();
|
|
|
|
const outbox = createRandomPagedOutbox(actor);
|
|
|
|
const page = createRandomOutboxPage(actor, outbox.id!, 10);
|
|
|
|
|
|
|
|
resolver._register(actor.id, actor);
|
|
|
|
resolver._register(actor.outbox as string, outbox);
|
|
|
|
resolver._register(outbox.first as string, page);
|
|
|
|
|
|
|
|
await personService.createPerson(actor.id, resolver);
|
|
|
|
|
|
|
|
for (const item of page.orderedItems as ICreate[]) {
|
|
|
|
const note = await noteService.fetchNote(item.object);
|
|
|
|
assert.ok(note);
|
|
|
|
assert.strictEqual(note.text, 'test test foo');
|
|
|
|
assert.strictEqual(note.uri, (item.object as IObject).id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Fetch only the first 100 items', async () => {
|
|
|
|
const actor = createRandomActor();
|
|
|
|
const outbox = createRandomNonPagedOutbox(actor, 200);
|
|
|
|
|
|
|
|
resolver._register(actor.id, actor);
|
|
|
|
resolver._register(actor.outbox as string, outbox);
|
|
|
|
|
|
|
|
await personService.createPerson(actor.id, resolver);
|
|
|
|
|
|
|
|
const items = outbox.orderedItems as ICreate[];
|
2023-07-08 13:40:48 +00:00
|
|
|
assert.ok(await noteService.fetchNote(items[19].object));
|
|
|
|
assert.ok(!await noteService.fetchNote(items[20].object));
|
2023-07-05 01:38:59 +00:00
|
|
|
});
|
|
|
|
});
|
2021-07-10 14:14:57 +00:00
|
|
|
});
|