Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
154 changes: 82 additions & 72 deletions apps/web/tests/tracks/track-sale.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const expectValidSaleResponse = (
});
};

describe("POST /track/sale", async () => {
describe.concurrent("POST /track/sale", async () => {
const h = new IntegrationHarness();
const { http } = await h.init();

Expand Down Expand Up @@ -79,48 +79,54 @@ describe("POST /track/sale", async () => {

test("track a sale with regular vs premium product ID (should create the right commission)", async () => {
const regularInvoiceId = `INV_${randomId()}`;
const response1 = await http.post<TrackSaleResponse>({
path: "/track/sale",
body: {
...sale,
amount: 2000,
customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
invoiceId: regularInvoiceId,
metadata: {
productId: "regularProductId",
},
},
});
expect(response1.status).toEqual(200);

const premiumInvoiceId = `INV_${randomId()}`;
const response2 = await http.post<TrackSaleResponse>({
path: "/track/sale",
body: {
...sale,
amount: 3000,
customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
invoiceId: premiumInvoiceId,
metadata: {
productId: "premiumProductId",

const [response1, response2] = await Promise.all([
http.post<TrackSaleResponse>({
path: "/track/sale",
body: {
...sale,
amount: 2000,
customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
invoiceId: regularInvoiceId,
metadata: {
productId: "regularProductId",
},
},
},
});
}),

http.post<TrackSaleResponse>({
path: "/track/sale",
body: {
...sale,
amount: 3000,
customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
invoiceId: premiumInvoiceId,
metadata: {
productId: "premiumProductId",
},
},
}),
]);

expect(response1.status).toEqual(200);
expect(response2.status).toEqual(200);

await verifyCommission({
http,
invoiceId: regularInvoiceId,
expectedAmount: response1.data.sale?.amount!,
expectedEarnings: E2E_SALE_REWARD.amountInCents,
});
await Promise.all([
verifyCommission({
http,
invoiceId: regularInvoiceId,
expectedAmount: response1.data.sale?.amount!,
expectedEarnings: E2E_SALE_REWARD.amountInCents,
}),

await verifyCommission({
http,
invoiceId: premiumInvoiceId,
expectedAmount: response2.data.sale?.amount!,
expectedEarnings: E2E_SALE_REWARD.modifiers[0].amountInCents!,
});
verifyCommission({
http,
invoiceId: premiumInvoiceId,
expectedAmount: response2.data.sale?.amount!,
expectedEarnings: E2E_SALE_REWARD.modifiers[0].amountInCents!,
}),
]);
});

test("track a sale with an externalId that does not exist (should return null customer and sale)", async () => {
Expand Down Expand Up @@ -257,48 +263,52 @@ describe("POST /track/sale", async () => {
const smallSaleInvoiceId = `INV_${randomId()}`;
const smallSaleAmount = 10000; // $100.00

const response1 = await http.post<TrackSaleResponse>({
path: "/track/sale",
body: {
...sale,
amount: smallSaleAmount,
customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
invoiceId: smallSaleInvoiceId,
},
});

expect(response1.status).toEqual(200);
expect(response1.data.sale?.amount).toEqual(smallSaleAmount);

// Test with a large sale amount
const largeSaleInvoiceId = `INV_${randomId()}`;
const largeSaleAmount = 20000; // $200.00

const response2 = await http.post<TrackSaleResponse>({
path: "/track/sale",
body: {
...sale,
amount: largeSaleAmount,
customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
invoiceId: largeSaleInvoiceId,
},
});
const [response1, response2] = await Promise.all([
http.post<TrackSaleResponse>({
path: "/track/sale",
body: {
...sale,
amount: smallSaleAmount,
customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
invoiceId: smallSaleInvoiceId,
},
}),

http.post<TrackSaleResponse>({
path: "/track/sale",
body: {
...sale,
amount: largeSaleAmount,
customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
invoiceId: largeSaleInvoiceId,
},
}),
]);

expect(response1.status).toEqual(200);
expect(response1.data.sale?.amount).toEqual(smallSaleAmount);

expect(response2.status).toEqual(200);
expect(response2.data.sale?.amount).toEqual(largeSaleAmount);

await verifyCommission({
http,
invoiceId: smallSaleInvoiceId,
expectedAmount: response1.data.sale?.amount!,
expectedEarnings: E2E_SALE_REWARD.amountInCents,
});
await Promise.all([
verifyCommission({
http,
invoiceId: smallSaleInvoiceId,
expectedAmount: response1.data.sale?.amount!,
expectedEarnings: E2E_SALE_REWARD.amountInCents,
}),

await verifyCommission({
http,
invoiceId: largeSaleInvoiceId,
expectedAmount: response2.data.sale?.amount!,
expectedEarnings: E2E_SALE_REWARD.modifiers[1].amountInCents!,
});
verifyCommission({
http,
invoiceId: largeSaleInvoiceId,
expectedAmount: response2.data.sale?.amount!,
expectedEarnings: E2E_SALE_REWARD.modifiers[1].amountInCents!,
}),
]);
});
});
56 changes: 36 additions & 20 deletions apps/web/tests/utils/verify-commission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ interface VerifyCommissionProps {
expectedEarnings: number;
}

const POLL_INTERVAL_MS = 5000; // 5 seconds
const TIMEOUT_MS = 30000; // 30 seconds

export const verifyCommission = async ({
http,
customerExternalId,
Expand All @@ -19,10 +22,7 @@ export const verifyCommission = async ({
}: VerifyCommissionProps) => {
let customerId: string | undefined;

// Pause for 3 seconds for data to be fully processed
await new Promise((resolve) => setTimeout(resolve, 3000));

// Optional: resolve customer ID if customerExternalId is given
// Resolve customer ID first if customerExternalId is given
if (customerExternalId) {
const { data: customers } = await http.get<Customer[]>({
path: "/customers",
Expand All @@ -43,27 +43,43 @@ export const verifyCommission = async ({
query.customerId = customerId;
}

const { status, data: commissions } = await http.get<CommissionResponse[]>({
path: "/commissions",
query,
});
// Poll for commission every 5 seconds, timeout after 30 seconds
const startTime = Date.now();

expect(status).toEqual(200);
expect(commissions).toHaveLength(1);
while (Date.now() - startTime < TIMEOUT_MS) {
const { status, data: commissions } = await http.get<CommissionResponse[]>({
path: "/commissions",
query,
});

const commission = commissions[0];
if (status === 200 && commissions.length === 1) {
const commission = commissions[0];

if (invoiceId) {
expect(commission.invoiceId).toEqual(invoiceId);
}
// Verify all expectations
if (invoiceId) {
expect(commission.invoiceId).toEqual(invoiceId);
}

if (customerId) {
expect(commission.customer?.id).toEqual(customerId);
}
if (customerId) {
expect(commission.customer?.id).toEqual(customerId);
}

if (expectedAmount !== undefined) {
expect(commission.amount).toEqual(expectedAmount);
}

expect(commission.earnings).toEqual(expectedEarnings);

return;
}

if (expectedAmount !== undefined) {
expect(commission.amount).toEqual(expectedAmount);
// Wait before next poll
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
}

expect(commission.earnings).toEqual(expectedEarnings);
// Timeout reached - fail the test
throw new Error(
`Commission not found within ${TIMEOUT_MS / 1000} seconds. ` +
`Query: ${JSON.stringify(query)}`,
);
};