Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .cursor/rules/testing.mdc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ describe("example", () => {
});
```

### Helpers

You can get mocks for emails, accounts, and rules here:

```tsx
import { getEmail, getEmailAccount, getRule } from "@/__tests__/helpers";
```

## Best Practices
- Each test should be independent
- Use descriptive test names
Expand Down
28 changes: 3 additions & 25 deletions apps/web/__tests__/ai-choose-rule.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, expect, test, vi } from "vitest";
import { aiChooseRule } from "@/utils/ai/choose-rule/ai-choose-rule";
import { type Action, ActionType, LogicalOperator } from "@prisma/client";
import { ActionType } from "@prisma/client";
import { defaultReplyTrackerInstructions } from "@/utils/reply-tracker/consts";
import { getEmail, getEmailAccount } from "@/__tests__/helpers";
import { getEmail, getEmailAccount, getRule } from "@/__tests__/helpers";

// pnpm test-ai ai-choose-rule

Expand Down Expand Up @@ -78,6 +78,7 @@ describe.runIf(isAiTest)("aiChooseRule", () => {
url: null,
folderName: null,
delayInMinutes: null,
folderId: null,
},
]);

Expand Down Expand Up @@ -327,26 +328,3 @@ describe.runIf(isAiTest)("aiChooseRule", () => {
});
});
});

// helpers
function getRule(instructions: string, actions: Action[] = []) {
return {
instructions,
name: "Joke requests",
actions,
id: "id",
userId: "userId",
createdAt: new Date(),
updatedAt: new Date(),
automate: false,
runOnThreads: false,
groupId: null,
from: null,
subject: null,
body: null,
to: null,
enabled: true,
categoryFilterType: null,
conditionalOperator: LogicalOperator.AND,
};
}
98 changes: 98 additions & 0 deletions apps/web/__tests__/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { EmailAccountWithAI } from "@/utils/llms/types";
import type { EmailForLLM } from "@/utils/types";
import { type Action, LogicalOperator } from "@prisma/client";
import type { Prisma } from "@prisma/client";

export function getEmailAccount(
overrides: Partial<EmailAccountWithAI> = {},
Expand All @@ -14,6 +16,9 @@ export function getEmailAccount(
aiProvider: null,
aiApiKey: null,
},
account: {
provider: "google",
},
};
}

Expand All @@ -35,3 +40,96 @@ export function getEmail({
...(cc && { cc }),
};
}

export function getRule(instructions: string, actions: Action[] = []) {
return {
instructions,
name: "Joke requests",
actions,
id: "id",
userId: "userId",
createdAt: new Date(),
updatedAt: new Date(),
automate: false,
runOnThreads: false,
groupId: null,
from: null,
subject: null,
body: null,
to: null,
enabled: true,
categoryFilterType: null,
conditionalOperator: LogicalOperator.AND,
};
}

export function getMockMessage({
id = "msg1",
threadId = "thread1",
historyId = "12345",
from = "test@example.com",
to = "user@example.com",
subject = "Test",
snippet = "Test message",
textPlain = "Test content",
textHtml = "<p>Test content</p>",
}: {
id?: string;
threadId?: string;
historyId?: string;
from?: string;
to?: string;
subject?: string;
snippet?: string;
textPlain?: string;
textHtml?: string;
} = {}) {
return {
id,
threadId,
historyId,
headers: {
from,
to,
subject,
date: new Date().toISOString(),
},
snippet,
textPlain,
textHtml,
attachments: [],
inline: [],
labelIds: [],
subject,
date: new Date().toISOString(),
};
}

export function getMockExecutedRule({
messageId = "msg1",
threadId = "thread1",
ruleId = "rule1",
ruleName = "Test Rule",
}: {
messageId?: string;
threadId?: string;
ruleId?: string;
ruleName?: string;
} = {}): Prisma.ExecutedRuleGetPayload<{
select: {
messageId: true;
threadId: true;
rule: {
select: {
id: true;
name: true;
};
};
};
}> {
return {
messageId,
threadId,
rule: { id: ruleId, name: ruleName },
};
}
Loading
Loading