Skip to content
29 changes: 29 additions & 0 deletions apps/web/e2e/account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,35 @@ test('sign out everywhere ends every session', async ({ newUser, openAs }) => {
await expect(phone.getByText(t('auth.signIn.sessionEnded'))).toBeVisible()
})

test('a device left open leaves by itself after sign out everywhere, saying why', async ({
newUser,
openAs,
}) => {
// The other device notices on its next 30 second check, with nobody touching it.
test.setTimeout(120_000)
const user = await newUser()
const phone = await openAs(null)
await signIn(phone, user.email, user.password)
await expect(phone).toHaveURL(/\/home$/)

const laptop = await openAs(null)
await signIn(laptop, user.email, user.password)
await expect(laptop).toHaveURL(/\/home$/)
await laptop.goto('/me')
await security(laptop)
.getByRole('button', { name: t('security.sessions.signOutAll') })
.click()
await laptop
.getByRole('dialog', { name: t('security.sessions.confirmTitle') })
.getByRole('button', { name: t('security.sessions.signOutAll') })
.click()
await expect(laptop).toHaveURL(/\/sign-in/)

// No reload, no click: the phone gets to sign in and says why.
await expect(phone).toHaveURL(/\/sign-in/, { timeout: 60_000 })
await expect(phone.getByText(t('auth.signIn.sessionEnded'))).toBeVisible()
})

