Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(email): stop pagination before the first possible UID
Afloatac4a07171a52
diff --git a/server/email/client.ts b/server/email/client.tsindex 0dba7adb58..ea91c613b9 100644--- a/server/email/client.ts+++ b/server/email/client.ts@@ -229,6 +229,7 @@ export async function listMessages(account: EmailAccount, guard: EmailGuard, inptry {const total = client.mailbox ? client.mailbox.exists : 0;if (!total) return { total: 0, messages: [] };+ if (input.beforeUid === 1) return { total, messages: [] };const out: MessageSummary[] = [];if (input.beforeUid) {const uids = (await client.search({ uid: `1:${input.beforeUid - 1}` }, { uid: true })) || [];diff --git a/server/email/pagination.test.ts b/server/email/pagination.test.tsnew file mode 100644index 0000000000..c27abdacdc--- /dev/null+++ b/server/email/pagination.test.ts@@ -0,0 +1,68 @@+import { beforeEach, describe, expect, it, vi } from "vitest";++import { listMessages, openGuard } from "./client.ts";+import type { EmailAccount } from "./store.ts";++const imap = vi.hoisted(() => ({+ search: vi.fn(),+ fetch: vi.fn(),+ release: vi.fn(),+ logout: vi.fn(async () => {}),+}));++vi.mock("imapflow", () => ({+ ImapFlow: class {+ mailbox = { exists: 5 };+ on() {}+ async connect() {}+ async getMailboxLock() { return { release: imap.release }; }+ search = imap.search;+ fetch = imap.fetch;+ logout = imap.logout;+ close() {}+ },+}));++const account: EmailAccount = {+ address: "[email protected]", username: "test", password: "fixture-only", connectedAt: 0,+ imap: { host: "imap.example.net", port: 993, secure: true },+};++beforeEach(() => {+ vi.clearAllMocks();+ imap.search.mockReset().mockResolvedValue([1, 2, 3, 4]);+ imap.fetch.mockImplementation(async function* (uids: number[]) {+ for (const uid of uids) yield { uid, envelope: { subject: `Message ${uid}` }, flags: new Set() };+ });+});++describe("Email pagination", () => {+ it("ends at UID 1 without asking the server for the invalid range 1:0", async () => {+ imap.search.mockRejectedValue(new Error("Invalid UID sequence 1:0"));+ await expect(listMessages(account, openGuard, { mailbox: "INBOX", limit: 20, beforeUid: 1 }))+ .resolves.toEqual({ total: 5, messages: [] });+ expect(imap.search).not.toHaveBeenCalled();+ expect(imap.fetch).not.toHaveBeenCalled();+ expect(imap.release).toHaveBeenCalledOnce();+ expect(imap.logout).toHaveBeenCalledOnce();+ });++ it("still searches older UIDs and returns the newest matching messages first", async () => {+ const page = await listMessages(account, openGuard, { mailbox: "INBOX", limit: 2, beforeUid: 5 });+ expect(imap.search).toHaveBeenCalledWith({ uid: "1:4" }, { uid: true });+ expect(imap.fetch.mock.calls[0][0]).toEqual([3, 4]);+ expect(page.total).toBe(5);+ expect(page.messages.map((message) => message.uid)).toEqual([4, 3]);+ expect(imap.release).toHaveBeenCalledOnce();+ expect(imap.logout).toHaveBeenCalledOnce();+ });++ it("returns an empty older page when the first existing UID is above 1", async () => {+ imap.search.mockResolvedValue([]);+ await expect(listMessages(account, openGuard, { mailbox: "INBOX", limit: 20, beforeUid: 4 }))+ .resolves.toEqual({ total: 5, messages: [] });+ expect(imap.search).toHaveBeenCalledWith({ uid: "1:3" }, { uid: true });+ expect(imap.release).toHaveBeenCalledOnce();+ expect(imap.logout).toHaveBeenCalledOnce();+ });+});