Skip to content

Commit 45204ac

Browse files
fix: issues in tests
1 parent 7d22df3 commit 45204ac

9 files changed

+50
-32
lines changed

packages/frames.js/src/core/composeMiddleware.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/require-await -- we expect promises */
12
import { composeMiddleware } from "./composeMiddleware";
23

34
describe("composeMiddleware", () => {
@@ -11,7 +12,7 @@ describe("composeMiddleware", () => {
1112
const context = { a: 1 };
1213

1314
const composedMiddleware = composeMiddleware<typeof context, unknown>([
14-
(ctx, next) => {
15+
async (ctx, next) => {
1516
ctx.a = 2;
1617
return next();
1718
},

packages/frames.js/src/getAddressForFid.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export async function getAddressForFid<
4040
throw new Error(
4141
`Failed to parse response body as JSON because server hub returned response with status "${response.status}" and body "${await response.clone().text()}"`
4242
);
43-
})) as { messages?: Record<string, any>[] };
43+
})) as { messages?: Record<string, unknown>[] };
4444

4545
let address: AddressReturnType<Options> | null = null;
4646

@@ -55,7 +55,11 @@ export async function getAddressForFid<
5555
}
5656
}
5757

58-
if (!address && fallbackToCustodyAddress) {
58+
if (address) {
59+
return address;
60+
}
61+
62+
if (fallbackToCustodyAddress) {
5963
const publicClient = createPublicClient({
6064
transport: http(),
6165
chain: optimism,
@@ -69,9 +73,5 @@ export async function getAddressForFid<
6973
});
7074
}
7175

72-
if (!address) {
73-
throw new Error(`No address found for fid ${fid}`);
74-
}
75-
76-
return address as AddressReturnType<Options>;
76+
return address as unknown as AddressReturnType<Options>;
7777
}

packages/frames.js/src/getAddressesForFid.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export async function getAddressesForFid({
5151
throw new Error(
5252
`Failed to parse response body as JSON because server hub returned response with status "${verificationsResponse.status}" and body "${await verificationsResponse.clone().text()}"`
5353
);
54-
})) as { messages?: Record<string, any>[] };
54+
})) as { messages?: Record<string, unknown>[] };
5555

5656
if (messages) {
5757
const verifiedAddresses = messages

packages/frames.js/src/middleware/farcaster.test.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,21 @@ describe("farcaster middleware", () => {
105105
await mw(context, next);
106106
enableNetConnect();
107107

108-
expect(next).toHaveBeenCalledWith(
109-
expect.objectContaining({
110-
message: {
111-
buttonIndex: 1,
112-
castId: {
113-
fid: 456,
114-
hash: "0x",
115-
},
116-
connectedAddress: "0x789",
117-
inputText: "hello",
118-
requesterFid: 123,
119-
state: JSON.stringify({ test: true }),
108+
expect(next).toHaveBeenCalledWith({
109+
clientProtocol: { id: "farcaster", version: "vNext" },
110+
message: {
111+
buttonIndex: 1,
112+
castId: {
113+
fid: 456,
114+
hash: "0x",
120115
},
121-
})
122-
);
116+
connectedAddress: "0x89",
117+
inputText: "hello",
118+
requesterFid: 123,
119+
state: JSON.stringify({ test: true }),
120+
transactionId: undefined,
121+
},
122+
});
123123
});
124124

125125
it("supports custom global typed context", async () => {

packages/frames.js/src/middleware/farcasterHubContext.test.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ describe("farcasterHubContext middleware", () => {
104104

105105
expect(next).toHaveBeenCalledWith(
106106
expect.objectContaining({
107-
message: {
107+
clientProtocol: { id: "farcaster", version: "vNext" },
108+
message: expect.objectContaining({
108109
buttonIndex: 1,
109110
castId: {
110111
fid: 456,
@@ -114,8 +115,8 @@ describe("farcasterHubContext middleware", () => {
114115
inputText: "hello",
115116
requesterFid: 123,
116117
state: JSON.stringify({ test: true }),
117-
requestedUserData: expect.anything() as UserDataReturnType,
118-
},
118+
requesterUserData: expect.anything() as UserDataReturnType,
119+
}) as unknown,
119120
})
120121
);
121122
});

packages/frames.js/src/middleware/openframes.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,11 @@ describe("openframes middleware", () => {
290290

291291
expect(next).toHaveBeenCalledWith(
292292
expect.objectContaining({
293-
message: {
293+
message: expect.objectContaining({
294294
buttonIndex: 1,
295295
inputText: "hello",
296296
state: { test: true },
297-
},
297+
}) as unknown,
298298
clientProtocol: {
299299
id: "xmtp",
300300
version: "2024-02-09",

packages/frames.js/src/middleware/renderResponse.test.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ jest.mock("@vercel/og", () => {
2121
arrayBufferMock,
2222
constructorMock,
2323
ImageResponse: class {
24-
constructor() {
25-
constructorMock();
24+
constructor(...args: unknown[]) {
25+
// @ts-expect-error -- we are mocking the constructor
26+
constructorMock(...args);
2627
}
2728
arrayBuffer = arrayBufferMock;
2829
},

packages/frames.js/src/middleware/stateMiddleware.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ export function stateMiddleware<
2727

2828
// since we are stringifyng state to JSON in renderResponse middleware, we need to parse decode JSON here
2929
// so it is easy to use in middleware chain and frames handler
30-
if (typeof ctx.message.state === "string") {
30+
if (ctx.message.state) {
3131
try {
32+
if (typeof ctx.message.state !== "string") {
33+
throw new Error("State is not a string");
34+
}
35+
3236
state = JSON.parse(ctx.message.state) as TState;
3337
} catch (e) {
3438
// eslint-disable-next-line no-console -- provide feedback

packages/frames.js/src/utils.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,16 @@ export function extractAddressFromJSONMessage(
131131
return null;
132132
}
133133

134-
return bytesToHexString(data.verificationAddAddressBody.address);
134+
/**
135+
* This is ugly hack but we want to return the address as a string that is expected by the users ( essentially what they see in the response from the hub ).
136+
* We could use Buffer.from(data.verificationAddAddressBody.address).toString('base64') here but that results in different base64.
137+
* Therefore we return address from source message and not from decoded message.
138+
*
139+
* For example for value 0x8d25687829d6b85d9e0020b8c89e3ca24de20a89 from API we get 0x8d25687829d6b85d9e0020b8c89e3ca24de20a8w== from Buffer.from(...).toString('base64').
140+
* The values are the same if you compare them as Buffer.from(a).equals(Buffer.from(b)).
141+
*/
142+
// @TODO type message properly or handle the return type properly
143+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- we know the data is there
144+
return (message as Record<string, any>).data.verificationAddAddressBody
145+
.address as `0x${string}`;
135146
}

0 commit comments

Comments
 (0)