test('two-step sign in: set up, asked at sign in, wrong code refused, off with a code', async ({
newUser,
openAs,
Expand Down
18 changes: 18 additions & 0 deletions apps/web/e2e/groups-invites.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,22 @@ test('create a group, share an invite, and a signed out friend joins', async ({
// The owner sees the new member.
await page.reload()
await expect(page.getByText(friend.name).first()).toBeVisible()

// Revoking from the invite sheet by mistake: one tap on Undo, right there, brings it back.
await page.getByRole('button', { name: t('members.invite') }).click()
const inviteSheet = page.getByRole('dialog', { name: t('invites.title') })
const sheetLink = inviteSheet.getByRole('region', { name: t('invites.latest') })
await expect(sheetLink.getByText(`/join/${code}`)).toBeVisible()
await sheetLink.getByRole('button', { name: t('invites.revoke') }).click()
await expect(inviteSheet.getByText(t('invites.revoked'))).toBeVisible()
await expect(sheetLink).toBeHidden()
await inviteSheet.getByRole('button', { name: t('common.undo') }).click()
await expect(inviteSheet).toBeVisible()
await expect(sheetLink.getByText(`/join/${code}`)).toBeVisible()
await expect(inviteSheet.getByText(t('invites.revoked'))).toBeHidden()

// And the link still lets people in.
const later = await openAs(null)
await later.goto(`/join/${code}`)
await expect(later.getByRole('link', { name: t('invites.join.signInToJoin') })).toBeVisible()
})
18 changes: 16 additions & 2 deletions apps/web/src/features/auth/sign-in-page.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { t } from '@potluck/i18n'
import { Toaster } from '@potluck/ui'
import { fireEvent, screen, waitFor } from '@testing-library/react'
import { act, fireEvent, screen, waitFor } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { endEndedSession } from '@/lib/auth'
import { confirmSession, endEndedSession } from '@/lib/auth'
import { renderWithProviders } from '@/test/render'
import { authCallsTo, resetDb, respondAuth } from '@/test/supabase'
import { SignInPage } from './sign-in-page'
Expand Down Expand Up @@ -59,6 +59,20 @@ describe('SignInPage', () => {
expect(screen.queryByText(t('auth.signIn.sessionEnded'))).not.toBeInTheDocument()
})

it('says why when the session check finds the session gone after the page opened', async () => {
renderPage()
expect(await screen.findByRole('button', { name: t('auth.signIn.submit') })).toBeVisible()
expect(screen.queryByText(t('auth.signIn.sessionEnded'))).not.toBeInTheDocument()

respondAuth('getSession', { data: { session: { user: { id: 'u-1' }, access_token: 'x' } } })
respondAuth('getUser', async () => {
respondAuth('getSession', { data: { session: null } })
return { data: { user: null }, error: { name: 'AuthSessionMissingError', status: 400 } }
})
await act(() => confirmSession())
expect(await screen.findByText(t('auth.signIn.sessionEnded'))).toBeInTheDocument()
})

it('never redirects off site after signing in', async () => {
const { router } = renderPage({ redirect: 'https://evil.example' })
await fill('[email protected]', 'hunter22')
Expand Down
22 changes: 18 additions & 4 deletions apps/web/src/features/auth/sign-in-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { Link, useNavigate } from '@tanstack/react-router'
import { currentLocale, t } from '@potluck/i18n'
import { Button, Field, Input, toast } from '@potluck/ui'
import { useEffect, useState, type FormEvent } from 'react'
import { authCallbackUrl, clearSessionEnded, safeRedirect, sessionEndedHere } from '@/lib/auth'
import {
authCallbackUrl,
clearSessionEnded,
onSessionEnded,
safeRedirect,
sessionEndedHere,
} from '@/lib/auth'
import { errorMessage } from '@/lib/errors'
import { detectTimezone } from '@/lib/locale'
import { supabase } from '@/lib/supabase'
Expand All @@ -19,9 +25,17 @@ type Mode = 'password' | 'magic' | 'sent'

export function SignInPage({ redirect, notice }: AuthPageProps) {
const navigate = useNavigate()
// Signed out because the session ended on another device: say so once, here.
const [ended] = useState(sessionEndedHere)
useEffect(() => clearSessionEnded(), [])
// Signed out because the session ended on another device: say so once, here. The periodic
// check can land on this page a moment before it leaves the note, so listen for it too.
const [ended, setEnded] = useState(sessionEndedHere)
useEffect(() => {
const take = () => {
if (sessionEndedHere()) setEnded(true)
clearSessionEnded()
}
take()
return onSessionEnded(take)
}, [])
const [mode, setMode] = useState<Mode>('password')
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
Expand Down
44 changes: 31 additions & 13 deletions apps/web/src/features/expenses/expense-detail-parts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ export function PayersLine({
)
}

/** "owes €5" or "gets back €5", or for the viewer's own row "owe" and "get back". */
function owesOrGetsBack(isMe: boolean, getsBack: boolean, amount: string): string {
if (getsBack) {
return t(isMe ? 'expenses.detail.you.getBack' : 'expenses.detail.getsBack', { amount })
}
return t(isMe ? 'expenses.detail.you.owe' : 'expenses.detail.owes', { amount })
}

/**
* Everyone on the expense: their share on the right, and underneath what they paid (or got
* back) and how the share was worked out. Payers who are not in the split are listed too, so
Expand Down Expand Up @@ -121,19 +129,31 @@ export function SplitCard({
const share = s?.share_cents ?? 0
const paid = paidBy.get(id)
const isMe = id === me
// Your own row sits under "You", so its verbs agree with you ("You / owe").
const line =
paid !== undefined
? s
? t(e.is_refund ? 'expenses.detail.gotAndShare' : 'expenses.detail.paidAndShare', {
paid: money(paid),
share: money(share),
})
: t(e.is_refund ? 'expenses.detail.gotOnly' : 'expenses.detail.paidOnly', {
amount: money(paid),
})
: e.is_refund
? t('expenses.detail.getsBack', { amount: money(share) })
: t('expenses.detail.owes', { amount: money(share) })
? t(
e.is_refund
? isMe
? 'expenses.detail.you.gotAndShare'
: 'expenses.detail.gotAndShare'
: isMe
? 'expenses.detail.you.paidAndShare'
: 'expenses.detail.paidAndShare',
{ paid: money(paid), share: money(share) },
)
: t(
e.is_refund
? isMe
? 'expenses.detail.you.gotOnly'
: 'expenses.detail.gotOnly'
: isMe
? 'expenses.detail.you.paidOnly'
: 'expenses.detail.paidOnly',
{ amount: money(paid) },
)
: owesOrGetsBack(isMe, e.is_refund, money(share))
return (
<li
key={id}
Expand Down Expand Up @@ -171,9 +191,7 @@ export function SplitCard({
sign * (paid - share) > 0 ? 'text-saffron-ink' : 'text-tomato-ink',
)}
>
{sign * (paid - share) > 0
? t('expenses.detail.getsBack', { amount: money(sign * (paid - share)) })
: t('expenses.detail.owes', { amount: money(-sign * (paid - share)) })}
{owesOrGetsBack(isMe, sign * (paid - share) > 0, money(Math.abs(paid - share)))}
</span>
)}
</span>
Expand Down
36 changes: 30 additions & 6 deletions apps/web/src/features/expenses/expense-detail.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { moneyKeys, type ExpenseComment, type ExpenseWithSplits } from '@potluck/api'
import { t } from '@potluck/i18n'
import { formatMoney, setLocale, t } from '@potluck/i18n'
import { Toaster } from '@potluck/ui'
import { fireEvent, screen, waitFor, within } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
Expand Down Expand Up @@ -144,7 +144,9 @@ describe('ExpenseDetail', () => {
})
expect(await screen.findByText(t('expenses.detail.receivedBy', { name: 'Ben' }))).toBeVisible()
expect(screen.getAllByText(t('expenses.refund.badge')).length).toBeGreaterThan(0)
expect(screen.getAllByText(t('expenses.detail.getsBack', { amount: '$10.00' }))).toHaveLength(2)
// Ben and Cleo get back; under "You" it reads "get back".
expect(screen.getAllByText(t('expenses.detail.getsBack', { amount: '$10.00' }))).toHaveLength(1)
expect(screen.getByText(t('expenses.detail.you.getBack', { amount: '$10.00' }))).toBeVisible()
})

it('shows stored items with tax, tip and discount, and adjustments', async () => {
Expand Down Expand Up @@ -273,11 +275,33 @@ describe('ExpenseDetail', () => {
).closest('li')!
expect(ben.querySelector('[data-tone="neutral"]')).not.toBeNull()
expect(within(ben).getByText(t('expenses.detail.getsBack', { amount: '$20.00' }))).toBeVisible()
// My line: my share is what I owe.
const mine = screen
.getAllByText(t('expenses.detail.owes', { amount: '$10.00' }))[0]!
.closest('li')!
// My line: my share is what I owe, and it says so in the second person.
const mine = screen.getByText(t('expenses.detail.you.owe', { amount: '$10.00' })).closest('li')!
expect(mine).toHaveTextContent(t('common.you'))
expect(mine.querySelector('[data-tone="owe"]')).not.toBeNull()
expect(screen.getAllByText(t('expenses.detail.owes', { amount: '$10.00' }))).toHaveLength(1)
})

it('speaks to the viewer on their own row in every language', async () => {
await setLocale('de')
try {
renderDetail({
amount_cents: 3000,
paid_by: BEN,
expense_payers: [{ user_id: BEN, paid_cents: 3000 }],
})
// Money formats with a no-break space, which the text matcher folds away.
const text = (key: 'expenses.detail.you.owe' | 'expenses.detail.owes') =>
t(key, { amount: formatMoney(1000, 'USD') }).replace(/\s/g, ' ')
const mine = (await screen.findByText(text('expenses.detail.you.owe'))).closest('li')!
expect(mine).toHaveTextContent(t('common.you'))
expect(mine).toHaveTextContent('schuldest')
const cleo = screen.getByText(text('expenses.detail.owes')).closest('li')!
expect(cleo).toHaveTextContent('Cleo')
expect(cleo).toHaveTextContent('schuldet')
} finally {
await setLocale('en')
}
})

it('is read only in an archived group', async () => {
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/features/expenses/expenses-tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ export function ExpensesTab({
settlement={item.settlement}
viewerId={me}
viewerRole={myRole}
archived={archived}
blockedInTab={blockedInTab}
label={settlementText(item.settlement, me, nameOf)}
/>
Expand Down
15 changes: 15 additions & 0 deletions apps/web/src/features/expenses/form/expense-form.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,21 @@ describe('ExpenseForm offline', () => {
await waitFor(() => expect(create).toHaveBeenCalledTimes(1))
})

it('names everyone blocked in one sentence that reads right for several', async () => {
renderWithClient(
<ExpenseForm mode="create" groupId={GROUP_ID} onDone={vi.fn()} onClose={() => {}} />,
seededClient((c) =>
c.setQueryData(peopleKeys.blocks(ME), [
{ user_id: BEN, display_name: 'Ben Okafor', avatar_path: null, blocked_at: 'x' },
{ user_id: CLEO, display_name: 'Cleo Park', avatar_path: null, blocked_at: 'x' },
]),
),
)
const alert = await screen.findByRole('alert')
expect(alert).toHaveTextContent(t('expenses.form.youBlocked', { names: 'Ben and Cleo' }))
expect(alert).not.toHaveTextContent('you two')
})

it('still reports a refused save as an error, not as the connection', async () => {
setup()
create.mockRejectedValueOnce({ message: 'payers_sum_mismatch', code: '23514' })
Expand Down
Loading
Loading