Files
serfaandClaude Opus 5 1808ad4cba Move the demo market to Mexico City, priced in US dollars
The showcase was a Barcelona market: Catalan names, +34 numbers, euro
rates and "Carrer Example 12" on every job. Presented to a Mexican
client, all of that reads as somebody else's product.

City comes from NEXT_PUBLIC_CITY_* as before, now Ciudad de México at
19.4326/-99.1332, with MAPBOX_COUNTRY=mx. The seed's fallbacks were
Barcelona literals, so an unset env quietly seeded a different city
than the app rendered — they now agree.

Two db tests pinned the Barcelona centre as a hardcoded constant, which
is why the deck returned zero cards on the first run here: every pro was
a continent outside the radius. They read the same env as the seed now,
so the trap cannot recur.

Money: formatCents defaults to USD/en-US, and the nine hardcoded euro
signs across the card, search rows, quote strip and forms are dollars.
The rate NUMBERS are unchanged and still read high for CDMX — that is a
pricing decision, not a currency one, and is left alone deliberately.

Seed people are Mexican, addressed on real Roma/Condesa streets rotated
by index rather than one placeholder repeated. Phones moved to +52 55,
which moves the demo login to +525500000000 / 000000.

Also in here, from the same session:
- Sending a job now confirms. The mutation always succeeded; the sheet
  just closed with no receipt, which from the customer's side is
  indistinguishable from a dead button. Dismissing that receipt resolves
  as 'sent', so the card does not return to the deck.
- Media moves to DigitalOcean Spaces, with the public origin derived
  from bucket and region instead of a second env var to keep in sync.
- Managed-Postgres TLS: DATABASE_CA_CERT takes a path or inline PEM.
- The client-facing project panel beside the running app.
- Two profiles removed and four renamed to match their photos.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-23 10:56:31 -04:00

147 lines
5.6 KiB
TypeScript

/**
* Integration tests for the notification dispatcher, against the live database.
*
* pnpm services:up && pnpm db:migrate && pnpm db:seed
*
* Three properties carry this module, and all three fail silently if broken —
* which is the whole reason `notification_deliveries` exists:
*
* 1. It never throws. Every caller runs it after a committed transaction, so a
* provider outage must not turn an accepted job into a 500.
* 2. It honours the preference that governs each kind, and ignores preferences
* for the transactional ones nobody may unsubscribe from.
* 3. It picks a channel that can actually reach the person — a phone signup has
* a synthetic email address, and posting that to a provider is a hard bounce
* against our sending reputation.
*
* No provider is configured under test, so `sendSms`/`sendEmail` take their
* development path and log instead of calling out. That is the behaviour being
* asserted: in production the same missing config throws, deliberately.
*/
import { config } from 'dotenv';
import { sql } from 'drizzle-orm';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
config({ path: '../../.env' });
const { closePool, db } = await import('@linkdr/db');
const { notify } = await import('../src/index');
const RUN = Math.random().toString(36).slice(2, 8);
/** Verified phone, no real email — a client who signed up by SMS. */
let phoneUser: string;
/** Real email, no phone — a pro who signed up with Google. */
let emailUser: string;
/** Neither. Nothing can reach them. */
let unreachable: string;
async function deliveries(userId: string) {
return db.execute<{ kind: string; channel: string; status: string; detail: string | null }>(sql`
SELECT kind, channel, status, detail FROM notification_deliveries
WHERE user_id = ${userId} ORDER BY created_at
`);
}
beforeAll(async () => {
// A phone signup: real number, and the synthetic address they were given
// because they never had a mailbox to give us.
const phone = `+34999${String(Math.floor(Math.random() * 1e6)).padStart(6, '0')}`;
const [a] = await db.execute<{ id: string }>(sql`
INSERT INTO users (name, email, phone, phone_verified, role)
VALUES ('Notify Phone', ${`${phone}@phone.linkder.local`}, ${phone}, true, 'client')
RETURNING id
`);
phoneUser = a!.id;
const [b] = await db.execute<{ id: string }>(sql`
INSERT INTO users (name, email, role)
VALUES ('Notify Email', ${`notify-${RUN}@example.com`}, 'pro')
RETURNING id
`);
emailUser = b!.id;
const [c] = await db.execute<{ id: string }>(sql`
INSERT INTO users (name, email, role)
VALUES ('Notify Nobody', ${`+3400000${RUN}@phone.linkder.local`}, 'client')
RETURNING id
`);
unreachable = c!.id;
});
afterAll(async () => {
await db.execute(sql`DELETE FROM users WHERE id IN (${phoneUser}, ${emailUser}, ${unreachable})`);
await closePool();
});
describe('notify', () => {
it('sends over SMS when there is a verified number', async () => {
const result = await notify(db, phoneUser, {
kind: 'request.received',
trade: 'Plumber',
distanceM: 2400,
expiresInHours: 12,
});
expect(result).toEqual({ status: 'sent', channel: 'sms' });
const rows = await deliveries(phoneUser);
expect(rows.at(-1)).toMatchObject({ kind: 'request.received', channel: 'sms', status: 'sent' });
});
it('falls back to email when there is no phone', async () => {
const result = await notify(db, emailUser, { kind: 'verification.approved' });
expect(result).toEqual({ status: 'sent', channel: 'email' });
});
it('skips someone nothing can reach, rather than bouncing a synthetic address', async () => {
// `+34…@phone.linkder.local` is what a phone signup gets when they never
// give us an address. Mailing it is a hard bounce against our reputation.
const result = await notify(db, unreachable, { kind: 'verification.approved' });
expect(result.status).toBe('skipped');
const rows = await deliveries(unreachable);
expect(rows.at(-1)!.detail).toMatch(/no verified phone/i);
});
it('honours the preference that governs a kind', async () => {
await db.execute(sql`
INSERT INTO notification_preferences (user_id, sms_new_request)
VALUES (${phoneUser}, false)
ON CONFLICT (user_id) DO UPDATE SET sms_new_request = false
`);
const result = await notify(db, phoneUser, {
kind: 'request.received',
trade: 'Plumber',
distanceM: 900,
expiresInHours: 12,
});
expect(result.status).toBe('skipped');
expect((await deliveries(phoneUser)).at(-1)!.detail).toMatch(/turned off/i);
});
it('sends a transactional message even with everything switched off', async () => {
await db.execute(sql`
UPDATE notification_preferences
SET sms_new_request = false, push_requests = false, email_receipts = false,
sms_marketing = false, email_marketing = false
WHERE user_id = ${phoneUser}
`);
// Being approved is the outcome of something they did. A pro who muted
// everything must still be told their account went live, or the product
// has broken its promise to them.
const result = await notify(db, phoneUser, { kind: 'verification.approved' });
expect(result.status).toBe('sent');
});
it('records a failure instead of throwing', async () => {
// A user id that does not exist stands in for any lookup that comes back
// empty. The contract is that callers never have to catch.
const missing = '00000000-0000-0000-0000-000000000000';
const result = await notify(db, missing, { kind: 'verification.approved' });
expect(result.status).toBe('skipped');
});
});