The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Update reward-conditions.test.ts
Steven Teya19c5bb9618f
diff --git a/apps/web/tests/rewards/reward-conditions.test.ts b/apps/web/tests/rewards/reward-conditions.test.tsindex 6dce205e87..8ee0a2d4f4 100644--- a/apps/web/tests/rewards/reward-conditions.test.ts+++ b/apps/web/tests/rewards/reward-conditions.test.ts@@ -711,6 +711,374 @@ describe("evaluateRewardConditions", () => {expect(result).toBe(null);});});++ describe("greater_than", () => {+ test("should match when numeric value is greater than condition value", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 5000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalClicks" as const,+ operator: "greater_than" as const,+ value: 100,+ },+ ],+ },+ ];++ const context: RewardContext = {+ partner: {+ totalClicks: 150,+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toEqual(conditions[0]);+ });++ test("should not match when numeric value is equal to condition value", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 5000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalClicks" as const,+ operator: "greater_than" as const,+ value: 100,+ },+ ],+ },+ ];++ const context: RewardContext = {+ partner: {+ totalClicks: 100,+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toBe(null);+ });++ test("should not match when numeric value is less than condition value", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 5000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalClicks" as const,+ operator: "greater_than" as const,+ value: 100,+ },+ ],+ },+ ];++ const context: RewardContext = {+ partner: {+ totalClicks: 50,+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toBe(null);+ });+ });++ describe("greater_than_or_equal", () => {+ test("should match when numeric value is greater than condition value", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 5000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalLeads" as const,+ operator: "greater_than_or_equal" as const,+ value: 50,+ },+ ],+ },+ ];++ const context: RewardContext = {+ partner: {+ totalLeads: 75,+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toEqual(conditions[0]);+ });++ test("should match when numeric value is equal to condition value", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 5000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalLeads" as const,+ operator: "greater_than_or_equal" as const,+ value: 50,+ },+ ],+ },+ ];++ const context: RewardContext = {+ partner: {+ totalLeads: 50,+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toEqual(conditions[0]);+ });++ test("should not match when numeric value is less than condition value", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 5000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalLeads" as const,+ operator: "greater_than_or_equal" as const,+ value: 50,+ },+ ],+ },+ ];++ const context: RewardContext = {+ partner: {+ totalLeads: 25,+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toBe(null);+ });+ });++ describe("less_than", () => {+ test("should match when numeric value is less than condition value", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 5000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalConversions" as const,+ operator: "less_than" as const,+ value: 10,+ },+ ],+ },+ ];++ const context: RewardContext = {+ partner: {+ totalConversions: 5,+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toEqual(conditions[0]);+ });++ test("should not match when numeric value is equal to condition value", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 5000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalConversions" as const,+ operator: "less_than" as const,+ value: 10,+ },+ ],+ },+ ];++ const context: RewardContext = {+ partner: {+ totalConversions: 10,+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toBe(null);+ });++ test("should not match when numeric value is greater than condition value", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 5000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalConversions" as const,+ operator: "less_than" as const,+ value: 10,+ },+ ],+ },+ ];++ const context: RewardContext = {+ partner: {+ totalConversions: 15,+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toBe(null);+ });+ });++ describe("less_than_or_equal", () => {+ test("should match when numeric value is less than condition value", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 5000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalSaleAmount" as const,+ operator: "less_than_or_equal" as const,+ value: 1000,+ },+ ],+ },+ ];++ const context: RewardContext = {+ partner: {+ totalSaleAmount: 750,+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toEqual(conditions[0]);+ });++ test("should match when numeric value is equal to condition value", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 5000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalSaleAmount" as const,+ operator: "less_than_or_equal" as const,+ value: 1000,+ },+ ],+ },+ ];++ const context: RewardContext = {+ partner: {+ totalSaleAmount: 1000,+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toEqual(conditions[0]);+ });++ test("should not match when numeric value is greater than condition value", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 5000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalSaleAmount" as const,+ operator: "less_than_or_equal" as const,+ value: 1000,+ },+ ],+ },+ ];++ const context: RewardContext = {+ partner: {+ totalSaleAmount: 1250,+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toBe(null);+ });+ });});describe("edge cases", () => {@@ -784,5 +1152,349 @@ describe("evaluateRewardConditions", () => {expect(result).toBe(null);});++ test("should handle numeric operators with string values (type coercion)", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 5000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalClicks" as const,+ operator: "greater_than" as const,+ value: "100", // String value that should be coerced to number+ },+ ],+ },+ ];++ const context: RewardContext = {+ partner: {+ totalClicks: 150,+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toEqual(conditions[0]);+ });++ test("should handle string field values with numeric operators (type coercion)", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 5000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalCommissions" as const,+ operator: "less_than_or_equal" as const,+ value: 500,+ },+ ],+ },+ ];++ const context: RewardContext = {+ partner: {+ totalCommissions: 300, // Should work with Number() conversion+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toEqual(conditions[0]);+ });++ test("should handle decimal numbers with numeric operators", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 5000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalSaleAmount" as const,+ operator: "greater_than_or_equal" as const,+ value: 999.99,+ },+ ],+ },+ ];++ const context: RewardContext = {+ partner: {+ totalSaleAmount: 1000.0,+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toEqual(conditions[0]);+ });++ test("should handle zero values with numeric operators", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 5000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalClicks" as const,+ operator: "greater_than" as const,+ value: 0,+ },+ ],+ },+ ];++ const context: RewardContext = {+ partner: {+ totalClicks: 0,+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toBe(null); // 0 is not greater than 0+ });++ test("should handle negative numbers with numeric operators", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 5000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalCommissions" as const,+ operator: "greater_than" as const,+ value: -100,+ },+ ],+ },+ ];++ const context: RewardContext = {+ partner: {+ totalCommissions: 50,+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toEqual(conditions[0]);+ });+ });++ describe("mixed condition scenarios", () => {+ test("should handle mixed string and numeric operators in AND condition", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 5000,+ conditions: [+ {+ entity: "customer" as const,+ attribute: "country" as const,+ operator: "equals_to" as const,+ value: "US",+ },+ {+ entity: "partner" as const,+ attribute: "totalClicks" as const,+ operator: "greater_than" as const,+ value: 100,+ },+ {+ entity: "partner" as const,+ attribute: "totalSaleAmount" as const,+ operator: "less_than_or_equal" as const,+ value: 10000,+ },+ ],+ },+ ];++ const context: RewardContext = {+ customer: {+ country: "US",+ },+ partner: {+ totalClicks: 150,+ totalSaleAmount: 8500,+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toEqual(conditions[0]);+ });++ test("should fail when one numeric condition in AND group fails", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 5000,+ conditions: [+ {+ entity: "customer" as const,+ attribute: "country" as const,+ operator: "equals_to" as const,+ value: "US",+ },+ {+ entity: "partner" as const,+ attribute: "totalClicks" as const,+ operator: "greater_than" as const,+ value: 100,+ },+ {+ entity: "partner" as const,+ attribute: "totalSaleAmount" as const,+ operator: "less_than" as const,+ value: 5000, // This will fail+ },+ ],+ },+ ];++ const context: RewardContext = {+ customer: {+ country: "US",+ },+ partner: {+ totalClicks: 150,+ totalSaleAmount: 8500, // Greater than 5000+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toBe(null);+ });++ test("should succeed when one numeric condition in OR group succeeds", () => {+ const conditions = [+ {+ operator: "OR" as const,+ amount: 5000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalClicks" as const,+ operator: "greater_than" as const,+ value: 1000, // This will fail+ },+ {+ entity: "partner" as const,+ attribute: "totalLeads" as const,+ operator: "greater_than_or_equal" as const,+ value: 50, // This will succeed+ },+ {+ entity: "partner" as const,+ attribute: "totalSaleAmount" as const,+ operator: "less_than" as const,+ value: 1000, // This will fail+ },+ ],+ },+ ];++ const context: RewardContext = {+ partner: {+ totalClicks: 100, // Less than 1000+ totalLeads: 75, // Greater than or equal to 50+ totalSaleAmount: 5000, // Greater than 1000+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toEqual(conditions[0]);+ });++ test("should return highest amount when multiple condition groups with numeric operators match", () => {+ const conditions = [+ {+ operator: "AND" as const,+ amount: 1000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalClicks" as const,+ operator: "greater_than" as const,+ value: 10,+ },+ ],+ },+ {+ operator: "AND" as const,+ amount: 3000, // Highest amount+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalLeads" as const,+ operator: "greater_than_or_equal" as const,+ value: 5,+ },+ ],+ },+ {+ operator: "AND" as const,+ amount: 2000,+ conditions: [+ {+ entity: "partner" as const,+ attribute: "totalConversions" as const,+ operator: "less_than_or_equal" as const,+ value: 100,+ },+ ],+ },+ ];++ const context: RewardContext = {+ partner: {+ totalClicks: 50, // > 10+ totalLeads: 20, // >= 5+ totalConversions: 25, // <= 100+ },+ };++ const result = evaluateRewardConditions({+ conditions,+ context,+ });++ expect(result).toEqual(conditions[1]); // Should return the highest amount (3000)+ });});});