trashposs/packages/backend/test/activitypub.ts

103 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-04-07 03:56:46 +02:00
process.env.NODE_ENV = "test";
2023-04-07 03:56:46 +02:00
import * as assert from "assert";
import rndstr from "rndstr";
import { initDb } from "../src/db/postgre.js";
import { initTestDb } from "./utils.js";
2023-04-07 03:56:46 +02:00
describe("ActivityPub", () => {
before(async () => {
2022-06-04 07:25:30 +02:00
//await initTestDb();
await initDb();
});
2023-04-07 03:56:46 +02:00
describe("Parse minimum object", () => {
const host = "https://host1.test";
const preferredUsername = `${rndstr("A-Z", 4)}${rndstr("a-z", 4)}`;
const actorId = `${host}/users/${preferredUsername.toLowerCase()}`;
const actor = {
2023-04-07 03:56:46 +02:00
"@context": "https://www.w3.org/ns/activitystreams",
id: actorId,
2023-04-07 03:56:46 +02:00
type: "Person",
preferredUsername,
inbox: `${actorId}/inbox`,
outbox: `${actorId}/outbox`,
};
const post = {
2023-04-07 03:56:46 +02:00
"@context": "https://www.w3.org/ns/activitystreams",
id: `${host}/users/${rndstr("0-9a-z", 8)}`,
type: "Note",
attributedTo: actor.id,
2023-04-07 03:56:46 +02:00
to: "https://www.w3.org/ns/activitystreams#Public",
content: "あ",
};
2023-04-07 03:56:46 +02:00
it("Minimum Actor", async () => {
const { MockResolver } = await import("./misc/mock-resolver.js");
const { createPerson } = await import(
"../src/remote/activitypub/models/person.js"
);
const resolver = new MockResolver();
resolver._register(actor.id, actor);
const user = await createPerson(actor.id, resolver);
assert.deepStrictEqual(user.uri, actor.id);
assert.deepStrictEqual(user.username, actor.preferredUsername);
assert.deepStrictEqual(user.inbox, actor.inbox);
});
2023-04-07 03:56:46 +02:00
it("Minimum Note", async () => {
const { MockResolver } = await import("./misc/mock-resolver.js");
const { createNote } = await import(
"../src/remote/activitypub/models/note.js"
);
const resolver = new MockResolver();
resolver._register(actor.id, actor);
resolver._register(post.id, post);
const note = await createNote(post.id, resolver, true);
assert.deepStrictEqual(note?.uri, post.id);
2023-04-07 03:56:46 +02:00
assert.deepStrictEqual(note.visibility, "public");
2022-05-21 15:21:41 +02:00
assert.deepStrictEqual(note.text, post.content);
});
});
2021-08-17 10:25:19 +02:00
2023-04-07 03:56:46 +02:00
describe("Truncate long name", () => {
const host = "https://host1.test";
const preferredUsername = `${rndstr("A-Z", 4)}${rndstr("a-z", 4)}`;
2021-08-17 10:25:19 +02:00
const actorId = `${host}/users/${preferredUsername.toLowerCase()}`;
2023-04-07 03:56:46 +02:00
const name = rndstr("0-9a-z", 129);
2021-08-17 10:25:19 +02:00
const actor = {
2023-04-07 03:56:46 +02:00
"@context": "https://www.w3.org/ns/activitystreams",
2021-08-17 10:25:19 +02:00
id: actorId,
2023-04-07 03:56:46 +02:00
type: "Person",
2021-08-17 10:25:19 +02:00
preferredUsername,
name,
inbox: `${actorId}/inbox`,
outbox: `${actorId}/outbox`,
};
2023-04-07 03:56:46 +02:00
it("Actor", async () => {
const { MockResolver } = await import("./misc/mock-resolver.js");
const { createPerson } = await import(
"../src/remote/activitypub/models/person.js"
);
2021-08-17 10:25:19 +02:00
const resolver = new MockResolver();
resolver._register(actor.id, actor);
const user = await createPerson(actor.id, resolver);
assert.deepStrictEqual(user.name, actor.name.substr(0, 128));
});
});
});