Skip to content

Commit 428f23b

Browse files
refactor(syndicator-mastodon): use masto.js
1 parent 8df0565 commit 428f23b

File tree

15 files changed

+507
-1018
lines changed

15 files changed

+507
-1018
lines changed

helpers/mock-agent/endpoint-syndicate.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,55 @@ export function mockClient() {
99
agent.disableNetConnect();
1010
agent.enableNetConnect(/(?:127\.0\.0\.1:\d{5})/);
1111

12-
const origin = "https://store.example";
12+
const statusId = "1234567890987654321";
13+
const storeOrigin = "https://store.example";
14+
const syndicatorOrigin = "https://mastodon.example";
15+
const syndicatorResponseOptions = {
16+
headers: { "Content-type": "application/json" },
17+
};
1318

1419
// Create file on content store
1520
agent
16-
.get(origin)
21+
.get(storeOrigin)
1722
.intercept({ path: /\/user\/.*\.(md|jpg)/, method: "PUT" })
1823
.reply(201);
1924

2025
// Update file on content store
2126
agent
22-
.get(origin)
27+
.get(storeOrigin)
2328
.intercept({ path: /\/user\/.*\.(md|jpg)/, method: "PATCH" })
2429
.reply(201);
2530

31+
// Get instance information from syndication target
32+
agent
33+
.get(syndicatorOrigin)
34+
.intercept({
35+
path: `/api/v1/instance`,
36+
})
37+
.reply(
38+
200,
39+
{
40+
uri: syndicatorOrigin,
41+
urls: { streaming_api: "https://streaming.mastodon.example" },
42+
version: "4.1.2",
43+
},
44+
syndicatorResponseOptions
45+
)
46+
.persist();
47+
48+
// Post status to syndication target
49+
agent
50+
.get(syndicatorOrigin)
51+
.intercept({ path: `/api/v1/statuses`, method: "POST" })
52+
.reply(
53+
200,
54+
{
55+
id: statusId,
56+
url: `https://mastodon.example/@username/${statusId}`,
57+
},
58+
syndicatorResponseOptions
59+
)
60+
.persist();
61+
2662
return agent;
2763
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import { MockAgent } from "undici";
2+
import { getFixture } from "@indiekit-test/fixtures";
3+
4+
/**
5+
* @returns {import("undici").MockAgent} Undici MockAgent
6+
* @see {@link https://undici.nodejs.org/#/docs/api/MockAgent}
7+
*/
8+
export function mockClient() {
9+
const agent = new MockAgent();
10+
agent.disableNetConnect();
11+
12+
const id = "1234567890987654321";
13+
const instanceOrigin = "https://mastodon.example";
14+
const instanceResponse = {
15+
uri: instanceOrigin,
16+
urls: { streaming_api: "https://streaming.mastodon.example" },
17+
version: "4.1.2",
18+
};
19+
const statusResponse = {
20+
id,
21+
url: `https://mastodon.example/@username/${id}`,
22+
};
23+
const responseOptions = {
24+
headers: { "Content-type": "application/json" },
25+
};
26+
const websiteOrigin = "https://website.example";
27+
28+
// Instance information
29+
agent
30+
.get(instanceOrigin)
31+
.intercept({
32+
path: `/api/v1/instance`,
33+
headers: { authorization: "Bearer token" },
34+
})
35+
.reply(200, instanceResponse, responseOptions)
36+
.persist();
37+
38+
// Instance information (Unauthorized, invalid access token)
39+
agent
40+
.get(instanceOrigin)
41+
.intercept({
42+
path: `/api/v1/instance`,
43+
headers: {
44+
authorization: "Bearer invalid",
45+
},
46+
})
47+
.reply(401)
48+
.persist();
49+
50+
// Instance information (Unauthorized, no access token provided)
51+
agent
52+
.get(instanceOrigin)
53+
.intercept({
54+
path: `/api/v1/instance`,
55+
})
56+
.reply(401)
57+
.persist();
58+
59+
// Post favourite
60+
agent
61+
.get(instanceOrigin)
62+
.intercept({ path: `/api/v1/statuses/${id}/favourite`, method: "POST" })
63+
.reply(200, statusResponse, responseOptions)
64+
.persist();
65+
66+
// Post reblog
67+
agent
68+
.get(instanceOrigin)
69+
.intercept({ path: `/api/v1/statuses/${id}/reblog`, method: "POST" })
70+
.reply(200, statusResponse, responseOptions)
71+
.persist();
72+
73+
// Post status
74+
agent
75+
.get(instanceOrigin)
76+
.intercept({ path: `/api/v1/statuses`, method: "POST" })
77+
.reply(200, statusResponse, responseOptions)
78+
.persist();
79+
80+
// Media information
81+
agent
82+
.get(instanceOrigin)
83+
.intercept({ path: "/api/v1/media/1" })
84+
.reply(
85+
200,
86+
{ id: 1, url: `https://mastodon.example/1.jpg` },
87+
responseOptions
88+
)
89+
.persist();
90+
91+
// Upload media
92+
agent
93+
.get(instanceOrigin)
94+
.intercept({
95+
path: `/api/v2/media`,
96+
method: "POST",
97+
})
98+
.reply(202, { id: 1, type: "image" }, responseOptions)
99+
.persist();
100+
101+
// Upload media (Not Found)
102+
agent
103+
.get(instanceOrigin)
104+
.intercept({
105+
path: `/api/v2/media`,
106+
method: "POST",
107+
})
108+
.reply(404, { error: "Record not found" }, responseOptions)
109+
.persist();
110+
111+
// Get media file
112+
agent
113+
.get(websiteOrigin)
114+
.intercept({ path: "/photo1.jpg" })
115+
.reply(200, getFixture("file-types/photo.jpg", false))
116+
.persist();
117+
118+
// Get media file (Not Found)
119+
agent
120+
.get(websiteOrigin)
121+
.intercept({ path: "/404.jpg" })
122+
.reply(404, {})
123+
.persist();
124+
125+
return agent;
126+
}

0 commit comments

Comments
 (0)