Skip to content

Commit ac3bad4

Browse files
authored
Fix flaky WebFinger test by mocking fetch (#486)
1 parent 52bce83 commit ac3bad4

File tree

1 file changed

+80
-23
lines changed

1 file changed

+80
-23
lines changed

packages/cli/src/webfinger/mod.test.ts

Lines changed: 80 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,19 @@ const ALIASES = [
1616
];
1717

1818
test("Test webFingerCommand", () => {
19-
// Resources only
2019
const argsWithResourcesOnly = [COMMAND, ...RESOURCES];
21-
assert.deepEqual(
22-
parse(webFingerCommand, argsWithResourcesOnly),
23-
{
24-
success: true,
25-
value: {
26-
debug: false,
27-
command: COMMAND,
28-
resources: RESOURCES,
29-
allowPrivateAddresses: undefined,
30-
maxRedirection: 5,
31-
userAgent: undefined,
32-
},
20+
assert.deepEqual(parse(webFingerCommand, argsWithResourcesOnly), {
21+
success: true,
22+
value: {
23+
debug: false,
24+
command: COMMAND,
25+
resources: RESOURCES,
26+
allowPrivateAddresses: undefined,
27+
maxRedirection: 5,
28+
userAgent: undefined,
3329
},
34-
);
35-
// With options
30+
});
31+
3632
const maxRedirection = 10;
3733
assert.deepEqual(
3834
parse(webFingerCommand, [
@@ -56,24 +52,85 @@ test("Test webFingerCommand", () => {
5652
},
5753
},
5854
);
59-
// Wrong option
55+
6056
const wrongOptionResult = parse(webFingerCommand, [
6157
...argsWithResourcesOnly,
6258
"-Q",
6359
]);
6460
assert.ok(!wrongOptionResult.success);
65-
// Wrong option value
61+
6662
const wrongOptionValueResult = parse(
6763
webFingerCommand,
6864
[...argsWithResourcesOnly, "--max-redirection", "-10"],
6965
);
7066
assert.ok(!wrongOptionValueResult.success);
7167
});
7268

73-
test("Test lookupSingleWebFinger", async () => {
74-
const aliases = (await Array.fromAsync(
75-
RESOURCES,
76-
(resource) => lookupSingleWebFinger({ resource }),
77-
)).map((w) => w?.aliases?.[0]);
78-
assert.deepEqual(aliases, ALIASES);
69+
// ----------------------------------------------------------------------
70+
// FIX FOR ISSUE #480 – MOCK FETCH TO REMOVE EXTERNAL DEPENDENCY
71+
// ----------------------------------------------------------------------
72+
73+
test("Test lookupSingleWebFinger", async (): Promise<void> => {
74+
const originalFetch = globalThis.fetch;
75+
76+
const mockResponses: Record<string, unknown> = {
77+
"https://hackers.pub/.well-known/webfinger?resource=acct%3Ahongminhee%40hackers.pub":
78+
{
79+
subject: "acct:[email protected]",
80+
aliases: [ALIASES[0]],
81+
links: [
82+
{
83+
rel: "self",
84+
type: "application/activity+json",
85+
href: ALIASES[0],
86+
},
87+
],
88+
},
89+
90+
"https://hollo.social/.well-known/webfinger?resource=acct%3Afedify%40hollo.social":
91+
{
92+
subject: "acct:[email protected]",
93+
aliases: [ALIASES[1]],
94+
links: [
95+
{
96+
rel: "self",
97+
type: "application/activity+json",
98+
href: ALIASES[1],
99+
},
100+
],
101+
},
102+
};
103+
104+
// Correct async fetch mock returning Promise<Response>
105+
globalThis.fetch = async (
106+
input: RequestInfo | URL,
107+
): Promise<Response> => {
108+
await Promise.resolve();
109+
110+
const url = String(input);
111+
const responseData = mockResponses[url];
112+
113+
if (responseData) {
114+
return new Response(JSON.stringify(responseData), {
115+
status: 200,
116+
headers: {
117+
"Content-Type": "application/jrd+json",
118+
},
119+
});
120+
}
121+
122+
throw new Error(`Unexpected URL: ${url}`);
123+
};
124+
125+
try {
126+
const results = await Array.fromAsync(
127+
RESOURCES,
128+
(resource) => lookupSingleWebFinger({ resource }),
129+
);
130+
131+
const aliases = results.map((w) => w?.aliases?.[0]);
132+
assert.deepEqual(aliases, ALIASES);
133+
} finally {
134+
globalThis.fetch = originalFetch;
135+
}
79136
});

0 commit comments

Comments
 (0)