From 0c8905259428cbac4db86acf1de7480cc22a940e Mon Sep 17 00:00:00 2001 From: Jessica Ho Date: Fri, 23 Feb 2024 20:43:44 -0800 Subject: [PATCH] refactored tests --- prisma/constants.ts | 8 + prisma/seed.ts | 381 ++---------------- prisma/utils.ts | 226 +++++++++++ .../components/Dashboard/OverlapsTable.svelte | 4 +- tests/calendar.spec.ts | 5 + tests/profile.spec.ts | 43 ++ 6 files changed, 309 insertions(+), 358 deletions(-) create mode 100644 prisma/constants.ts create mode 100644 prisma/utils.ts create mode 100644 tests/profile.spec.ts diff --git a/prisma/constants.ts b/prisma/constants.ts new file mode 100644 index 0000000..5ce46fe --- /dev/null +++ b/prisma/constants.ts @@ -0,0 +1,8 @@ +export const PHONES = [ + '+12015550121', + '+12015550122', + '+12015550123', + '+12015550124', + '+12015550125', + '+12015550126' +]; diff --git a/prisma/seed.ts b/prisma/seed.ts index 5b8aaee..1db487a 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -1,372 +1,41 @@ -import { AvailabilityStatus, PrismaClient, Pronoun } from '@prisma/client'; -import { DateTime } from 'luxon'; +import { PrismaClient } from '@prisma/client'; +import { + createActiveSession, + createExpiredLink, + createFriendRequest, + createHouseholdConnection, + createHouseholdInvite, + createUserWithEmptyHousehold, + createUserWithKid, + createUserWithNothing, + deleteAllFriendRequests +} from './utils'; + +export const prisma = new PrismaClient(); -const prisma = new PrismaClient(); async function main() { - const phones: string[] = [ - '+12015550121', - '+12015550122', - '+12015550123', - '+12015550124', - '+12015550125', - '+12015550126' - ]; const now = new Date(); const expires = new Date(); expires.setHours(expires.getHours() + 1); - function permsYes(phone: string) { - return { - phonePermissions: { - connectOrCreate: { - where: { - phone - }, - create: { - phone, - blocked: false, - allowInvites: true, - allowReminders: true, - acceptedTermsAt: now - } - } - } - }; - } - - function emptyHousehold(ind: number) { - return { - household: { - connectOrCreate: { - where: { - id: ind - }, - create: { - // phone: phones[ind - 1], - id: ind, - name: `Household ${ind}` - } - } - } - }; - } - - function householdWithKid(ind: number, kidInd: number) { - return { - household: { - connectOrCreate: { - where: { - id: ind - }, - create: { - id: ind, - name: `Household ${ind}`, - children: { - connectOrCreate: [ - { - where: { - id: kidInd - }, - create: { - firstName: `User ${ind} Kid ${kidInd}`, - pronouns: Pronoun['HE_HIM_HIS'] - } - } - ] - } - } - } - } - }; - } - - function basicUser(id: number) { - return { - firstName: `User ${id}`, - locale: 'English', - pronouns: Pronoun['SHE_HER_HERS'], - timeZone: 'America/Los_Angeles', - reminderDatetime: new Date(), - reminderIntervalDays: 7, - acceptedTermsAt: new Date() - }; - } - - await prisma.friendRequest - .deleteMany() - .catch(() => console.log('No friend request table to delete')); - - const expiredLink = { - token: '3e99472f1003794c', - phone: '+12015550121', - expires: new Date('8/5/2020') - }; - await prisma.magicLink.upsert({ - where: { - id: 1 - }, - update: expiredLink, - create: expiredLink - }); - - // User 1 - await prisma.user.upsert({ - where: { - phone: phones[0] - }, - update: basicUser(1), - create: { - ...basicUser(1), - ...permsYes(phones[0]) - } - }); - - // User 2 - await prisma.user.upsert({ - where: { - phone: phones[1] - }, - update: basicUser(2), - create: { - ...basicUser(2), - ...permsYes(phones[1]) - } - }); - - const user2session = 'user2session'; - const session = { - token: user2session, - phone: phones[1], - expires - }; - await prisma.session.upsert({ - where: { - token: user2session - }, - update: session, - create: session - }); - - // User 3 - const user3 = { - ...basicUser(3), - ...emptyHousehold(3) - }; - - await prisma.user.upsert({ - where: { - phone: phones[2] - }, - update: user3, - create: { - ...user3, - ...permsYes(phones[2]) - } - }); + await deleteAllFriendRequests(); - // User 4 - const user4 = { - ...basicUser(4), - ...emptyHousehold(4) - }; + await createExpiredLink(1); - await prisma.user.upsert({ - where: { - phone: phones[3] - }, - update: user4, - create: { - ...user4, - ...permsYes(phones[3]) - } - }); - - // friend req from User 4 to User 3 - const friendReq = { - id: 3, - targetPhone: phones[2], - fromHouseholdId: 4, - fromUserId: 4 - }; - await prisma.friendRequest.upsert({ - where: { - id: 3 - }, - update: friendReq, - create: friendReq - }); - - const user4session = 'user4session'; - const session4 = { - token: user4session, - phone: phones[3], - expires - }; - await prisma.session.upsert({ - where: { - token: user4session - }, - update: session4, - create: session4 - }); - - // User 5 - const user5 = { - ...basicUser(5), - ...emptyHousehold(5) - }; - - await prisma.user.upsert({ - where: { - phone: phones[4] - }, - update: user5, - create: { - ...user5, - ...permsYes(phones[4]) - } - }); - - // household connection b/t Households 3 and 5 - const householdConnection = { - id: 3, - householdId: 3, - friendHouseholdId: 5 - }; - await prisma.householdConnection.upsert({ - where: { - id: 3 - }, - update: householdConnection, - create: householdConnection - }); - - // household invite from User 5 to User 2 - const householdInvite = { - id: 2, - targetPhone: phones[1], - householdId: 5, - fromUserId: 5 - }; - - await prisma.joinHouseholdRequest.upsert({ - where: { - id: 2 - }, - update: householdInvite, - create: householdInvite - }); - - // User 6 - const user6 = { - ...basicUser(6), - ...householdWithKid(6, 1) - }; - - await prisma.user.upsert({ - where: { - phone: phones[5] - }, - update: user6, - create: { - ...user6, - ...permsYes(phones[5]) - } - }); - - const kid1 = { - householdId: 6, - firstName: 'User 6 Kid 1', - pronouns: Pronoun['HE_HIM_HIS'] - }; - await prisma.householdChild.upsert({ - where: { - id: 1 - }, - update: kid1, - create: kid1 - }); - - const user6session = 'user6session'; - const session6 = { - token: user6session, - phone: phones[5], - expires - }; - await prisma.session.upsert({ - where: { - token: user6session - }, - update: session6, - create: session6 - }); - - const firstDate = DateTime.fromObject( - { - year: 2001, - month: 8, - day: 11 - }, - { - zone: 'utc' - } + await Promise.all([1, 2].map(async (userInd) => await createUserWithNothing(now, userInd))); + await Promise.all( + [3, 4].map(async (userInd) => await createUserWithEmptyHousehold(now, userInd)) ); + await Promise.all([2, 4, 6].map(async (userInd) => await createActiveSession(now, userInd))); + await createUserWithEmptyHousehold(now, 5); + await createUserWithKid(now, 6); - const firstDateBase = { - status: AvailabilityStatus.AVAILABLE, - notes: 'first date', - emoticons: '', - startTime: firstDate.set({ hour: 14 }).toJSDate(), - endTime: firstDate.set({ hour: 15 }).toJSDate() - }; - await prisma.availabilityDate.upsert({ - where: { - householdId_date: { - householdId: 3, - date: firstDate.toJSDate(), - } - }, - update: firstDateBase, - create: { - householdId: 3, - date: firstDate.toJSDate(), - ...firstDateBase - } - }); + await createFriendRequest(4, 3); - const lastDate = DateTime.fromObject( - { - year: 2001, - month: 3, - day: 8 - }, - { - zone: 'utc' - } - ); + await createHouseholdConnection(3, 5); - const lastDateBase = { - status: AvailabilityStatus.AVAILABLE, - notes: 'last date', - emoticons: '', - startTime: lastDate.set({ hour: 14 }).toJSDate(), - endTime: lastDate.set({ hour: 15 }).toJSDate() - }; - await prisma.availabilityDate.upsert({ - where: { - householdId_date: { - householdId: 3, - date: lastDate.toJSDate(), - } - }, - update: lastDateBase, - create: { - householdId: 3, - date: lastDate.toJSDate(), - ...lastDateBase - } - }); + await createHouseholdInvite(5, 2); } export async function run() { diff --git a/prisma/utils.ts b/prisma/utils.ts new file mode 100644 index 0000000..5ea4805 --- /dev/null +++ b/prisma/utils.ts @@ -0,0 +1,226 @@ +import { Pronoun } from '@prisma/client'; +import { prisma } from './seed'; +import { PHONES } from './constants'; + +function permsYes(phone: string, now: Date) { + return { + phonePermissions: { + connectOrCreate: { + where: { + phone + }, + create: { + phone, + blocked: false, + allowInvites: true, + allowReminders: true, + acceptedTermsAt: now + } + } + } + }; +} + +function emptyHousehold(ind: number) { + return { + household: { + connectOrCreate: { + where: { + id: ind + }, + create: { + // phone: phones[ind - 1], + id: ind, + name: `Household ${ind}` + } + } + } + }; +} + +function householdWithKid(ind: number, kidInd: number) { + return { + household: { + connectOrCreate: { + where: { + id: ind + }, + create: { + id: ind, + name: `Household ${ind}`, + children: { + connectOrCreate: [ + { + where: { + id: kidInd + }, + create: { + firstName: `User ${ind} Kid ${kidInd}`, + pronouns: Pronoun['HE_HIM_HIS'] + } + } + ] + } + } + } + } + }; +} + +function basicUser(id: number) { + return { + firstName: `User ${id}`, + locale: 'English', + pronouns: Pronoun['SHE_HER_HERS'], + timeZone: 'America/Los_Angeles', + reminderDatetime: new Date(), + reminderIntervalDays: 7, + acceptedTermsAt: new Date() + }; +} + +export async function deleteAllFriendRequests() { + await prisma.friendRequest + .deleteMany() + .catch(() => console.log('No friend request table to delete')); +} + +export async function createExpiredLink(userInd: number) { + const expiredLink = { + token: '3e99472f1003794c', + phone: PHONES[userInd - 1], + expires: new Date('8/5/2020') + }; + await prisma.magicLink.upsert({ + where: { + id: userInd + }, + update: expiredLink, + create: expiredLink + }); +} + +export async function createUserWithNothing(now: Date, userInd: number) { + const phone = PHONES[userInd - 1]; + + await prisma.user.upsert({ + where: { + phone + }, + update: basicUser(userInd), + create: { + ...basicUser(userInd), + ...permsYes(phone, now) + } + }); +} + +export async function createActiveSession(now: Date, userInd: number) { + const phone = PHONES[userInd - 1]; + + const expires = new Date(now); + expires.setHours(expires.getHours() + 1); + + const userSessionToken = `user${userInd}session`; + const session = { + token: userSessionToken, + phone, + expires: now + }; + await prisma.session.upsert({ + where: { + token: userSessionToken + }, + update: session, + create: session + }); +} + +export async function createUserWithEmptyHousehold(now: Date, userInd: number) { + const phone = PHONES[userInd - 1]; + const user = { + ...basicUser(userInd), + ...emptyHousehold(userInd) + }; + + await prisma.user.upsert({ + where: { + phone + }, + update: user, + create: { + ...user, + ...permsYes(phone, now) + } + }); +} + +export async function createFriendRequest(fromUserInd: number, toUserInd: number) { + const friendReq = { + id: toUserInd, + targetPhone: PHONES[toUserInd - 1], + fromHouseholdId: fromUserInd, + fromUserId: fromUserInd + }; + + await prisma.friendRequest.upsert({ + where: { + id: toUserInd + }, + update: friendReq, + create: friendReq + }); +} + +export async function createHouseholdConnection(hId1: number, hId2: number) { + const householdConnection = { + id: hId1, + householdId: hId1, + friendHouseholdId: hId2 + }; + await prisma.householdConnection.upsert({ + where: { + id: hId1 + }, + update: householdConnection, + create: householdConnection + }); +} + +export async function createHouseholdInvite(fromUserInd: number, toUserInd: number) { + // household invite from User 5 to User 2 + const householdInvite = { + id: toUserInd, + targetPhone: PHONES[toUserInd - 1], + householdId: fromUserInd, + fromUserId: fromUserInd + }; + + await prisma.joinHouseholdRequest.upsert({ + where: { + id: toUserInd + }, + update: householdInvite, + create: householdInvite + }); +} + +export async function createUserWithKid(now: Date, userInd: number) { + const user = { + ...basicUser(userInd), + ...householdWithKid(userInd, 1) + }; + + const phone = PHONES[userInd - 1]; + + await prisma.user.upsert({ + where: { + phone + }, + update: user, + create: { + ...user, + ...permsYes(phone, now) + } + }); +} diff --git a/src/lib/components/Dashboard/OverlapsTable.svelte b/src/lib/components/Dashboard/OverlapsTable.svelte index 1e26e82..b937e1b 100644 --- a/src/lib/components/Dashboard/OverlapsTable.svelte +++ b/src/lib/components/Dashboard/OverlapsTable.svelte @@ -26,7 +26,7 @@

- You: {userEmoticons} + You: {userEmoticons.length ? userEmoticons : 'N/A'} @@ -35,7 +35,7 @@ |

- Them: {friendEmoticons} + Them: {friendEmoticons.length ? friendEmoticons : 'N/A'} diff --git a/tests/calendar.spec.ts b/tests/calendar.spec.ts index f544866..19d8415 100644 --- a/tests/calendar.spec.ts +++ b/tests/calendar.spec.ts @@ -11,3 +11,8 @@ b. BUSY? - Clear btn c. AVAILABLE? */ +/* +create profile +create kid +save basic info +*/ \ No newline at end of file diff --git a/tests/profile.spec.ts b/tests/profile.spec.ts new file mode 100644 index 0000000..39cd36a --- /dev/null +++ b/tests/profile.spec.ts @@ -0,0 +1,43 @@ +import { test, expect } from '@playwright/test'; +import { run } from '../prisma/seed'; + +// const url = 'http://localhost:5173/profile'; +const host = 'http://localhost:5173'; + +test.beforeEach(async () => { + await run(); +}); + +test.only("User can create new profile with valid phone number", async ({ page, context }) => { + await page.goto('http://localhost:5173'); + + await page.waitForTimeout(3000); + await page.getByRole('textbox').fill('2016660127'); + await page.getByRole('button').click(); + + let token: string, phone: string; + page.on('dialog', async (dialog) => { + const thing = dialog.message().split(' '); + phone = thing[0]; + token = thing[1]; + dialog.accept(); + }); + page.on('console', async (msg) => { + const first = await msg.args()[0]?.jsonValue(); + if (first === 'PHONE_TOKEN') { + phone = await msg.args()[1].jsonValue(); + token = await msg.args()[2].jsonValue(); + } + }); + await new Promise((resolve) => { + let intervalId = setInterval(() => { + if (phone && token) { + clearInterval(intervalId); + resolve(); + } + }, 100); + }); + await page.goto(`http://localhost:5173/login/${phone!}/${token!}`); + await page.mainFrame().waitForLoadState(); + await expect(page).toHaveURL(host + '/profile'); +})