Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: clean up tests and split them #9

Merged
merged 2 commits into from
Dec 19, 2024
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
56 changes: 56 additions & 0 deletions test/cron.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { env, createExecutionContext, SELF } from 'cloudflare:test';
import { describe, it, expect, beforeAll } from 'vitest';
import { calculateConversionsAndCredits } from '../src/index';
import { createRefcode, createReferral } from '../src/db';
import Stripe from 'stripe'


function useDummyStripeEnvironmentConstants () {
const referrerEmail = 'referrer@example.com'
const referralEmail = 'referral@example.com'
return { referrerEmail, referralEmail }
}

async function createDummyStripeEnvironment (stripe: Stripe) {
const { referralEmail, referrerEmail } = useDummyStripeEnvironmentConstants()
const referralStripeCustomer = await stripe.customers.create({ email: referralEmail })
const referrerStripeCustomer = await stripe.customers.create({ email: referrerEmail })
return ({
referralEmail, referrerEmail, referralStripeCustomer, referrerStripeCustomer
})
}

async function cleanupDummyStripeEnvironment (stripe: Stripe) {
const { referrerEmail, referralEmail } = useDummyStripeEnvironmentConstants()
for (const email of [referralEmail, referrerEmail]) {
const result = await stripe.customers.search({ query: `email:"${email}"` })
for (const customer of result.data) {
await stripe.customers.del(customer.id)
}
}
}

async function resetDummyStripeEnvironment (stripe: Stripe) {
await cleanupDummyStripeEnvironment(stripe)
await createDummyStripeEnvironment(stripe)
}

// TODO: Work in Progress
// I'm trying to use Stripe sandboxes to create a static remote environment reliable enough for testing
// the credits and conversions reconciliation logic but haven't finished yet - TBD after we get v1 out the door
describe.skip('conversion and credit cronjob', () => {
it('checks stripe', async () => {
const stripe = new Stripe(env.STRIPE_API_KEY)
// uncomment this to reset the stripe environment
// await resetDummyStripeEnvironment(stripe)
const { referralEmail, referrerEmail } = useDummyStripeEnvironmentConstants()
const refcode = await createRefcode(env.REFERRALS, referrerEmail)
await createReferral(env.REFERRALS, referralEmail, refcode)
await stripe.customers.search({ query: `email:"${referralEmail}"` })
//const stripeCustomer = await stripe.customers.create({email: referralEmail})

await calculateConversionsAndCredits({} as ScheduledController, env, createExecutionContext())

})

})
107 changes: 107 additions & 0 deletions test/http.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { SELF } from 'cloudflare:test';
import { describe, it, expect } from 'vitest';

async function createRefcode(email: string){
const form = new FormData()
form.append('email', email)
return SELF.fetch('http://example.com/refcode/create',
{ method: 'POST', body: form }
);
}

async function createReferral(refcode: string, email: string){
return SELF.fetch('http://example.com/referrals/create',
{
method: 'POST',
body: (() => {
const form = new FormData()
form.append('refcode', refcode)
form.append('email', email)
return form
})()
}
)
}

async function getReferrals(refcode: string){
return SELF.fetch(`http://example.com/referrals/${encodeURIComponent(refcode)}`)
}

async function getRefcode(email: string){
return SELF.fetch(`http://example.com/refcode/${encodeURIComponent(email)}`)
}

async function getReferredBy(email: string){
return SELF.fetch(`http://example.com/referredby/${encodeURIComponent(email)}`);
}

describe('the referral service', () => {
it('creates a refcode successfully', async () => {
const email = 'test@example.com'
const createResponse = await createRefcode(email)
expect(await createResponse.json()).toHaveProperty('refcode');

const getResponse = await getRefcode(email);
expect(await getResponse.json()).toHaveProperty('refcode');
});

it('creates a referral successfully', async () => {

// create a refcode
const email = 'test@example.com'
const createRefcodeResponse = await createRefcode(email);
const refcode = (await createRefcodeResponse.json() as any).refcode as string

// there shouldn't be any referrals yet
let referralsResponse = await getReferrals(refcode);
let referralsResponseJson = await referralsResponse.json() as any
expect(referralsResponseJson).toHaveProperty('referrals');
expect(referralsResponseJson.referrals).toBeInstanceOf(Array);
expect(referralsResponseJson.referrals).toHaveLength(0)

// create a referral
const newUserEmail = 'newuser@example.com'
await createReferral(refcode, newUserEmail)

// now there should be one referral
referralsResponse = await getReferrals(refcode)
referralsResponseJson = await referralsResponse.json() as any
expect(referralsResponseJson).toHaveProperty('referrals');
expect(referralsResponseJson.referrals).toBeInstanceOf(Array);
expect(referralsResponseJson.referrals).toHaveLength(1)

const referredByResponse = await getReferredBy(newUserEmail)
const referredByResponseJson = await referredByResponse.json() as any
expect(referredByResponseJson).toHaveProperty('refcode')

// create a second referral
const secondNewUserEmail = 'secondnewuser@example.com'
await createReferral(refcode, secondNewUserEmail);

// now there should be two referrals
referralsResponse = await getReferrals(refcode)
referralsResponseJson = await referralsResponse.json() as any
expect(referralsResponseJson).toHaveProperty('referrals');
expect(referralsResponseJson.referrals).toBeInstanceOf(Array);
expect(referralsResponseJson.referrals).toHaveLength(2)

const secondReferredByResponse = await getReferredBy(secondNewUserEmail)
const secondReferredByResponseJson = await secondReferredByResponse.json() as any
expect(secondReferredByResponseJson).toHaveProperty('refcode')
});

it('will not create a referral for the user who created the refcode', async () => {

// create a refcode
const email = 'test@example.com'
const form = new FormData()
form.append('email', email)
const createRefcodeResponse = await createRefcode(email)
const refcode = (await createRefcodeResponse.json() as any).refcode as string

const response = await createReferral(refcode, email)
expect(response.status).toBe(400)
})
});


181 changes: 0 additions & 181 deletions test/index.spec.ts

This file was deleted.

Loading