Skip to content

Commit cb59fb3

Browse files
committed
Tighten Astro package test typing
Replace the test's `as never` shortcuts with explicit mock helper functions and concrete `FederationFetchOptions` / `APIContext` annotations. This keeps the self-import regression test readable while preserving plain `deno test` in *packages/astro/* and the existing Node packaging checks.
1 parent 6b48053 commit cb59fb3

1 file changed

Lines changed: 46 additions & 22 deletions

File tree

packages/astro/src/package.test.ts

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
import { deepStrictEqual, strictEqual } from "node:assert/strict";
22
import { createRequire } from "node:module";
33
import test from "node:test";
4+
import type {
5+
Federation,
6+
FederationFetchOptions,
7+
} from "@fedify/fedify/federation";
8+
import type { APIContext } from "astro";
49

510
const require = createRequire(import.meta.url);
611

12+
type MockFederation<TContextData> = Pick<Federation<TContextData>, "fetch">;
13+
14+
function toFederation<TContextData>(
15+
federation: MockFederation<TContextData>,
16+
): Federation<TContextData> {
17+
return federation as Federation<TContextData>;
18+
}
19+
20+
function toApiContext(context: Pick<APIContext, "request">): APIContext {
21+
return context as APIContext;
22+
}
23+
724
function expectResponse(response: void | Response): Response {
825
if (!(response instanceof Response)) {
926
throw new TypeError("Expected middleware to return a Response");
@@ -42,27 +59,29 @@ test("self-reference ESM import exposes working Astro integration API", async ()
4259
let capturedRequest: Request | undefined;
4360
let capturedContextData: unknown;
4461
const middleware = mod.fedifyMiddleware(
45-
{
46-
fetch(
62+
toFederation<string>({
63+
async fetch(
4764
request: Request,
48-
options: {
49-
contextData: string;
50-
onNotAcceptable(request: Request): Promise<Response>;
51-
},
65+
options: FederationFetchOptions<string>,
5266
) {
5367
capturedRequest = request;
5468
capturedContextData = options.contextData;
69+
if (options.onNotAcceptable == null) {
70+
throw new TypeError("Expected onNotAcceptable to be defined");
71+
}
5572
return options.onNotAcceptable(request);
5673
},
57-
} as never,
74+
}),
5875
() => "test-context",
5976
);
6077

6178
const request = new Request("https://example.com/");
62-
const response = expectResponse(await middleware(
63-
{ request } as never,
64-
() => Promise.resolve(new Response("Not found", { status: 404 })),
65-
));
79+
const response = expectResponse(
80+
await middleware(
81+
toApiContext({ request }),
82+
() => Promise.resolve(new Response("Not found", { status: 404 })),
83+
),
84+
);
6685
strictEqual(capturedRequest, request);
6786
strictEqual(capturedContextData, "test-context");
6887
strictEqual(response.status, 406);
@@ -80,24 +99,29 @@ test(
8099

81100
let nextCalled = false;
82101
const middleware = mod.fedifyMiddleware(
83-
{
84-
fetch(
102+
toFederation<void>({
103+
async fetch(
85104
_request: Request,
86-
options: { onNotFound(request: Request): Promise<Response> },
105+
options: FederationFetchOptions<void>,
87106
) {
107+
if (options.onNotFound == null) {
108+
throw new TypeError("Expected onNotFound to be defined");
109+
}
88110
return options.onNotFound(new Request("https://example.com/actor"));
89111
},
90-
} as never,
112+
}),
91113
() => undefined,
92114
);
93115

94-
const response = expectResponse(await middleware(
95-
{ request: new Request("https://example.com/inbox") } as never,
96-
() => {
97-
nextCalled = true;
98-
return Promise.resolve(new Response("Handled by Astro"));
99-
},
100-
));
116+
const response = expectResponse(
117+
await middleware(
118+
toApiContext({ request: new Request("https://example.com/inbox") }),
119+
() => {
120+
nextCalled = true;
121+
return Promise.resolve(new Response("Handled by Astro"));
122+
},
123+
),
124+
);
101125

102126
strictEqual(nextCalled, true);
103127
strictEqual(response.status, 200);

0 commit comments

Comments
 (0)