Skip to content

Commit 83cba3e

Browse files
committed
chore: add better typing in backend-helpers
1 parent 7207f2e commit 83cba3e

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

apps/e2e/tests/backend/backend-helpers.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,25 +186,42 @@ export async function bumpEmailAddress(options: { unindexed?: boolean } = {}) {
186186
return mailbox;
187187
}
188188

189+
// Type for outbox email items (simplified - full type is EmailOutboxCrud["Server"]["Read"])
190+
type OutboxEmail = {
191+
id: string,
192+
subject?: string,
193+
status: string,
194+
simple_status: string,
195+
to?: {
196+
type: string,
197+
user_id?: string,
198+
[key: string]: unknown,
199+
},
200+
[key: string]: unknown,
201+
};
202+
189203
// Helper to get emails from the outbox, filtered by subject if provided
190-
export async function getOutboxEmails(options?: { subject?: string }) {
204+
export async function getOutboxEmails(options?: { subject?: string }): Promise<OutboxEmail[]> {
191205
const listResponse = await niceBackendFetch("/api/v1/emails/outbox", {
192206
method: "GET",
193207
accessType: "server",
194208
});
209+
const items = listResponse.body.items as OutboxEmail[];
195210
if (options?.subject) {
196-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
197-
return listResponse.body.items.filter((e: any) => e.subject === options.subject);
211+
return items.filter((e) => e.subject === options.subject);
198212
}
199-
return listResponse.body.items;
213+
return items;
200214
}
201215

202-
// Helper to poll the outbox until an email with the expected subject and status appears
203-
export async function waitForOutboxEmailWithStatus(subject: string, status: string) {
216+
// Helper to poll the outbox until the most recent email with the expected subject has the expected status.
217+
// Note: emails are returned ordered by createdAt desc (newest first), so we check emails[0] specifically
218+
// to ensure we're waiting for the MOST RECENT email, not an older one with the same subject.
219+
export async function waitForOutboxEmailWithStatus(subject: string, status: string): Promise<OutboxEmail[]> {
204220
const maxRetries = 24;
205-
let emails: any[] = [];
221+
let emails: OutboxEmail[] = [];
206222
for (let i = 0; i < maxRetries; i++) {
207223
emails = await getOutboxEmails({ subject });
224+
// Check the most recent email (first in the list due to createdAt desc ordering)
208225
if (emails.length > 0 && emails[0].status === status) {
209226
return emails;
210227
}

apps/e2e/tests/backend/endpoints/api/v1/emails/outbox-api.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,8 +1155,9 @@ describe("email outbox API", () => {
11551155
const emails = await waitForOutboxEmailWithStatus("Recipient Type Test", "sent");
11561156
const email = emails[0];
11571157

1158-
expect(email.to.type).toBe("user-primary-email");
1159-
expect(email.to.user_id).toBe(userId);
1158+
expect(email.to).toBeDefined();
1159+
expect(email.to!.type).toBe("user-primary-email");
1160+
expect(email.to!.user_id).toBe(userId);
11601161
});
11611162

11621163
it("should show correct fields for PREPARING state (before rendering starts)", async ({ expect }) => {
@@ -1253,7 +1254,7 @@ describe("email outbox API", () => {
12531254
expect(typeof email.skip_deliverability_check).toBe("boolean");
12541255
expect(email.variables).toBeDefined();
12551256
expect(email.to).toBeDefined();
1256-
expect(email.to.type).toBe("user-primary-email");
1257+
expect(email.to!.type).toBe("user-primary-email");
12571258
});
12581259

12591260
it("should list emails across multiple status types", async ({ expect }) => {

0 commit comments

Comments
 (0)